@modern-js/server 1.4.1 → 1.4.2

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 CHANGED
@@ -1,5 +1,15 @@
1
1
  # @modern-js/server
2
2
 
3
+ ## 1.4.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 735b2a81: prevent ssr compiler to send socket message
8
+ - Updated dependencies [b376c8d6]
9
+ - Updated dependencies [e62c4efd]
10
+ - Updated dependencies [e2a8233f]
11
+ - @modern-js/core@1.4.2
12
+
3
13
  ## 1.4.1
4
14
 
5
15
  ### Patch Changes
@@ -8,6 +8,7 @@ export default class SocketServer {
8
8
  this.options = void 0;
9
9
  this.app = void 0;
10
10
  this.stats = void 0;
11
+ this.timer = null;
11
12
  this.options = options;
12
13
  } // create socket, install socket handler, bind socket event
13
14
 
@@ -32,7 +33,7 @@ export default class SocketServer {
32
33
  // only dev server, use default logger
33
34
  logger.error(err);
34
35
  });
35
- setInterval(() => {
36
+ this.timer = setInterval(() => {
36
37
  this.wsServer.clients.forEach(socket => {
37
38
  const extWs = socket;
38
39
 
@@ -45,49 +46,7 @@ export default class SocketServer {
45
46
  });
46
47
  }, 30000);
47
48
  this.wsServer.on('connection', socket => {
48
- const connection = socket;
49
- connection.isAlive = true;
50
- connection.on('pong', () => {
51
- connection.isAlive = true;
52
- });
53
-
54
- if (!connection) {
55
- return;
56
- }
57
-
58
- this.sockets.push(connection);
59
- connection.on('close', () => {
60
- const idx = this.sockets.indexOf(connection);
61
-
62
- if (idx >= 0) {
63
- this.sockets.splice(idx, 1);
64
- }
65
- });
66
-
67
- if (this.options.client.logging) {
68
- this.sockWrite('logging', this.options.client.logging);
69
- }
70
-
71
- if (this.options.hot || this.options.hot === 'only') {
72
- this.sockWrite('hot');
73
- }
74
-
75
- if (this.options.liveReload) {
76
- this.sockWrite('liveReload');
77
- }
78
-
79
- if (this.options.client.progress) {
80
- this.sockWrite('progress', this.options.client.progress);
81
- }
82
-
83
- if (this.options.client.overlay) {
84
- this.sockWrite('overlay', this.options.client.overlay);
85
- } // send first stats to active client sock if stats exist
86
-
87
-
88
- if (this.stats) {
89
- this.sendStats(true);
90
- }
49
+ this.onConnect(socket);
91
50
  });
92
51
  }
93
52
 
@@ -106,10 +65,68 @@ export default class SocketServer {
106
65
  });
107
66
  }
108
67
 
68
+ singleWrite(socket, type, data) {
69
+ this.send(socket, JSON.stringify({
70
+ type,
71
+ data
72
+ }));
73
+ }
74
+
109
75
  close() {
110
76
  this.sockets.forEach(socket => {
111
77
  socket.close();
112
78
  });
79
+
80
+ if (this.timer) {
81
+ clearInterval(this.timer);
82
+ this.timer = null;
83
+ }
84
+ }
85
+
86
+ onConnect(socket) {
87
+ const connection = socket;
88
+ connection.isAlive = true;
89
+ connection.on('pong', () => {
90
+ connection.isAlive = true;
91
+ });
92
+
93
+ if (!connection) {
94
+ return;
95
+ }
96
+
97
+ this.sockets.push(connection);
98
+ connection.on('close', () => {
99
+ const idx = this.sockets.indexOf(connection);
100
+
101
+ if (idx >= 0) {
102
+ this.sockets.splice(idx, 1);
103
+ }
104
+ });
105
+
106
+ if (this.options.client.logging) {
107
+ this.singleWrite(connection, 'logging', this.options.client.logging);
108
+ }
109
+
110
+ if (this.options.hot || this.options.hot === 'only') {
111
+ this.singleWrite(connection, 'hot');
112
+ }
113
+
114
+ if (this.options.liveReload) {
115
+ this.singleWrite(connection, 'liveReload');
116
+ }
117
+
118
+ if (this.options.client.progress) {
119
+ this.singleWrite(connection, 'progress', this.options.client.progress);
120
+ }
121
+
122
+ if (this.options.client.overlay) {
123
+ this.singleWrite(connection, 'overlay', this.options.client.overlay);
124
+ } // send first stats to active client sock if stats exist
125
+
126
+
127
+ if (this.stats) {
128
+ this.sendStats(true);
129
+ }
113
130
  } // get standard stats
