@modern-js/server 2.0.0-beta.3 → 2.0.0-beta.6
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 +159 -0
- package/dist/js/modern/constants.js +10 -9
- package/dist/js/modern/dev-tools/dev-middleware/dev-server-plugin.js +14 -20
- package/dist/js/modern/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +14 -19
- package/dist/js/modern/dev-tools/dev-middleware/hmr-client/index.js +134 -184
- package/dist/js/modern/dev-tools/dev-middleware/hmr-client/types.d.js +0 -0
- package/dist/js/modern/dev-tools/dev-middleware/index.js +65 -60
- package/dist/js/modern/dev-tools/dev-middleware/socket-server.js +33 -59
- package/dist/js/modern/dev-tools/https/global.d.js +0 -0
- package/dist/js/modern/dev-tools/https/index.js +28 -5
- package/dist/js/modern/dev-tools/mock/getMockData.js +71 -45
- package/dist/js/modern/dev-tools/mock/index.js +34 -15
- package/dist/js/modern/dev-tools/register/index.js +71 -54
- package/dist/js/modern/dev-tools/watcher/dependency-tree.js +25 -34
- package/dist/js/modern/dev-tools/watcher/index.js +51 -33
- package/dist/js/modern/dev-tools/watcher/stats-cache.js +13 -20
- package/dist/js/modern/index.js +7 -4
- package/dist/js/modern/server/dev-server.js +191 -215
- package/dist/js/modern/server/index.js +7 -4
- package/dist/js/node/constants.js +31 -14
- package/dist/js/node/dev-tools/dev-middleware/dev-server-plugin.js +38 -24
- package/dist/js/node/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +36 -24
- package/dist/js/node/dev-tools/dev-middleware/hmr-client/index.js +56 -98
- package/dist/js/node/dev-tools/dev-middleware/hmr-client/types.d.js +0 -0
- package/dist/js/node/dev-tools/dev-middleware/index.js +94 -72
- package/dist/js/node/dev-tools/dev-middleware/socket-server.js +59 -66
- package/dist/js/node/dev-tools/https/global.d.js +0 -0
- package/dist/js/node/dev-tools/https/index.js +56 -13
- package/dist/js/node/dev-tools/mock/getMockData.js +90 -51
- package/dist/js/node/dev-tools/mock/index.js +66 -28
- package/dist/js/node/dev-tools/register/index.js +100 -64
- package/dist/js/node/dev-tools/watcher/dependency-tree.js +55 -43
- package/dist/js/node/dev-tools/watcher/index.js +83 -47
- package/dist/js/node/dev-tools/watcher/stats-cache.js +44 -30
- package/dist/js/node/index.js +29 -15
- package/dist/js/node/server/dev-server.js +220 -237
- package/dist/js/node/server/index.js +30 -11
- package/dist/js/node/types.js +15 -0
- package/dist/js/treeshaking/constants.js +21 -0
- package/dist/js/treeshaking/dev-tools/dev-middleware/dev-server-plugin.js +74 -0
- package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +51 -0
- package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/index.js +157 -0
- package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/types.d.js +1 -0
- package/dist/js/treeshaking/dev-tools/dev-middleware/index.js +363 -0
- package/dist/js/treeshaking/dev-tools/dev-middleware/socket-server.js +209 -0
- package/dist/js/treeshaking/dev-tools/https/global.d.js +1 -0
- package/dist/js/treeshaking/dev-tools/https/index.js +161 -0
- package/dist/js/treeshaking/dev-tools/mock/getMockData.js +327 -0
- package/dist/js/treeshaking/dev-tools/mock/index.js +191 -0
- package/dist/js/treeshaking/dev-tools/register/index.js +153 -0
- package/dist/js/treeshaking/dev-tools/watcher/dependency-tree.js +150 -0
- package/dist/js/treeshaking/dev-tools/watcher/index.js +200 -0
- package/dist/js/treeshaking/dev-tools/watcher/stats-cache.js +128 -0
- package/dist/js/treeshaking/index.js +9 -0
- package/dist/js/treeshaking/server/dev-server.js +800 -0
- package/dist/js/treeshaking/server/index.js +92 -0
- package/dist/js/treeshaking/types.js +1 -0
- package/dist/types/dev-tools/mock/getMockData.d.ts +2 -0
- package/dist/types/dev-tools/watcher/dependency-tree.d.ts +2 -0
- package/dist/types/dev-tools/watcher/index.d.ts +1 -1
- package/dist/types/index.d.ts +2 -0
- package/package.json +15 -12
|
@@ -1,60 +1,70 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var socket_server_exports = {};
|
|
25
|
+
__export(socket_server_exports, {
|
|
26
|
+
default: () => SocketServer
|
|
5
27
|
});
|
|
6
|
-
exports
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
-
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; }
|
|
28
|
+
module.exports = __toCommonJS(socket_server_exports);
|
|
29
|
+
var import_ws = __toESM(require("ws"));
|
|
30
|
+
var import_utils = require("@modern-js/utils");
|
|
11
31
|
class SocketServer {
|
|
12
32
|
constructor(options) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
_defineProperty(this, "options", void 0);
|
|
16
|
-
_defineProperty(this, "app", void 0);
|
|
17
|
-
_defineProperty(this, "stats", void 0);
|
|
18
|
-
_defineProperty(this, "timer", null);
|
|
33
|
+
this.sockets = [];
|
|
34
|
+
this.timer = null;
|
|
19
35
|
this.options = options;
|
|
20
36
|
}
|
|
21
|
-
|
|
22
|
-
// create socket, install socket handler, bind socket event
|
|
23
37
|
prepare(app) {
|
|
24
|
-
var
|
|
38
|
+
var _a;
|
|
25
39
|
this.app = app;
|
|
26
|
-
this.wsServer = new
|
|
40
|
+
this.wsServer = new import_ws.default.Server({
|
|
27
41
|
noServer: true,
|
|
28
|
-
path: (
|
|
42
|
+
path: (_a = this.options.client) == null ? void 0 : _a.path
|
|
29
43
|
});
|
|
30
|
-
|
|
31
|
-
// listen upgrade event to handle socket
|
|
32
|
-
this.app.on('upgrade', (req, sock, head) => {
|
|
44
|
+
this.app.on("upgrade", (req, sock, head) => {
|
|
33
45
|
if (!this.wsServer.shouldHandle(req)) {
|
|
34
46
|
return;
|
|
35
47
|
}
|
|
36
|
-
this.wsServer.handleUpgrade(req, sock, head, connection => {
|
|
37
|
-
this.wsServer.emit(
|
|
48
|
+
this.wsServer.handleUpgrade(req, sock, head, (connection) => {
|
|
49
|
+
this.wsServer.emit("connection", connection, req);
|
|
38
50
|
});
|
|
39
51
|
});
|
|
40
|
-
this.wsServer.on(
|
|
41
|
-
|
|
42
|
-
_utils.logger.error(err);
|
|
52
|
+
this.wsServer.on("error", (err) => {
|
|
53
|
+
import_utils.logger.error(err);
|
|
43
54
|
});
|
|
44
55
|
this.timer = setInterval(() => {
|
|
45
|
-
this.wsServer.clients.forEach(socket => {
|
|
56
|
+
this.wsServer.clients.forEach((socket) => {
|
|
46
57
|
const extWs = socket;
|
|
47
58
|
if (!extWs.isAlive) {
|
|
48
59
|
extWs.terminate();
|
|
49
60
|
} else {
|
|
50
61
|
extWs.isAlive = false;
|
|
51
62
|
extWs.ping(() => {
|
|
52
|
-
// empty
|
|
53
63
|
});
|
|
54
64
|
}
|
|
55
65
|
});
|
|
56
|
-
},
|
|
57
|
-
this.wsServer.on(
|
|
66
|
+
}, 3e4);
|
|
67
|
+
this.wsServer.on("connection", (socket) => {
|
|
58
68
|
this.onConnect(socket);
|
|
59
69
|
});
|
|
60
70
|
}
|
|
@@ -62,24 +72,16 @@ class SocketServer {
|
|
|
62
72
|
this.stats = stats;
|
|
63
73
|
this.sendStats();
|
|
64
74
|
}
|
|
65
|
-
|
|
66
|
-
// write message to each socket
|
|
67
75
|
sockWrite(type, data) {
|
|
68
|
-
this.sockets.forEach(socket => {
|
|
69
|
-
this.send(socket, JSON.stringify({
|
|
70
|
-
type,
|
|
71
|
-
data
|
|
72
|
-
}));
|
|
76
|
+
this.sockets.forEach((socket) => {
|
|
77
|
+
this.send(socket, JSON.stringify({ type, data }));
|
|
73
78
|
});
|
|
74
79
|
}
|
|
75
80
|
singleWrite(socket, type, data) {
|
|
76
|
-
this.send(socket, JSON.stringify({
|
|
77
|
-
type,
|
|
78
|
-
data
|
|
79
|
-
}));
|
|
81
|
+
this.send(socket, JSON.stringify({ type, data }));
|
|
80
82
|
}
|
|
81
83
|
close() {
|
|
82
|
-
this.sockets.forEach(socket => {
|
|
84
|
+
this.sockets.forEach((socket) => {
|
|
83
85
|
socket.close();
|
|
84
86
|
});
|
|
85
87
|
if (this.timer) {
|
|
@@ -90,33 +92,29 @@ class SocketServer {
|
|
|
90
92
|
onConnect(socket) {
|
|
91
93
|
const connection = socket;
|
|
92
94
|
connection.isAlive = true;
|
|
93
|
-
connection.on(
|
|
95
|
+
connection.on("pong", () => {
|
|
94
96
|
connection.isAlive = true;
|
|
95
97
|
});
|
|
96
98
|
if (!connection) {
|
|
97
99
|
return;
|
|
98
100
|
}
|
|
99
101
|
this.sockets.push(connection);
|
|
100
|
-
connection.on(
|
|
102
|
+
connection.on("close", () => {
|
|
101
103
|
const idx = this.sockets.indexOf(connection);
|
|
102
104
|
if (idx >= 0) {
|
|
103
105
|
this.sockets.splice(idx, 1);
|
|
104
106
|
}
|
|
105
107
|
});
|
|
106
|
-
if (this.options.hot || this.options.hot ===
|
|
107
|
-
this.singleWrite(connection,
|
|
108
|
+
if (this.options.hot || this.options.hot === "only") {
|
|
109
|
+
this.singleWrite(connection, "hot");
|
|
108
110
|
}
|
|
109
111
|
if (this.options.liveReload) {
|
|
110
|
-
this.singleWrite(connection,
|
|
112
|
+
this.singleWrite(connection, "liveReload");
|
|
111
113
|
}
|
|
112
|
-
|
|
113
|
-
// send first stats to active client sock if stats exist
|
|
114
114
|
if (this.stats) {
|
|
115
115
|
this.sendStats(true);
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
|
-
|
|
119
|
-
// get standard stats
|
|
120
118
|
getStats() {
|
|
121
119
|
const curStats = this.stats;
|
|
122
120
|
if (!curStats) {
|
|
@@ -132,30 +130,24 @@ class SocketServer {
|
|
|
132
130
|
};
|
|
133
131
|
return curStats.toJson(defaultStats);
|
|
134
132
|
}
|
|
135
|
-
|
|
136
|
-
// determine what message should send by stats
|
|
137
133
|
sendStats(force = false) {
|
|
138
134
|
const stats = this.getStats();
|
|
139
|
-
|
|
140
|
-
// this should never happened
|
|
141
135
|
if (!stats) {
|
|
142
136
|
return null;
|
|
143
137
|
}
|
|
144
|
-
const shouldEmit = !force && stats && (!stats.errors || stats.errors.length === 0) && stats.assets && stats.assets.every(asset => !asset.emitted);
|
|
138
|
+
const shouldEmit = !force && stats && (!stats.errors || stats.errors.length === 0) && stats.assets && stats.assets.every((asset) => !asset.emitted);
|
|
145
139
|
if (shouldEmit) {
|
|
146
|
-
return this.sockWrite(
|
|
140
|
+
return this.sockWrite("still-ok");
|
|
147
141
|
}
|
|
148
|
-
this.sockWrite(
|
|
142
|
+
this.sockWrite("hash", stats.hash);
|
|
149
143
|
if (stats.errors && stats.errors.length > 0) {
|
|
150
|
-
return this.sockWrite(
|
|
144
|
+
return this.sockWrite("errors", stats.errors);
|
|
151
145
|
} else if (stats.warnings && stats.warnings.length > 0) {
|
|
152
|
-
return this.sockWrite(
|
|
146
|
+
return this.sockWrite("warnings", stats.warnings);
|
|
153
147
|
} else {
|
|
154
|
-
return this.sockWrite(
|
|
148
|
+
return this.sockWrite("ok");
|
|
155
149
|
}
|
|
156
150
|
}
|
|
157
|
-
|
|
158
|
-
// send message to connecting socket
|
|
159
151
|
send(connection, message) {
|
|
160
152
|
if (connection.readyState !== 1) {
|
|
161
153
|
return;
|
|
@@ -163,4 +155,5 @@ class SocketServer {
|
|
|
163
155
|
connection.send(message);
|
|
164
156
|
}
|
|
165
157
|
}
|
|
166
|
-
|
|
158
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
159
|
+
0 && (module.exports = {});
|
|
File without changes
|
|
@@ -1,18 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var __async = (__this, __arguments, generator) => {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
var fulfilled = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.next(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var rejected = (value) => {
|
|
34
|
+
try {
|
|
35
|
+
step(generator.throw(value));
|
|
36
|
+
} catch (e) {
|
|
37
|
+
reject(e);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
41
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var https_exports = {};
|
|
45
|
+
__export(https_exports, {
|
|
46
|
+
genHttpsOptions: () => genHttpsOptions
|
|
5
47
|
});
|
|
6
|
-
exports
|
|
7
|
-
var devcert =
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const genHttpsOptions = async userOptions => {
|
|
11
|
-
const httpsOptions = typeof userOptions === 'boolean' ? {} : userOptions;
|
|
48
|
+
module.exports = __toCommonJS(https_exports);
|
|
49
|
+
var devcert = __toESM(require("devcert"));
|
|
50
|
+
const genHttpsOptions = (userOptions) => __async(void 0, null, function* () {
|
|
51
|
+
const httpsOptions = typeof userOptions === "boolean" ? {} : userOptions;
|
|
12
52
|
if (!httpsOptions.key || !httpsOptions.cert) {
|
|
13
|
-
const selfsign =
|
|
53
|
+
const selfsign = yield devcert.certificateFor(["localhost"]);
|
|
14
54
|
return selfsign;
|
|
15
55
|
}
|
|
16
56
|
return httpsOptions;
|
|
17
|
-
};
|
|
18
|
-
|
|
57
|
+
});
|
|
58
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
59
|
+
0 && (module.exports = {
|
|
60
|
+
genHttpsOptions
|
|
61
|
+
});
|
|
@@ -1,54 +1,99 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
+
var __spreadValues = (a, b) => {
|
|
11
|
+
for (var prop in b || (b = {}))
|
|
12
|
+
if (__hasOwnProp.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
if (__getOwnPropSymbols)
|
|
15
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
+
if (__propIsEnum.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
}
|
|
19
|
+
return a;
|
|
20
|
+
};
|
|
21
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
22
|
+
var __export = (target, all) => {
|
|
23
|
+
for (var name in all)
|
|
24
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
25
|
+
};
|
|
26
|
+
var __copyProps = (to, from, except, desc) => {
|
|
27
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
28
|
+
for (let key of __getOwnPropNames(from))
|
|
29
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
30
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
31
|
+
}
|
|
32
|
+
return to;
|
|
33
|
+
};
|
|
34
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
35
|
+
var __async = (__this, __arguments, generator) => {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
var fulfilled = (value) => {
|
|
38
|
+
try {
|
|
39
|
+
step(generator.next(value));
|
|
40
|
+
} catch (e) {
|
|
41
|
+
reject(e);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var rejected = (value) => {
|
|
45
|
+
try {
|
|
46
|
+
step(generator.throw(value));
|
|
47
|
+
} catch (e) {
|
|
48
|
+
reject(e);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
52
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
var getMockData_exports = {};
|
|
56
|
+
__export(getMockData_exports, {
|
|
57
|
+
default: () => getMockData_default,
|
|
58
|
+
getMatched: () => getMatched
|
|
5
59
|
});
|
|
6
|
-
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const VALID_METHODS = ['get', 'post', 'put', 'delete', 'patch'];
|
|
14
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
15
|
-
const BODY_PARSED_METHODS = ['post', 'put', 'delete', 'patch'];
|
|
16
|
-
const createFunctionDataHandler = (method, handler) => async (context, next) => {
|
|
17
|
-
const {
|
|
18
|
-
req,
|
|
19
|
-
res
|
|
20
|
-
} = context;
|
|
60
|
+
module.exports = __toCommonJS(getMockData_exports);
|
|
61
|
+
var import_utils = require("@modern-js/utils");
|
|
62
|
+
var import_path_to_regexp = require("path-to-regexp");
|
|
63
|
+
const VALID_METHODS = ["get", "post", "put", "delete", "patch"];
|
|
64
|
+
const BODY_PARSED_METHODS = ["post", "put", "delete", "patch"];
|
|
65
|
+
const createFunctionDataHandler = (method, handler) => (context, next) => __async(void 0, null, function* () {
|
|
66
|
+
const { req, res } = context;
|
|
21
67
|
return handler(req, res, next);
|
|
22
|
-
};
|
|
23
|
-
const createStaticDataHandler = (method, handler) => context => {
|
|
24
|
-
const {
|
|
25
|
-
|
|
26
|
-
} = context;
|
|
27
|
-
res.setHeader('Content-Type', 'application/json');
|
|
68
|
+
});
|
|
69
|
+
const createStaticDataHandler = (method, handler) => (context) => {
|
|
70
|
+
const { res } = context;
|
|
71
|
+
res.setHeader("Content-Type", "application/json");
|
|
28
72
|
res.end(JSON.stringify(handler));
|
|
29
73
|
};
|
|
30
|
-
const allowTypes = [
|
|
31
|
-
const normalizeConfig = mockConfig => Object.keys(mockConfig).reduce((acc, key) => {
|
|
74
|
+
const allowTypes = ["object", "function"];
|
|
75
|
+
const normalizeConfig = (mockConfig) => Object.keys(mockConfig).reduce((acc, key) => {
|
|
32
76
|
const handler = mockConfig[key];
|
|
33
77
|
const type = typeof handler;
|
|
34
78
|
if (!allowTypes.includes(type)) {
|
|
35
|
-
throw new Error(
|
|
79
|
+
throw new Error(
|
|
80
|
+
`mock value of ${key} should be object or function, but got ${type}`
|
|
81
|
+
);
|
|
36
82
|
}
|
|
37
83
|
const meta = parseKey(key);
|
|
38
|
-
if (type ===
|
|
39
|
-
acc.push(
|
|
84
|
+
if (type === "object") {
|
|
85
|
+
acc.push(__spreadProps(__spreadValues({}, meta), {
|
|
40
86
|
handler: createStaticDataHandler(meta.method, handler)
|
|
41
87
|
}));
|
|
42
88
|
} else {
|
|
43
|
-
acc.push(
|
|
89
|
+
acc.push(__spreadProps(__spreadValues({}, meta), {
|
|
44
90
|
handler: createFunctionDataHandler(meta.method, handler)
|
|
45
91
|
}));
|
|
46
92
|
}
|
|
47
93
|
return acc;
|
|
48
94
|
}, []);
|
|
49
|
-
const _blank =
|
|
50
|
-
const parseKey = key => {
|
|
51
|
-
// 'Method /pathname' | '/pathname'
|
|
95
|
+
const _blank = " ";
|
|
96
|
+
const parseKey = (key) => {
|
|
52
97
|
const splitted = key.split(_blank).filter(Boolean);
|
|
53
98
|
if (splitted.length > 1) {
|
|
54
99
|
const [method, pathname] = splitted;
|
|
@@ -57,34 +102,25 @@ const parseKey = key => {
|
|
|
57
102
|
path: pathname
|
|
58
103
|
};
|
|
59
104
|
}
|
|
60
|
-
|
|
61
|
-
// default return get method
|
|
62
105
|
return {
|
|
63
|
-
method:
|
|
106
|
+
method: "get",
|
|
64
107
|
path: key
|
|
65
108
|
};
|
|
66
109
|
};
|
|
67
|
-
var
|
|
68
|
-
const mockModule = (0,
|
|
110
|
+
var getMockData_default = (filepath) => {
|
|
111
|
+
const mockModule = (0, import_utils.compatRequire)(filepath);
|
|
69
112
|
if (!mockModule) {
|
|
70
113
|
throw new Error(`Mock file ${filepath} parsed failed!`);
|
|
71
114
|
}
|
|
72
115
|
const data = normalizeConfig(mockModule);
|
|
73
116
|
return data;
|
|
74
117
|
};
|
|
75
|
-
exports.default = _default;
|
|
76
118
|
const getMatched = (context, mockApiList) => {
|
|
77
|
-
const {
|
|
78
|
-
|
|
79
|
-
method:
|
|
80
|
-
} = context;
|
|
81
|
-
const matched = mockApiList.find(mockApi => {
|
|
82
|
-
const {
|
|
83
|
-
method,
|
|
84
|
-
path: pathname
|
|
85
|
-
} = mockApi;
|
|
119
|
+
const { path: targetPathname, method: targetMethod } = context;
|
|
120
|
+
const matched = mockApiList.find((mockApi) => {
|
|
121
|
+
const { method, path: pathname } = mockApi;
|
|
86
122
|
if (method.toLowerCase() === targetMethod.toLowerCase()) {
|
|
87
|
-
return (0,
|
|
123
|
+
return (0, import_path_to_regexp.match)(pathname, {
|
|
88
124
|
encode: encodeURI,
|
|
89
125
|
decode: decodeURIComponent
|
|
90
126
|
})(targetPathname);
|
|
@@ -93,4 +129,7 @@ const getMatched = (context, mockApiList) => {
|
|
|
93
129
|
});
|
|
94
130
|
return matched;
|
|
95
131
|
};
|
|
96
|
-
|
|
132
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
133
|
+
0 && (module.exports = {
|
|
134
|
+
getMatched
|
|
135
|
+
});
|
|
@@ -1,24 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var __async = (__this, __arguments, generator) => {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
var fulfilled = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.next(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var rejected = (value) => {
|
|
34
|
+
try {
|
|
35
|
+
step(generator.throw(value));
|
|
36
|
+
} catch (e) {
|
|
37
|
+
reject(e);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
41
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var mock_exports = {};
|
|
45
|
+
__export(mock_exports, {
|
|
46
|
+
createMockHandler: () => createMockHandler
|
|
5
47
|
});
|
|
6
|
-
exports
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const createMockHandler = ({
|
|
15
|
-
pwd
|
|
16
|
-
}) => {
|
|
17
|
-
const exts = ['.ts', '.js'];
|
|
18
|
-
let filepath = '';
|
|
48
|
+
module.exports = __toCommonJS(mock_exports);
|
|
49
|
+
var import_path = __toESM(require("path"));
|
|
50
|
+
var import_utils = require("@modern-js/utils");
|
|
51
|
+
var import_prod_server = require("@modern-js/prod-server");
|
|
52
|
+
var import_getMockData = __toESM(require("./getMockData"));
|
|
53
|
+
const createMockHandler = ({ pwd }) => {
|
|
54
|
+
const exts = [".ts", ".js"];
|
|
55
|
+
let filepath = "";
|
|
19
56
|
for (const ext of exts) {
|
|
20
|
-
const maybeMatch =
|
|
21
|
-
if (
|
|
57
|
+
const maybeMatch = import_path.default.join(pwd, `${import_prod_server.AGGRED_DIR.mock}/index${ext}`);
|
|
58
|
+
if (import_utils.fs.existsSync(maybeMatch)) {
|
|
22
59
|
filepath = maybeMatch;
|
|
23
60
|
break;
|
|
24
61
|
}
|
|
@@ -26,20 +63,21 @@ const createMockHandler = ({
|
|
|
26
63
|
if (!filepath) {
|
|
27
64
|
return null;
|
|
28
65
|
}
|
|
29
|
-
const apiList = (0,
|
|
66
|
+
const apiList = (0, import_getMockData.default)(filepath);
|
|
30
67
|
if (!apiList || apiList.length === 0) {
|
|
31
68
|
return null;
|
|
32
69
|
}
|
|
33
|
-
return
|
|
34
|
-
const {
|
|
35
|
-
|
|
36
|
-
} = context;
|
|
37
|
-
const matched = (0, _getMockData.getMatched)(context, apiList);
|
|
70
|
+
return (context, next) => __async(void 0, null, function* () {
|
|
71
|
+
const { res } = context;
|
|
72
|
+
const matched = (0, import_getMockData.getMatched)(context, apiList);
|
|
38
73
|
if (!matched) {
|
|
39
74
|
return next();
|
|
40
75
|
}
|
|
41
|
-
res.setHeader(
|
|
76
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
42
77
|
return matched.handler(context, next);
|
|
43
|
-
};
|
|
78
|
+
});
|
|
44
79
|
};
|
|
45
|
-
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
createMockHandler
|
|
83
|
+
});
|