@modern-js/bff-core 1.2.2 → 1.15.0
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/dist/js/modern/index.js +2 -2
- package/dist/js/modern/router/utils.js +3 -1
- package/dist/js/modern/utils/alias.js +91 -0
- package/dist/js/modern/utils/debug.js +2 -0
- package/dist/js/modern/utils/index.js +5 -0
- package/dist/js/modern/utils/meta.js +4 -0
- package/dist/js/modern/utils/storage.js +48 -0
- package/dist/js/modern/utils/validate.js +49 -0
- package/dist/js/node/index.js +39 -16
- package/dist/js/node/router/utils.js +3 -1
- package/dist/js/node/utils/alias.js +115 -0
- package/dist/js/node/utils/debug.js +11 -0
- package/dist/js/node/utils/index.js +72 -0
- package/dist/js/node/utils/meta.js +14 -0
- package/dist/js/node/utils/storage.js +59 -0
- package/dist/js/node/utils/validate.js +68 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/utils/alias.d.ts +7 -0
- package/dist/types/utils/debug.d.ts +1 -0
- package/dist/types/utils/index.d.ts +5 -0
- package/dist/types/utils/meta.d.ts +2 -0
- package/dist/types/utils/storage.d.ts +5 -0
- package/dist/types/utils/validate.d.ts +5 -0
- package/package.json +5 -5
- package/dist/js/modern/utils.js +0 -136
- package/dist/js/node/utils.js +0 -170
- package/dist/types/utils.d.ts +0 -14
package/dist/js/modern/index.js
CHANGED
|
@@ -2,6 +2,6 @@ export { Api } from "./api";
|
|
|
2
2
|
export { HttpError, ValidationError } from "./errors/http";
|
|
3
3
|
export * from "./router";
|
|
4
4
|
export * from "./types";
|
|
5
|
-
export * from "./utils";
|
|
6
5
|
export * from "./client";
|
|
7
|
-
export * from "./operators/http";
|
|
6
|
+
export * from "./operators/http";
|
|
7
|
+
export { getRelativeRuntimePath, HANDLER_WITH_META, isWithMetaHandler, createStorage, registerPaths } from "./utils";
|
|
@@ -71,7 +71,9 @@ const enableRegister = requireFn => {
|
|
|
71
71
|
const isFunction = input => input && {}.toString.call(input) === '[object Function]';
|
|
72
72
|
|
|
73
73
|
export const requireHandlerModule = enableRegister(modulePath => {
|
|
74
|
-
|
|
74
|
+
// 测试环境不走缓存,因为缓存的 h andler 文件,会被 mockAPI 函数进行 mock,升级 jest28,setupFilesAfterEnv 能做异步操作的话,可解此问题
|
|
75
|
+
const originRequire = process.env.NODE_ENV === 'test' ? jest.requireActual : require;
|
|
76
|
+
const module = originRequire(modulePath);
|
|
75
77
|
|
|
76
78
|
if (isFunction(module)) {
|
|
77
79
|
return {
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import * as os from 'os';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import Module from 'module';
|
|
5
|
+
export const getRelativeRuntimePath = (appDirectory, serverRuntimePath) => {
|
|
6
|
+
let relativeRuntimePath = '';
|
|
7
|
+
|
|
8
|
+
if (os.platform() === 'win32') {
|
|
9
|
+
// isRelative function in babel-plugin-resolver plugin can't handle windows relative path correctly, see babel-plugin-resolver's utils.
|
|
10
|
+
relativeRuntimePath = `../${path.relative(appDirectory, serverRuntimePath)}`;
|
|
11
|
+
} else {
|
|
12
|
+
// Look up one level, because the artifacts after build have dist directories
|
|
13
|
+
relativeRuntimePath = path.join('../', path.relative(appDirectory, serverRuntimePath));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
|
17
|
+
relativeRuntimePath = `./${path.relative(appDirectory, serverRuntimePath)}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return relativeRuntimePath;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const sortByLongestPrefix = arr => {
|
|
24
|
+
return arr.concat().sort((a, b) => b.length - a.length);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const createMatchPath = paths => {
|
|
28
|
+
const sortedKeys = sortByLongestPrefix(Object.keys(paths));
|
|
29
|
+
const sortedPaths = {};
|
|
30
|
+
sortedKeys.forEach(key => {
|
|
31
|
+
sortedPaths[key] = paths[key];
|
|
32
|
+
});
|
|
33
|
+
return request => {
|
|
34
|
+
const found = Object.keys(sortedPaths).find(key => {
|
|
35
|
+
return request.startsWith(key);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
if (found) {
|
|
39
|
+
let foundPaths = sortedPaths[found];
|
|
40
|
+
|
|
41
|
+
if (!Array.isArray(foundPaths)) {
|
|
42
|
+
foundPaths = [foundPaths];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
foundPaths = foundPaths.filter(foundPath => path.isAbsolute(foundPath));
|
|
46
|
+
|
|
47
|
+
for (const p of foundPaths) {
|
|
48
|
+
const foundPath = request.replace(found, p);
|
|
49
|
+
|
|
50
|
+
if (fs.existsSync(foundPath)) {
|
|
51
|
+
return foundPath;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return request.replace(found, foundPaths[0]);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return null;
|
|
59
|
+
};
|
|
60
|
+
}; // every path must be a absolute path;
|
|
61
|
+
|
|
62
|
+
export const registerPaths = paths => {
|
|
63
|
+
const originalResolveFilename = Module._resolveFilename; // eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
64
|
+
|
|
65
|
+
const {
|
|
66
|
+
builtinModules
|
|
67
|
+
} = Module;
|
|
68
|
+
const matchPath = createMatchPath(paths);
|
|
69
|
+
|
|
70
|
+
Module._resolveFilename = function (request, _parent) {
|
|
71
|
+
const isCoreModule = builtinModules.includes(request);
|
|
72
|
+
|
|
73
|
+
if (!isCoreModule) {
|
|
74
|
+
const matched = matchPath(request);
|
|
75
|
+
|
|
76
|
+
if (matched) {
|
|
77
|
+
// eslint-disable-next-line prefer-rest-params
|
|
78
|
+
const modifiedArguments = [matched, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
|
|
79
|
+
|
|
80
|
+
return originalResolveFilename.apply(this, modifiedArguments);
|
|
81
|
+
}
|
|
82
|
+
} // eslint-disable-next-line prefer-rest-params
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
return originalResolveFilename.apply(this, arguments);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
return () => {
|
|
89
|
+
Module._resolveFilename = originalResolveFilename;
|
|
90
|
+
};
|
|
91
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as ah from 'async_hooks';
|
|
2
|
+
|
|
3
|
+
const createStorage = () => {
|
|
4
|
+
let storage;
|
|
5
|
+
|
|
6
|
+
if (typeof ah.AsyncLocalStorage !== 'undefined') {
|
|
7
|
+
storage = new ah.AsyncLocalStorage();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const run = (context, cb) => {
|
|
11
|
+
if (!storage) {
|
|
12
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
13
|
+
`);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
storage.run(context, () => {
|
|
18
|
+
try {
|
|
19
|
+
return resolve(cb());
|
|
20
|
+
} catch (error) {
|
|
21
|
+
return reject(error);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const useContext = () => {
|
|
28
|
+
if (!storage) {
|
|
29
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
30
|
+
`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const context = storage.getStore();
|
|
34
|
+
|
|
35
|
+
if (!context) {
|
|
36
|
+
throw new Error(`Can't call useContext out of scope, it should be placed in the bff function`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return context;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
run,
|
|
44
|
+
useContext
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export { createStorage };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import util from 'util'; // fork from https://github.com/nodejs/node/blob/master/lib/internal/errors.js
|
|
2
|
+
|
|
3
|
+
export const getTypeErrorMessage = actual => {
|
|
4
|
+
let msg = '';
|
|
5
|
+
|
|
6
|
+
if (actual == null) {
|
|
7
|
+
msg += `. Received ${actual}`;
|
|
8
|
+
} else if (typeof actual === 'function' && actual.name) {
|
|
9
|
+
msg += `. Received function ${actual.name}`;
|
|
10
|
+
} else if (typeof actual === 'object') {
|
|
11
|
+
var _actual$constructor;
|
|
12
|
+
|
|
13
|
+
if ((_actual$constructor = actual.constructor) !== null && _actual$constructor !== void 0 && _actual$constructor.name) {
|
|
14
|
+
msg += `. Received an instance of ${actual.constructor.name}`;
|
|
15
|
+
} else {
|
|
16
|
+
const inspected = util.inspect(actual, {
|
|
17
|
+
depth: -1
|
|
18
|
+
});
|
|
19
|
+
msg += `. Received ${inspected}`;
|
|
20
|
+
}
|
|
21
|
+
} else {
|
|
22
|
+
let inspected = util.inspect(actual, {
|
|
23
|
+
colors: false
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
if (inspected.length > 25) {
|
|
27
|
+
inspected = `${inspected.slice(0, 25)}...`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
msg += `. Received type ${typeof actual} (${inspected})`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return msg;
|
|
34
|
+
}; // eslint-disable-next-line @typescript-eslint/naming-convention
|
|
35
|
+
|
|
36
|
+
export class ERR_INVALID_ARG_TYPE extends Error {
|
|
37
|
+
constructor(funcName, expectedType, actual) {
|
|
38
|
+
const message = `[ERR_INVALID_ARG_TYPE]: The '${funcName}' argument must be of type ${expectedType}${getTypeErrorMessage(actual)}`;
|
|
39
|
+
super(message);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
export const validateFunction = (maybeFunc, name) => {
|
|
44
|
+
if (typeof maybeFunc !== 'function') {
|
|
45
|
+
throw new ERR_INVALID_ARG_TYPE(name, 'function', maybeFunc);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return true;
|
|
49
|
+
};
|
package/dist/js/node/index.js
CHANGED
|
@@ -6,7 +6,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
var _exportNames = {
|
|
7
7
|
Api: true,
|
|
8
8
|
HttpError: true,
|
|
9
|
-
ValidationError: true
|
|
9
|
+
ValidationError: true,
|
|
10
|
+
getRelativeRuntimePath: true,
|
|
11
|
+
HANDLER_WITH_META: true,
|
|
12
|
+
isWithMetaHandler: true,
|
|
13
|
+
createStorage: true,
|
|
14
|
+
registerPaths: true
|
|
10
15
|
};
|
|
11
16
|
Object.defineProperty(exports, "Api", {
|
|
12
17
|
enumerable: true,
|
|
@@ -14,6 +19,12 @@ Object.defineProperty(exports, "Api", {
|
|
|
14
19
|
return _api.Api;
|
|
15
20
|
}
|
|
16
21
|
});
|
|
22
|
+
Object.defineProperty(exports, "HANDLER_WITH_META", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
get: function () {
|
|
25
|
+
return _utils.HANDLER_WITH_META;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
17
28
|
Object.defineProperty(exports, "HttpError", {
|
|
18
29
|
enumerable: true,
|
|
19
30
|
get: function () {
|
|
@@ -26,6 +37,30 @@ Object.defineProperty(exports, "ValidationError", {
|
|
|
26
37
|
return _http.ValidationError;
|
|
27
38
|
}
|
|
28
39
|
});
|
|
40
|
+
Object.defineProperty(exports, "createStorage", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
get: function () {
|
|
43
|
+
return _utils.createStorage;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
Object.defineProperty(exports, "getRelativeRuntimePath", {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
get: function () {
|
|
49
|
+
return _utils.getRelativeRuntimePath;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
Object.defineProperty(exports, "isWithMetaHandler", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: function () {
|
|
55
|
+
return _utils.isWithMetaHandler;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
Object.defineProperty(exports, "registerPaths", {
|
|
59
|
+
enumerable: true,
|
|
60
|
+
get: function () {
|
|
61
|
+
return _utils.registerPaths;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
29
64
|
|
|
30
65
|
var _api = require("./api");
|
|
31
66
|
|
|
@@ -59,20 +94,6 @@ Object.keys(_types).forEach(function (key) {
|
|
|
59
94
|
});
|
|
60
95
|
});
|
|
61
96
|
|
|
62
|
-
var _utils = require("./utils");
|
|
63
|
-
|
|
64
|
-
Object.keys(_utils).forEach(function (key) {
|
|
65
|
-
if (key === "default" || key === "__esModule") return;
|
|
66
|
-
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
67
|
-
if (key in exports && exports[key] === _utils[key]) return;
|
|
68
|
-
Object.defineProperty(exports, key, {
|
|
69
|
-
enumerable: true,
|
|
70
|
-
get: function () {
|
|
71
|
-
return _utils[key];
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
97
|
var _client = require("./client");
|
|
77
98
|
|
|
78
99
|
Object.keys(_client).forEach(function (key) {
|
|
@@ -99,4 +120,6 @@ Object.keys(_http2).forEach(function (key) {
|
|
|
99
120
|
return _http2[key];
|
|
100
121
|
}
|
|
101
122
|
});
|
|
102
|
-
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
var _utils = require("./utils");
|
|
@@ -90,7 +90,9 @@ const enableRegister = requireFn => {
|
|
|
90
90
|
const isFunction = input => input && {}.toString.call(input) === '[object Function]';
|
|
91
91
|
|
|
92
92
|
const requireHandlerModule = enableRegister(modulePath => {
|
|
93
|
-
|
|
93
|
+
// 测试环境不走缓存,因为缓存的 h andler 文件,会被 mockAPI 函数进行 mock,升级 jest28,setupFilesAfterEnv 能做异步操作的话,可解此问题
|
|
94
|
+
const originRequire = process.env.NODE_ENV === 'test' ? jest.requireActual : require;
|
|
95
|
+
const module = originRequire(modulePath);
|
|
94
96
|
|
|
95
97
|
if (isFunction(module)) {
|
|
96
98
|
return {
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.registerPaths = exports.getRelativeRuntimePath = exports.createMatchPath = void 0;
|
|
7
|
+
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
9
|
+
|
|
10
|
+
var os = _interopRequireWildcard(require("os"));
|
|
11
|
+
|
|
12
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
13
|
+
|
|
14
|
+
var _module = _interopRequireDefault(require("module"));
|
|
15
|
+
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
+
|
|
18
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
19
|
+
|
|
20
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
21
|
+
|
|
22
|
+
const getRelativeRuntimePath = (appDirectory, serverRuntimePath) => {
|
|
23
|
+
let relativeRuntimePath = '';
|
|
24
|
+
|
|
25
|
+
if (os.platform() === 'win32') {
|
|
26
|
+
// isRelative function in babel-plugin-resolver plugin can't handle windows relative path correctly, see babel-plugin-resolver's utils.
|
|
27
|
+
relativeRuntimePath = `../${path.relative(appDirectory, serverRuntimePath)}`;
|
|
28
|
+
} else {
|
|
29
|
+
// Look up one level, because the artifacts after build have dist directories
|
|
30
|
+
relativeRuntimePath = path.join('../', path.relative(appDirectory, serverRuntimePath));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
|
34
|
+
relativeRuntimePath = `./${path.relative(appDirectory, serverRuntimePath)}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return relativeRuntimePath;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
exports.getRelativeRuntimePath = getRelativeRuntimePath;
|
|
41
|
+
|
|
42
|
+
const sortByLongestPrefix = arr => {
|
|
43
|
+
return arr.concat().sort((a, b) => b.length - a.length);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const createMatchPath = paths => {
|
|
47
|
+
const sortedKeys = sortByLongestPrefix(Object.keys(paths));
|
|
48
|
+
const sortedPaths = {};
|
|
49
|
+
sortedKeys.forEach(key => {
|
|
50
|
+
sortedPaths[key] = paths[key];
|
|
51
|
+
});
|
|
52
|
+
return request => {
|
|
53
|
+
const found = Object.keys(sortedPaths).find(key => {
|
|
54
|
+
return request.startsWith(key);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (found) {
|
|
58
|
+
let foundPaths = sortedPaths[found];
|
|
59
|
+
|
|
60
|
+
if (!Array.isArray(foundPaths)) {
|
|
61
|
+
foundPaths = [foundPaths];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
foundPaths = foundPaths.filter(foundPath => path.isAbsolute(foundPath));
|
|
65
|
+
|
|
66
|
+
for (const p of foundPaths) {
|
|
67
|
+
const foundPath = request.replace(found, p);
|
|
68
|
+
|
|
69
|
+
if (_fs.default.existsSync(foundPath)) {
|
|
70
|
+
return foundPath;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return request.replace(found, foundPaths[0]);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return null;
|
|
78
|
+
};
|
|
79
|
+
}; // every path must be a absolute path;
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
exports.createMatchPath = createMatchPath;
|
|
83
|
+
|
|
84
|
+
const registerPaths = paths => {
|
|
85
|
+
const originalResolveFilename = _module.default._resolveFilename; // eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
86
|
+
|
|
87
|
+
const {
|
|
88
|
+
builtinModules
|
|
89
|
+
} = _module.default;
|
|
90
|
+
const matchPath = createMatchPath(paths);
|
|
91
|
+
|
|
92
|
+
_module.default._resolveFilename = function (request, _parent) {
|
|
93
|
+
const isCoreModule = builtinModules.includes(request);
|
|
94
|
+
|
|
95
|
+
if (!isCoreModule) {
|
|
96
|
+
const matched = matchPath(request);
|
|
97
|
+
|
|
98
|
+
if (matched) {
|
|
99
|
+
// eslint-disable-next-line prefer-rest-params
|
|
100
|
+
const modifiedArguments = [matched, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
|
|
101
|
+
|
|
102
|
+
return originalResolveFilename.apply(this, modifiedArguments);
|
|
103
|
+
}
|
|
104
|
+
} // eslint-disable-next-line prefer-rest-params
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
return originalResolveFilename.apply(this, arguments);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return () => {
|
|
111
|
+
_module.default._resolveFilename = originalResolveFilename;
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
exports.registerPaths = registerPaths;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
debug: true
|
|
8
|
+
};
|
|
9
|
+
Object.defineProperty(exports, "debug", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function () {
|
|
12
|
+
return _debug.debug;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
var _storage = require("./storage");
|
|
17
|
+
|
|
18
|
+
Object.keys(_storage).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
21
|
+
if (key in exports && exports[key] === _storage[key]) return;
|
|
22
|
+
Object.defineProperty(exports, key, {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
get: function () {
|
|
25
|
+
return _storage[key];
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
var _alias = require("./alias");
|
|
31
|
+
|
|
32
|
+
Object.keys(_alias).forEach(function (key) {
|
|
33
|
+
if (key === "default" || key === "__esModule") return;
|
|
34
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
35
|
+
if (key in exports && exports[key] === _alias[key]) return;
|
|
36
|
+
Object.defineProperty(exports, key, {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function () {
|
|
39
|
+
return _alias[key];
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
var _debug = require("./debug");
|
|
45
|
+
|
|
46
|
+
var _meta = require("./meta");
|
|
47
|
+
|
|
48
|
+
Object.keys(_meta).forEach(function (key) {
|
|
49
|
+
if (key === "default" || key === "__esModule") return;
|
|
50
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
51
|
+
if (key in exports && exports[key] === _meta[key]) return;
|
|
52
|
+
Object.defineProperty(exports, key, {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: function () {
|
|
55
|
+
return _meta[key];
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
var _validate = require("./validate");
|
|
61
|
+
|
|
62
|
+
Object.keys(_validate).forEach(function (key) {
|
|
63
|
+
if (key === "default" || key === "__esModule") return;
|
|
64
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
65
|
+
if (key in exports && exports[key] === _validate[key]) return;
|
|
66
|
+
Object.defineProperty(exports, key, {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
get: function () {
|
|
69
|
+
return _validate[key];
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isWithMetaHandler = exports.HANDLER_WITH_META = void 0;
|
|
7
|
+
const HANDLER_WITH_META = 'HANDLER_WITH_META';
|
|
8
|
+
exports.HANDLER_WITH_META = HANDLER_WITH_META;
|
|
9
|
+
|
|
10
|
+
const isWithMetaHandler = handler => {
|
|
11
|
+
return typeof handler === 'function' && handler[HANDLER_WITH_META];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
exports.isWithMetaHandler = isWithMetaHandler;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createStorage = void 0;
|
|
7
|
+
|
|
8
|
+
var ah = _interopRequireWildcard(require("async_hooks"));
|
|
9
|
+
|
|
10
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
|
+
|
|
12
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
13
|
+
|
|
14
|
+
const createStorage = () => {
|
|
15
|
+
let storage;
|
|
16
|
+
|
|
17
|
+
if (typeof ah.AsyncLocalStorage !== 'undefined') {
|
|
18
|
+
storage = new ah.AsyncLocalStorage();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const run = (context, cb) => {
|
|
22
|
+
if (!storage) {
|
|
23
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
24
|
+
`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
storage.run(context, () => {
|
|
29
|
+
try {
|
|
30
|
+
return resolve(cb());
|
|
31
|
+
} catch (error) {
|
|
32
|
+
return reject(error);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const useContext = () => {
|
|
39
|
+
if (!storage) {
|
|
40
|
+
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
|
|
41
|
+
`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const context = storage.getStore();
|
|
45
|
+
|
|
46
|
+
if (!context) {
|
|
47
|
+
throw new Error(`Can't call useContext out of scope, it should be placed in the bff function`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return context;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
run,
|
|
55
|
+
useContext
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
exports.createStorage = createStorage;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.validateFunction = exports.getTypeErrorMessage = exports.ERR_INVALID_ARG_TYPE = void 0;
|
|
7
|
+
|
|
8
|
+
var _util = _interopRequireDefault(require("util"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
// fork from https://github.com/nodejs/node/blob/master/lib/internal/errors.js
|
|
13
|
+
const getTypeErrorMessage = actual => {
|
|
14
|
+
let msg = '';
|
|
15
|
+
|
|
16
|
+
if (actual == null) {
|
|
17
|
+
msg += `. Received ${actual}`;
|
|
18
|
+
} else if (typeof actual === 'function' && actual.name) {
|
|
19
|
+
msg += `. Received function ${actual.name}`;
|
|
20
|
+
} else if (typeof actual === 'object') {
|
|
21
|
+
var _actual$constructor;
|
|
22
|
+
|
|
23
|
+
if ((_actual$constructor = actual.constructor) !== null && _actual$constructor !== void 0 && _actual$constructor.name) {
|
|
24
|
+
msg += `. Received an instance of ${actual.constructor.name}`;
|
|
25
|
+
} else {
|
|
26
|
+
const inspected = _util.default.inspect(actual, {
|
|
27
|
+
depth: -1
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
msg += `. Received ${inspected}`;
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
let inspected = _util.default.inspect(actual, {
|
|
34
|
+
colors: false
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (inspected.length > 25) {
|
|
38
|
+
inspected = `${inspected.slice(0, 25)}...`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
msg += `. Received type ${typeof actual} (${inspected})`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return msg;
|
|
45
|
+
}; // eslint-disable-next-line @typescript-eslint/naming-convention
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
exports.getTypeErrorMessage = getTypeErrorMessage;
|
|
49
|
+
|
|
50
|
+
class ERR_INVALID_ARG_TYPE extends Error {
|
|
51
|
+
constructor(funcName, expectedType, actual) {
|
|
52
|
+
const message = `[ERR_INVALID_ARG_TYPE]: The '${funcName}' argument must be of type ${expectedType}${getTypeErrorMessage(actual)}`;
|
|
53
|
+
super(message);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
exports.ERR_INVALID_ARG_TYPE = ERR_INVALID_ARG_TYPE;
|
|
59
|
+
|
|
60
|
+
const validateFunction = (maybeFunc, name) => {
|
|
61
|
+
if (typeof maybeFunc !== 'function') {
|
|
62
|
+
throw new ERR_INVALID_ARG_TYPE(name, 'function', maybeFunc);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return true;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
exports.validateFunction = validateFunction;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -2,6 +2,6 @@ export { Api } from './api';
|
|
|
2
2
|
export { HttpError, ValidationError } from './errors/http';
|
|
3
3
|
export * from './router';
|
|
4
4
|
export * from './types';
|
|
5
|
-
export * from './utils';
|
|
6
5
|
export * from './client';
|
|
7
|
-
export * from './operators/http';
|
|
6
|
+
export * from './operators/http';
|
|
7
|
+
export { getRelativeRuntimePath, HANDLER_WITH_META, isWithMetaHandler, createStorage, registerPaths } from './utils';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
interface Paths {
|
|
2
|
+
[key: string]: string[] | string;
|
|
3
|
+
}
|
|
4
|
+
export declare const getRelativeRuntimePath: (appDirectory: string, serverRuntimePath: string) => string;
|
|
5
|
+
export declare const createMatchPath: (paths: Paths) => (request: string) => string | null;
|
|
6
|
+
export declare const registerPaths: (paths: Paths) => () => void;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const debug: import("@modern-js/utils/compiled/debug").Debugger;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const getTypeErrorMessage: (actual: unknown) => string;
|
|
2
|
+
export declare class ERR_INVALID_ARG_TYPE extends Error {
|
|
3
|
+
constructor(funcName: string, expectedType: string, actual: unknown);
|
|
4
|
+
}
|
|
5
|
+
export declare const validateFunction: (maybeFunc: unknown, name: string) => boolean;
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.15.0",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -25,16 +25,16 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@babel/runtime": "^7.18.0",
|
|
28
|
-
"@modern-js/bff-runtime": "
|
|
29
|
-
"@modern-js/utils": "
|
|
28
|
+
"@modern-js/bff-runtime": "1.15.0",
|
|
29
|
+
"@modern-js/utils": "1.15.0",
|
|
30
30
|
"esbuild": "^0.14.38",
|
|
31
31
|
"esbuild-register": "^3.3.3",
|
|
32
32
|
"koa-compose": "^4.1.0",
|
|
33
33
|
"reflect-metadata": "^0.1.13"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@scripts/build": "
|
|
37
|
-
"@scripts/jest-config": "
|
|
36
|
+
"@scripts/build": "1.15.0",
|
|
37
|
+
"@scripts/jest-config": "1.15.0",
|
|
38
38
|
"@types/jest": "^27",
|
|
39
39
|
"@types/koa-compose": "^3.2.5",
|
|
40
40
|
"@types/node": "^14",
|
package/dist/js/modern/utils.js
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import util from 'util';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import Module from 'module';
|
|
4
|
-
import path from 'path';
|
|
5
|
-
import { createDebugger } from '@modern-js/utils';
|
|
6
|
-
export const HANDLER_WITH_META = 'HANDLER_WITH_META';
|
|
7
|
-
export const debug = createDebugger('bff'); // export const pick = <T extends Record<string, unknown>, K extends keyof T>(
|
|
8
|
-
// obj: T,
|
|
9
|
-
// keys: readonly K[],
|
|
10
|
-
// ) => {
|
|
11
|
-
// Object.entries(obj).filter(([key]) => {
|
|
12
|
-
// return (keys as readonly string[]).includes(key);
|
|
13
|
-
// });
|
|
14
|
-
// };
|
|
15
|
-
// fork from https://github.com/nodejs/node/blob/master/lib/internal/errors.js
|
|
16
|
-
|
|
17
|
-
export const getTypeErrorMessage = actual => {
|
|
18
|
-
let msg = '';
|
|
19
|
-
|
|
20
|
-
if (actual == null) {
|
|
21
|
-
msg += `. Received ${actual}`;
|
|
22
|
-
} else if (typeof actual === 'function' && actual.name) {
|
|
23
|
-
msg += `. Received function ${actual.name}`;
|
|
24
|
-
} else if (typeof actual === 'object') {
|
|
25
|
-
var _actual$constructor;
|
|
26
|
-
|
|
27
|
-
if ((_actual$constructor = actual.constructor) !== null && _actual$constructor !== void 0 && _actual$constructor.name) {
|
|
28
|
-
msg += `. Received an instance of ${actual.constructor.name}`;
|
|
29
|
-
} else {
|
|
30
|
-
const inspected = util.inspect(actual, {
|
|
31
|
-
depth: -1
|
|
32
|
-
});
|
|
33
|
-
msg += `. Received ${inspected}`;
|
|
34
|
-
}
|
|
35
|
-
} else {
|
|
36
|
-
let inspected = util.inspect(actual, {
|
|
37
|
-
colors: false
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
if (inspected.length > 25) {
|
|
41
|
-
inspected = `${inspected.slice(0, 25)}...`;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
msg += `. Received type ${typeof actual} (${inspected})`;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return msg;
|
|
48
|
-
}; // eslint-disable-next-line @typescript-eslint/naming-convention
|
|
49
|
-
|
|
50
|
-
export class ERR_INVALID_ARG_TYPE extends Error {
|
|
51
|
-
constructor(funcName, expectedType, actual) {
|
|
52
|
-
const message = `[ERR_INVALID_ARG_TYPE]: The '${funcName}' argument must be of type ${expectedType}${getTypeErrorMessage(actual)}`;
|
|
53
|
-
super(message);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
export const validateFunction = (maybeFunc, name) => {
|
|
58
|
-
if (typeof maybeFunc !== 'function') {
|
|
59
|
-
throw new ERR_INVALID_ARG_TYPE(name, 'function', maybeFunc);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return true;
|
|
63
|
-
};
|
|
64
|
-
export const isWithMetaHandler = handler => {
|
|
65
|
-
return typeof handler === 'function' && handler[HANDLER_WITH_META];
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const sortByLongestPrefix = arr => {
|
|
69
|
-
return arr.concat().sort((a, b) => b.length - a.length);
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
export const createMatchPath = paths => {
|
|
73
|
-
const sortedKeys = sortByLongestPrefix(Object.keys(paths));
|
|
74
|
-
const sortedPaths = {};
|
|
75
|
-
sortedKeys.forEach(key => {
|
|
76
|
-
sortedPaths[key] = paths[key];
|
|
77
|
-
});
|
|
78
|
-
return request => {
|
|
79
|
-
const found = Object.keys(sortedPaths).find(key => {
|
|
80
|
-
return request.startsWith(key);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
if (found) {
|
|
84
|
-
let foundPaths = sortedPaths[found];
|
|
85
|
-
|
|
86
|
-
if (!Array.isArray(foundPaths)) {
|
|
87
|
-
foundPaths = [foundPaths];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
foundPaths = foundPaths.filter(foundPath => path.isAbsolute(foundPath));
|
|
91
|
-
|
|
92
|
-
for (const p of foundPaths) {
|
|
93
|
-
const foundPath = request.replace(found, p);
|
|
94
|
-
|
|
95
|
-
if (fs.existsSync(foundPath)) {
|
|
96
|
-
return foundPath;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return request.replace(found, foundPaths[0]);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return null;
|
|
104
|
-
};
|
|
105
|
-
}; // every path must be a absolute path;
|
|
106
|
-
|
|
107
|
-
export const registerPaths = paths => {
|
|
108
|
-
const originalResolveFilename = Module._resolveFilename; // eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
109
|
-
|
|
110
|
-
const {
|
|
111
|
-
builtinModules
|
|
112
|
-
} = Module;
|
|
113
|
-
const matchPath = createMatchPath(paths);
|
|
114
|
-
|
|
115
|
-
Module._resolveFilename = function (request, _parent) {
|
|
116
|
-
const isCoreModule = builtinModules.includes(request);
|
|
117
|
-
|
|
118
|
-
if (!isCoreModule) {
|
|
119
|
-
const matched = matchPath(request);
|
|
120
|
-
|
|
121
|
-
if (matched) {
|
|
122
|
-
// eslint-disable-next-line prefer-rest-params
|
|
123
|
-
const modifiedArguments = [matched, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
|
|
124
|
-
|
|
125
|
-
return originalResolveFilename.apply(this, modifiedArguments);
|
|
126
|
-
}
|
|
127
|
-
} // eslint-disable-next-line prefer-rest-params
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return originalResolveFilename.apply(this, arguments);
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
return () => {
|
|
134
|
-
Module._resolveFilename = originalResolveFilename;
|
|
135
|
-
};
|
|
136
|
-
};
|
package/dist/js/node/utils.js
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.validateFunction = exports.registerPaths = exports.isWithMetaHandler = exports.getTypeErrorMessage = exports.debug = exports.createMatchPath = exports.HANDLER_WITH_META = exports.ERR_INVALID_ARG_TYPE = void 0;
|
|
7
|
-
|
|
8
|
-
var _util = _interopRequireDefault(require("util"));
|
|
9
|
-
|
|
10
|
-
var _fs = _interopRequireDefault(require("fs"));
|
|
11
|
-
|
|
12
|
-
var _module = _interopRequireDefault(require("module"));
|
|
13
|
-
|
|
14
|
-
var _path = _interopRequireDefault(require("path"));
|
|
15
|
-
|
|
16
|
-
var _utils = require("@modern-js/utils");
|
|
17
|
-
|
|
18
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
-
|
|
20
|
-
const HANDLER_WITH_META = 'HANDLER_WITH_META';
|
|
21
|
-
exports.HANDLER_WITH_META = HANDLER_WITH_META;
|
|
22
|
-
const debug = (0, _utils.createDebugger)('bff'); // export const pick = <T extends Record<string, unknown>, K extends keyof T>(
|
|
23
|
-
// obj: T,
|
|
24
|
-
// keys: readonly K[],
|
|
25
|
-
// ) => {
|
|
26
|
-
// Object.entries(obj).filter(([key]) => {
|
|
27
|
-
// return (keys as readonly string[]).includes(key);
|
|
28
|
-
// });
|
|
29
|
-
// };
|
|
30
|
-
// fork from https://github.com/nodejs/node/blob/master/lib/internal/errors.js
|
|
31
|
-
|
|
32
|
-
exports.debug = debug;
|
|
33
|
-
|
|
34
|
-
const getTypeErrorMessage = actual => {
|
|
35
|
-
let msg = '';
|
|
36
|
-
|
|
37
|
-
if (actual == null) {
|
|
38
|
-
msg += `. Received ${actual}`;
|
|
39
|
-
} else if (typeof actual === 'function' && actual.name) {
|
|
40
|
-
msg += `. Received function ${actual.name}`;
|
|
41
|
-
} else if (typeof actual === 'object') {
|
|
42
|
-
var _actual$constructor;
|
|
43
|
-
|
|
44
|
-
if ((_actual$constructor = actual.constructor) !== null && _actual$constructor !== void 0 && _actual$constructor.name) {
|
|
45
|
-
msg += `. Received an instance of ${actual.constructor.name}`;
|
|
46
|
-
} else {
|
|
47
|
-
const inspected = _util.default.inspect(actual, {
|
|
48
|
-
depth: -1
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
msg += `. Received ${inspected}`;
|
|
52
|
-
}
|
|
53
|
-
} else {
|
|
54
|
-
let inspected = _util.default.inspect(actual, {
|
|
55
|
-
colors: false
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
if (inspected.length > 25) {
|
|
59
|
-
inspected = `${inspected.slice(0, 25)}...`;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
msg += `. Received type ${typeof actual} (${inspected})`;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return msg;
|
|
66
|
-
}; // eslint-disable-next-line @typescript-eslint/naming-convention
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
exports.getTypeErrorMessage = getTypeErrorMessage;
|
|
70
|
-
|
|
71
|
-
class ERR_INVALID_ARG_TYPE extends Error {
|
|
72
|
-
constructor(funcName, expectedType, actual) {
|
|
73
|
-
const message = `[ERR_INVALID_ARG_TYPE]: The '${funcName}' argument must be of type ${expectedType}${getTypeErrorMessage(actual)}`;
|
|
74
|
-
super(message);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
exports.ERR_INVALID_ARG_TYPE = ERR_INVALID_ARG_TYPE;
|
|
80
|
-
|
|
81
|
-
const validateFunction = (maybeFunc, name) => {
|
|
82
|
-
if (typeof maybeFunc !== 'function') {
|
|
83
|
-
throw new ERR_INVALID_ARG_TYPE(name, 'function', maybeFunc);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return true;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
exports.validateFunction = validateFunction;
|
|
90
|
-
|
|
91
|
-
const isWithMetaHandler = handler => {
|
|
92
|
-
return typeof handler === 'function' && handler[HANDLER_WITH_META];
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
exports.isWithMetaHandler = isWithMetaHandler;
|
|
96
|
-
|
|
97
|
-
const sortByLongestPrefix = arr => {
|
|
98
|
-
return arr.concat().sort((a, b) => b.length - a.length);
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const createMatchPath = paths => {
|
|
102
|
-
const sortedKeys = sortByLongestPrefix(Object.keys(paths));
|
|
103
|
-
const sortedPaths = {};
|
|
104
|
-
sortedKeys.forEach(key => {
|
|
105
|
-
sortedPaths[key] = paths[key];
|
|
106
|
-
});
|
|
107
|
-
return request => {
|
|
108
|
-
const found = Object.keys(sortedPaths).find(key => {
|
|
109
|
-
return request.startsWith(key);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
if (found) {
|
|
113
|
-
let foundPaths = sortedPaths[found];
|
|
114
|
-
|
|
115
|
-
if (!Array.isArray(foundPaths)) {
|
|
116
|
-
foundPaths = [foundPaths];
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
foundPaths = foundPaths.filter(foundPath => _path.default.isAbsolute(foundPath));
|
|
120
|
-
|
|
121
|
-
for (const p of foundPaths) {
|
|
122
|
-
const foundPath = request.replace(found, p);
|
|
123
|
-
|
|
124
|
-
if (_fs.default.existsSync(foundPath)) {
|
|
125
|
-
return foundPath;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return request.replace(found, foundPaths[0]);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return null;
|
|
133
|
-
};
|
|
134
|
-
}; // every path must be a absolute path;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
exports.createMatchPath = createMatchPath;
|
|
138
|
-
|
|
139
|
-
const registerPaths = paths => {
|
|
140
|
-
const originalResolveFilename = _module.default._resolveFilename; // eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
141
|
-
|
|
142
|
-
const {
|
|
143
|
-
builtinModules
|
|
144
|
-
} = _module.default;
|
|
145
|
-
const matchPath = createMatchPath(paths);
|
|
146
|
-
|
|
147
|
-
_module.default._resolveFilename = function (request, _parent) {
|
|
148
|
-
const isCoreModule = builtinModules.includes(request);
|
|
149
|
-
|
|
150
|
-
if (!isCoreModule) {
|
|
151
|
-
const matched = matchPath(request);
|
|
152
|
-
|
|
153
|
-
if (matched) {
|
|
154
|
-
// eslint-disable-next-line prefer-rest-params
|
|
155
|
-
const modifiedArguments = [matched, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
|
|
156
|
-
|
|
157
|
-
return originalResolveFilename.apply(this, modifiedArguments);
|
|
158
|
-
}
|
|
159
|
-
} // eslint-disable-next-line prefer-rest-params
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return originalResolveFilename.apply(this, arguments);
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
return () => {
|
|
166
|
-
_module.default._resolveFilename = originalResolveFilename;
|
|
167
|
-
};
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
exports.registerPaths = registerPaths;
|
package/dist/types/utils.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export declare const HANDLER_WITH_META = "HANDLER_WITH_META";
|
|
2
|
-
export declare const debug: import("@modern-js/utils/compiled/debug").Debugger;
|
|
3
|
-
export declare const getTypeErrorMessage: (actual: unknown) => string;
|
|
4
|
-
export declare class ERR_INVALID_ARG_TYPE extends Error {
|
|
5
|
-
constructor(funcName: string, expectedType: string, actual: unknown);
|
|
6
|
-
}
|
|
7
|
-
export declare const validateFunction: (maybeFunc: unknown, name: string) => boolean;
|
|
8
|
-
export declare const isWithMetaHandler: (handler: any) => any;
|
|
9
|
-
interface Paths {
|
|
10
|
-
[key: string]: string[] | string;
|
|
11
|
-
}
|
|
12
|
-
export declare const createMatchPath: (paths: Paths) => (request: string) => string | null;
|
|
13
|
-
export declare const registerPaths: (paths: Paths) => () => void;
|
|
14
|
-
export {};
|