@modern-js/bff-core 1.1.0-alpha.0 → 1.1.2

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
  /**
@@ -38,17 +38,39 @@ const clearRouteName = routeName => {
38
38
 
39
39
  export const isHandler = input => input && typeof input === 'function';
40
40
 
41
- const isFunction = input => input && {}.toString.call(input) === '[object Function]';
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
- export const requireHandlerModule = modulePath => {
44
- const {
45
- register
46
- } = require('esbuild-register/dist/node');
66
+ const requiredModule = requireFn(modulePath);
67
+ return requiredModule;
68
+ };
69
+ };
47
70
 
48
- const {
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(':')) {
@@ -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
  /**
@@ -57,17 +57,39 @@ const isHandler = input => input && typeof input === 'function';
57
57
 
58
58
  exports.isHandler = isHandler;
59
59
 
60
- const isFunction = input => input && {}.toString.call(input) === '[object Function]';
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
- const requireHandlerModule = modulePath => {
63
- const {
64
- register
65
- } = require('esbuild-register/dist/node');
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
- const {
68
- unregister
69
- } = register({});
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 => {
@@ -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;
@@ -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) => any;
8
+ export declare const requireHandlerModule: (modulePath: string) => HandlerModule;
9
9
  export declare const sortRoutes: (apiHandlers: APIHandlerInfo[]) => APIHandlerInfo[];
10
10
  export {};
@@ -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.0-alpha.0",
14
+ "version": "1.1.2",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -25,8 +25,8 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@babel/runtime": "^7.18.0",
28
- "@modern-js/bff-runtime": "^1.3.0-alpha.0",
29
- "@modern-js/utils": "^1.7.9-alpha.0",
28
+ "@modern-js/bff-runtime": "^1.3.0",
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",
package/CHANGELOG.md DELETED
@@ -1,16 +0,0 @@
1
- # @modern-js/bff-core
2
-
3
- ## 1.1.0-alpha.0
4
-
5
- ### Minor Changes
6
-
7
- - 77a8e9e1b: feat: support bff operators
8
-
9
- ### Patch Changes
10
-
11
- - 7b9e302e2: fix: incorrect @babel/runtime version
12
- - Updated dependencies [77a8e9e1b]
13
- - Updated dependencies [9cd364e06]
14
- - Updated dependencies [a90bc96bd]
15
- - @modern-js/bff-runtime@1.3.0-alpha.0
16
- - @modern-js/utils@1.7.9-alpha.0