@modern-js/server 2.15.0 → 2.15.1-alpha.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.
Files changed (53) hide show
  1. package/dist/cjs/server/devServer.js +2 -2
  2. package/dist/esm/server/devServer.js +68 -24
  3. package/dist/esm-node/server/devServer.js +2 -2
  4. package/dist/js/modern/constants.js +19 -0
  5. package/dist/js/modern/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +41 -0
  6. package/dist/js/modern/dev-tools/dev-middleware/hmr-client/index.js +143 -0
  7. package/dist/js/modern/dev-tools/dev-middleware/index.js +94 -0
  8. package/dist/js/modern/dev-tools/dev-middleware/socket-server.js +132 -0
  9. package/dist/js/modern/dev-tools/https/index.js +49 -0
  10. package/dist/js/modern/dev-tools/mock/getMockData.js +114 -0
  11. package/dist/js/modern/dev-tools/mock/index.js +54 -0
  12. package/dist/js/modern/dev-tools/register/index.js +111 -0
  13. package/dist/js/modern/dev-tools/watcher/dependency-tree.js +59 -0
  14. package/dist/js/modern/dev-tools/watcher/index.js +106 -0
  15. package/dist/js/modern/dev-tools/watcher/stats-cache.js +58 -0
  16. package/dist/js/modern/index.js +12 -0
  17. package/dist/js/modern/server/dev-server.js +293 -0
  18. package/dist/js/modern/server/index.js +16 -0
  19. package/dist/js/modern/types.js +0 -0
  20. package/dist/js/node/constants.js +42 -0
  21. package/dist/js/node/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +65 -0
  22. package/dist/js/node/dev-tools/dev-middleware/hmr-client/index.js +152 -0
  23. package/dist/js/node/dev-tools/dev-middleware/index.js +119 -0
  24. package/dist/js/node/dev-tools/dev-middleware/socket-server.js +159 -0
  25. package/dist/js/node/dev-tools/https/index.js +72 -0
  26. package/dist/js/node/dev-tools/mock/getMockData.js +135 -0
  27. package/dist/js/node/dev-tools/mock/index.js +83 -0
  28. package/dist/js/node/dev-tools/register/index.js +138 -0
  29. package/dist/js/node/dev-tools/watcher/dependency-tree.js +89 -0
  30. package/dist/js/node/dev-tools/watcher/index.js +135 -0
  31. package/dist/js/node/dev-tools/watcher/stats-cache.js +87 -0
  32. package/dist/js/node/index.js +35 -0
  33. package/dist/js/node/server/dev-server.js +310 -0
  34. package/dist/js/node/server/index.js +39 -0
  35. package/dist/js/node/types.js +15 -0
  36. package/dist/js/treeshaking/constants.js +21 -0
  37. package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/createSocketUrl.js +51 -0
  38. package/dist/js/treeshaking/dev-tools/dev-middleware/hmr-client/index.js +157 -0
  39. package/dist/js/treeshaking/dev-tools/dev-middleware/index.js +322 -0
  40. package/dist/js/treeshaking/dev-tools/dev-middleware/socket-server.js +209 -0
  41. package/dist/js/treeshaking/dev-tools/https/index.js +193 -0
  42. package/dist/js/treeshaking/dev-tools/mock/getMockData.js +327 -0
  43. package/dist/js/treeshaking/dev-tools/mock/index.js +191 -0
  44. package/dist/js/treeshaking/dev-tools/register/index.js +153 -0
  45. package/dist/js/treeshaking/dev-tools/watcher/dependency-tree.js +150 -0
  46. package/dist/js/treeshaking/dev-tools/watcher/index.js +200 -0
  47. package/dist/js/treeshaking/dev-tools/watcher/stats-cache.js +128 -0
  48. package/dist/js/treeshaking/index.js +9 -0
  49. package/dist/js/treeshaking/server/dev-server.js +799 -0
  50. package/dist/js/treeshaking/server/index.js +92 -0
  51. package/dist/js/treeshaking/types.js +1 -0
  52. package/dist/types/server/devServer.d.ts +1 -1
  53. package/package.json +4 -4
