2021-09-08 12:06:37 -05:00
|
|
|
// import LRU from 'lru-cache';
|
2020-08-21 00:51:12 -05:00
|
|
|
import buildDebug from 'debug';
|
2020-08-19 13:27:35 -05:00
|
|
|
import _ from 'lodash';
|
2021-10-29 10:33:05 -05:00
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
import { errorUtils, fileUtils, pluginUtils, searchUtils } from '@verdaccio/core';
|
2021-09-08 12:06:37 -05:00
|
|
|
import { Config, IPackageStorage, LocalStorage, Logger } from '@verdaccio/types';
|
|
|
|
import { getMatchedPackagesSpec } from '@verdaccio/utils';
|
2020-08-19 13:27:35 -05:00
|
|
|
|
2021-10-29 10:33:05 -05:00
|
|
|
import { searchOnStorage } from './dir-utils';
|
|
|
|
import { mkdirPromise, writeFilePromise } from './fs';
|
2020-08-19 13:27:35 -05:00
|
|
|
import LocalDriver, { noSuchFile } from './local-fs';
|
|
|
|
import { loadPrivatePackages } from './pkg-utils';
|
2021-01-02 02:11:32 -05:00
|
|
|
import TokenActions from './token';
|
2021-03-30 07:05:58 -05:00
|
|
|
import { _dbGenPath } from './utils';
|
2020-08-19 13:27:35 -05:00
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
const DB_NAME = process.env.VERDACCIO_STORAGE_NAME ?? fileUtils.Files.DatabaseName;
|
|
|
|
|
|
|
|
const debug = buildDebug('verdaccio:plugin:local-storage:experimental');
|
2020-08-19 13:27:35 -05:00
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
export const ERROR_DB_LOCKED =
|
|
|
|
'Database is locked, please check error message printed during startup to prevent data loss';
|
2020-08-21 00:51:12 -05:00
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
type IPluginStorage = pluginUtils.IPluginStorage<{}>;
|
|
|
|
|
|
|
|
class LocalDatabase extends TokenActions implements IPluginStorage {
|
|
|
|
private readonly path: string;
|
|
|
|
private readonly logger: Logger;
|
|
|
|
public readonly config: Config;
|
|
|
|
public readonly storages: Map<string, string>;
|
|
|
|
public data: LocalStorage | void;
|
2020-08-19 13:27:35 -05:00
|
|
|
public locked: boolean;
|
|
|
|
|
|
|
|
public constructor(config: Config, logger: Logger) {
|
2021-01-02 02:11:32 -05:00
|
|
|
super(config);
|
2020-08-19 13:27:35 -05:00
|
|
|
this.config = config;
|
|
|
|
this.logger = logger;
|
|
|
|
this.locked = false;
|
2021-09-08 12:06:37 -05:00
|
|
|
this.data = undefined;
|
2021-03-30 07:05:58 -05:00
|
|
|
this.path = _dbGenPath(DB_NAME, config);
|
2021-09-08 12:06:37 -05:00
|
|
|
this.storages = this._getCustomPackageLocalStorages();
|
2021-03-30 07:05:58 -05:00
|
|
|
debug('plugin storage path %o', this.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async init(): Promise<void> {
|
|
|
|
debug('plugin init');
|
|
|
|
this.data = await this._fetchLocalPackages();
|
2021-09-08 12:06:37 -05:00
|
|
|
debug('local packages loaded');
|
|
|
|
await this._sync();
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
public async getSecret(): Promise<string> {
|
|
|
|
if (typeof this.data === 'undefined') {
|
|
|
|
throw Error('no data secret available');
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:27:35 -05:00
|
|
|
return Promise.resolve(this.data.secret);
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
public async setSecret(secret: string): Promise<void> {
|
|
|
|
if (typeof this.data === 'undefined') {
|
|
|
|
throw Error('no data secret available');
|
|
|
|
} else {
|
2020-08-19 13:27:35 -05:00
|
|
|
this.data.secret = secret;
|
2021-09-08 12:06:37 -05:00
|
|
|
}
|
2020-08-19 13:27:35 -05:00
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
await this._sync();
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
public async add(name: string): Promise<void> {
|
|
|
|
if (typeof this.data === 'undefined') {
|
|
|
|
throw Error('no data secret available');
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:27:35 -05:00
|
|
|
if (this.data.list.indexOf(name) === -1) {
|
|
|
|
this.data.list.push(name);
|
2021-09-08 12:06:37 -05:00
|
|
|
debug('the private package %s has been added', name);
|
|
|
|
await this._sync();
|
2020-08-19 13:27:35 -05:00
|
|
|
} else {
|
2021-09-08 12:06:37 -05:00
|
|
|
debug('the private package %s already exist on database', name);
|
|
|
|
return Promise.resolve();
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
/**
|
|
|
|
* The field storage could be absolute or relative.
|
|
|
|
* If relative, it will be resolved against the config path.
|
|
|
|
* If absolute, it will be returned as is.
|
|
|
|
**/
|
|
|
|
private getStoragePath() {
|
|
|
|
const { storage } = this.config;
|
|
|
|
if (typeof storage !== 'string') {
|
|
|
|
throw new TypeError('storage field is mandatory');
|
|
|
|
}
|
|
|
|
|
|
|
|
const storagePath = path.isAbsolute(storage)
|
|
|
|
? storage
|
|
|
|
: path.normalize(path.join(this.getBaseConfigPath(), storage));
|
|
|
|
debug('storage path %o', storagePath);
|
|
|
|
return storagePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
private getBaseConfigPath(): string {
|
|
|
|
return path.dirname(this.config.config_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter by query.
|
|
|
|
**/
|
|
|
|
public async filterByQuery(results: searchUtils.SearchItemPkg[], query: searchUtils.SearchQuery) {
|
|
|
|
// FUTURE: apply new filters, keyword, version, ...
|
|
|
|
return results.filter((item: searchUtils.SearchItemPkg) => {
|
|
|
|
return item?.name?.match(query.text) !== null;
|
|
|
|
}) as searchUtils.SearchItemPkg[];
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
public async getScore(_pkg: searchUtils.SearchItemPkg): Promise<searchUtils.Score> {
|
|
|
|
return Promise.resolve({
|
|
|
|
final: 1,
|
|
|
|
detail: {
|
|
|
|
maintenance: 0,
|
|
|
|
popularity: 1,
|
|
|
|
quality: 1,
|
2020-08-19 13:27:35 -05:00
|
|
|
},
|
2021-09-08 12:06:37 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async search(query: searchUtils.SearchQuery): Promise<searchUtils.SearchItem[]> {
|
|
|
|
const results: searchUtils.SearchItem[] = [];
|
|
|
|
const storagePath = this.getStoragePath();
|
|
|
|
const packagesOnStorage = await this.filterByQuery(
|
|
|
|
await searchOnStorage(storagePath, this.storages),
|
|
|
|
query
|
2020-08-19 13:27:35 -05:00
|
|
|
);
|
2021-09-08 12:06:37 -05:00
|
|
|
debug('packages found %o', packagesOnStorage.length);
|
|
|
|
for (let storage of packagesOnStorage) {
|
|
|
|
const score = await this.getScore(storage);
|
|
|
|
results.push({
|
|
|
|
package: storage,
|
|
|
|
// there is no particular reason to predefined scores
|
|
|
|
// could be improved by using
|
|
|
|
score,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return results;
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
public async remove(name: string): Promise<void> {
|
|
|
|
try {
|
|
|
|
if (typeof this.data === 'undefined') {
|
|
|
|
throw Error('no data secret available');
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
const data = await this.get();
|
|
|
|
|
2020-08-19 13:27:35 -05:00
|
|
|
const pkgName = data.indexOf(name);
|
|
|
|
if (pkgName !== -1) {
|
|
|
|
this.data.list.splice(pkgName, 1);
|
2020-08-21 00:51:12 -05:00
|
|
|
debug('remove package %o has been removed', name);
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
2021-09-08 12:06:37 -05:00
|
|
|
await this._sync();
|
|
|
|
} catch (err) {
|
|
|
|
this.logger.error({ err }, 'remove the private package has failed @{err}');
|
|
|
|
throw errorUtils.getInternalError('error remove private package');
|
|
|
|
}
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
public async get(): Promise<any> {
|
|
|
|
if (typeof this.data === 'undefined') {
|
|
|
|
throw Error('no data secret available');
|
|
|
|
}
|
2020-08-19 13:27:35 -05:00
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
const { list } = this.data;
|
|
|
|
const totalItems = list?.length;
|
2020-08-21 00:51:12 -05:00
|
|
|
debug('get full list of packages (%o) has been fetched', totalItems);
|
2021-09-08 12:06:37 -05:00
|
|
|
return Promise.resolve(list);
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public getPackageStorage(packageName: string): IPackageStorage {
|
2021-09-08 12:06:37 -05:00
|
|
|
const packageAccess = getMatchedPackagesSpec(packageName, this.config.packages);
|
2020-08-19 13:27:35 -05:00
|
|
|
|
2020-09-16 23:48:16 -05:00
|
|
|
const packagePath: string = this._getLocalStoragePath(
|
|
|
|
packageAccess ? packageAccess.storage : undefined
|
|
|
|
);
|
2020-08-21 00:51:12 -05:00
|
|
|
debug('storage path selected: ', packagePath);
|
2020-08-19 13:27:35 -05:00
|
|
|
if (_.isString(packagePath) === false) {
|
2020-08-21 00:51:12 -05:00
|
|
|
debug('the package %o has no storage defined ', packageName);
|
2020-08-19 13:27:35 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
const packageStoragePath: string = path.join(
|
|
|
|
// FIXME: use getBaseStoragePath instead
|
|
|
|
path.resolve(path.dirname(this.config.config_path || ''), packagePath),
|
2020-09-16 23:48:16 -05:00
|
|
|
packageName
|
|
|
|
);
|
2020-08-19 13:27:35 -05:00
|
|
|
|
2020-08-21 00:51:12 -05:00
|
|
|
debug('storage absolute path: ', packageStoragePath);
|
2020-08-19 13:27:35 -05:00
|
|
|
|
|
|
|
return new LocalDriver(packageStoragePath, this.logger);
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
public async clean(): Promise<void> {
|
|
|
|
await this._sync();
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
private _getCustomPackageLocalStorages(): Map<string, string> {
|
|
|
|
const storages = new Map<string, string>();
|
2020-08-19 13:27:35 -05:00
|
|
|
const { packages } = this.config;
|
|
|
|
|
|
|
|
if (packages) {
|
2021-09-08 12:06:37 -05:00
|
|
|
Object.keys(packages || {}).map((pkg) => {
|
|
|
|
const { storage } = packages[pkg];
|
|
|
|
if (typeof storage === 'string') {
|
|
|
|
const storagePath = path.join(this.getStoragePath(), storage);
|
|
|
|
debug('add custom storage for %s on %s', storage, storagePath);
|
|
|
|
storages.set(storage, storagePath);
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return storages;
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
private async _sync(): Promise<null> {
|
2020-08-21 00:51:12 -05:00
|
|
|
debug('sync database started');
|
2020-08-19 13:27:35 -05:00
|
|
|
|
|
|
|
if (this.locked) {
|
2021-09-08 12:06:37 -05:00
|
|
|
this.logger.error(ERROR_DB_LOCKED);
|
|
|
|
throw new Error(ERROR_DB_LOCKED);
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
// Uses sync to prevent ugly race condition
|
|
|
|
try {
|
2021-09-08 12:06:37 -05:00
|
|
|
const folderName = path.dirname(this.path);
|
2021-03-30 07:05:58 -05:00
|
|
|
debug('creating folder %o', folderName);
|
2021-09-08 12:06:37 -05:00
|
|
|
await mkdirPromise(folderName, { recursive: true });
|
|
|
|
debug('creating folder %o created succeed', folderName);
|
|
|
|
} catch (err) {
|
|
|
|
this.logger.error({ err }, 'sync create folder has failed with error: @{err}');
|
|
|
|
throw err;
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-09-08 12:06:37 -05:00
|
|
|
await writeFilePromise(this.path, JSON.stringify(this.data));
|
2020-08-21 00:51:12 -05:00
|
|
|
debug('sync write succeed');
|
2020-08-19 13:27:35 -05:00
|
|
|
|
|
|
|
return null;
|
2021-08-30 01:19:08 -05:00
|
|
|
} catch (err: any) {
|
2021-09-08 12:06:37 -05:00
|
|
|
this.logger.error({ err }, 'sync database file failed: @{err}');
|
|
|
|
throw err;
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _getLocalStoragePath(storage: string | void): string {
|
2021-09-08 12:06:37 -05:00
|
|
|
const globalConfigStorage = this.getStoragePath();
|
2020-08-19 13:27:35 -05:00
|
|
|
if (_.isNil(globalConfigStorage)) {
|
2021-09-08 12:06:37 -05:00
|
|
|
this.logger.error('property storage in config.yaml is required for using this plugin');
|
|
|
|
throw new Error('property storage in config.yaml is required for using this plugin');
|
2020-08-19 13:27:35 -05:00
|
|
|
} else {
|
2021-09-08 12:06:37 -05:00
|
|
|
if (typeof storage === 'string') {
|
|
|
|
return path.join(globalConfigStorage as string, storage as string);
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return globalConfigStorage as string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 07:05:58 -05:00
|
|
|
private async _fetchLocalPackages(): Promise<LocalStorage> {
|
2020-08-19 13:27:35 -05:00
|
|
|
try {
|
2021-03-30 07:05:58 -05:00
|
|
|
return await loadPrivatePackages(this.path, this.logger);
|
2021-08-30 01:19:08 -05:00
|
|
|
} catch (err: any) {
|
2020-08-19 13:27:35 -05:00
|
|
|
// readFileSync is platform specific, macOS, Linux and Windows thrown an error
|
|
|
|
// Only recreate if file not found to prevent data loss
|
2021-09-08 12:06:37 -05:00
|
|
|
this.logger.warn(
|
|
|
|
{ path: this.path },
|
|
|
|
'no private database found, recreating new one on @{path}'
|
|
|
|
);
|
2020-08-19 13:27:35 -05:00
|
|
|
if (err.code !== noSuchFile) {
|
|
|
|
this.locked = true;
|
2020-09-16 23:48:16 -05:00
|
|
|
this.logger.error(
|
|
|
|
'Failed to read package database file, please check the error printed below:\n',
|
|
|
|
`File Path: ${this.path}\n\n ${err.message}`
|
|
|
|
);
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
|
2021-09-08 12:06:37 -05:00
|
|
|
return { list: [], secret: '' };
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default LocalDatabase;
|