@modern-js/plugin-express 2.14.0 → 2.16.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 +25 -0
- package/dist/cjs/cli/index.js +56 -42
- package/dist/cjs/context.js +13 -26
- package/dist/cjs/index.js +28 -37
- package/dist/cjs/plugin.js +178 -155
- package/dist/cjs/registerRoutes.js +11 -27
- package/dist/cjs/runtime/hook.js +7 -25
- package/dist/cjs/runtime/index.js +30 -31
- package/dist/cjs/runtime/operators.js +15 -28
- package/dist/cjs/utils.js +36 -54
- package/dist/esm/cli/index.js +51 -51
- package/dist/esm/index.js +1 -2
- package/dist/esm/plugin.js +385 -376
- package/dist/esm/registerRoutes.js +12 -13
- package/dist/esm/runtime/hook.js +2 -3
- package/dist/esm/runtime/index.js +2 -3
- package/dist/esm/runtime/operators.js +196 -187
- package/dist/esm/utils.js +541 -515
- package/dist/esm-node/cli/index.js +1 -4
- package/dist/esm-node/context.js +1 -4
- package/dist/esm-node/index.js +1 -4
- package/dist/esm-node/plugin.js +108 -107
- package/dist/esm-node/registerRoutes.js +1 -4
- package/dist/esm-node/runtime/hook.js +1 -4
- package/dist/esm-node/runtime/index.js +2 -6
- package/dist/esm-node/runtime/operators.js +2 -6
- package/dist/esm-node/utils.js +4 -8
- package/package.json +15 -11
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
# @modern-js/plugin-express
|
|
2
2
|
|
|
3
|
+
## 2.16.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 4e876ab: chore: package.json include the monorepo-relative directory
|
|
8
|
+
|
|
9
|
+
chore: 在 package.json 中声明 monorepo 的子路径
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [5954330]
|
|
12
|
+
- Updated dependencies [7596520]
|
|
13
|
+
- Updated dependencies [4e876ab]
|
|
14
|
+
- @modern-js/utils@2.16.0
|
|
15
|
+
- @modern-js/bff-runtime@2.16.0
|
|
16
|
+
- @modern-js/bff-core@2.16.0
|
|
17
|
+
- @modern-js/types@2.16.0
|
|
18
|
+
|
|
19
|
+
## 2.15.0
|
|
20
|
+
|
|
21
|
+
### Patch Changes
|
|
22
|
+
|
|
23
|
+
- @modern-js/bff-core@2.15.0
|
|
24
|
+
- @modern-js/bff-runtime@2.15.0
|
|
25
|
+
- @modern-js/types@2.15.0
|
|
26
|
+
- @modern-js/utils@2.15.0
|
|
27
|
+
|
|
3
28
|
## 2.14.0
|
|
4
29
|
|
|
5
30
|
### Patch Changes
|
package/dist/cjs/cli/index.js
CHANGED
|
@@ -1,51 +1,67 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var cli_exports = {};
|
|
29
|
-
__export(cli_exports, {
|
|
30
|
-
default: () => cli_default
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: () => _default
|
|
31
8
|
});
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
9
|
+
const _path = /* @__PURE__ */ _interop_require_wildcard(require("path"));
|
|
10
|
+
const _utils = require("@modern-js/utils");
|
|
11
|
+
const _bffcore = require("@modern-js/bff-core");
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
13
|
+
if (typeof WeakMap !== "function")
|
|
14
|
+
return null;
|
|
15
|
+
var cacheBabelInterop = /* @__PURE__ */ new WeakMap();
|
|
16
|
+
var cacheNodeInterop = /* @__PURE__ */ new WeakMap();
|
|
17
|
+
return (_getRequireWildcardCache = function(nodeInterop2) {
|
|
18
|
+
return nodeInterop2 ? cacheNodeInterop : cacheBabelInterop;
|
|
19
|
+
})(nodeInterop);
|
|
20
|
+
}
|
|
21
|
+
function _interop_require_wildcard(obj, nodeInterop) {
|
|
22
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
23
|
+
return obj;
|
|
24
|
+
}
|
|
25
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
26
|
+
return {
|
|
27
|
+
default: obj
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
31
|
+
if (cache && cache.has(obj)) {
|
|
32
|
+
return cache.get(obj);
|
|
33
|
+
}
|
|
34
|
+
var newObj = {};
|
|
35
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
36
|
+
for (var key in obj) {
|
|
37
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
38
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
39
|
+
if (desc && (desc.get || desc.set)) {
|
|
40
|
+
Object.defineProperty(newObj, key, desc);
|
|
41
|
+
} else {
|
|
42
|
+
newObj[key] = obj[key];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
newObj.default = obj;
|
|
47
|
+
if (cache) {
|
|
48
|
+
cache.set(obj, newObj);
|
|
49
|
+
}
|
|
50
|
+
return newObj;
|
|
51
|
+
}
|
|
52
|
+
const _default = () => ({
|
|
37
53
|
name: "@modern-js/plugin-express",
|
|
38
54
|
setup: (api) => {
|
|
39
55
|
let bffExportsUtils;
|
|
40
56
|
const { useAppContext } = api;
|
|
41
|
-
const runtimeModulePath =
|
|
57
|
+
const runtimeModulePath = _path.resolve(__dirname, "../runtime");
|
|
42
58
|
return {
|
|
43
59
|
config() {
|
|
44
60
|
const appContext = useAppContext();
|
|
45
61
|
const { appDirectory } = appContext;
|
|
46
|
-
bffExportsUtils = (0,
|
|
62
|
+
bffExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, "server");
|
|
47
63
|
const serverRuntimePath = bffExportsUtils.getPath();
|
|
48
|
-
const relativeRuntimePath = (0,
|
|
64
|
+
const relativeRuntimePath = (0, _bffcore.getRelativeRuntimePath)(appDirectory, serverRuntimePath);
|
|
49
65
|
if (process.env.NODE_ENV === "production") {
|
|
50
66
|
return {
|
|
51
67
|
source: {
|
|
@@ -76,8 +92,8 @@ var cli_default = () => ({
|
|
|
76
92
|
},
|
|
77
93
|
addRuntimeExports(input) {
|
|
78
94
|
const currentFile = bffExportsUtils.getPath();
|
|
79
|
-
const relativeRuntimeModulePath =
|
|
80
|
-
const relativeFramePath =
|
|
95
|
+
const relativeRuntimeModulePath = _path.relative(_path.dirname(currentFile), runtimeModulePath);
|
|
96
|
+
const relativeFramePath = _path.relative(_path.dirname(currentFile), require.resolve("express"));
|
|
81
97
|
bffExportsUtils.addExport(`const pluginRuntime = require('${relativeRuntimeModulePath}');
|
|
82
98
|
const express = require('${relativeFramePath}')
|
|
83
99
|
module.exports = {
|
|
@@ -90,5 +106,3 @@ var cli_default = () => ({
|
|
|
90
106
|
};
|
|
91
107
|
}
|
|
92
108
|
});
|
|
93
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
94
|
-
0 && (module.exports = {});
|
package/dist/cjs/context.js
CHANGED
|
@@ -1,30 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
6
|
for (var name in all)
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
var context_exports = {};
|
|
19
|
-
__export(context_exports, {
|
|
7
|
+
Object.defineProperty(target, name, {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: all[name]
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
_export(exports, {
|
|
20
13
|
run: () => run,
|
|
21
14
|
useContext: () => useContext
|
|
22
15
|
});
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const { run, useContext } = (0, import_bff_core.createStorage)();
|
|
26
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
27
|
-
0 && (module.exports = {
|
|
28
|
-
run,
|
|
29
|
-
useContext
|
|
30
|
-
});
|
|
16
|
+
const _bffcore = require("@modern-js/bff-core");
|
|
17
|
+
const { run, useContext } = (0, _bffcore.createStorage)();
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,38 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var src_exports = {};
|
|
30
|
-
__export(src_exports, {
|
|
31
|
-
default: () => src_default
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
32
4
|
});
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: () => _default
|
|
8
|
+
});
|
|
9
|
+
const _plugin = /* @__PURE__ */ _interop_require_default(require("./plugin"));
|
|
10
|
+
_export_star(require("./context"), exports);
|
|
11
|
+
function _export_star(from, to) {
|
|
12
|
+
Object.keys(from).forEach(function(k) {
|
|
13
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
14
|
+
Object.defineProperty(to, k, {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function() {
|
|
17
|
+
return from[k];
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
return from;
|
|
23
|
+
}
|
|
24
|
+
function _interop_require_default(obj) {
|
|
25
|
+
return obj && obj.__esModule ? obj : {
|
|
26
|
+
default: obj
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const _default = _plugin.default;
|
package/dist/cjs/plugin.js
CHANGED
|
@@ -1,54 +1,75 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var plugin_exports = {};
|
|
29
|
-
__export(plugin_exports, {
|
|
30
|
-
default: () => plugin_default
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: () => _default
|
|
31
8
|
});
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
9
|
+
const _path = /* @__PURE__ */ _interop_require_wildcard(require("path"));
|
|
10
|
+
const _express = /* @__PURE__ */ _interop_require_default(require("express"));
|
|
11
|
+
const _cookieparser = /* @__PURE__ */ _interop_require_default(require("cookie-parser"));
|
|
12
|
+
const _utils = require("@modern-js/utils");
|
|
13
|
+
const _finalhandler = /* @__PURE__ */ _interop_require_default(require("finalhandler"));
|
|
14
|
+
const _context = require("./context");
|
|
15
|
+
const _registerRoutes = /* @__PURE__ */ _interop_require_default(require("./registerRoutes"));
|
|
16
|
+
function _interop_require_default(obj) {
|
|
17
|
+
return obj && obj.__esModule ? obj : {
|
|
18
|
+
default: obj
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
22
|
+
if (typeof WeakMap !== "function")
|
|
23
|
+
return null;
|
|
24
|
+
var cacheBabelInterop = /* @__PURE__ */ new WeakMap();
|
|
25
|
+
var cacheNodeInterop = /* @__PURE__ */ new WeakMap();
|
|
26
|
+
return (_getRequireWildcardCache = function(nodeInterop2) {
|
|
27
|
+
return nodeInterop2 ? cacheNodeInterop : cacheBabelInterop;
|
|
28
|
+
})(nodeInterop);
|
|
29
|
+
}
|
|
30
|
+
function _interop_require_wildcard(obj, nodeInterop) {
|
|
31
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
32
|
+
return obj;
|
|
33
|
+
}
|
|
34
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
35
|
+
return {
|
|
36
|
+
default: obj
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
40
|
+
if (cache && cache.has(obj)) {
|
|
41
|
+
return cache.get(obj);
|
|
42
|
+
}
|
|
43
|
+
var newObj = {};
|
|
44
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
45
|
+
for (var key in obj) {
|
|
46
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
47
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
48
|
+
if (desc && (desc.get || desc.set)) {
|
|
49
|
+
Object.defineProperty(newObj, key, desc);
|
|
50
|
+
} else {
|
|
51
|
+
newObj[key] = obj[key];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
newObj.default = obj;
|
|
56
|
+
if (cache) {
|
|
57
|
+
cache.set(obj, newObj);
|
|
58
|
+
}
|
|
59
|
+
return newObj;
|
|
60
|
+
}
|
|
61
|
+
const debug = (0, _utils.createDebugger)("express");
|
|
41
62
|
const findAppModule = async (apiDir) => {
|
|
42
63
|
const exts = [
|
|
43
64
|
".ts",
|
|
44
65
|
".js"
|
|
45
66
|
];
|
|
46
|
-
const paths = exts.map((ext) =>
|
|
67
|
+
const paths = exts.map((ext) => _path.resolve(apiDir, `app${ext}`));
|
|
47
68
|
for (const filename of paths) {
|
|
48
|
-
if (await
|
|
69
|
+
if (await _utils.fs.pathExists(filename)) {
|
|
49
70
|
delete require.cache[filename];
|
|
50
71
|
return [
|
|
51
|
-
(0,
|
|
72
|
+
(0, _utils.compatRequire)(filename),
|
|
52
73
|
require(filename)
|
|
53
74
|
];
|
|
54
75
|
}
|
|
@@ -57,136 +78,138 @@ const findAppModule = async (apiDir) => {
|
|
|
57
78
|
};
|
|
58
79
|
const initMiddlewares = (middleware, app) => {
|
|
59
80
|
middleware.forEach((middlewareItem) => {
|
|
60
|
-
const middlewareFunc = typeof middlewareItem === "string" ? (0,
|
|
81
|
+
const middlewareFunc = typeof middlewareItem === "string" ? (0, _utils.compatRequire)(middlewareItem) : middlewareItem;
|
|
61
82
|
app.use(middlewareFunc);
|
|
62
83
|
});
|
|
63
84
|
};
|
|
64
85
|
const useRun = (app) => {
|
|
65
86
|
app.use((req, res, next) => {
|
|
66
|
-
(0,
|
|
87
|
+
(0, _context.run)({
|
|
67
88
|
req,
|
|
68
89
|
res
|
|
69
90
|
}, next);
|
|
70
91
|
});
|
|
71
92
|
};
|
|
72
93
|
const initApp = (app) => {
|
|
73
|
-
app.use((0,
|
|
74
|
-
app.use(
|
|
75
|
-
app.use(
|
|
94
|
+
app.use((0, _cookieparser.default)());
|
|
95
|
+
app.use(_express.default.text());
|
|
96
|
+
app.use(_express.default.urlencoded({
|
|
76
97
|
extended: true
|
|
77
98
|
}));
|
|
78
|
-
app.use(
|
|
99
|
+
app.use(_express.default.json());
|
|
79
100
|
return app;
|
|
80
101
|
};
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
102
|
+
const _default = () => {
|
|
103
|
+
return {
|
|
104
|
+
name: "@modern-js/plugin-express",
|
|
105
|
+
pre: [
|
|
106
|
+
"@modern-js/plugin-bff"
|
|
107
|
+
],
|
|
108
|
+
post: [
|
|
109
|
+
"@modern-js/plugin-server"
|
|
110
|
+
],
|
|
111
|
+
setup: (api) => {
|
|
112
|
+
return {
|
|
113
|
+
async prepareApiServer({ pwd, config, render }) {
|
|
114
|
+
var _userConfig_bff;
|
|
115
|
+
let app;
|
|
116
|
+
const appContext = api.useAppContext();
|
|
117
|
+
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
118
|
+
const apiDirectory = appContext.apiDirectory;
|
|
119
|
+
const apiDir = apiDirectory || _path.join(pwd, "./api");
|
|
120
|
+
const mode = appContext.apiMode;
|
|
121
|
+
const userConfig = api.useConfigContext();
|
|
122
|
+
if (mode === "framework") {
|
|
123
|
+
const appModule = await findAppModule(apiDir);
|
|
124
|
+
app = appModule[0];
|
|
125
|
+
const hooks = appModule[1];
|
|
126
|
+
if (!app || !app.use) {
|
|
127
|
+
app = (0, _express.default)();
|
|
128
|
+
}
|
|
129
|
+
initApp(app);
|
|
130
|
+
if (config) {
|
|
131
|
+
const { middleware } = config;
|
|
132
|
+
initMiddlewares(middleware, app);
|
|
133
|
+
}
|
|
134
|
+
useRun(app);
|
|
135
|
+
(0, _registerRoutes.default)(app, apiHandlerInfos);
|
|
136
|
+
if (hooks) {
|
|
137
|
+
const { afterLambdaRegisted } = hooks;
|
|
138
|
+
if (afterLambdaRegisted) {
|
|
139
|
+
afterLambdaRegisted(app);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
} else if (mode === "function") {
|
|
143
|
+
app = (0, _express.default)();
|
|
144
|
+
initApp(app);
|
|
145
|
+
if (config) {
|
|
146
|
+
const { middleware } = config;
|
|
147
|
+
initMiddlewares(middleware, app);
|
|
148
|
+
}
|
|
149
|
+
useRun(app);
|
|
150
|
+
(0, _registerRoutes.default)(app, apiHandlerInfos);
|
|
151
|
+
} else {
|
|
152
|
+
throw new Error(`mode must be function or framework`);
|
|
117
153
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
useRun(app);
|
|
127
|
-
(0, import_registerRoutes.default)(app, apiHandlerInfos);
|
|
128
|
-
} else {
|
|
129
|
-
throw new Error(`mode must be function or framework`);
|
|
130
|
-
}
|
|
131
|
-
if (((_a = userConfig.bff) == null ? void 0 : _a.enableHandleWeb) && render) {
|
|
132
|
-
app.use(async (req, res, next) => {
|
|
133
|
-
const html = await render(req, res);
|
|
134
|
-
if (html) {
|
|
135
|
-
res.end(html);
|
|
154
|
+
if (((_userConfig_bff = userConfig.bff) === null || _userConfig_bff === void 0 ? void 0 : _userConfig_bff.enableHandleWeb) && render) {
|
|
155
|
+
app.use(async (req, res, next) => {
|
|
156
|
+
const html = await render(req, res);
|
|
157
|
+
if (html) {
|
|
158
|
+
res.end(html);
|
|
159
|
+
}
|
|
160
|
+
next();
|
|
161
|
+
});
|
|
136
162
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
163
|
+
return (req, res) => new Promise((resolve, reject) => {
|
|
164
|
+
const handler = (err) => {
|
|
165
|
+
if (err) {
|
|
166
|
+
return reject(err);
|
|
167
|
+
}
|
|
168
|
+
return (0, _finalhandler.default)(req, res, {})(null);
|
|
169
|
+
};
|
|
170
|
+
res.on("finish", (err) => {
|
|
171
|
+
if (err) {
|
|
172
|
+
return reject(err);
|
|
173
|
+
}
|
|
174
|
+
return resolve();
|
|
175
|
+
});
|
|
176
|
+
return app(req, res, handler);
|
|
177
|
+
});
|
|
178
|
+
},
|
|
179
|
+
prepareWebServer({ config }, next) {
|
|
180
|
+
var _userConfig_server;
|
|
181
|
+
const userConfig = api.useConfigContext();
|
|
182
|
+
if (!(userConfig === null || userConfig === void 0 ? void 0 : (_userConfig_server = userConfig.server) === null || _userConfig_server === void 0 ? void 0 : _userConfig_server.enableFrameworkExt)) {
|
|
183
|
+
return next();
|
|
144
184
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
185
|
+
const app = (0, _express.default)();
|
|
186
|
+
initApp(app);
|
|
187
|
+
if (config) {
|
|
188
|
+
const { middleware } = config;
|
|
189
|
+
debug("web middleware", middleware);
|
|
190
|
+
initMiddlewares(middleware, app);
|
|
150
191
|
}
|
|
151
|
-
return
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (err) {
|
|
173
|
-
return reject(err);
|
|
174
|
-
}
|
|
175
|
-
if (res.headersSent && res.statusCode !== 200) {
|
|
176
|
-
(0, import_finalhandler.default)(req, res, {})(null);
|
|
177
|
-
}
|
|
178
|
-
return resolve();
|
|
179
|
-
};
|
|
180
|
-
res.on("finish", (err) => {
|
|
181
|
-
if (err) {
|
|
182
|
-
return reject(err);
|
|
183
|
-
}
|
|
184
|
-
return resolve();
|
|
185
|
-
});
|
|
186
|
-
return app(req, res, handler);
|
|
187
|
-
});
|
|
192
|
+
return (ctx) => new Promise((resolve, reject) => {
|
|
193
|
+
const { source: { req, res } } = ctx;
|
|
194
|
+
const handler = (err) => {
|
|
195
|
+
if (err) {
|
|
196
|
+
return reject(err);
|
|
197
|
+
}
|
|
198
|
+
if (res.headersSent && res.statusCode !== 200) {
|
|
199
|
+
(0, _finalhandler.default)(req, res, {})(null);
|
|
200
|
+
}
|
|
201
|
+
return resolve();
|
|
202
|
+
};
|
|
203
|
+
res.on("finish", (err) => {
|
|
204
|
+
if (err) {
|
|
205
|
+
return reject(err);
|
|
206
|
+
}
|
|
207
|
+
return resolve();
|
|
208
|
+
});
|
|
209
|
+
return app(req, res, handler);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
};
|
|
188
213
|
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
192
|
-
0 && (module.exports = {});
|
|
214
|
+
};
|
|
215
|
+
};
|