@modern-js/server 2.15.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 +16 -0
- package/dist/cjs/constants.js +12 -28
- package/dist/cjs/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +14 -32
- package/dist/cjs/dev-tools/dev-middleware/hmr-client/index.js +24 -42
- package/dist/cjs/dev-tools/dev-middleware/index.js +45 -50
- package/dist/cjs/dev-tools/dev-middleware/socketServer.js +48 -46
- package/dist/cjs/dev-tools/https/index.js +21 -38
- package/dist/cjs/dev-tools/mock/getMockData.js +33 -33
- package/dist/cjs/dev-tools/mock/index.js +67 -47
- package/dist/cjs/dev-tools/register/index.js +59 -70
- package/dist/cjs/dev-tools/watcher/dependencyTree.js +52 -55
- package/dist/cjs/dev-tools/watcher/index.js +52 -53
- package/dist/cjs/dev-tools/watcher/statsCache.js +37 -47
- package/dist/cjs/index.js +16 -28
- package/dist/cjs/server/devServer.js +163 -109
- package/dist/cjs/server/index.js +11 -29
- package/dist/cjs/types.js +4 -15
- package/dist/esm/constants.js +19 -20
- package/dist/esm/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +43 -44
- package/dist/esm/dev-tools/dev-middleware/hmr-client/index.js +148 -157
- package/dist/esm/dev-tools/dev-middleware/index.js +307 -291
- package/dist/esm/dev-tools/dev-middleware/socketServer.js +216 -202
- package/dist/esm/dev-tools/https/index.js +189 -182
- package/dist/esm/dev-tools/mock/getMockData.js +297 -278
- package/dist/esm/dev-tools/mock/index.js +198 -192
- package/dist/esm/dev-tools/register/index.js +141 -141
- package/dist/esm/dev-tools/watcher/dependencyTree.js +153 -138
- package/dist/esm/dev-tools/watcher/index.js +187 -175
- package/dist/esm/dev-tools/watcher/statsCache.js +118 -116
- package/dist/esm/index.js +9 -8
- package/dist/esm/server/devServer.js +803 -722
- package/dist/esm/server/index.js +80 -76
- package/dist/esm/types.js +1 -1
- package/dist/esm-node/constants.js +4 -5
- package/dist/esm-node/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +2 -11
- package/dist/esm-node/dev-tools/dev-middleware/hmr-client/index.js +125 -139
- package/dist/esm-node/dev-tools/dev-middleware/index.js +31 -17
- package/dist/esm-node/dev-tools/dev-middleware/socketServer.js +33 -12
- package/dist/esm-node/dev-tools/https/index.js +13 -15
- package/dist/esm-node/dev-tools/mock/getMockData.js +20 -12
- package/dist/esm-node/dev-tools/mock/index.js +7 -7
- package/dist/esm-node/dev-tools/register/index.js +38 -29
- package/dist/esm-node/dev-tools/watcher/dependencyTree.js +32 -21
- package/dist/esm-node/dev-tools/watcher/index.js +31 -18
- package/dist/esm-node/dev-tools/watcher/statsCache.js +18 -8
- package/dist/esm-node/index.js +2 -5
- package/dist/esm-node/server/devServer.js +87 -62
- package/dist/esm-node/server/index.js +1 -4
- package/dist/esm-node/types.js +1 -0
- package/dist/types/server/devServer.d.ts +1 -1
- package/package.json +13 -9
|
@@ -1,214 +1,228 @@
|
|
|
1
|
-
function
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
function _class_call_check(instance, Constructor) {
|
|
2
|
+
if (!(instance instanceof Constructor)) {
|
|
3
|
+
throw new TypeError("Cannot call a class as a function");
|
|
4
|
+
}
|
|
5
5
|
}
|
|
6
6
|
function _defineProperties(target, props) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
for (var i = 0; i < props.length; i++) {
|
|
8
|
+
var descriptor = props[i];
|
|
9
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
10
|
+
descriptor.configurable = true;
|
|
11
|
+
if ("value" in descriptor)
|
|
12
|
+
descriptor.writable = true;
|
|
13
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
14
|
+
}
|
|
14
15
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
function _create_class(Constructor, protoProps, staticProps) {
|
|
17
|
+
if (protoProps)
|
|
18
|
+
_defineProperties(Constructor.prototype, protoProps);
|
|
19
|
+
if (staticProps)
|
|
20
|
+
_defineProperties(Constructor, staticProps);
|
|
21
|
+
return Constructor;
|
|
19
22
|
}
|
|
20
|
-
function
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
function _define_property(obj, key, value) {
|
|
24
|
+
if (key in obj) {
|
|
25
|
+
Object.defineProperty(obj, key, {
|
|
26
|
+
value,
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true
|
|
30
|
+
});
|
|
31
|
+
} else {
|
|
32
|
+
obj[key] = value;
|
|
33
|
+
}
|
|
34
|
+
return obj;
|
|
32
35
|
}
|
|
33
36
|
import ws from "ws";
|
|
34
37
|
import { logger } from "@modern-js/utils";
|
|
35
|
-
var SocketServer =
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
_this.wsServer.handleUpgrade(req, sock, head, function(connection) {
|
|
64
|
-
_this.wsServer.emit("connection", connection, req);
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
this.wsServer.on("error", function(err) {
|
|
68
|
-
logger.error(err);
|
|
69
|
-
});
|
|
70
|
-
this.timer = setInterval(function() {
|
|
71
|
-
_this.wsServer.clients.forEach(function(socket) {
|
|
72
|
-
var extWs = socket;
|
|
73
|
-
if (!extWs.isAlive) {
|
|
74
|
-
extWs.terminate();
|
|
75
|
-
} else {
|
|
76
|
-
extWs.isAlive = false;
|
|
77
|
-
extWs.ping(function() {});
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
}, 3e4);
|
|
81
|
-
this.wsServer.on("connection", function(socket) {
|
|
82
|
-
_this.onConnect(socket);
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
},
|
|
86
|
-
{
|
|
87
|
-
key: "updateStats",
|
|
88
|
-
value: function updateStats(stats) {
|
|
89
|
-
this.stats = stats;
|
|
90
|
-
this.sendStats();
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
{
|
|
94
|
-
// write message to each socket
|
|
95
|
-
key: "sockWrite",
|
|
96
|
-
value: function sockWrite(type, data) {
|
|
97
|
-
var _this = this;
|
|
98
|
-
this.sockets.forEach(function(socket) {
|
|
99
|
-
_this.send(socket, JSON.stringify({
|
|
100
|
-
type: type,
|
|
101
|
-
data: data
|
|
102
|
-
}));
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
key: "singleWrite",
|
|
108
|
-
value: function singleWrite(socket, type, data) {
|
|
109
|
-
this.send(socket, JSON.stringify({
|
|
110
|
-
type: type,
|
|
111
|
-
data: data
|
|
112
|
-
}));
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
{
|
|
116
|
-
key: "close",
|
|
117
|
-
value: function close() {
|
|
118
|
-
this.sockets.forEach(function(socket) {
|
|
119
|
-
socket.close();
|
|
120
|
-
});
|
|
121
|
-
if (this.timer) {
|
|
122
|
-
clearInterval(this.timer);
|
|
123
|
-
this.timer = null;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
},
|
|
127
|
-
{
|
|
128
|
-
key: "onConnect",
|
|
129
|
-
value: function onConnect(socket) {
|
|
130
|
-
var _this = this;
|
|
131
|
-
var connection = socket;
|
|
132
|
-
connection.isAlive = true;
|
|
133
|
-
connection.on("pong", function() {
|
|
134
|
-
connection.isAlive = true;
|
|
135
|
-
});
|
|
136
|
-
if (!connection) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
this.sockets.push(connection);
|
|
140
|
-
connection.on("close", function() {
|
|
141
|
-
var idx = _this.sockets.indexOf(connection);
|
|
142
|
-
if (idx >= 0) {
|
|
143
|
-
_this.sockets.splice(idx, 1);
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
if (this.options.hot || this.options.hot === "only") {
|
|
147
|
-
this.singleWrite(connection, "hot");
|
|
148
|
-
}
|
|
149
|
-
if (this.options.liveReload) {
|
|
150
|
-
this.singleWrite(connection, "liveReload");
|
|
151
|
-
}
|
|
152
|
-
if (this.stats) {
|
|
153
|
-
this.sendStats(true);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
},
|
|
157
|
-
{
|
|
158
|
-
// get standard stats
|
|
159
|
-
key: "getStats",
|
|
160
|
-
value: function getStats() {
|
|
161
|
-
var curStats = this.stats;
|
|
162
|
-
if (!curStats) {
|
|
163
|
-
return null;
|
|
164
|
-
}
|
|
165
|
-
var defaultStats = {
|
|
166
|
-
all: false,
|
|
167
|
-
hash: true,
|
|
168
|
-
assets: true,
|
|
169
|
-
warnings: true,
|
|
170
|
-
errors: true,
|
|
171
|
-
errorDetails: false
|
|
172
|
-
};
|
|
173
|
-
return curStats.toJson(defaultStats);
|
|
38
|
+
var SocketServer = /* @__PURE__ */ function() {
|
|
39
|
+
"use strict";
|
|
40
|
+
function SocketServer2(options) {
|
|
41
|
+
_class_call_check(this, SocketServer2);
|
|
42
|
+
_define_property(this, "wsServer", void 0);
|
|
43
|
+
_define_property(this, "sockets", []);
|
|
44
|
+
_define_property(this, "options", void 0);
|
|
45
|
+
_define_property(this, "app", void 0);
|
|
46
|
+
_define_property(this, "stats", void 0);
|
|
47
|
+
_define_property(this, "timer", null);
|
|
48
|
+
this.options = options;
|
|
49
|
+
}
|
|
50
|
+
_create_class(SocketServer2, [
|
|
51
|
+
{
|
|
52
|
+
key: "prepare",
|
|
53
|
+
value: (
|
|
54
|
+
// create socket, install socket handler, bind socket event
|
|
55
|
+
function prepare(app) {
|
|
56
|
+
var _this = this;
|
|
57
|
+
var _this_options_client;
|
|
58
|
+
this.app = app;
|
|
59
|
+
this.wsServer = new ws.Server({
|
|
60
|
+
noServer: true,
|
|
61
|
+
path: (_this_options_client = this.options.client) === null || _this_options_client === void 0 ? void 0 : _this_options_client.path
|
|
62
|
+
});
|
|
63
|
+
this.app.on("upgrade", function(req, sock, head) {
|
|
64
|
+
if (!_this.wsServer.shouldHandle(req)) {
|
|
65
|
+
return;
|
|
174
66
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
67
|
+
_this.wsServer.handleUpgrade(req, sock, head, function(connection) {
|
|
68
|
+
_this.wsServer.emit("connection", connection, req);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
this.wsServer.on("error", function(err) {
|
|
72
|
+
logger.error(err);
|
|
73
|
+
});
|
|
74
|
+
this.timer = setInterval(function() {
|
|
75
|
+
_this.wsServer.clients.forEach(function(socket) {
|
|
76
|
+
var extWs = socket;
|
|
77
|
+
if (!extWs.isAlive) {
|
|
78
|
+
extWs.terminate();
|
|
79
|
+
} else {
|
|
80
|
+
extWs.isAlive = false;
|
|
81
|
+
extWs.ping(function() {
|
|
187
82
|
});
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}, 3e4);
|
|
86
|
+
this.wsServer.on("connection", function(socket) {
|
|
87
|
+
_this.onConnect(socket);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
)
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
key: "updateStats",
|
|
94
|
+
value: function updateStats(stats) {
|
|
95
|
+
this.stats = stats;
|
|
96
|
+
this.sendStats();
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
key: "sockWrite",
|
|
101
|
+
value: (
|
|
102
|
+
// write message to each socket
|
|
103
|
+
function sockWrite(type, data) {
|
|
104
|
+
var _this = this;
|
|
105
|
+
this.sockets.forEach(function(socket) {
|
|
106
|
+
_this.send(socket, JSON.stringify({
|
|
107
|
+
type,
|
|
108
|
+
data
|
|
109
|
+
}));
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
)
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
key: "singleWrite",
|
|
116
|
+
value: function singleWrite(socket, type, data) {
|
|
117
|
+
this.send(socket, JSON.stringify({
|
|
118
|
+
type,
|
|
119
|
+
data
|
|
120
|
+
}));
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
key: "close",
|
|
125
|
+
value: function close() {
|
|
126
|
+
this.sockets.forEach(function(socket) {
|
|
127
|
+
socket.close();
|
|
128
|
+
});
|
|
129
|
+
if (this.timer) {
|
|
130
|
+
clearInterval(this.timer);
|
|
131
|
+
this.timer = null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
key: "onConnect",
|
|
137
|
+
value: function onConnect(socket) {
|
|
138
|
+
var _this = this;
|
|
139
|
+
var connection = socket;
|
|
140
|
+
connection.isAlive = true;
|
|
141
|
+
connection.on("pong", function() {
|
|
142
|
+
connection.isAlive = true;
|
|
143
|
+
});
|
|
144
|
+
if (!connection) {
|
|
145
|
+
return;
|
|
210
146
|
}
|
|
211
|
-
|
|
212
|
-
|
|
147
|
+
this.sockets.push(connection);
|
|
148
|
+
connection.on("close", function() {
|
|
149
|
+
var idx = _this.sockets.indexOf(connection);
|
|
150
|
+
if (idx >= 0) {
|
|
151
|
+
_this.sockets.splice(idx, 1);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
if (this.options.hot || this.options.hot === "only") {
|
|
155
|
+
this.singleWrite(connection, "hot");
|
|
156
|
+
}
|
|
157
|
+
if (this.options.liveReload) {
|
|
158
|
+
this.singleWrite(connection, "liveReload");
|
|
159
|
+
}
|
|
160
|
+
if (this.stats) {
|
|
161
|
+
this.sendStats(true);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
key: "getStats",
|
|
167
|
+
value: (
|
|
168
|
+
// get standard stats
|
|
169
|
+
function getStats() {
|
|
170
|
+
var curStats = this.stats;
|
|
171
|
+
if (!curStats) {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
var defaultStats = {
|
|
175
|
+
all: false,
|
|
176
|
+
hash: true,
|
|
177
|
+
assets: true,
|
|
178
|
+
warnings: true,
|
|
179
|
+
errors: true,
|
|
180
|
+
errorDetails: false
|
|
181
|
+
};
|
|
182
|
+
return curStats.toJson(defaultStats);
|
|
183
|
+
}
|
|
184
|
+
)
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
key: "sendStats",
|
|
188
|
+
value: (
|
|
189
|
+
// determine what message should send by stats
|
|
190
|
+
function sendStats() {
|
|
191
|
+
var force = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : false;
|
|
192
|
+
var stats = this.getStats();
|
|
193
|
+
if (!stats) {
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
var shouldEmit = !force && stats && (!stats.errors || stats.errors.length === 0) && stats.assets && stats.assets.every(function(asset) {
|
|
197
|
+
return !asset.emitted;
|
|
198
|
+
});
|
|
199
|
+
if (shouldEmit) {
|
|
200
|
+
return this.sockWrite("still-ok");
|
|
201
|
+
}
|
|
202
|
+
this.sockWrite("hash", stats.hash);
|
|
203
|
+
if (stats.errors && stats.errors.length > 0) {
|
|
204
|
+
return this.sockWrite("errors", stats.errors);
|
|
205
|
+
} else if (stats.warnings && stats.warnings.length > 0) {
|
|
206
|
+
return this.sockWrite("warnings", stats.warnings);
|
|
207
|
+
} else {
|
|
208
|
+
return this.sockWrite("ok");
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
)
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
key: "send",
|
|
215
|
+
value: (
|
|
216
|
+
// send message to connecting socket
|
|
217
|
+
function send(connection, message) {
|
|
218
|
+
if (connection.readyState !== 1) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
connection.send(message);
|
|
222
|
+
}
|
|
223
|
+
)
|
|
224
|
+
}
|
|
225
|
+
]);
|
|
226
|
+
return SocketServer2;
|
|
213
227
|
}();
|
|
214
228
|
export { SocketServer as default };
|