@modern-js/bff-core 1.21.7-beta.0 → 1.22.1

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.
@@ -4,15 +4,24 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.registerPaths = exports.getRelativeRuntimePath = exports.createMatchPath = void 0;
7
+
7
8
  var path = _interopRequireWildcard(require("path"));
9
+
8
10
  var os = _interopRequireWildcard(require("os"));
11
+
9
12
  var _fs = _interopRequireDefault(require("fs"));
13
+
10
14
  var _module = _interopRequireDefault(require("module"));
15
+
11
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
12
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
+
13
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
+
14
22
  const getRelativeRuntimePath = (appDirectory, serverRuntimePath) => {
15
23
  let relativeRuntimePath = '';
24
+
16
25
  if (os.platform() === 'win32') {
17
26
  // isRelative function in babel-plugin-resolver plugin can't handle windows relative path correctly, see babel-plugin-resolver's utils.
18
27
  relativeRuntimePath = `../${path.relative(appDirectory, serverRuntimePath)}`;
@@ -20,15 +29,20 @@ const getRelativeRuntimePath = (appDirectory, serverRuntimePath) => {
20
29
  // Look up one level, because the artifacts after build have dist directories
21
30
  relativeRuntimePath = path.join('../', path.relative(appDirectory, serverRuntimePath));
22
31
  }
32
+
23
33
  if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
24
34
  relativeRuntimePath = `./${path.relative(appDirectory, serverRuntimePath)}`;
25
35
  }
36
+
26
37
  return relativeRuntimePath;
27
38
  };
39
+
28
40
  exports.getRelativeRuntimePath = getRelativeRuntimePath;
41
+
29
42
  const sortByLongestPrefix = arr => {
30
43
  return arr.concat().sort((a, b) => b.length - a.length);
31
44
  };
45
+
32
46
  const createMatchPath = paths => {
33
47
  const sortedKeys = sortByLongestPrefix(Object.keys(paths));
34
48
  const sortedPaths = {};
@@ -39,48 +53,63 @@ const createMatchPath = paths => {
39
53
  const found = Object.keys(sortedPaths).find(key => {
40
54
  return request.startsWith(key);
41
55
  });
56
+
42
57
  if (found) {
43
58
  let foundPaths = sortedPaths[found];
59
+
44
60
  if (!Array.isArray(foundPaths)) {
45
61
  foundPaths = [foundPaths];
46
62
  }
63
+
47
64
  foundPaths = foundPaths.filter(foundPath => path.isAbsolute(foundPath));
65
+
48
66
  for (const p of foundPaths) {
49
67
  const foundPath = request.replace(found, p);
68
+
50
69
  if (_fs.default.existsSync(foundPath)) {
51
70
  return foundPath;
52
71
  }
53
72
  }
73
+
54
74
  return request.replace(found, foundPaths[0]);
55
75
  }
76
+
56
77
  return null;
57
78
  };
58
- };
79
+ }; // every path must be a absolute path;
80
+
59
81
 
60
- // every path must be a absolute path;
61
82
  exports.createMatchPath = createMatchPath;
83
+
62
84
  const registerPaths = paths => {
63
- const originalResolveFilename = _module.default._resolveFilename;
64
- // eslint-disable-next-line node/no-unsupported-features/node-builtins
85
+ const originalResolveFilename = _module.default._resolveFilename; // eslint-disable-next-line node/no-unsupported-features/node-builtins
86
+
65
87
  const {
66
88
  builtinModules
67
89
  } = _module.default;
68
90
  const matchPath = createMatchPath(paths);
91
+
69
92
  _module.default._resolveFilename = function (request, _parent) {
70
93
  const isCoreModule = builtinModules.includes(request);
94
+
71
95
  if (!isCoreModule) {
72
96
  const matched = matchPath(request);
97
+
73
98
  if (matched) {
74
99
  // eslint-disable-next-line prefer-rest-params
75
100
  const modifiedArguments = [matched, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
101
+
76
102
  return originalResolveFilename.apply(this, modifiedArguments);
77
103
  }
78
- }
79
- // eslint-disable-next-line prefer-rest-params
104
+ } // eslint-disable-next-line prefer-rest-params
105
+
106
+
80
107
  return originalResolveFilename.apply(this, arguments);
81
108
  };
109
+
82
110
  return () => {
83
111
  _module.default._resolveFilename = originalResolveFilename;
84
112
  };
85
113
  };
114
+
86
115
  exports.registerPaths = registerPaths;
@@ -4,6 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.debug = void 0;
7
+
7
8
  var _utils = require("@modern-js/utils");