114
131
 
115
132
 
@@ -183,6 +183,10 @@ export class ModernDevServer extends ModernServer {
183
183
  };
184
184
 
185
185
  const addHooks = compiler => {
186
+ if (compiler.name === 'server') {
187
+ return;
188
+ }
189
+
186
190
  const {
187
191
  compile,
188
192
  invalid,
@@ -20,6 +20,7 @@ class SocketServer {
20
20
  this.options = void 0;
21
21
  this.app = void 0;
22
22
  this.stats = void 0;
23
+ this.timer = null;
23
24
  this.options = options;
24
25
  } // create socket, install socket handler, bind socket event
25
26
 
@@ -44,7 +45,7 @@ class SocketServer {
44
45
  // only dev server, use default logger
45
46
  _utils.logger.error(err);
46
47
  });
47
- setInterval(() => {
48
+ this.timer = setInterval(() => {
48
49
  this.wsServer.clients.forEach(socket => {
49
50
  const extWs = socket;
50
51
 
@@ -57,49 +58,7 @@ class SocketServer {
57
58
  });
58
59
  }, 30000);
59
60
  this.wsServer.on('connection', socket => {
60
- const connection = socket;
61
- connection.isAlive = true;
62
- connection.on('pong', () => {
63
- connection.isAlive = true;
64
- });
65
-
66
- if (!connection) {
67
- return;
68
- }
69
-
70
- this.sockets.push(connection);
71
- connection.on('close', () => {
72
- const idx = this.sockets.indexOf(connection);
73
-
74
- if (idx >= 0) {
75
- this.sockets.splice(idx, 1);
76
- }
77
- });
78
-
79
- if (this.options.client.logging) {
80
- this.sockWrite('logging', this.options.client.logging);
81
- }
82
-
83
- if (this.options.hot || this.options.hot === 'only') {
84
- this.sockWrite('hot');
85
- }
86
-
87
- if (this.options.liveReload) {
88
- this.sockWrite('liveReload');
89
- }
90
-
91
- if (this.options.client.progress) {
92
- this.sockWrite('progress', this.options.client.progress);
93
- }
94
-
95
- if (this.options.client.overlay) {
96
- this.sockWrite('overlay', this.options.client.overlay);
97
- } // send first stats to active client sock if stats exist
98
-
99
-
100
- if (this.stats) {
101
- this.sendStats(true);
102
- }
61
+ this.onConnect(socket);
103
62
  });
104
63
  }
105
64
 
@@ -118,10 +77,68 @@ class SocketServer {
118
77
  });
119
78
  }
120
79
 
80
+ singleWrite(socket, type, data) {
81
+ this.send(socket, JSON.stringify({
82
+ type,
83
+ data
84
+ }));
85
+ }
86
+
121
87
  close() {
122
88
  this.sockets.forEach(socket => {
123
89
  socket.close();
124
90
  });
91
+
92
+ if (this.timer) {
93
+ clearInterval(this.timer);
94
+ this.timer = null;
95
+ }
96
+ }
97
+
98
+ onConnect(socket) {
99
+ const connection = socket;
100
+ connection.isAlive = true;
101
+ connection.on('pong', () => {
102
+ connection.isAlive = true;
103
+ });
104
+
105
+ if (!connection) {
106
+ return;
107
+ }
108
+
109
+ this.sockets.push(connection);
110
+ connection.on('close', () => {
111
+ const idx = this.sockets.indexOf(connection);
112
+
113
+ if (idx >= 0) {
114
+ this.sockets.splice(idx, 1);
115
+ }
116
+ });
117
+
118
+ if (this.options.client.logging) {
119
+ this.singleWrite(connection, 'logging', this.options.client.logging);
120
+ }
121
+
122
+ if (this.options.hot || this.options.hot === 'only') {
123
+ this.singleWrite(connection, 'hot');
124
+ }
125
+
126
+ if (this.options.liveReload) {
127
+ this.singleWrite(connection, 'liveReload');
128
+ }
129
+
130
+ if (this.options.client.progress) {
131
+ this.singleWrite(connection, 'progress', this.options.client.progress);
132
+ }
133
+
134
+ if (this.options.client.overlay) {
135
+ this.singleWrite(connection, 'overlay', this.options.client.overlay);
136
+ } // send first stats to active client sock if stats exist
137
+
138
+
139
+ if (this.stats) {
140
+ this.sendStats(true);
141
+ }
125
142
  } // get standard stats
126
143
 
127
144
 
@@ -212,6 +212,10 @@ class ModernDevServer extends _modernServer.ModernServer {
212
212
  };
213
213
 
214
214
  const addHooks = compiler => {
215
+ if (compiler.name === 'server') {
216
+ return;
217
+ }
218
+
215
219
  const {
216
220
  compile,
217
221
  invalid,
@@ -1,5 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { Server } from 'http';
3
+ import ws from 'ws';
3
4
  import type { Stats } from 'webpack';
4
5
  import { DevServerOptions } from '../type';
5
6
  export default class SocketServer {
@@ -8,11 +9,14 @@ export default class SocketServer {
8
9
  private readonly options;
9
10
  private app?;
10
11
  private stats?;
12
+ private timer;
11
13
  constructor(options: DevServerOptions);
12
14
  prepare(app: Server): void;
13
15
  updateStats(stats: Stats): void;
14
16
  sockWrite(type: string, data?: Record<string, any> | string | boolean): void;
17
+ singleWrite(socket: ws, type: string, data?: Record<string, any> | string | boolean): void;
15
18
  close(): void;
19
+ private onConnect;
16
20
  private getStats;
17
21
  private sendStats;
18
22
  private send;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.4.1",
14
+ "version": "1.4.2",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -34,7 +34,7 @@
34
34
  "@babel/preset-typescript": "^7.15.0",
35
35
  "@babel/register": "^7.15.3",
36
36
  "@babel/runtime": "^7",
37
- "@modern-js/core": "^1.4.1",
37
+ "@modern-js/core": "^1.4.2",
38
38
  "@modern-js/hmr-client": "^1.2.1",
39
39
  "@modern-js/server-core": "^1.2.2",
40
40
  "@modern-js/server-utils": "^1.2.1",
@@ -65,7 +65,7 @@
65
65
  },
66
66
  "devDependencies": {
67
67
  "@scripts/build": "0.0.0",
68
- "@modern-js/types": "^1.3.1",
68
+ "@modern-js/types": "^1.3.2",
69
69
  "@types/jest": "^26",
70
70
  "@types/lru-cache": "^5.1.1",
71
71
  "@types/mime-types": "^2.1.0",
package/tests/dev.test.ts CHANGED
@@ -53,14 +53,31 @@ describe('test dev tools', () => {
53
53
 
54
54
  const socket = {
55
55
  state: 1,
56
+ readyState: 1,
57
+ data: '',
56
58
  close() {
57
59
  socket.state = 0;
60
+ },
61
+ send(data: string) {
62
+ socket.data = data;
63
+ },
64
+ on() {
58
65
  // empty
59
66
  },
60
67
  };
68
+
69
+ socketServer.onConnect(socket);
70
+
61
71
  socketServer.sockets = [socket];
72
+ socketServer.sockWrite('test');
73
+ expect(socket.data).toBe(JSON.stringify({ type: 'test' }));
74
+
75
+ socketServer.singleWrite(socket, 'single');
76
+ expect(socket.data).toBe(JSON.stringify({ type: 'single' }));
77
+
62
78
  socketServer.close();
63
79
  expect(socket.state).toBe(0);
80
+ app.close();
64
81
  });
65
82
 
66
83
  test('should dev server plugin work correctly', () => {