@@ -0,0 +1,157 @@
1
+ var __getOwnPropNames = Object.getOwnPropertyNames;
2
+ var __commonJS = function(cb, mod) {
3
+ return function __require() {
4
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = {
5
+ exports: {}
6
+ }).exports, mod), mod.exports;
7
+ };
8
+ };
9
+ import stripAnsi from "@modern-js/utils/strip-ansi";
10
+ import { formatWebpackMessages } from "@modern-js/utils/format";
11
+ import { createSocketUrl } from "./createSocketUrl";
12
+ var require_hmr_client = __commonJS({
13
+ "src/dev-tools/dev-middleware/hmr-client/index.ts": function(exports, module) {
14
+ var clearOutdatedErrors = function clearOutdatedErrors() {
15
+ if (typeof console !== "undefined" && typeof console.clear === "function") {
16
+ if (hasCompileErrors) {
17
+ console.clear();
18
+ }
19
+ }
20
+ };
21
+ var handleSuccess = function handleSuccess() {
22
+ clearOutdatedErrors();
23
+ var isHotUpdate = !isFirstCompilation;
24
+ isFirstCompilation = false;
25
+ hasCompileErrors = false;
26
+ if (isHotUpdate) {
27
+ tryApplyUpdates();
28
+ }
29
+ };
30
+ var handleWarnings = function handleWarnings(warnings) {
31
+ clearOutdatedErrors();
32
+ var isHotUpdate = !isFirstCompilation;
33
+ isFirstCompilation = false;
34
+ hasCompileErrors = false;
35
+ function printWarnings() {
36
+ var formatted = formatWebpackMessages({
37
+ warnings: warnings,
38
+ errors: []
39
+ });
40
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
41
+ for(var i = 0; i < formatted.warnings.length; i++){
42
+ if (i === 5) {
43
+ console.warn("There were more warnings in other files.\nYou can find a complete log in the terminal.");
44
+ break;
45
+ }
46
+ console.warn(stripAnsi(formatted.warnings[i]));
47
+ }
48
+ }
49
+ }
50
+ printWarnings();
51
+ if (isHotUpdate) {
52
+ tryApplyUpdates();
53
+ }
54
+ };
55
+ var handleErrors = function handleErrors(errors) {
56
+ clearOutdatedErrors();
57
+ isFirstCompilation = false;
58
+ hasCompileErrors = true;
59
+ var formatted = formatWebpackMessages({
60
+ errors: errors,
61
+ warnings: []
62
+ });
63
+ if (typeof console !== "undefined" && typeof console.error === "function") {
64
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
65
+ try {
66
+ for(var _iterator = formatted.errors[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
67
+ var error = _step.value;
68
+ console.error(stripAnsi(error));
69
+ }
70
+ } catch (err) {
71
+ _didIteratorError = true;
72
+ _iteratorError = err;
73
+ } finally{
74
+ try {
75
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
76
+ _iterator.return();
77
+ }
78
+ } finally{
79
+ if (_didIteratorError) {
80
+ throw _iteratorError;
81
+ }
82
+ }
83
+ }
84
+ }
85
+ };
86
+ var handleAvailableHash = function handleAvailableHash(hash) {
87
+ mostRecentCompilationHash = hash;
88
+ };
89
+ var isUpdateAvailable = function isUpdateAvailable() {
90
+ return mostRecentCompilationHash !== __webpack_hash__;
91
+ };
92
+ var canApplyUpdates = function canApplyUpdates() {
93
+ return module.hot.status() === "idle";
94
+ };
95
+ var hadRuntimeError = false;
96
+ var socketUrl = createSocketUrl(__resourceQuery);
97
+ var connection = new WebSocket(socketUrl);
98
+ connection.onclose = function() {
99
+ if (typeof console !== "undefined" && typeof console.info === "function") {
100
+ console.info("The development server has disconnected.\nRefresh the page if necessary.");
101
+ }
102
+ };
103
+ var isFirstCompilation = true;
104
+ var mostRecentCompilationHash = null;
105
+ var hasCompileErrors = false;
106
+ connection.onmessage = function(e) {
107
+ var message = JSON.parse(e.data);
108
+ switch(message.type){
109
+ case "hash":
110
+ handleAvailableHash(message.data);
111
+ break;
112
+ case "still-ok":
113
+ case "ok":
114
+ handleSuccess();
115
+ break;
116
+ case "content-changed":
117
+ window.location.reload();
118
+ break;
119
+ case "warnings":
120
+ handleWarnings(message.data);
121
+ break;
122
+ case "errors":
123
+ handleErrors(message.data);
124
+ break;
125
+ default:
126
+ }
127
+ };
128
+ function tryApplyUpdates() {
129
+ if (!module.hot) {
130
+ window.location.reload();
131
+ return;
132
+ }
133
+ if (!isUpdateAvailable() || !canApplyUpdates()) {
134
+ return;
135
+ }
136
+ function handleApplyUpdates(err, updatedModules) {
137
+ var wantsForcedReload = err || !updatedModules || hadRuntimeError;
138
+ if (wantsForcedReload) {
139
+ window.location.reload();
140
+ return;
141
+ }
142
+ if (isUpdateAvailable()) {
143
+ tryApplyUpdates();
144
+ }
145
+ }
146
+ var result = module.hot.check(true, handleApplyUpdates);
147
+ if (result === null || result === void 0 ? void 0 : result.then) {
148
+ result.then(function(updatedModules) {
149
+ handleApplyUpdates(null, updatedModules);
150
+ }, function(err) {
151
+ handleApplyUpdates(err, null);
152
+ });
153
+ }
154
+ }
155
+ }
156
+ });
157
+ export default require_hmr_client();
@@ -0,0 +1,322 @@
1
+ function _assertThisInitialized(self) {
2
+ if (self === void 0) {
3
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
4
+ }
5
+ return self;
6
+ }
7
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
8
+ try {
9
+ var info = gen[key](arg);
10
+ var value = info.value;
11
+ } catch (error) {
12
+ reject(error);
13
+ return;
14
+ }
15
+ if (info.done) {
16
+ resolve(value);
17
+ } else {
18
+ Promise.resolve(value).then(_next, _throw);
19
+ }
20
+ }
21
+ function _asyncToGenerator(fn) {
22
+ return function() {
23
+ var self = this, args = arguments;
24
+ return new Promise(function(resolve, reject) {
25
+ var gen = fn.apply(self, args);
26
+ function _next(value) {
27
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
28
+ }
29
+ function _throw(err) {
30
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
31
+ }
32
+ _next(undefined);
33
+ });
34
+ };
35
+ }
36
+ function _classCallCheck(instance, Constructor) {
37
+ if (!(instance instanceof Constructor)) {
38
+ throw new TypeError("Cannot call a class as a function");
39
+ }
40
+ }
41
+ function _defineProperties(target, props) {
42
+ for(var i = 0; i < props.length; i++){
43
+ var descriptor = props[i];
44
+ descriptor.enumerable = descriptor.enumerable || false;
45
+ descriptor.configurable = true;
46
+ if ("value" in descriptor) descriptor.writable = true;
47
+ Object.defineProperty(target, descriptor.key, descriptor);
48
+ }
49
+ }
50
+ function _createClass(Constructor, protoProps, staticProps) {
51
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
52
+ if (staticProps) _defineProperties(Constructor, staticProps);
53
+ return Constructor;
54
+ }
55
+ function _defineProperty(obj, key, value) {
56
+ if (key in obj) {
57
+ Object.defineProperty(obj, key, {
58
+ value: value,
59
+ enumerable: true,
60
+ configurable: true,
61
+ writable: true
62
+ });
63
+ } else {
64
+ obj[key] = value;
65
+ }
66
+ return obj;
67
+ }
68
+ function _getPrototypeOf(o) {
69
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
70
+ return o.__proto__ || Object.getPrototypeOf(o);
71
+ };
72
+ return _getPrototypeOf(o);
73
+ }
74
+ function _inherits(subClass, superClass) {
75
+ if (typeof superClass !== "function" && superClass !== null) {
76
+ throw new TypeError("Super expression must either be null or a function");
77
+ }
78
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
79
+ constructor: {
80
+ value: subClass,
81
+ writable: true,
82
+ configurable: true
83
+ }
84
+ });
85
+ if (superClass) _setPrototypeOf(subClass, superClass);
86
+ }
87
+ function _objectSpread(target) {
88
+ for(var i = 1; i < arguments.length; i++){
89
+ var source = arguments[i] != null ? arguments[i] : {};
90
+ var ownKeys = Object.keys(source);
91
+ if (typeof Object.getOwnPropertySymbols === "function") {
92
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
93
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
94
+ }));
95
+ }
96
+ ownKeys.forEach(function(key) {
97
+ _defineProperty(target, key, source[key]);
98
+ });
99
+ }
100
+ return target;
101
+ }
102
+ function _possibleConstructorReturn(self, call) {
103
+ if (call && (_typeof(call) === "object" || typeof call === "function")) {
104
+ return call;
105
+ }
106
+ return _assertThisInitialized(self);
107
+ }
108
+ function _setPrototypeOf(o, p) {
109
+ _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
110
+ o.__proto__ = p;
111
+ return o;
112
+ };
113
+ return _setPrototypeOf(o, p);
114
+ }
115
+ var _typeof = function(obj) {
116
+ "@swc/helpers - typeof";
117
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
118
+ };
119
+ function _isNativeReflectConstruct() {
120
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
121
+ if (Reflect.construct.sham) return false;
122
+ if (typeof Proxy === "function") return true;
123
+ try {
124
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
125
+ return true;
126
+ } catch (e) {
127
+ return false;
128
+ }
129
+ }
130
+ function _createSuper(Derived) {
131
+ var hasNativeReflectConstruct = _isNativeReflectConstruct();
132
+ return function _createSuperInternal() {
133
+ var Super = _getPrototypeOf(Derived), result;
134
+ if (hasNativeReflectConstruct) {
135
+ var NewTarget = _getPrototypeOf(this).constructor;
136
+ result = Reflect.construct(Super, arguments, NewTarget);
137
+ } else {
138
+ result = Super.apply(this, arguments);
139
+ }
140
+ return _possibleConstructorReturn(this, result);
141
+ };
142
+ }
143
+ var __generator = this && this.__generator || function(thisArg, body) {
144
+ var f, y, t, g, _ = {
145
+ label: 0,
146
+ sent: function() {
147
+ if (t[0] & 1) throw t[1];
148
+ return t[1];
149
+ },
150
+ trys: [],
151
+ ops: []
152
+ };
153
+ return g = {
154
+ next: verb(0),
155
+ "throw": verb(1),
156
+ "return": verb(2)
157
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
158
+ return this;
159
+ }), g;
160
+ function verb(n) {
161
+ return function(v) {
162
+ return step([
163
+ n,
164
+ v
165
+ ]);
166
+ };
167
+ }
168
+ function step(op) {
169
+ if (f) throw new TypeError("Generator is already executing.");
170
+ while(_)try {
171
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
172
+ if (y = 0, t) op = [
173
+ op[0] & 2,
174
+ t.value
175
+ ];
176
+ switch(op[0]){
177
+ case 0:
178
+ case 1:
179
+ t = op;
180
+ break;
181
+ case 4:
182
+ _.label++;
183
+ return {
184
+ value: op[1],
185
+ done: false
186
+ };
187
+ case 5:
188
+ _.label++;
189
+ y = op[1];
190
+ op = [
191
+ 0
192
+ ];
193
+ continue;
194
+ case 7:
195
+ op = _.ops.pop();
196
+ _.trys.pop();
197
+ continue;
198
+ default:
199
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
200
+ _ = 0;
201
+ continue;
202
+ }
203
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
204
+ _.label = op[1];
205
+ break;
206
+ }
207
+ if (op[0] === 6 && _.label < t[1]) {
208
+ _.label = t[1];
209
+ t = op;
210
+ break;
211
+ }
212
+ if (t && _.label < t[2]) {
213
+ _.label = t[2];
214
+ _.ops.push(op);
215
+ break;
216
+ }
217
+ if (t[2]) _.ops.pop();
218
+ _.trys.pop();
219
+ continue;
220
+ }
221
+ op = body.call(thisArg, _);
222
+ } catch (e) {
223
+ op = [
224
+ 6,
225
+ e
226
+ ];
227
+ y = 0;
228
+ } finally{
229
+ f = t = 0;
230
+ }
231
+ if (op[0] & 5) throw op[1];
232
+ return {
233
+ value: op[0] ? op[1] : void 0,
234
+ done: true
235
+ };
236
+ }
237
+ };
238
+ import { EventEmitter } from "events";
239
+ import SocketServer from "./socket-server";
240
+ var noop = function() {};
241
+ function getHMRClientPath(client) {
242
+ var protocol = (client === null || client === void 0 ? void 0 : client.protocol) ? "&protocol=".concat(client.protocol) : "";
243
+ var host = (client === null || client === void 0 ? void 0 : client.host) ? "&host=".concat(client.host) : "";
244
+ var path = (client === null || client === void 0 ? void 0 : client.path) ? "&path=".concat(client.path) : "";
245
+ var port = (client === null || client === void 0 ? void 0 : client.port) ? "&port=".concat(client.port) : "";
246
+ var clientEntry = "".concat(require.resolve("./hmr-client"), "?").concat(host).concat(path).concat(port).concat(protocol);
247
+ return clientEntry;
248
+ }
249
+ var DevMiddleware = /*#__PURE__*/ function(EventEmitter) {
250
+ "use strict";
251
+ _inherits(DevMiddleware, EventEmitter);
252
+ var _super = _createSuper(DevMiddleware);
253
+ function DevMiddleware(param) {
254
+ var dev = param.dev, devMiddleware = param.devMiddleware;
255
+ _classCallCheck(this, DevMiddleware);
256
+ var _this;
257
+ _this = _super.call(this);
258
+ _defineProperty(_assertThisInitialized(_this), "middleware", void 0);
259
+ _defineProperty(_assertThisInitialized(_this), "devOptions", void 0);
260
+ _defineProperty(_assertThisInitialized(_this), "socketServer", void 0);
261
+ _this.devOptions = dev;
262
+ _this.socketServer = new SocketServer(dev);
263
+ if (devMiddleware) {
264
+ _this.middleware = _this.setupDevMiddleware(devMiddleware);
265
+ }
266
+ return _this;
267
+ }
268
+ _createClass(DevMiddleware, [
269
+ {
270
+ key: "init",
271
+ value: function init(app) {
272
+ var _this = this;
273
+ app.on("listening", function() {
274
+ _this.socketServer.prepare(app);
275
+ });
276
+ var _this1 = this;
277
+ app.on("close", /*#__PURE__*/ _asyncToGenerator(function() {
278
+ var ref;
279
+ return __generator(this, function(_state) {
280
+ (ref = _this1.middleware) === null || ref === void 0 ? void 0 : ref.close(noop);
281
+ _this1.socketServer.close();
282
+ return [
283
+ 2
284
+ ];
285
+ });
286
+ }));
287
+ }
288
+ },
289
+ {
290
+ key: "sockWrite",
291
+ value: function sockWrite(type, data) {
292
+ this.socketServer.sockWrite(type, data);
293
+ }
294
+ },
295
+ {
296
+ key: "setupDevMiddleware",
297
+ value: function setupDevMiddleware(devMiddleware) {
298
+ var _this = this;
299
+ var devOptions = this.devOptions;
300
+ var callbacks = {
301
+ onInvalid: function() {
302
+ _this.socketServer.sockWrite("invalid");
303
+ },
304
+ onDone: function(stats) {
305
+ _this.socketServer.updateStats(stats);
306
+ _this.emit("change", stats);
307
+ }
308
+ };
309
+ var enableHMR = this.devOptions.hot || this.devOptions.liveReload;
310
+ var middleware = devMiddleware(_objectSpread({
311
+ headers: devOptions.headers,
312
+ stats: false,
313
+ callbacks: callbacks,
314
+ hmrClientPath: enableHMR ? getHMRClientPath(devOptions.client) : void 0
315
+ }, devOptions.devMiddleware));
316
+ return middleware;
317
+ }
318
+ }
319
+ ]);
320
+ return DevMiddleware;
321
+ }(EventEmitter);
322
+ export { DevMiddleware as default };
@@ -0,0 +1,209 @@
1
+ function _classCallCheck(instance, Constructor) {
2
+ if (!(instance instanceof Constructor)) {
3
+ throw new TypeError("Cannot call a class as a function");
4
+ }
5
+ }
6
+ function _defineProperties(target, props) {
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) descriptor.writable = true;
12
+ Object.defineProperty(target, descriptor.key, descriptor);
13
+ }
14
+ }
15
+ function _createClass(Constructor, protoProps, staticProps) {
16
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
17
+ if (staticProps) _defineProperties(Constructor, staticProps);
18
+ return Constructor;
19
+ }
20
+ function _defineProperty(obj, key, value) {
21
+ if (key in obj) {
22
+ Object.defineProperty(obj, key, {
23
+ value: value,
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true
27
+ });
28
+ } else {
29
+ obj[key] = value;
30
+ }
31
+ return obj;
32
+ }
33
+ import ws from "ws";
34
+ import { logger } from "@modern-js/utils";
35
+ var SocketServer = /*#__PURE__*/ function() {
36
+ "use strict";
37
+ function SocketServer(options) {
38
+ _classCallCheck(this, SocketServer);
39
+ _defineProperty(this, "wsServer", void 0);
40
+ _defineProperty(this, "sockets", []);
41
+ _defineProperty(this, "options", void 0);
42
+ _defineProperty(this, "app", void 0);
43
+ _defineProperty(this, "stats", void 0);
44
+ _defineProperty(this, "timer", null);
45
+ this.options = options;
46
+ }
47
+ _createClass(SocketServer, [
48
+ {
49
+ key: "prepare",
50
+ value: function prepare(app) {
51
+ var _this = this;
52
+ var ref;
53
+ this.app = app;
54
+ this.wsServer = new ws.Server({
55
+ noServer: true,
56
+ path: (ref = this.options.client) === null || ref === void 0 ? void 0 : ref.path
57
+ });
58
+ this.app.on("upgrade", function(req, sock, head) {
59
+ if (!_this.wsServer.shouldHandle(req)) {
60
+ return;
61
+ }
62
+ _this.wsServer.handleUpgrade(req, sock, head, function(connection) {
63
+ _this.wsServer.emit("connection", connection, req);
64
+ });
65
+ });
66
+ this.wsServer.on("error", function(err) {
67
+ logger.error(err);
68
+ });
69
+ this.timer = setInterval(function() {
70
+ _this.wsServer.clients.forEach(function(socket) {
71
+ var extWs = socket;
72
+ if (!extWs.isAlive) {
73
+ extWs.terminate();
74
+ } else {
75
+ extWs.isAlive = false;
76
+ extWs.ping(function() {});
77
+ }
78
+ });
79
+ }, 3e4);
80
+ this.wsServer.on("connection", function(socket) {
81
+ _this.onConnect(socket);
82
+ });
83
+ }
84
+ },
85
+ {
86
+ key: "updateStats",
87
+ value: function updateStats(stats) {
88
+ this.stats = stats;
89
+ this.sendStats();
90
+ }
91
+ },
92
+ {
93
+ key: "sockWrite",
94
+ value: function sockWrite(type, data) {
95
+ var _this = this;
96
+ this.sockets.forEach(function(socket) {
97
+ _this.send(socket, JSON.stringify({
98
+ type: type,
99
+ data: data
100
+ }));
101
+ });
102
+ }
103
+ },
104
+ {
105
+ key: "singleWrite",
106
+ value: function singleWrite(socket, type, data) {
107
+ this.send(socket, JSON.stringify({
108
+ type: type,
109
+ data: data
110
+ }));
111
+ }
112
+ },
113
+ {
114
+ key: "close",
115
+ value: function close() {
116
+ this.sockets.forEach(function(socket) {
117
+ socket.close();
118
+ });
119
+ if (this.timer) {
120
+ clearInterval(this.timer);
121
+ this.timer = null;
122
+ }
123
+ }
124
+ },
125
+ {
126
+ key: "onConnect",
127
+ value: function onConnect(socket) {
128
+ var _this = this;
129
+ var connection = socket;
130
+ connection.isAlive = true;
131
+ connection.on("pong", function() {
132
+ connection.isAlive = true;
133
+ });
134
+ if (!connection) {
135
+ return;
136
+ }
137
+ this.sockets.push(connection);
138
+ connection.on("close", function() {
139
+ var idx = _this.sockets.indexOf(connection);
140
+ if (idx >= 0) {
141
+ _this.sockets.splice(idx, 1);
142
+ }
143
+ });
144
+ if (this.options.hot || this.options.hot === "only") {
145
+ this.singleWrite(connection, "hot");
146
+ }
147
+ if (this.options.liveReload) {
148
+ this.singleWrite(connection, "liveReload");
149
+ }
150
+ if (this.stats) {
151
+ this.sendStats(true);
152
+ }
153
+ }
154
+ },
155
+ {
156
+ key: "getStats",
157
+ value: function getStats() {
158
+ var curStats = this.stats;
159
+ if (!curStats) {
160
+ return null;
161
+ }
162
+ var defaultStats = {
163
+ all: false,
164
+ hash: true,
165
+ assets: true,
166
+ warnings: true,
167
+ errors: true,
168
+ errorDetails: false
169
+ };
170
+ return curStats.toJson(defaultStats);
171
+ }
172
+ },
173
+ {
174
+ key: "sendStats",
175
+ value: function sendStats() {
176
+ var force = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : false;
177
+ var stats = this.getStats();
178
+ if (!stats) {
179
+ return null;
180
+ }
181
+ var shouldEmit = !force && stats && (!stats.errors || stats.errors.length === 0) && stats.assets && stats.assets.every(function(asset) {
182
+ return !asset.emitted;
183
+ });
184
+ if (shouldEmit) {
185
+ return this.sockWrite("still-ok");
186
+ }
187
+ this.sockWrite("hash", stats.hash);
188
+ if (stats.errors && stats.errors.length > 0) {
189
+ return this.sockWrite("errors", stats.errors);
190
+ } else if (stats.warnings && stats.warnings.length > 0) {
191
+ return this.sockWrite("warnings", stats.warnings);
192
+ } else {
193
+ return this.sockWrite("ok");
194
+ }
195
+ }
196
+ },
197
+ {
198
+ key: "send",
199
+ value: function send(connection, message) {
200
+ if (connection.readyState !== 1) {
201
+ return;
202
+ }
203
+ connection.send(message);
204
+ }
205
+ }
206
+ ]);
207
+ return SocketServer;
208
+ }();
209
+ export { SocketServer as default };