9
+
8
10
  const debug = (0, _utils.createDebugger)('bff');
9
11
  exports.debug = debug;
@@ -12,7 +12,9 @@ Object.defineProperty(exports, "debug", {
12
12
  return _debug.debug;
13
13
  }
14
14
  });
15
+
15
16
  var _storage = require("./storage");
17
+
16
18
  Object.keys(_storage).forEach(function (key) {
17
19
  if (key === "default" || key === "__esModule") return;
18
20
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -24,7 +26,9 @@ Object.keys(_storage).forEach(function (key) {
24
26
  }
25
27
  });
26
28
  });
29
+
27
30
  var _alias = require("./alias");
31
+
28
32
  Object.keys(_alias).forEach(function (key) {
29
33
  if (key === "default" || key === "__esModule") return;
30
34
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -36,8 +40,11 @@ Object.keys(_alias).forEach(function (key) {
36
40
  }
37
41
  });
38
42
  });
43
+
39
44
  var _debug = require("./debug");
45
+
40
46
  var _meta = require("./meta");
47
+
41
48
  Object.keys(_meta).forEach(function (key) {
42
49
  if (key === "default" || key === "__esModule") return;
43
50
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -49,7 +56,9 @@ Object.keys(_meta).forEach(function (key) {
49
56
  }
50
57
  });
51
58
  });
59
+
52
60
  var _validate = require("./validate");
61
+
53
62
  Object.keys(_validate).forEach(function (key) {
54
63
  if (key === "default" || key === "__esModule") return;
55
64
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -6,7 +6,9 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.isWithMetaHandler = exports.HANDLER_WITH_META = void 0;
7
7
  const HANDLER_WITH_META = 'HANDLER_WITH_META';
8
8
  exports.HANDLER_WITH_META = HANDLER_WITH_META;
9
+
9
10
  const isWithMetaHandler = handler => {
10
11
  return typeof handler === 'function' && handler[HANDLER_WITH_META];
11
12
  };
13
+
12
14
  exports.isWithMetaHandler = isWithMetaHandler;
@@ -4,19 +4,26 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.createStorage = void 0;
7
+
7
8
  var ah = _interopRequireWildcard(require("async_hooks"));
9
+
8
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
+
9
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
+
10
14
  const createStorage = () => {
11
15
  let storage;
16
+
12
17
  if (typeof ah.AsyncLocalStorage !== 'undefined') {
13
18
  storage = new ah.AsyncLocalStorage();
14
19
  }
20
+
15
21
  const run = (context, cb) => {
16
22
  if (!storage) {
17
23
  throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
18
24
  `);
19
25
  }
26
+
20
27
  return new Promise((resolve, reject) => {
21
28
  storage.run(context, () => {
22
29
  try {
@@ -27,20 +34,26 @@ const createStorage = () => {
27
34
  });
28
35
  });
29
36
  };
37
+
30
38
  const useContext = () => {
31
39
  if (!storage) {
32
40
  throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
33
41
  `);
34
42
  }
43
+
35
44
  const context = storage.getStore();
45
+
36
46
  if (!context) {
37
47
  throw new Error(`Can't call useContext out of scope, it should be placed in the bff function`);
38
48
  }
49
+
39
50
  return context;
40
51
  };
52
+
41
53
  return {
42
54
  run,
43
55
  useContext
44
56
  };
45
57
  };
58
+
46
59
  exports.createStorage = createStorage;
@@ -4,50 +4,65 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.validateFunction = exports.getTypeErrorMessage = exports.ERR_INVALID_ARG_TYPE = void 0;
7
+
7
8
  var _util = _interopRequireDefault(require("util"));
9
+
8
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
9
12
  // fork from https://github.com/nodejs/node/blob/master/lib/internal/errors.js
10
13
  const getTypeErrorMessage = actual => {
11
14
  let msg = '';
15
+
12
16
  if (actual == null) {
13
17
  msg += `. Received ${actual}`;
14
18
  } else if (typeof actual === 'function' && actual.name) {
15
19
  msg += `. Received function ${actual.name}`;
16
20
  } else if (typeof actual === 'object') {
17
21
  var _actual$constructor;
22
+
18
23
  if ((_actual$constructor = actual.constructor) !== null && _actual$constructor !== void 0 && _actual$constructor.name) {
19
24
  msg += `. Received an instance of ${actual.constructor.name}`;
20
25
  } else {
21
26
  const inspected = _util.default.inspect(actual, {
22
27
  depth: -1
23
28
  });
29
+
24
30
  msg += `. Received ${inspected}`;
25
31
  }
26
32
  } else {
27
33
  let inspected = _util.default.inspect(actual, {
28
34
  colors: false
29
35
  });
36
+
30
37
  if (inspected.length > 25) {
31
38
  inspected = `${inspected.slice(0, 25)}...`;
32
39
  }
40
+
33
41
  msg += `. Received type ${typeof actual} (${inspected})`;
34
42
  }
43
+
35
44
  return msg;
36
- };
45
+ }; // eslint-disable-next-line @typescript-eslint/naming-convention
46
+
37
47
 
