@modern-js/bff-core 1.1.0 → 1.1.3
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.
|
@@ -186,7 +186,7 @@ export class ApiRouter {
|
|
|
186
186
|
const filenames = this.getApiFiles();
|
|
187
187
|
const moduleInfos = this.getModuleInfos(filenames);
|
|
188
188
|
const apiHandlers = this.getHandlerInfos(moduleInfos);
|
|
189
|
-
debug('apiHandlers', apiHandlers);
|
|
189
|
+
debug('apiHandlers', apiHandlers.length, apiHandlers);
|
|
190
190
|
return apiHandlers;
|
|
191
191
|
}
|
|
192
192
|
/**
|
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
|
+
}; // 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
|
+
};
|
|
63
136
|
};
|
|
@@ -230,7 +230,7 @@ class ApiRouter {
|
|
|
230
230
|
const filenames = this.getApiFiles();
|
|
231
231
|
const moduleInfos = this.getModuleInfos(filenames);
|
|
232
232
|
const apiHandlers = this.getHandlerInfos(moduleInfos);
|
|
233
|
-
(0, _utils2.debug)('apiHandlers', apiHandlers);
|
|
233
|
+
(0, _utils2.debug)('apiHandlers', apiHandlers.length, apiHandlers);
|
|
234
234
|
return apiHandlers;
|
|
235
235
|
}
|
|
236
236
|
/**
|
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
|
+
}; // 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
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.1.
|
|
14
|
+
"version": "1.1.3",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@babel/runtime": "^7.18.0",
|
|
28
28
|
"@modern-js/bff-runtime": "^1.3.0",
|
|
29
|
-
"@modern-js/utils": "^1.7.
|
|
29
|
+
"@modern-js/utils": "^1.7.12",
|
|
30
30
|
"esbuild": "^0.14.38",
|
|
31
31
|
"esbuild-register": "^3.3.3",
|
|
32
32
|
"koa-compose": "^4.1.0",
|