@modern-js/bff-core 1.0.1-beta.5 → 1.0.1-beta.8
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/operators/http.js +3 -3
- package/dist/js/modern/router/utils.js +31 -10
- package/dist/js/modern/utils.js +73 -0
- package/dist/js/node/operators/http.js +3 -7
- package/dist/js/node/router/utils.js +31 -11
- package/dist/js/node/utils.js +83 -2
- package/dist/types/client/result.d.ts +3 -3
- package/dist/types/router/utils.d.ts +2 -2
- package/dist/types/utils.d.ts +7 -1
- package/package.json +2 -3
|
@@ -6,10 +6,10 @@ const validateInput = async (schema, input) => {
|
|
|
6
6
|
await schema.parseAsync(input);
|
|
7
7
|
} catch (error) {
|
|
8
8
|
const {
|
|
9
|
-
z
|
|
10
|
-
} =
|
|
9
|
+
z: zod
|
|
10
|
+
} = require('zod');
|
|
11
11
|
|
|
12
|
-
if (error instanceof
|
|
12
|
+
if (error instanceof zod.ZodError) {
|
|
13
13
|
throw new ValidationError(400, error.message);
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -38,17 +38,39 @@ const clearRouteName = routeName => {
|
|
|
38
38
|
|
|
39
39
|
export const isHandler = input => input && typeof input === 'function';
|
|
40
40
|
|
|
41
|
-
const
|
|
41
|
+
const enableRegister = requireFn => {
|
|
42
|
+
// esbuild-register 做 unRegister 时,不会删除 register 添加的 require.extensions,导致第二次调用时 require.extensions['.ts'] 是 nodejs 默认 loader
|
|
43
|
+
// 所以这里根据第一次调用时,require.extensions 有没有,来判断是否需要使用 esbuild-register
|
|
44
|
+
let existTsLoader = false;
|
|
45
|
+
let firstCall = true;
|
|
46
|
+
return modulePath => {
|
|
47
|
+
if (firstCall) {
|
|
48
|
+
// eslint-disable-next-line node/no-deprecated-api
|
|
49
|
+
existTsLoader = Boolean(require.extensions['.ts']);
|
|
50
|
+
firstCall = false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!existTsLoader) {
|
|
54
|
+
const {
|
|
55
|
+
register
|
|
56
|
+
} = require('esbuild-register/dist/node');
|
|
57
|
+
|
|
58
|
+
const {
|
|
59
|
+
unregister
|
|
60
|
+
} = register({});
|
|
61
|
+
const requiredModule = requireFn(modulePath);
|
|
62
|
+
unregister();
|
|
63
|
+
return requiredModule;
|
|
64
|
+
}
|
|
42
65
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
66
|
+
const requiredModule = requireFn(modulePath);
|
|
67
|
+
return requiredModule;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
47
70
|
|
|
48
|
-
|
|
49
|
-
unregister
|
|
50
|
-
} = register({});
|
|
71
|
+
const isFunction = input => input && {}.toString.call(input) === '[object Function]';
|
|
51
72
|
|
|
73
|
+
export const requireHandlerModule = enableRegister(modulePath => {
|
|
52
74
|
const module = require(modulePath);
|
|
53
75
|
|
|
54
76
|
if (isFunction(module)) {
|
|
@@ -57,9 +79,8 @@ export const requireHandlerModule = modulePath => {
|
|
|
57
79
|
};
|
|
58
80
|
}
|
|
59
81
|
|
|
60
|
-
unregister();
|
|
61
82
|
return module;
|
|
62
|
-
};
|
|
83
|
+
});
|
|
63
84
|
|
|
64
85
|
const routeValue = routePath => {
|
|
65
86
|
if (routePath.includes(':')) {
|
package/dist/js/modern/utils.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import util from 'util';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import Module from 'module';
|
|
4
|
+
import path from 'path';
|
|
2
5
|
import { createDebugger } from '@modern-js/utils';
|
|
3
6
|
export const HANDLER_WITH_META = 'HANDLER_WITH_META';
|
|
4
7
|
export const debug = createDebugger('bff'); // export const pick = <T extends Record<string, unknown>, K extends keyof T>(
|
|
@@ -60,4 +63,74 @@ export const validateFunction = (maybeFunc, name) => {
|
|
|
60
63
|
};
|
|
61
64
|
export const isWithMetaHandler = handler => {
|
|
62
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
|
+
}; // paths 中的值只能是绝对路径
|
|
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
|
+
};
|
|
63
136
|
};
|
|
@@ -9,19 +9,15 @@ var _types = require("../types");
|
|
|
9
9
|
|
|
10
10
|
var _http = require("../errors/http");
|
|
11
11
|
|
|
12
|
-
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); }
|
|
13
|
-
|
|
14
|
-
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; }
|
|
15
|
-
|
|
16
12
|
const validateInput = async (schema, input) => {
|
|
17
13
|
try {
|
|
18
14
|
await schema.parseAsync(input);
|
|
19
15
|
} catch (error) {
|
|
20
16
|
const {
|
|
21
|
-
z
|
|
22
|
-
} =
|
|
17
|
+
z: zod
|
|
18
|
+
} = require('zod');
|
|
23
19
|
|
|
24
|
-
if (error instanceof
|
|
20
|
+
if (error instanceof zod.ZodError) {
|
|
25
21
|
throw new _http.ValidationError(400, error.message);
|
|
26
22
|
}
|
|
27
23
|
|
|
@@ -57,17 +57,39 @@ const isHandler = input => input && typeof input === 'function';
|
|
|
57
57
|
|
|
58
58
|
exports.isHandler = isHandler;
|
|
59
59
|
|
|
60
|
-
const
|
|
60
|
+
const enableRegister = requireFn => {
|
|
61
|
+
// esbuild-register 做 unRegister 时,不会删除 register 添加的 require.extensions,导致第二次调用时 require.extensions['.ts'] 是 nodejs 默认 loader
|
|
62
|
+
// 所以这里根据第一次调用时,require.extensions 有没有,来判断是否需要使用 esbuild-register
|
|
63
|
+
let existTsLoader = false;
|
|
64
|
+
let firstCall = true;
|
|
65
|
+
return modulePath => {
|
|
66
|
+
if (firstCall) {
|
|
67
|
+
// eslint-disable-next-line node/no-deprecated-api
|
|
68
|
+
existTsLoader = Boolean(require.extensions['.ts']);
|
|
69
|
+
firstCall = false;
|
|
70
|
+
}
|
|
61
71
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
if (!existTsLoader) {
|
|
73
|
+
const {
|
|
74
|
+
register
|
|
75
|
+
} = require('esbuild-register/dist/node');
|
|
76
|
+
|
|
77
|
+
const {
|
|
78
|
+
unregister
|
|
79
|
+
} = register({});
|
|
80
|
+
const requiredModule = requireFn(modulePath);
|
|
81
|
+
unregister();
|
|
82
|
+
return requiredModule;
|
|
83
|
+
}
|
|
66
84
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
85
|
+
const requiredModule = requireFn(modulePath);
|
|
86
|
+
return requiredModule;
|
|
87
|
+
};
|
|
88
|
+
};
|
|
70
89
|
|
|
90
|
+
const isFunction = input => input && {}.toString.call(input) === '[object Function]';
|
|
91
|
+
|
|
92
|
+
const requireHandlerModule = enableRegister(modulePath => {
|
|
71
93
|
const module = require(modulePath);
|
|
72
94
|
|
|
73
95
|
if (isFunction(module)) {
|
|
@@ -76,10 +98,8 @@ const requireHandlerModule = modulePath => {
|
|
|
76
98
|
};
|
|
77
99
|
}
|
|
78
100
|
|
|
79
|
-
unregister();
|
|
80
101
|
return module;
|
|
81
|
-
};
|
|
82
|
-
|
|
102
|
+
});
|
|
83
103
|
exports.requireHandlerModule = requireHandlerModule;
|
|
84
104
|
|
|
85
105
|
const routeValue = routePath => {
|
package/dist/js/node/utils.js
CHANGED
|
@@ -3,10 +3,16 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.validateFunction = exports.isWithMetaHandler = exports.getTypeErrorMessage = exports.debug = exports.HANDLER_WITH_META = exports.ERR_INVALID_ARG_TYPE = void 0;
|
|
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
7
|
|
|
8
8
|
var _util = _interopRequireDefault(require("util"));
|
|
9
9
|
|
|
10
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
11
|
+
|
|
12
|
+
var _module = _interopRequireDefault(require("module"));
|
|
13
|
+
|
|
14
|
+
var _path = _interopRequireDefault(require("path"));
|
|
15
|
+
|
|
10
16
|
var _utils = require("@modern-js/utils");
|
|
11
17
|
|
|
12
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -86,4 +92,79 @@ const isWithMetaHandler = handler => {
|
|
|
86
92
|
return typeof handler === 'function' && handler[HANDLER_WITH_META];
|
|
87
93
|
};
|
|
88
94
|
|
|
89
|
-
exports.isWithMetaHandler = isWithMetaHandler;
|
|
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
|
+
}; // paths 中的值只能是绝对路径
|
|
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;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
export declare type Err<T =
|
|
1
|
+
export declare type Err<T = unknown> = {
|
|
2
2
|
kind: 'Err';
|
|
3
3
|
value: T;
|
|
4
4
|
isErr: true;
|
|
5
5
|
isOk: false;
|
|
6
6
|
};
|
|
7
|
-
export declare type Ok<T =
|
|
7
|
+
export declare type Ok<T = unknown> = {
|
|
8
8
|
kind: 'Ok';
|
|
9
9
|
value: T;
|
|
10
10
|
isErr: false;
|
|
11
11
|
isOk: true;
|
|
12
12
|
};
|
|
13
|
-
export declare type Result<T =
|
|
13
|
+
export declare type Result<T = unknown, E = string> = Err<E> | Ok<T>;
|
|
14
14
|
export declare const Err: <E = string>(value: E) => Err<E>;
|
|
15
15
|
export declare const Ok: <T, E = string>(value: T) => Result<T, E>;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { APIHandlerInfo } from './types';
|
|
1
|
+
import { APIHandlerInfo, HandlerModule } from './types';
|
|
2
2
|
declare type MaybeAsync<I> = I | Promise<I>;
|
|
3
3
|
export declare type NormalHandler = (...args: any[]) => any;
|
|
4
4
|
export declare type Handler<I, O> = (input: I) => MaybeAsync<O>;
|
|
5
5
|
export declare const getFiles: (lambdaDir: string, rules: string | string[]) => string[];
|
|
6
6
|
export declare const getPathFromFilename: (baseDir: string, filename: string) => string;
|
|
7
7
|
export declare const isHandler: (input: any) => input is Handler<any, any>;
|
|
8
|
-
export declare const requireHandlerModule: (modulePath: string) =>
|
|
8
|
+
export declare const requireHandlerModule: (modulePath: string) => HandlerModule;
|
|
9
9
|
export declare const sortRoutes: (apiHandlers: APIHandlerInfo[]) => APIHandlerInfo[];
|
|
10
10
|
export {};
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -5,4 +5,10 @@ export declare class ERR_INVALID_ARG_TYPE extends Error {
|
|
|
5
5
|
constructor(funcName: string, expectedType: string, actual: unknown);
|
|
6
6
|
}
|
|
7
7
|
export declare const validateFunction: (maybeFunc: unknown, name: string) => boolean;
|
|
8
|
-
export declare const isWithMetaHandler: (handler: any) => any;
|
|
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 {};
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.0.1-beta.
|
|
14
|
+
"version": "1.0.1-beta.8",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -24,12 +24,11 @@
|
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@babel/runtime": "^7.
|
|
27
|
+
"@babel/runtime": "^7.18.0",
|
|
28
28
|
"@modern-js/bff-runtime": "^1.2.3",
|
|
29
29
|
"@modern-js/utils": "^1.7.6",
|
|
30
30
|
"esbuild": "^0.14.38",
|
|
31
31
|
"esbuild-register": "^3.3.3",
|
|
32
|
-
"http-errors": "^2.0.0",
|
|
33
32
|
"koa-compose": "^4.1.0",
|
|
34
33
|
"reflect-metadata": "^0.1.13"
|
|
35
34
|
},
|