0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-13 22:48:31 -05:00
verdaccio/packages/plugins/local-storage/src/token.ts
Behrang Yarahmadi 13310814da
#2606 add prettier plugin sort imports (#2607)
* #2606 add prettier plugin sort imprts

* #2606 update pnpm-lock.yaml

* #2606 update eslint rules

* #2606 fixes website directory formatting

Co-authored-by: Ayush Sharma <ayush.sharma@trivago.com>
2021-10-29 17:33:05 +02:00

82 lines
2.5 KiB
TypeScript

import buildDebug from 'debug';
import _ from 'lodash';
import low from 'lowdb';
import FileAsync from 'lowdb/adapters/FileAsync';
import FileMemory from 'lowdb/adapters/Memory';
import { Config, ITokenActions, Token, TokenFilter } from '@verdaccio/types';
import { _dbGenPath } from './utils';
const debug = buildDebug('verdaccio:plugin:local-storage:token');
const TOKEN_DB_NAME = '.token-db.json';
export default class TokenActions implements ITokenActions {
public config: Config;
public tokenDb: low.LowdbAsync<any> | null;
public constructor(config: Config) {
this.config = config;
this.tokenDb = null;
}
private async getTokenDb(): Promise<low.LowdbAsync<any>> {
if (!this.tokenDb) {
debug('token database is not defined');
let adapter;
if (process.env.NODE_ENV === 'test') {
debug('token memory adapter');
adapter = new FileMemory('');
} else {
debug('token async adapter');
const pathDb = _dbGenPath(TOKEN_DB_NAME, this.config);
adapter = new FileAsync(pathDb);
}
debug('token bd generated');
this.tokenDb = await low(adapter);
}
return this.tokenDb;
}
public async saveToken(token: Token): Promise<void> {
debug('token key %o', token.key);
const db = await this.getTokenDb();
const userData = await db.get(token.user).value();
debug('user data %o', userData);
if (_.isNil(userData)) {
await db.set(token.user, [token]).write();
debug('token user %o new database', token.user);
} else {
// types does not match with valid implementation
// @ts-ignore
await db.get(token.user).push(token).write();
}
debug('data %o', await db.getState());
debug('token saved %o', token.user);
}
public async deleteToken(user: string, tokenKey: string): Promise<void> {
const db = await this.getTokenDb();
const userTokens = await db.get(user).value();
if (_.isNil(userTokens)) {
throw new Error('user not found');
}
debug('tokens %o - %o', userTokens, userTokens.length);
const remainingTokens = userTokens.filter(({ key }) => {
debug('key %o', key);
return key !== tokenKey;
});
await db.set(user, remainingTokens).write();
debug('removed tokens key %o', tokenKey);
}
public async readTokens(filter: TokenFilter): Promise<Token[]> {
const { user } = filter;
debug('read tokens with %o', user);
const db = await this.getTokenDb();
const tokens = await db.get(user).value();
return tokens || [];
}
}