38
- // eslint-disable-next-line @typescript-eslint/naming-convention
39
48
  exports.getTypeErrorMessage = getTypeErrorMessage;
49
+
40
50
  class ERR_INVALID_ARG_TYPE extends Error {
41
51
  constructor(funcName, expectedType, actual) {
42
52
  const message = `[ERR_INVALID_ARG_TYPE]: The '${funcName}' argument must be of type ${expectedType}${getTypeErrorMessage(actual)}`;
43
53
  super(message);
44
54
  }
55
+
45
56
  }
57
+
46
58
  exports.ERR_INVALID_ARG_TYPE = ERR_INVALID_ARG_TYPE;
59
+
47
60
  const validateFunction = (maybeFunc, name) => {
48
61
  if (typeof maybeFunc !== 'function') {
49
62
  throw new ERR_INVALID_ARG_TYPE(name, 'function', maybeFunc);
50
63
  }
64
+
51
65
  return true;
52
66
  };
67
+
53
68
  exports.validateFunction = validateFunction;
@@ -4,6 +4,7 @@ export declare enum APIMode {
4
4
  * 框架模式
5
5
  */
6
6
  FARMEWORK = "framework",
7
+
7
8
  /**
8
9
  * 函数模式
9
10
  */
@@ -36,6 +36,7 @@ export declare class ApiRouter {
36
36
  * 如果用户未传入或传入空串,默认为 /api
37
37
  * 如果传入 /,则 prefix 为 /
38
38
  */
39
+
39
40
  private initPrefix;
40
41
  private validateAbsolute;
41
42
  private getExactApiMode;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.21.7-beta.0",
14
+ "version": "1.22.1",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -26,23 +26,23 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@babel/runtime": "^7.18.0",
29
- "@modern-js/bff-runtime": "2.0.0-beta.2",
30
- "@modern-js/utils": "2.0.0-beta.2",
31
- "esbuild": "0.15.7",
29
+ "esbuild": "^0.14.38",
32
30
  "esbuild-register": "^3.3.3",
33
31
  "koa-compose": "^4.1.0",
34
- "reflect-metadata": "^0.1.13"
32
+ "reflect-metadata": "^0.1.13",
33
+ "@modern-js/bff-runtime": "1.22.1",
34
+ "@modern-js/utils": "1.22.1"
35
35
  },
36
36
  "devDependencies": {
37
- "@scripts/build": "2.0.0-beta.2",
38
- "@scripts/jest-config": "2.0.0-beta.2",
39
37
  "@types/jest": "^27",
40
38
  "@types/koa-compose": "^3.2.5",
41
39
  "@types/node": "^14",
42
40
  "jest": "^27",
43
- "type-fest": "2.15.0",
41
+ "type-fest": "^2.13.0",
44
42
  "typescript": "^4",
45
- "zod": "^3.17.3"
43
+ "zod": "^3.17.3",
44
+ "@scripts/build": "1.22.1",
45
+ "@scripts/jest-config": "1.22.1"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "zod": "^3.17.3"
@@ -61,9 +61,32 @@
61
61
  "registry": "https://registry.npmjs.org/",
62
62
  "access": "public"
63
63
  },
64
+ "wireit": {
65
+ "build": {
66
+ "command": "modern build",
67
+ "files": [
68
+ "src/**/*",
69
+ "tsconfig.json",
70
+ "package.json"
71
+ ],
72
+ "output": [
73
+ "dist/**/*"
74
+ ]
75
+ },
76
+ "test": {
77
+ "command": "jest --passWithNoTests",
78
+ "files": [
79
+ "src/**/*",
80
+ "tsconfig.json",
81
+ "package.json",
82
+ "tests/**/*"
83
+ ],
84
+ "output": []
85
+ }
86
+ },
64
87
  "scripts": {
65
- "new": "modern-lib new",
66
- "build": "modern-lib build",
67
- "test": "jest --passWithNoTests"
88
+ "new": "modern new",
89
+ "build": "wireit",
90
+ "test": "wireit"
68
91
  }
69
92
  }