@verdaccio/loaders 6.0.0-6-next.17 → 6.0.0-6-next.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/build/plugin-async-loader.d.ts +5 -3
- package/build/plugin-async-loader.js +9 -2
- package/build/plugin-async-loader.js.map +1 -1
- package/build/utils.d.ts +5 -5
- package/build/utils.js +1 -5
- package/build/utils.js.map +1 -1
- package/package.json +5 -5
- package/src/plugin-async-loader.ts +24 -12
- package/src/utils.ts +7 -9
- package/test/plugin_loader_async.spec.ts +19 -4
- package/tsconfig.build.json +2 -1
- package/tsconfig.json +2 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { pluginUtils } from '@verdaccio/core';
|
|
2
|
+
import { Config, Logger } from '@verdaccio/types';
|
|
3
|
+
import { PluginType } from './utils';
|
|
2
4
|
export declare type Params = {
|
|
3
5
|
config: Config;
|
|
4
6
|
logger: Logger;
|
|
@@ -25,5 +27,5 @@ export declare type Params = {
|
|
|
25
27
|
* @param {*} prefix by default is verdaccio but can be override with config.server.pluginPrefix
|
|
26
28
|
* @return {Array} list of plugins
|
|
27
29
|
*/
|
|
28
|
-
export declare function asyncLoadPlugin<T extends
|
|
29
|
-
export declare function executePlugin(plugin:
|
|
30
|
+
export declare function asyncLoadPlugin<T extends pluginUtils.Plugin<T>>(pluginConfigs: any, params: Params, sanityCheck: (plugin: PluginType<T>) => boolean, prefix?: string): Promise<PluginType<T>[]>;
|
|
31
|
+
export declare function executePlugin<T>(plugin: PluginType<T>, pluginConfig: unknown, params: Params): PluginType<T>;
|
|
@@ -25,6 +25,11 @@ async function isDirectory(pathFolder) {
|
|
|
25
25
|
return stat.isDirectory();
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
// type Plugins<T> =
|
|
29
|
+
// | pluginUtils.Auth<T>
|
|
30
|
+
// | pluginUtils.Storage<T>
|
|
31
|
+
// | pluginUtils.ExpressMiddleware<T, unknown, unknown>;
|
|
32
|
+
|
|
28
33
|
/**
|
|
29
34
|
* The plugin loader find recursively plugins, if one plugin fails is ignored and report the error to the logger.
|
|
30
35
|
*
|
|
@@ -145,11 +150,13 @@ async function asyncLoadPlugin(pluginConfigs = {}, params, sanityCheck, prefix =
|
|
|
145
150
|
|
|
146
151
|
function executePlugin(plugin, pluginConfig, params) {
|
|
147
152
|
if ((0, _utils.isES6)(plugin)) {
|
|
148
|
-
debug('plugin is ES6'); //
|
|
153
|
+
debug('plugin is ES6'); // @ts-expect-error no relevant for the code
|
|
154
|
+
// eslint-disable-next-line new-cap
|
|
149
155
|
|
|
150
156
|
return new plugin.default(pluginConfig, params);
|
|
151
157
|
} else {
|
|
152
|
-
debug('plugin is commonJS');
|
|
158
|
+
debug('plugin is commonJS'); // @ts-expect-error improve this type
|
|
159
|
+
|
|
153
160
|
return plugin(pluginConfig, params);
|
|
154
161
|
}
|
|
155
162
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin-async-loader.js","names":["debug","buildDebug","isDirectory","pathFolder","stat","lstat","asyncLoadPlugin","pluginConfigs","params","sanityCheck","prefix","pluginsIds","Object","keys","config","plugins","pluginId","pluginsPath","isAbsolute","config_path","configPath","logger","error","resolve","join","dirname","path","pluginDir","externalFilePlugin","plugin","tryLoad","isValid","executePlugin","content","push","err","warn","message","isScoped","startsWith","includes","pluginName","length","pluginConfig","isES6","default"],"sources":["../src/plugin-async-loader.ts"],"sourcesContent":["import buildDebug from 'debug';\nimport { lstat } from 'fs/promises';\nimport { dirname, isAbsolute, join, resolve } from 'path';\n\nimport { logger } from '@verdaccio/logger';\nimport { Config,
|
|
1
|
+
{"version":3,"file":"plugin-async-loader.js","names":["debug","buildDebug","isDirectory","pathFolder","stat","lstat","asyncLoadPlugin","pluginConfigs","params","sanityCheck","prefix","pluginsIds","Object","keys","config","plugins","pluginId","pluginsPath","isAbsolute","config_path","configPath","logger","error","resolve","join","dirname","path","pluginDir","externalFilePlugin","plugin","tryLoad","isValid","executePlugin","content","push","err","warn","message","isScoped","startsWith","includes","pluginName","length","pluginConfig","isES6","default"],"sources":["../src/plugin-async-loader.ts"],"sourcesContent":["import buildDebug from 'debug';\nimport { lstat } from 'fs/promises';\nimport { dirname, isAbsolute, join, resolve } from 'path';\n\nimport { pluginUtils } from '@verdaccio/core';\nimport { logger } from '@verdaccio/logger';\nimport { Config, Logger } from '@verdaccio/types';\n\nimport { PluginType, isES6, isValid, tryLoad } from './utils';\n\nconst debug = buildDebug('verdaccio:plugin:loader:async');\n\nasync function isDirectory(pathFolder: string) {\n const stat = await lstat(pathFolder);\n return stat.isDirectory();\n}\n\nexport type Params = { config: Config; logger: Logger };\n\n// type Plugins<T> =\n// | pluginUtils.Auth<T>\n// | pluginUtils.Storage<T>\n// | pluginUtils.ExpressMiddleware<T, unknown, unknown>;\n\n/**\n * The plugin loader find recursively plugins, if one plugin fails is ignored and report the error to the logger.\n *\n * The loader follows the order:\n * - If the at the `config.yaml` file the `plugins: ./plugins` is defined\n * - If is absolute will use the provided path\n * - If is relative, will use the base path of the config file. eg: /root/config.yaml the plugins folder should be\n * hosted at /root/plugins\n * - The next step is find at the node_modules or global based on the `require` native algorithm.\n * - If the package is scoped eg: @scope/foo, try to load the package `@scope/foo`\n * - If the package is not scoped, will use the default prefix: verdaccio-foo.\n * - If a custom prefix is provided, the verdaccio- is replaced by the config.server.pluginPrefix.\n *\n * The `sanityCheck` is the validation for the required methods to load the plugin, if the validation fails the plugin won't be loaded.\n * The `params` is an object that contains the global configuration and the logger.\n *\n * @param {*} pluginConfigs the custom plugin section\n * @param {*} params a set of params to initialize the plugin\n * @param {*} sanityCheck callback that check the shape that should fulfill the plugin\n * @param {*} prefix by default is verdaccio but can be override with config.server.pluginPrefix\n * @return {Array} list of plugins\n */\nexport async function asyncLoadPlugin<T extends pluginUtils.Plugin<T>>(\n pluginConfigs: any = {},\n params: Params,\n sanityCheck: (plugin: PluginType<T>) => boolean,\n prefix: string = 'verdaccio'\n): Promise<PluginType<T>[]> {\n const pluginsIds = Object.keys(pluginConfigs);\n const { config } = params;\n let plugins: PluginType<T>[] = [];\n for (let pluginId of pluginsIds) {\n debug('plugin %s', pluginId);\n if (typeof config.plugins === 'string') {\n let pluginsPath = config.plugins;\n debug('plugin path %s', pluginsPath);\n if (!isAbsolute(pluginsPath)) {\n if (typeof config.config_path === 'string' && !config.configPath) {\n logger.error(\n 'configPath is missing and the legacy config.config_path is not available for loading plugins'\n );\n }\n\n if (!config.configPath) {\n logger.error('config path property is required for loading plugins');\n continue;\n }\n pluginsPath = resolve(join(dirname(config.configPath), pluginsPath));\n }\n\n logger.debug({ path: pluginsPath }, 'plugins folder defined, loading plugins from @{path} ');\n // throws if is nto a directory\n try {\n await isDirectory(pluginsPath);\n const pluginDir = pluginsPath;\n const externalFilePlugin = resolve(pluginDir, `${prefix}-${pluginId}`);\n let plugin = tryLoad<T>(externalFilePlugin);\n if (plugin && isValid(plugin)) {\n plugin = executePlugin(plugin, pluginConfigs[pluginId], params);\n if (!sanityCheck(plugin)) {\n logger.error(\n { content: externalFilePlugin },\n \"@{content} doesn't look like a valid plugin\"\n );\n continue;\n }\n plugins.push(plugin);\n continue;\n }\n } catch (err: any) {\n logger.warn(\n { err: err.message, pluginsPath, pluginId },\n '@{err} on loading plugins at @{pluginsPath} for @{pluginId}'\n );\n }\n }\n\n if (typeof pluginId === 'string') {\n const isScoped: boolean = pluginId.startsWith('@') && pluginId.includes('/');\n debug('is scoped plugin %s', isScoped);\n const pluginName = isScoped ? pluginId : `${prefix}-${pluginId}`;\n debug('plugin pkg name %s', pluginName);\n let plugin = tryLoad<T>(pluginName);\n if (plugin && isValid(plugin)) {\n plugin = executePlugin(plugin, pluginConfigs[pluginId], params);\n if (!sanityCheck(plugin)) {\n logger.error({ content: pluginName }, \"@{content} doesn't look like a valid plugin\");\n continue;\n }\n plugins.push(plugin);\n continue;\n } else {\n logger.error(\n { pluginName },\n 'package not found, try to install @{pluginName} with a package manager'\n );\n continue;\n }\n }\n }\n debug('plugin found %s', plugins.length);\n return plugins;\n}\n\nexport function executePlugin<T>(\n plugin: PluginType<T>,\n pluginConfig: unknown,\n params: Params\n): PluginType<T> {\n if (isES6(plugin)) {\n debug('plugin is ES6');\n // @ts-expect-error no relevant for the code\n // eslint-disable-next-line new-cap\n return new plugin.default(pluginConfig, params) as Plugin;\n } else {\n debug('plugin is commonJS');\n // @ts-expect-error improve this type\n return plugin(pluginConfig, params) as PluginType<T>;\n }\n}\n"],"mappings":";;;;;;;;AAAA;;AACA;;AACA;;AAGA;;AAGA;;;;AAEA,MAAMA,KAAK,GAAG,IAAAC,cAAA,EAAW,+BAAX,CAAd;;AAEA,eAAeC,WAAf,CAA2BC,UAA3B,EAA+C;EAC7C,MAAMC,IAAI,GAAG,MAAM,IAAAC,eAAA,EAAMF,UAAN,CAAnB;EACA,OAAOC,IAAI,CAACF,WAAL,EAAP;AACD;;AAID;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,eAAeI,eAAf,CACLC,aAAkB,GAAG,EADhB,EAELC,MAFK,EAGLC,WAHK,EAILC,MAAc,GAAG,WAJZ,EAKqB;EAC1B,MAAMC,UAAU,GAAGC,MAAM,CAACC,IAAP,CAAYN,aAAZ,CAAnB;EACA,MAAM;IAAEO;EAAF,IAAaN,MAAnB;EACA,IAAIO,OAAwB,GAAG,EAA/B;;EACA,KAAK,IAAIC,QAAT,IAAqBL,UAArB,EAAiC;IAC/BX,KAAK,CAAC,WAAD,EAAcgB,QAAd,CAAL;;IACA,IAAI,OAAOF,MAAM,CAACC,OAAd,KAA0B,QAA9B,EAAwC;MACtC,IAAIE,WAAW,GAAGH,MAAM,CAACC,OAAzB;MACAf,KAAK,CAAC,gBAAD,EAAmBiB,WAAnB,CAAL;;M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CAAP;EACD,CALD,MAKO;IACLR,KAAK,CAAC,oBAAD,CAAL,CADK,CAEL;;IACA,OAAO6B,MAAM,CAACc,YAAD,EAAenC,MAAf,CAAb;EACD;AACF"}
|
package/build/utils.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare
|
|
3
|
-
export declare function isValid(plugin:
|
|
4
|
-
export declare function isES6(plugin:
|
|
1
|
+
import { pluginUtils } from '@verdaccio/core';
|
|
2
|
+
export declare type PluginType<T> = T extends pluginUtils.Plugin<T> ? T : never;
|
|
3
|
+
export declare function isValid<T>(plugin: PluginType<T>): boolean;
|
|
4
|
+
export declare function isES6<T>(plugin: PluginType<T>): boolean;
|
|
5
5
|
/**
|
|
6
6
|
* Requires a module.
|
|
7
7
|
* @param {*} path the module's path
|
|
8
8
|
* @return {Object}
|
|
9
9
|
*/
|
|
10
|
-
export declare function tryLoad(path: string):
|
|
10
|
+
export declare function tryLoad<T>(path: string): PluginType<T> | null;
|
package/build/utils.js
CHANGED
|
@@ -5,7 +5,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.isES6 = isES6;
|
|
7
7
|
exports.isValid = isValid;
|
|
8
|
-
exports.mergeConfig = mergeConfig;
|
|
9
8
|
exports.tryLoad = tryLoad;
|
|
10
9
|
|
|
11
10
|
var _debug = _interopRequireDefault(require("debug"));
|
|
@@ -19,11 +18,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
19
18
|
const debug = (0, _debug.default)('verdaccio:plugin:loader:utils');
|
|
20
19
|
const MODULE_NOT_FOUND = 'MODULE_NOT_FOUND';
|
|
21
20
|
|
|
22
|
-
function mergeConfig(appConfig, pluginConfig) {
|
|
23
|
-
return _lodash.default.merge(appConfig, pluginConfig);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
21
|
function isValid(plugin) {
|
|
22
|
+
// @ts-expect-error default not relevant
|
|
27
23
|
return _lodash.default.isFunction(plugin) || _lodash.default.isFunction(plugin.default);
|
|
28
24
|
}
|
|
29
25
|
|
package/build/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","names":["debug","buildDebug","MODULE_NOT_FOUND","
|
|
1
|
+
{"version":3,"file":"utils.js","names":["debug","buildDebug","MODULE_NOT_FOUND","isValid","plugin","_","isFunction","default","isES6","Object","keys","includes","tryLoad","path","require","err","code","logger","error","msg"],"sources":["../src/utils.ts"],"sourcesContent":["import buildDebug from 'debug';\nimport _ from 'lodash';\n\nimport { pluginUtils } from '@verdaccio/core';\nimport { logger } from '@verdaccio/logger';\n\nconst debug = buildDebug('verdaccio:plugin:loader:utils');\nconst MODULE_NOT_FOUND = 'MODULE_NOT_FOUND';\n\nexport type PluginType<T> = T extends pluginUtils.Plugin<T> ? T : never;\n\nexport function isValid<T>(plugin: PluginType<T>): boolean {\n // @ts-expect-error default not relevant\n return _.isFunction(plugin) || _.isFunction(plugin.default);\n}\n\nexport function isES6<T>(plugin: PluginType<T>): boolean {\n return Object.keys(plugin).includes('default');\n}\n\n/**\n * Requires a module.\n * @param {*} path the module's path\n * @return {Object}\n */\nexport function tryLoad<T>(path: string): PluginType<T> | null {\n try {\n debug('loading plugin %s', path);\n return require(path) as PluginType<T>;\n } catch (err: any) {\n if (err.code === MODULE_NOT_FOUND) {\n debug('plugin %s not found', path);\n return null;\n }\n logger.error({ err: err.msg }, 'error loading plugin @{err}');\n throw err;\n }\n}\n"],"mappings":";;;;;;;;;AAAA;;AACA;;AAGA;;;;AAEA,MAAMA,KAAK,GAAG,IAAAC,cAAA,EAAW,+BAAX,CAAd;AACA,MAAMC,gBAAgB,GAAG,kBAAzB;;AAIO,SAASC,OAAT,CAAoBC,MAApB,EAAoD;EACzD;EACA,OAAOC,eAAA,CAAEC,UAAF,CAAaF,MAAb,KAAwBC,eAAA,CAAEC,UAAF,CAAaF,MAAM,CAACG,OAApB,CAA/B;AACD;;AAEM,SAASC,KAAT,CAAkBJ,MAAlB,EAAkD;EACvD,OAAOK,MAAM,CAACC,IAAP,CAAYN,MAAZ,EAAoBO,QAApB,CAA6B,SAA7B,CAAP;AACD;AAED;AACA;AACA;AACA;AACA;;;AACO,SAASC,OAAT,CAAoBC,IAApB,EAAwD;EAC7D,IAAI;IACFb,KAAK,CAAC,mBAAD,EAAsBa,IAAtB,CAAL;IACA,OAAOC,OAAO,CAACD,IAAD,CAAd;EACD,CAHD,CAGE,OAAOE,GAAP,EAAiB;IACjB,IAAIA,GAAG,CAACC,IAAJ,KAAad,gBAAjB,EAAmC;MACjCF,KAAK,CAAC,qBAAD,EAAwBa,IAAxB,CAAL;MACA,OAAO,IAAP;IACD;;IACDI,cAAA,CAAOC,KAAP,CAAa;MAAEH,GAAG,EAAEA,GAAG,CAACI;IAAX,CAAb,EAA+B,6BAA/B;;IACA,MAAMJ,GAAN;EACD;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verdaccio/loaders",
|
|
3
|
-
"version": "6.0.0-6-next.
|
|
3
|
+
"version": "6.0.0-6-next.19",
|
|
4
4
|
"description": "loaders logic",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -13,16 +13,16 @@
|
|
|
13
13
|
"url": "https://github.com/verdaccio/verdaccio"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@verdaccio/logger": "6.0.0-6-next.
|
|
16
|
+
"@verdaccio/logger": "6.0.0-6-next.18",
|
|
17
17
|
"debug": "4.3.4",
|
|
18
18
|
"lodash": "4.17.21"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@verdaccio/core": "6.0.0-6-next.
|
|
22
|
-
"@verdaccio/config": "6.0.0-6-next.
|
|
21
|
+
"@verdaccio/core": "6.0.0-6-next.50",
|
|
22
|
+
"@verdaccio/config": "6.0.0-6-next.50",
|
|
23
23
|
"@verdaccio/types": "11.0.0-6-next.17",
|
|
24
24
|
"@verdaccio-scope/verdaccio-auth-foo": "0.0.2",
|
|
25
|
-
"verdaccio-auth-memory": "11.0.0-6-next.
|
|
25
|
+
"verdaccio-auth-memory": "11.0.0-6-next.15",
|
|
26
26
|
"customprefix-auth": "1.0.0-6-next.0"
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://verdaccio.org",
|
|
@@ -2,20 +2,26 @@ import buildDebug from 'debug';
|
|
|
2
2
|
import { lstat } from 'fs/promises';
|
|
3
3
|
import { dirname, isAbsolute, join, resolve } from 'path';
|
|
4
4
|
|
|
5
|
+
import { pluginUtils } from '@verdaccio/core';
|
|
5
6
|
import { logger } from '@verdaccio/logger';
|
|
6
|
-
import { Config,
|
|
7
|
+
import { Config, Logger } from '@verdaccio/types';
|
|
7
8
|
|
|
8
|
-
import { isES6, isValid, tryLoad } from './utils';
|
|
9
|
+
import { PluginType, isES6, isValid, tryLoad } from './utils';
|
|
9
10
|
|
|
10
11
|
const debug = buildDebug('verdaccio:plugin:loader:async');
|
|
11
12
|
|
|
12
|
-
async function isDirectory(pathFolder) {
|
|
13
|
+
async function isDirectory(pathFolder: string) {
|
|
13
14
|
const stat = await lstat(pathFolder);
|
|
14
15
|
return stat.isDirectory();
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
export type Params = { config: Config; logger: Logger };
|
|
18
19
|
|
|
20
|
+
// type Plugins<T> =
|
|
21
|
+
// | pluginUtils.Auth<T>
|
|
22
|
+
// | pluginUtils.Storage<T>
|
|
23
|
+
// | pluginUtils.ExpressMiddleware<T, unknown, unknown>;
|
|
24
|
+
|
|
19
25
|
/**
|
|
20
26
|
* The plugin loader find recursively plugins, if one plugin fails is ignored and report the error to the logger.
|
|
21
27
|
*
|
|
@@ -38,15 +44,15 @@ export type Params = { config: Config; logger: Logger };
|
|
|
38
44
|
* @param {*} prefix by default is verdaccio but can be override with config.server.pluginPrefix
|
|
39
45
|
* @return {Array} list of plugins
|
|
40
46
|
*/
|
|
41
|
-
export async function asyncLoadPlugin<T extends
|
|
47
|
+
export async function asyncLoadPlugin<T extends pluginUtils.Plugin<T>>(
|
|
42
48
|
pluginConfigs: any = {},
|
|
43
49
|
params: Params,
|
|
44
|
-
sanityCheck:
|
|
50
|
+
sanityCheck: (plugin: PluginType<T>) => boolean,
|
|
45
51
|
prefix: string = 'verdaccio'
|
|
46
|
-
): Promise<
|
|
52
|
+
): Promise<PluginType<T>[]> {
|
|
47
53
|
const pluginsIds = Object.keys(pluginConfigs);
|
|
48
54
|
const { config } = params;
|
|
49
|
-
let plugins:
|
|
55
|
+
let plugins: PluginType<T>[] = [];
|
|
50
56
|
for (let pluginId of pluginsIds) {
|
|
51
57
|
debug('plugin %s', pluginId);
|
|
52
58
|
if (typeof config.plugins === 'string') {
|
|
@@ -72,7 +78,7 @@ export async function asyncLoadPlugin<T extends IPlugin<T>>(
|
|
|
72
78
|
await isDirectory(pluginsPath);
|
|
73
79
|
const pluginDir = pluginsPath;
|
|
74
80
|
const externalFilePlugin = resolve(pluginDir, `${prefix}-${pluginId}`);
|
|
75
|
-
let plugin = tryLoad(externalFilePlugin);
|
|
81
|
+
let plugin = tryLoad<T>(externalFilePlugin);
|
|
76
82
|
if (plugin && isValid(plugin)) {
|
|
77
83
|
plugin = executePlugin(plugin, pluginConfigs[pluginId], params);
|
|
78
84
|
if (!sanityCheck(plugin)) {
|
|
@@ -98,7 +104,7 @@ export async function asyncLoadPlugin<T extends IPlugin<T>>(
|
|
|
98
104
|
debug('is scoped plugin %s', isScoped);
|
|
99
105
|
const pluginName = isScoped ? pluginId : `${prefix}-${pluginId}`;
|
|
100
106
|
debug('plugin pkg name %s', pluginName);
|
|
101
|
-
let plugin = tryLoad(pluginName);
|
|
107
|
+
let plugin = tryLoad<T>(pluginName);
|
|
102
108
|
if (plugin && isValid(plugin)) {
|
|
103
109
|
plugin = executePlugin(plugin, pluginConfigs[pluginId], params);
|
|
104
110
|
if (!sanityCheck(plugin)) {
|
|
@@ -120,13 +126,19 @@ export async function asyncLoadPlugin<T extends IPlugin<T>>(
|
|
|
120
126
|
return plugins;
|
|
121
127
|
}
|
|
122
128
|
|
|
123
|
-
export function executePlugin(
|
|
129
|
+
export function executePlugin<T>(
|
|
130
|
+
plugin: PluginType<T>,
|
|
131
|
+
pluginConfig: unknown,
|
|
132
|
+
params: Params
|
|
133
|
+
): PluginType<T> {
|
|
124
134
|
if (isES6(plugin)) {
|
|
125
135
|
debug('plugin is ES6');
|
|
136
|
+
// @ts-expect-error no relevant for the code
|
|
126
137
|
// eslint-disable-next-line new-cap
|
|
127
|
-
return new plugin.default(pluginConfig, params);
|
|
138
|
+
return new plugin.default(pluginConfig, params) as Plugin;
|
|
128
139
|
} else {
|
|
129
140
|
debug('plugin is commonJS');
|
|
130
|
-
|
|
141
|
+
// @ts-expect-error improve this type
|
|
142
|
+
return plugin(pluginConfig, params) as PluginType<T>;
|
|
131
143
|
}
|
|
132
144
|
}
|
package/src/utils.ts
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
import buildDebug from 'debug';
|
|
2
2
|
import _ from 'lodash';
|
|
3
3
|
|
|
4
|
+
import { pluginUtils } from '@verdaccio/core';
|
|
4
5
|
import { logger } from '@verdaccio/logger';
|
|
5
|
-
import { Config } from '@verdaccio/types';
|
|
6
6
|
|
|
7
7
|
const debug = buildDebug('verdaccio:plugin:loader:utils');
|
|
8
|
-
|
|
9
8
|
const MODULE_NOT_FOUND = 'MODULE_NOT_FOUND';
|
|
10
9
|
|
|
11
|
-
export
|
|
12
|
-
return _.merge(appConfig, pluginConfig);
|
|
13
|
-
}
|
|
10
|
+
export type PluginType<T> = T extends pluginUtils.Plugin<T> ? T : never;
|
|
14
11
|
|
|
15
|
-
export function isValid(plugin): boolean {
|
|
12
|
+
export function isValid<T>(plugin: PluginType<T>): boolean {
|
|
13
|
+
// @ts-expect-error default not relevant
|
|
16
14
|
return _.isFunction(plugin) || _.isFunction(plugin.default);
|
|
17
15
|
}
|
|
18
16
|
|
|
19
|
-
export function isES6(plugin): boolean {
|
|
17
|
+
export function isES6<T>(plugin: PluginType<T>): boolean {
|
|
20
18
|
return Object.keys(plugin).includes('default');
|
|
21
19
|
}
|
|
22
20
|
|
|
@@ -25,10 +23,10 @@ export function isES6(plugin): boolean {
|
|
|
25
23
|
* @param {*} path the module's path
|
|
26
24
|
* @return {Object}
|
|
27
25
|
*/
|
|
28
|
-
export function tryLoad(path: string):
|
|
26
|
+
export function tryLoad<T>(path: string): PluginType<T> | null {
|
|
29
27
|
try {
|
|
30
28
|
debug('loading plugin %s', path);
|
|
31
|
-
return require(path)
|
|
29
|
+
return require(path) as PluginType<T>;
|
|
32
30
|
} catch (err: any) {
|
|
33
31
|
if (err.code === MODULE_NOT_FOUND) {
|
|
34
32
|
debug('plugin %s not found', path);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
|
|
3
3
|
import { Config, parseConfigFile } from '@verdaccio/config';
|
|
4
|
+
import { pluginUtils } from '@verdaccio/core';
|
|
4
5
|
import { logger, setup } from '@verdaccio/logger';
|
|
5
6
|
|
|
6
7
|
import { asyncLoadPlugin } from '../src/plugin-async-loader';
|
|
@@ -16,7 +17,7 @@ const authSanitize = function (plugin) {
|
|
|
16
17
|
|
|
17
18
|
const pluginsPartialsFolder = path.join(__dirname, './partials/test-plugin-storage');
|
|
18
19
|
|
|
19
|
-
setup();
|
|
20
|
+
setup({});
|
|
20
21
|
|
|
21
22
|
describe('plugin loader', () => {
|
|
22
23
|
describe('file plugins', () => {
|
|
@@ -43,7 +44,12 @@ describe('plugin loader', () => {
|
|
|
43
44
|
test('testing load auth npm package invalid method check', async () => {
|
|
44
45
|
const config = getConfig('valid-plugin.yaml');
|
|
45
46
|
config.plugins = pluginsPartialsFolder;
|
|
46
|
-
const plugins = await asyncLoadPlugin
|
|
47
|
+
const plugins = await asyncLoadPlugin<pluginUtils.Auth<unknown>>(
|
|
48
|
+
config.auth,
|
|
49
|
+
{ config, logger },
|
|
50
|
+
// @ts-expect-error
|
|
51
|
+
(p) => typeof p.somethingFake !== 'undefined'
|
|
52
|
+
);
|
|
47
53
|
|
|
48
54
|
expect(plugins).toHaveLength(0);
|
|
49
55
|
});
|
|
@@ -117,14 +123,23 @@ describe('plugin loader', () => {
|
|
|
117
123
|
|
|
118
124
|
test('should handle not found installed package', async () => {
|
|
119
125
|
const config = getConfig('npm-plugin-not-found.yaml');
|
|
120
|
-
const plugins = await asyncLoadPlugin
|
|
126
|
+
const plugins = await asyncLoadPlugin<pluginUtils.Auth<unknown>>(
|
|
127
|
+
config.auth,
|
|
128
|
+
{ config, logger },
|
|
129
|
+
(p) => typeof p.authenticate !== 'undefined'
|
|
130
|
+
);
|
|
121
131
|
|
|
122
132
|
expect(plugins).toHaveLength(0);
|
|
123
133
|
});
|
|
124
134
|
|
|
125
135
|
test('testing load auth npm package invalid method check', async () => {
|
|
126
136
|
const config = getConfig('npm-plugin-auth.yaml');
|
|
127
|
-
const plugins = await asyncLoadPlugin
|
|
137
|
+
const plugins = await asyncLoadPlugin<pluginUtils.Auth<unknown>>(
|
|
138
|
+
config.auth,
|
|
139
|
+
{ config, logger },
|
|
140
|
+
// @ts-expect-error
|
|
141
|
+
(p) => typeof p.somethingFake !== 'undefined'
|
|
142
|
+
);
|
|
128
143
|
|
|
129
144
|
expect(plugins).toHaveLength(0);
|
|
130
145
|
});
|
package/tsconfig.build.json
CHANGED
package/tsconfig.json
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
"extends": "../../tsconfig.reference.json",
|
|
3
3
|
"compilerOptions": {
|
|
4
4
|
"rootDir": "./src",
|
|
5
|
-
"outDir": "./build"
|
|
5
|
+
"outDir": "./build",
|
|
6
|
+
"noImplicitAny": true
|
|
6
7
|
},
|
|
7
8
|
"include": ["src/**/*.ts"],
|
|
8
9
|
"exclude": ["src/**/*.test.ts"],
|
|
@@ -15,9 +16,6 @@
|
|
|
15
16
|
},
|
|
16
17
|
{
|
|
17
18
|
"path": "../logger"
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
"path": "../mock"
|
|
21
19
|
}
|
|
22
20
|
]
|
|
23
21
|
}
|