@modern-js/bff-core 1.21.2-beta.4 → 1.21.3-beta.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/CHANGELOG.md +9 -0
- package/package.json +6 -5
- package/dist/js/modern/api.js +0 -54
- package/dist/js/modern/client/generate-client.js +0 -63
- package/dist/js/modern/client/index.js +0 -1
- package/dist/js/modern/client/result.js +0 -20
- package/dist/js/modern/errors/http.js +0 -22
- package/dist/js/modern/index.js +0 -7
- package/dist/js/modern/operators/http.js +0 -187
- package/dist/js/modern/router/constants.js +0 -14
- package/dist/js/modern/router/index.js +0 -299
- package/dist/js/modern/router/types.js +0 -1
- package/dist/js/modern/router/utils.js +0 -101
- package/dist/js/modern/types.js +0 -47
- package/dist/js/modern/utils/alias.js +0 -91
- package/dist/js/modern/utils/debug.js +0 -2
- package/dist/js/modern/utils/index.js +0 -5
- package/dist/js/modern/utils/meta.js +0 -4
- package/dist/js/modern/utils/storage.js +0 -48
- package/dist/js/modern/utils/validate.js +0 -49
- package/dist/js/node/api.js +0 -66
- package/dist/js/node/client/generate-client.js +0 -81
- package/dist/js/node/client/index.js +0 -18
- package/dist/js/node/client/result.js +0 -32
- package/dist/js/node/errors/http.js +0 -34
- package/dist/js/node/index.js +0 -125
- package/dist/js/node/operators/http.js +0 -226
- package/dist/js/node/router/constants.js +0 -29
- package/dist/js/node/router/index.js +0 -345
- package/dist/js/node/router/types.js +0 -5
- package/dist/js/node/router/utils.js +0 -123
- package/dist/js/node/types.js +0 -59
- package/dist/js/node/utils/alias.js +0 -115
- package/dist/js/node/utils/debug.js +0 -11
- package/dist/js/node/utils/index.js +0 -72
- package/dist/js/node/utils/meta.js +0 -14
- package/dist/js/node/utils/storage.js +0 -59
- package/dist/js/node/utils/validate.js +0 -68
- package/dist/types/api.d.ts +0 -3
- package/dist/types/client/generate-client.d.ts +0 -24
- package/dist/types/client/index.d.ts +0 -1
- package/dist/types/client/result.d.ts +0 -15
- package/dist/types/errors/http.d.ts +0 -8
- package/dist/types/index.d.ts +0 -7
- package/dist/types/operators/http.d.ts +0 -39
- package/dist/types/router/constants.d.ts +0 -17
- package/dist/types/router/index.d.ts +0 -51
- package/dist/types/router/types.d.ts +0 -17
- package/dist/types/router/utils.d.ts +0 -10
- package/dist/types/types.d.ts +0 -58
- package/dist/types/utils/alias.d.ts +0 -7
- package/dist/types/utils/debug.d.ts +0 -1
- package/dist/types/utils/index.d.ts +0 -5
- package/dist/types/utils/meta.d.ts +0 -2
- package/dist/types/utils/storage.d.ts +0 -5
- package/dist/types/utils/validate.d.ts +0 -5
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
2
|
-
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import { fs, logger } from '@modern-js/utils';
|
|
5
|
-
import 'reflect-metadata';
|
|
6
|
-
import { HttpMethod, httpMethods, OperatorType, TriggerType } from "../types";
|
|
7
|
-
import { debug } from "../utils";
|
|
8
|
-
import { APIMode, FRAMEWORK_MODE_LAMBDA_DIR, API_FILE_RULES, FRAMEWORK_MODE_APP_DIR } from "./constants";
|
|
9
|
-
import { getFiles, getPathFromFilename, requireHandlerModule, sortRoutes } from "./utils";
|
|
10
|
-
export * from "./types";
|
|
11
|
-
export * from "./constants";
|
|
12
|
-
export class ApiRouter {
|
|
13
|
-
// lambdaDir is the dir which equal to the apiDir in function mode, and equal to the api/lambda dir in framework mode
|
|
14
|
-
constructor({
|
|
15
|
-
apiDir: _apiDir,
|
|
16
|
-
lambdaDir: _lambdaDir,
|
|
17
|
-
prefix
|
|
18
|
-
}) {
|
|
19
|
-
_defineProperty(this, "apiMode", void 0);
|
|
20
|
-
|
|
21
|
-
_defineProperty(this, "apiDir", void 0);
|
|
22
|
-
|
|
23
|
-
_defineProperty(this, "existLambdaDir", void 0);
|
|
24
|
-
|
|
25
|
-
_defineProperty(this, "lambdaDir", void 0);
|
|
26
|
-
|
|
27
|
-
_defineProperty(this, "prefix", void 0);
|
|
28
|
-
|
|
29
|
-
_defineProperty(this, "apiFiles", []);
|
|
30
|
-
|
|
31
|
-
_defineProperty(this, "getExactApiMode", apiDir => {
|
|
32
|
-
const exist = this.createExistChecker(apiDir);
|
|
33
|
-
const existLambdaDir = exist(FRAMEWORK_MODE_LAMBDA_DIR);
|
|
34
|
-
const existAppDir = exist(FRAMEWORK_MODE_APP_DIR);
|
|
35
|
-
const existAppFile = exist('app.ts') || exist('app.js');
|
|
36
|
-
|
|
37
|
-
if (existLambdaDir || existAppDir || existAppFile) {
|
|
38
|
-
return APIMode.FARMEWORK;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return APIMode.FUNCTION;
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
_defineProperty(this, "createExistChecker", base => target => fs.pathExistsSync(path.resolve(base, target)));
|
|
45
|
-
|
|
46
|
-
_defineProperty(this, "getExactLambdaDir", apiDir => {
|
|
47
|
-
if (this.lambdaDir) {
|
|
48
|
-
return this.lambdaDir;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const lambdaDir = this.apiMode === APIMode.FARMEWORK ? path.join(apiDir, FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
|
|
52
|
-
return lambdaDir;
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
this.validateAbsolute(_apiDir, 'apiDir');
|
|
56
|
-
this.validateAbsolute(_lambdaDir, 'lambdaDir');
|
|
57
|
-
this.prefix = this.initPrefix(prefix);
|
|
58
|
-
this.apiDir = _apiDir;
|
|
59
|
-
this.apiMode = this.getExactApiMode(_apiDir);
|
|
60
|
-
this.lambdaDir = _lambdaDir || this.getExactLambdaDir(this.apiDir);
|
|
61
|
-
this.existLambdaDir = fs.existsSync(this.lambdaDir);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
isExistLambda() {
|
|
65
|
-
return this.existLambdaDir;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
getApiMode() {
|
|
69
|
-
return this.apiMode;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
getLambdaDir() {
|
|
73
|
-
return this.lambdaDir;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
isApiFile(filename) {
|
|
77
|
-
if (this.existLambdaDir) {
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (!this.apiFiles.includes(filename)) {
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return true;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
getSingleModuleHandlers(filename) {
|
|
89
|
-
const moduleInfo = this.getModuleInfo(filename);
|
|
90
|
-
|
|
91
|
-
if (moduleInfo) {
|
|
92
|
-
return this.getModuleHandlerInfos(moduleInfo);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
getHandlerInfo(filename, originFuncName, handler) {
|
|
99
|
-
const httpMethod = this.getHttpMethod(originFuncName, handler);
|
|
100
|
-
const routeName = this.getRouteName(filename, handler);
|
|
101
|
-
|
|
102
|
-
if (httpMethod && routeName) {
|
|
103
|
-
return {
|
|
104
|
-
handler,
|
|
105
|
-
name: originFuncName,
|
|
106
|
-
httpMethod,
|
|
107
|
-
routeName,
|
|
108
|
-
filename,
|
|
109
|
-
routePath: this.getRoutePath(this.prefix, routeName)
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return null;
|
|
114
|
-
} // TODO: 性能提升,开发环境,判断下 lambda 目录修改时间
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
getSafeRoutePath(filename, handler) {
|
|
118
|
-
this.loadApiFiles();
|
|
119
|
-
this.validateValidApifile(filename);
|
|
120
|
-
return this.getRouteName(filename, handler);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
getRouteName(filename, handler) {
|
|
124
|
-
if (handler) {
|
|
125
|
-
const trigger = Reflect.getMetadata(OperatorType.Trigger, handler);
|
|
126
|
-
|
|
127
|
-
if (trigger && trigger.type === TriggerType.Http) {
|
|
128
|
-
if (!trigger.path) {
|
|
129
|
-
throw new Error(`The http trigger ${trigger.name} needs to specify a path`);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return trigger.path;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const routePath = getPathFromFilename(this.lambdaDir, filename);
|
|
137
|
-
return routePath;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
getHttpMethod(originHandlerName, handler) {
|
|
141
|
-
if (handler) {
|
|
142
|
-
const trigger = Reflect.getMetadata(OperatorType.Trigger, handler);
|
|
143
|
-
|
|
144
|
-
if (trigger && httpMethods.includes(trigger.method)) {
|
|
145
|
-
return trigger.method;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const upperName = originHandlerName.toUpperCase();
|
|
150
|
-
|
|
151
|
-
switch (upperName) {
|
|
152
|
-
case 'GET':
|
|
153
|
-
return HttpMethod.Get;
|
|
154
|
-
|
|
155
|
-
case 'POST':
|
|
156
|
-
return HttpMethod.Post;
|
|
157
|
-
|
|
158
|
-
case 'PUT':
|
|
159
|
-
return HttpMethod.Put;
|
|
160
|
-
|
|
161
|
-
case 'DELETE':
|
|
162
|
-
case 'DEL':
|
|
163
|
-
return HttpMethod.Delete;
|
|
164
|
-
|
|
165
|
-
case 'CONNECT':
|
|
166
|
-
return HttpMethod.Connect;
|
|
167
|
-
|
|
168
|
-
case 'TRACE':
|
|
169
|
-
return HttpMethod.Trace;
|
|
170
|
-
|
|
171
|
-
case 'PATCH':
|
|
172
|
-
return HttpMethod.Patch;
|
|
173
|
-
|
|
174
|
-
case 'OPTION':
|
|
175
|
-
return HttpMethod.Option;
|
|
176
|
-
|
|
177
|
-
case 'DEFAULT':
|
|
178
|
-
{
|
|
179
|
-
return HttpMethod.Get;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
default:
|
|
183
|
-
logger.warn(`Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`);
|
|
184
|
-
return null;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
loadApiFiles() {
|
|
189
|
-
if (!this.existLambdaDir) {
|
|
190
|
-
return [];
|
|
191
|
-
} // eslint-disable-next-line no-multi-assign
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
const apiFiles = this.apiFiles = getFiles(this.lambdaDir, API_FILE_RULES);
|
|
195
|
-
return apiFiles;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
getApiFiles() {
|
|
199
|
-
if (!this.existLambdaDir) {
|
|
200
|
-
return [];
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (this.apiFiles.length > 0) {
|
|
204
|
-
return this.apiFiles;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
return this.loadApiFiles();
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
getApiHandlers() {
|
|
211
|
-
const filenames = this.getApiFiles();
|
|
212
|
-
const moduleInfos = this.getModuleInfos(filenames);
|
|
213
|
-
const apiHandlers = this.getHandlerInfos(moduleInfos);
|
|
214
|
-
debug('apiHandlers', apiHandlers.length, apiHandlers);
|
|
215
|
-
return apiHandlers;
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* 如果用户未传入或传入空串,默认为 /api
|
|
219
|
-
* 如果传入 /,则 prefix 为 /
|
|
220
|
-
*/
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
initPrefix(prefix) {
|
|
224
|
-
if (prefix === '/') {
|
|
225
|
-
return '';
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return prefix || '/api';
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
validateAbsolute(filename, paramsName) {
|
|
232
|
-
if (typeof filename === 'string' && !path.isAbsolute(filename)) {
|
|
233
|
-
throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
getModuleInfos(filenames) {
|
|
238
|
-
return filenames.map(filename => this.getModuleInfo(filename)).filter(moduleInfo => Boolean(moduleInfo));
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
getModuleInfo(filename) {
|
|
242
|
-
try {
|
|
243
|
-
const module = requireHandlerModule(filename);
|
|
244
|
-
return {
|
|
245
|
-
filename,
|
|
246
|
-
module
|
|
247
|
-
};
|
|
248
|
-
} catch (err) {
|
|
249
|
-
if (process.env.NODE_ENV === 'production') {
|
|
250
|
-
throw err;
|
|
251
|
-
} else {
|
|
252
|
-
console.error(err);
|
|
253
|
-
return null;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
getHandlerInfos(moduleInfos) {
|
|
259
|
-
let apiHandlers = [];
|
|
260
|
-
moduleInfos.forEach(moduleInfo => {
|
|
261
|
-
const handlerInfos = this.getModuleHandlerInfos(moduleInfo);
|
|
262
|
-
|
|
263
|
-
if (handlerInfos) {
|
|
264
|
-
apiHandlers = apiHandlers.concat(handlerInfos);
|
|
265
|
-
}
|
|
266
|
-
});
|
|
267
|
-
const sortedHandlers = sortRoutes(apiHandlers);
|
|
268
|
-
return sortedHandlers;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
getModuleHandlerInfos(moduleInfo) {
|
|
272
|
-
const {
|
|
273
|
-
module,
|
|
274
|
-
filename
|
|
275
|
-
} = moduleInfo;
|
|
276
|
-
return Object.entries(module).filter(([, handler]) => typeof handler === 'function').map(([key]) => {
|
|
277
|
-
const handler = module[key];
|
|
278
|
-
const handlerInfo = this.getHandlerInfo(filename, key, handler);
|
|
279
|
-
return handlerInfo;
|
|
280
|
-
}).filter(handlerInfo => Boolean(handlerInfo));
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
validateValidApifile(filename) {
|
|
284
|
-
if (!this.apiFiles.includes(filename)) {
|
|
285
|
-
throw new Error(`The ${filename} is not a valid api file.`);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
getRoutePath(prefix, routeName) {
|
|
290
|
-
const finalRouteName = routeName === '/' ? '' : routeName;
|
|
291
|
-
|
|
292
|
-
if (prefix === '' && finalRouteName === '') {
|
|
293
|
-
return '/';
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
return `${prefix}${finalRouteName}`;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { globby } from '@modern-js/utils';
|
|
3
|
-
import { INDEX_SUFFIX } from "./constants";
|
|
4
|
-
export const getFiles = (lambdaDir, rules) => globby.sync(rules, {
|
|
5
|
-
cwd: lambdaDir,
|
|
6
|
-
gitignore: true
|
|
7
|
-
}).map(file => path.resolve(lambdaDir, file));
|
|
8
|
-
export const getPathFromFilename = (baseDir, filename) => {
|
|
9
|
-
const relativeName = filename.substring(baseDir.length);
|
|
10
|
-
const relativePath = relativeName.split('.').slice(0, -1).join('.');
|
|
11
|
-
const nameSplit = relativePath.split(path.sep).map(item => {
|
|
12
|
-
if (item.length > 2) {
|
|
13
|
-
if (item.startsWith('[') && item.endsWith(']')) {
|
|
14
|
-
return `:${item.substring(1, item.length - 1)}`;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return item;
|
|
19
|
-
});
|
|
20
|
-
const name = nameSplit.join('/');
|
|
21
|
-
const finalName = name.endsWith(INDEX_SUFFIX) ? name.substring(0, name.length - INDEX_SUFFIX.length) : name;
|
|
22
|
-
return clearRouteName(finalName);
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const clearRouteName = routeName => {
|
|
26
|
-
let finalRouteName = routeName.trim();
|
|
27
|
-
|
|
28
|
-
if (!finalRouteName.startsWith('/')) {
|
|
29
|
-
finalRouteName = `/${finalRouteName}`;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (finalRouteName.length > 1 && finalRouteName.endsWith('/')) {
|
|
33
|
-
finalRouteName = finalRouteName.substring(0, finalRouteName.length - 1);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return finalRouteName;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export const isHandler = input => input && typeof input === 'function';
|
|
40
|
-
|
|
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
|
-
extension: ['.ts']
|
|
62
|
-
});
|
|
63
|
-
const requiredModule = requireFn(modulePath);
|
|
64
|
-
unregister();
|
|
65
|
-
return requiredModule;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const requiredModule = requireFn(modulePath);
|
|
69
|
-
return requiredModule;
|
|
70
|
-
};
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const isFunction = input => input && {}.toString.call(input) === '[object Function]';
|
|
74
|
-
|
|
75
|
-
export const requireHandlerModule = enableRegister(modulePath => {
|
|
76
|
-
// 测试环境不走缓存,因为缓存的 h andler 文件,会被 mockAPI 函数进行 mock,升级 jest28,setupFilesAfterEnv 能做异步操作的话,可解此问题
|
|
77
|
-
const originRequire = process.env.NODE_ENV === 'test' ? jest.requireActual : require;
|
|
78
|
-
const module = originRequire(modulePath);
|
|
79
|
-
|
|
80
|
-
if (isFunction(module)) {
|
|
81
|
-
return {
|
|
82
|
-
default: module
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return module;
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
const routeValue = routePath => {
|
|
90
|
-
if (routePath.includes(':')) {
|
|
91
|
-
return 11;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return 1;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
export const sortRoutes = apiHandlers => {
|
|
98
|
-
return apiHandlers.sort((handlerA, handlerB) => {
|
|
99
|
-
return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
|
|
100
|
-
});
|
|
101
|
-
};
|
package/dist/js/modern/types.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
export let OperatorType;
|
|
2
|
-
|
|
3
|
-
(function (OperatorType) {
|
|
4
|
-
OperatorType[OperatorType["Trigger"] = 0] = "Trigger";
|
|
5
|
-
OperatorType[OperatorType["Middleware"] = 1] = "Middleware";
|
|
6
|
-
})(OperatorType || (OperatorType = {}));
|
|
7
|
-
|
|
8
|
-
export let TriggerType;
|
|
9
|
-
|
|
10
|
-
(function (TriggerType) {
|
|
11
|
-
TriggerType[TriggerType["Http"] = 0] = "Http";
|
|
12
|
-
})(TriggerType || (TriggerType = {}));
|
|
13
|
-
|
|
14
|
-
export let HttpMetadata;
|
|
15
|
-
|
|
16
|
-
(function (HttpMetadata) {
|
|
17
|
-
HttpMetadata["Method"] = "METHOD";
|
|
18
|
-
HttpMetadata["Data"] = "DATA";
|
|
19
|
-
HttpMetadata["Query"] = "QUERY";
|
|
20
|
-
HttpMetadata["Params"] = "PARAMS";
|
|
21
|
-
HttpMetadata["Headers"] = "HEADERS";
|
|
22
|
-
HttpMetadata["Response"] = "RESPONSE";
|
|
23
|
-
})(HttpMetadata || (HttpMetadata = {}));
|
|
24
|
-
|
|
25
|
-
export let ResponseMetaType;
|
|
26
|
-
|
|
27
|
-
(function (ResponseMetaType) {
|
|
28
|
-
ResponseMetaType[ResponseMetaType["StatusCode"] = 0] = "StatusCode";
|
|
29
|
-
ResponseMetaType[ResponseMetaType["Redirect"] = 1] = "Redirect";
|
|
30
|
-
ResponseMetaType[ResponseMetaType["Headers"] = 2] = "Headers";
|
|
31
|
-
})(ResponseMetaType || (ResponseMetaType = {}));
|
|
32
|
-
|
|
33
|
-
export let HttpMethod;
|
|
34
|
-
|
|
35
|
-
(function (HttpMethod) {
|
|
36
|
-
HttpMethod["Get"] = "GET";
|
|
37
|
-
HttpMethod["Post"] = "POST";
|
|
38
|
-
HttpMethod["Put"] = "PUT";
|
|
39
|
-
HttpMethod["Delete"] = "DELETE";
|
|
40
|
-
HttpMethod["Connect"] = "CONNECT";
|
|
41
|
-
HttpMethod["Trace"] = "TRACE";
|
|
42
|
-
HttpMethod["Patch"] = "PATCH";
|
|
43
|
-
HttpMethod["Option"] = "OPTION";
|
|
44
|
-
HttpMethod["Head"] = "HEAD";
|
|
45
|
-
})(HttpMethod || (HttpMethod = {}));
|
|
46
|
-
|
|
47
|
-
export const httpMethods = Object.values(HttpMethod);
|
|
@@ -1,91 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,48 +0,0 @@
|
|
|
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 };
|
|
@@ -1,49 +0,0 @@
|
|
|
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
|
-
};
|