mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-30 22:34:10 -05:00
refactor: use plugins type on loadPlugin
This commit is contained in:
parent
73ab307260
commit
61e33f0a30
10 changed files with 67 additions and 79 deletions
1
.babelrc
1
.babelrc
|
@ -38,7 +38,6 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"testOldEnv": {
|
"testOldEnv": {
|
||||||
/** FIXME: for an issue on jest env we need to use es2015-node4 **/
|
|
||||||
"presets": [ "es2015-node4", "flow"],
|
"presets": [ "es2015-node4", "flow"],
|
||||||
"plugins": [
|
"plugins": [
|
||||||
"transform-class-properties",
|
"transform-class-properties",
|
||||||
|
|
|
@ -52,7 +52,7 @@
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@commitlint/cli": "6.1.3",
|
"@commitlint/cli": "6.1.3",
|
||||||
"@commitlint/config-conventional": "6.1.3",
|
"@commitlint/config-conventional": "6.1.3",
|
||||||
"@verdaccio/types": "3.2.0",
|
"@verdaccio/types": "3.3.0",
|
||||||
"babel-cli": "6.26.0",
|
"babel-cli": "6.26.0",
|
||||||
"babel-core": "6.26.0",
|
"babel-core": "6.26.0",
|
||||||
"babel-eslint": "8.2.2",
|
"babel-eslint": "8.2.2",
|
||||||
|
|
|
@ -5,18 +5,26 @@ import express from 'express';
|
||||||
import compression from 'compression';
|
import compression from 'compression';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import Storage from '../lib/storage';
|
import Storage from '../lib/storage';
|
||||||
import {loadPlugin} from '../lib/plugin-loader';
|
import loadPlugin from '../lib/plugin-loader';
|
||||||
import hookDebug from './debug';
|
import hookDebug from './debug';
|
||||||
import Auth from '../lib/auth';
|
import Auth from '../lib/auth';
|
||||||
import apiEndpoint from './endpoint';
|
import apiEndpoint from './endpoint';
|
||||||
|
|
||||||
import type {$Application} from 'express';
|
|
||||||
import type {$ResponseExtend, $RequestExtend, $NextFunctionVer, IStorageHandler, IAuth} from '../../types';
|
|
||||||
import type {Config as IConfig} from '@verdaccio/types';
|
|
||||||
import {ErrorCode} from '../lib/utils';
|
import {ErrorCode} from '../lib/utils';
|
||||||
import {API_ERROR, HTTP_STATUS} from '../lib/constants';
|
import {API_ERROR, HTTP_STATUS} from '../lib/constants';
|
||||||
import AppConfig from '../lib/config';
|
import AppConfig from '../lib/config';
|
||||||
|
|
||||||
|
import type {$Application} from 'express';
|
||||||
|
import type {
|
||||||
|
$ResponseExtend,
|
||||||
|
$RequestExtend,
|
||||||
|
$NextFunctionVer,
|
||||||
|
IStorageHandler,
|
||||||
|
IAuth} from '../../types';
|
||||||
|
import type {
|
||||||
|
Config as IConfig,
|
||||||
|
IPluginMiddleware,
|
||||||
|
} from '@verdaccio/types';
|
||||||
|
|
||||||
const LoggerApp = require('../lib/logger');
|
const LoggerApp = require('../lib/logger');
|
||||||
const Middleware = require('./middleware');
|
const Middleware = require('./middleware');
|
||||||
const Cats = require('../lib/status-cats');
|
const Cats = require('../lib/status-cats');
|
||||||
|
@ -54,10 +62,10 @@ const defineAPI = function(config: IConfig, storage: IStorageHandler) {
|
||||||
config: config,
|
config: config,
|
||||||
logger: LoggerApp.logger,
|
logger: LoggerApp.logger,
|
||||||
};
|
};
|
||||||
const plugins = loadPlugin(config, config.middlewares, plugin_params, function(plugin) {
|
const plugins = loadPlugin(config, config.middlewares, plugin_params, function(plugin: IPluginMiddleware) {
|
||||||
return plugin.register_middlewares;
|
return plugin.register_middlewares;
|
||||||
});
|
});
|
||||||
plugins.forEach(function(plugin) {
|
plugins.forEach((plugin) => {
|
||||||
plugin.register_middlewares(app, auth, storage);
|
plugin.register_middlewares(app, auth, storage);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
import {API_ERROR, HTTP_STATUS, ROLES, TOKEN_BASIC, TOKEN_BEARER} from './constants';
|
import {API_ERROR, HTTP_STATUS, ROLES, TOKEN_BASIC, TOKEN_BEARER} from './constants';
|
||||||
import {loadPlugin} from '../lib/plugin-loader';
|
import loadPlugin from '../lib/plugin-loader';
|
||||||
import {buildBase64Buffer, ErrorCode} from './utils';
|
import {buildBase64Buffer, ErrorCode} from './utils';
|
||||||
import {aesDecrypt, aesEncrypt, signPayload, verifyPayload} from './crypto-utils';
|
import {aesDecrypt, aesEncrypt, signPayload, verifyPayload} from './crypto-utils';
|
||||||
import {getDefaultPlugins} from './auth-utils';
|
import {getDefaultPlugins} from './auth-utils';
|
||||||
|
@ -39,7 +39,7 @@ class Auth implements IAuth {
|
||||||
logger: this.logger,
|
logger: this.logger,
|
||||||
};
|
};
|
||||||
|
|
||||||
return loadPlugin<IPluginAuth>(config, config.auth, pluginOptions, (plugin: IPluginAuth) => {
|
return loadPlugin(config, config.auth, pluginOptions, (plugin: IPluginAuth) => {
|
||||||
const {authenticate, allow_access, allow_publish} = plugin;
|
const {authenticate, allow_access, allow_publish} = plugin;
|
||||||
|
|
||||||
return authenticate || allow_access || allow_publish;
|
return authenticate || allow_access || allow_publish;
|
||||||
|
|
|
@ -13,7 +13,7 @@ fileExist, noSuchFile, DEFAULT_REVISION, pkgFileName,
|
||||||
} from './storage-utils';
|
} from './storage-utils';
|
||||||
import {createTarballHash} from './crypto-utils';
|
import {createTarballHash} from './crypto-utils';
|
||||||
import {prepareSearchPackage} from './storage-utils';
|
import {prepareSearchPackage} from './storage-utils';
|
||||||
import {loadPlugin} from '../lib/plugin-loader';
|
import loadPlugin from '../lib/plugin-loader';
|
||||||
import LocalDatabase from '@verdaccio/local-storage';
|
import LocalDatabase from '@verdaccio/local-storage';
|
||||||
import {UploadTarball, ReadTarball} from '@verdaccio/streams';
|
import {UploadTarball, ReadTarball} from '@verdaccio/streams';
|
||||||
import type {
|
import type {
|
||||||
|
@ -570,7 +570,7 @@ class LocalStorage implements IStorage {
|
||||||
* @return {Function}
|
* @return {Function}
|
||||||
*/
|
*/
|
||||||
search(startKey: string, options: any) {
|
search(startKey: string, options: any) {
|
||||||
const stream = new UploadTarball({objectMode: true});
|
const stream = new ReadTarball({objectMode: true});
|
||||||
|
|
||||||
this._searchEachPackage((item, cb) => {
|
this._searchEachPackage((item, cb) => {
|
||||||
if (item.time > parseInt(startKey, 10)) {
|
if (item.time > parseInt(startKey, 10)) {
|
||||||
|
|
|
@ -45,7 +45,7 @@ function isES6(plugin) {
|
||||||
* @param {*} sanityCheck callback that check the shape that should fulfill the plugin
|
* @param {*} sanityCheck callback that check the shape that should fulfill the plugin
|
||||||
* @return {Array} list of plugins
|
* @return {Array} list of plugins
|
||||||
*/
|
*/
|
||||||
function loadPlugin<T>(
|
export default function loadPlugin<T>(
|
||||||
config: Config,
|
config: Config,
|
||||||
pluginConfigs: any = {},
|
pluginConfigs: any = {},
|
||||||
params: any,
|
params: any,
|
||||||
|
@ -97,5 +97,3 @@ function loadPlugin<T>(
|
||||||
return plugin;
|
return plugin;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export {loadPlugin};
|
|
||||||
|
|
|
@ -10,13 +10,13 @@ const readMetadata = (fileName: string = 'metadata') => readFile(`../../unit/par
|
||||||
import type {
|
import type {
|
||||||
Config as AppConfig,
|
Config as AppConfig,
|
||||||
IPluginMiddleware,
|
IPluginMiddleware,
|
||||||
IBasicStorage,
|
IStorageManager,
|
||||||
IBasicAuth,
|
IBasicAuth,
|
||||||
} from '@verdaccio/types';
|
} from '@verdaccio/types';
|
||||||
import type { IUploadTarball, IReadTarball } from '@verdaccio/streams';
|
import type { IUploadTarball, IReadTarball } from '@verdaccio/streams';
|
||||||
|
|
||||||
export default class ExampleMiddlewarePlugin implements IPluginMiddleware {
|
export default class ExampleMiddlewarePlugin implements IPluginMiddleware {
|
||||||
register_middlewares(app: any, auth: IBasicAuth, storage: IBasicStorage): void {
|
register_middlewares(app: any, auth: IBasicAuth, storage: IStorageManager): void {
|
||||||
auth.authenticate('user', 'password', () => {});
|
auth.authenticate('user', 'password', () => {});
|
||||||
auth.allow_access('packageName', 'user', () => {});
|
auth.allow_access('packageName', 'user', () => {});
|
||||||
auth.add_user('user', 'password', () => {});
|
auth.add_user('user', 'password', () => {});
|
||||||
|
@ -27,10 +27,8 @@ export default class ExampleMiddlewarePlugin implements IPluginMiddleware {
|
||||||
storage.mergeTags('name', {'latest': '1.0.0'}, () => {});
|
storage.mergeTags('name', {'latest': '1.0.0'}, () => {});
|
||||||
storage.changePackage('name', readMetadata(), 'revision', () => {});
|
storage.changePackage('name', readMetadata(), 'revision', () => {});
|
||||||
storage.removePackage('name', () => {});
|
storage.removePackage('name', () => {});
|
||||||
storage.updateVersions('name', generatePackageTemplate('test'), () => {});
|
|
||||||
storage.mergeTags('name', {'latest': '1.0.0'}, () => {});
|
storage.mergeTags('name', {'latest': '1.0.0'}, () => {});
|
||||||
storage.removeTarball('name', 'filename', 'revision', () => {});
|
storage.removeTarball('name', 'filename', 'revision', () => {});
|
||||||
storage.getPackageMetadata('test', () => {});
|
|
||||||
/* eslint no-unused-vars: 0 */
|
/* eslint no-unused-vars: 0 */
|
||||||
const config1: AppConfig = new Config({
|
const config1: AppConfig = new Config({
|
||||||
storage: './storage',
|
storage: './storage',
|
||||||
|
@ -39,8 +37,7 @@ export default class ExampleMiddlewarePlugin implements IPluginMiddleware {
|
||||||
const add: IUploadTarball = storage.addTarball('name', 'filename');
|
const add: IUploadTarball = storage.addTarball('name', 'filename');
|
||||||
storage.getTarball('name', 'filename');
|
storage.getTarball('name', 'filename');
|
||||||
const read: IReadTarball = storage.getTarball('name', 'filename');
|
const read: IReadTarball = storage.getTarball('name', 'filename');
|
||||||
const search: IUploadTarball = storage.search('test');
|
const search: IReadTarball = storage.search('test');
|
||||||
const secret: Promise<any> = storage.getSecret(config1);
|
|
||||||
/* eslint no-unused-vars: 0 */
|
/* eslint no-unused-vars: 0 */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {loadPlugin} from '../../../src/lib/plugin-loader';
|
import loadPlugin from '../../../src/lib/plugin-loader';
|
||||||
import logger from '../../../src/lib/logger';
|
import logger from '../../../src/lib/logger';
|
||||||
|
|
||||||
logger.setup([]);
|
logger.setup([]);
|
||||||
|
|
|
@ -3,18 +3,17 @@
|
||||||
import type {
|
import type {
|
||||||
IBasicAuth,
|
IBasicAuth,
|
||||||
IBasicStorage,
|
IBasicStorage,
|
||||||
|
IStorageManager,
|
||||||
UpLinkConf,
|
UpLinkConf,
|
||||||
Callback,
|
Callback,
|
||||||
Versions,
|
Versions,
|
||||||
Version,
|
Version,
|
||||||
MergeTags,
|
|
||||||
Config,
|
Config,
|
||||||
Logger,
|
Logger,
|
||||||
PackageAccess,
|
PackageAccess,
|
||||||
StringValue as verdaccio$StringValue,
|
StringValue as verdaccio$StringValue,
|
||||||
Package} from '@verdaccio/types';
|
Package} from '@verdaccio/types';
|
||||||
import type {
|
import type {
|
||||||
IUploadTarball,
|
|
||||||
IReadTarball,
|
IReadTarball,
|
||||||
} from '@verdaccio/streams';
|
} from '@verdaccio/streams';
|
||||||
import type {ILocalData} from '@verdaccio/local-storage';
|
import type {ILocalData} from '@verdaccio/local-storage';
|
||||||
|
@ -22,6 +21,42 @@ import type {NextFunction, $Request, $Response} from 'request';
|
||||||
|
|
||||||
export type StringValue = verdaccio$StringValue;
|
export type StringValue = verdaccio$StringValue;
|
||||||
|
|
||||||
|
export type StartUpConfig = {
|
||||||
|
storage: string;
|
||||||
|
self_path: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type MatchedPackage = PackageAccess | void;
|
||||||
|
|
||||||
|
export type JWTPayload = {
|
||||||
|
user: string;
|
||||||
|
group: string | void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type JWTSignOptions = {
|
||||||
|
expiresIn: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ProxyList = {
|
||||||
|
[key: string]: IProxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Utils = {
|
||||||
|
ErrorCode: any;
|
||||||
|
getLatestVersion: Callback;
|
||||||
|
isObject: (value: any) => boolean;
|
||||||
|
validate_name: (value: any) => boolean;
|
||||||
|
tag_version: (value: any, version: string, tag: string) => void;
|
||||||
|
normalizeDistTags: (pkg: Package) => void;
|
||||||
|
semverSort: (keys: Array<string>) => Array<string>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type $RequestExtend = $Request & {remote_user?: any}
|
||||||
|
export type $ResponseExtend = $Response & {cookies?: any}
|
||||||
|
export type $NextFunctionVer = NextFunction & mixed;
|
||||||
|
export type $SidebarPackage = Package & {latest: mixed}
|
||||||
|
|
||||||
|
|
||||||
interface IAuthWebUI {
|
interface IAuthWebUI {
|
||||||
issueUIjwt(user: string, time: string): string;
|
issueUIjwt(user: string, time: string): string;
|
||||||
}
|
}
|
||||||
|
@ -66,64 +101,15 @@ export interface IProxy {
|
||||||
getRemoteMetadata(name: string, options: any, callback: Callback): void;
|
getRemoteMetadata(name: string, options: any, callback: Callback): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ProxyList = {
|
|
||||||
[key: string]: IProxy;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Utils = {
|
|
||||||
ErrorCode: any;
|
|
||||||
getLatestVersion: Callback;
|
|
||||||
isObject: (value: any) => boolean;
|
|
||||||
validate_name: (value: any) => boolean;
|
|
||||||
tag_version: (value: any, version: string, tag: string) => void;
|
|
||||||
normalizeDistTags: (pkg: Package) => void;
|
|
||||||
semverSort: (keys: Array<string>) => Array<string>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IStorageHandler {
|
|
||||||
config: Config;
|
|
||||||
localStorage: IStorage;
|
|
||||||
logger: Logger;
|
|
||||||
uplinks: ProxyList;
|
|
||||||
addPackage(name: string, metadata: any, callback: Function): Promise<any>;
|
|
||||||
init(config: Config): Promise<any>;
|
|
||||||
addVersion(name: string, version: string, metadata: Version, tag: StringValue, callback: Callback): void;
|
|
||||||
mergeTags(name: string, tagHash: MergeTags, callback: Callback): void;
|
|
||||||
changePackage(name: string, metadata: Package, revision: string, callback: Callback): void;
|
|
||||||
removePackage(name: string, callback: Callback): void;
|
|
||||||
removeTarball(name: string, filename: string, revision: string, callback: Callback): void;
|
|
||||||
addTarball(name: string, filename: string): IUploadTarball;
|
|
||||||
getTarball(name: string, filename: string): IReadTarball;
|
|
||||||
getPackage(options: any): void;
|
|
||||||
search(startkey: string, options: any): void;
|
|
||||||
getLocalDatabase(callback: Callback): void;
|
|
||||||
_syncUplinksMetadata(name: string, packageInfo: Package, options: any, callback: Callback): void;
|
|
||||||
_updateVersionsHiddenUpLink(versions: Versions, upLink: IProxy): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type StartUpConfig = {
|
|
||||||
storage: string;
|
|
||||||
self_path: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type MatchedPackage = PackageAccess | void;
|
|
||||||
|
|
||||||
export interface IStorage extends IBasicStorage {
|
export interface IStorage extends IBasicStorage {
|
||||||
config: Config;
|
config: Config;
|
||||||
localData: ILocalData;
|
localData: ILocalData;
|
||||||
logger: Logger;
|
logger: Logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type JWTPayload = {
|
export interface IStorageHandler extends IStorageManager {
|
||||||
user: string;
|
localStorage: IStorage;
|
||||||
group: string | void;
|
uplinks: ProxyList;
|
||||||
|
_syncUplinksMetadata(name: string, packageInfo: Package, options: any, callback: Callback): void;
|
||||||
|
_updateVersionsHiddenUpLink(versions: Versions, upLink: IProxy): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type JWTSignOptions = {
|
|
||||||
expiresIn: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type $RequestExtend = $Request & {remote_user?: any}
|
|
||||||
export type $ResponseExtend = $Response & {cookies?: any}
|
|
||||||
export type $NextFunctionVer = NextFunction & mixed;
|
|
||||||
export type $SidebarPackage = Package & {latest: mixed}
|
|
||||||
|
|
BIN
yarn.lock
BIN
yarn.lock
Binary file not shown.
Loading…
Reference in a new issue