@callstack/repack-dev-server 5.0.0-rc.2 → 5.0.0-rc.4

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 (88) hide show
  1. package/dist/createServer.d.ts +3 -2
  2. package/dist/createServer.js +110 -123
  3. package/dist/index.d.ts +2 -2
  4. package/dist/index.js +2 -3
  5. package/dist/plugins/api/apiPlugin.d.ts +1 -1
  6. package/dist/plugins/api/apiPlugin.js +21 -28
  7. package/dist/plugins/api/index.d.ts +1 -1
  8. package/dist/plugins/api/index.js +1 -2
  9. package/dist/plugins/compiler/compilerPlugin.d.ts +1 -1
  10. package/dist/plugins/compiler/compilerPlugin.js +53 -62
  11. package/dist/plugins/compiler/index.d.ts +2 -2
  12. package/dist/plugins/compiler/index.js +2 -3
  13. package/dist/plugins/compiler/types.d.ts +1 -1
  14. package/dist/plugins/compiler/types.js +0 -1
  15. package/dist/plugins/devtools/devtoolsPlugin.d.ts +1 -1
  16. package/dist/plugins/devtools/devtoolsPlugin.js +16 -19
  17. package/dist/plugins/devtools/index.d.ts +1 -1
  18. package/dist/plugins/devtools/index.js +1 -2
  19. package/dist/plugins/favicon/faviconPlugin.js +3 -7
  20. package/dist/plugins/favicon/index.d.ts +1 -1
  21. package/dist/plugins/favicon/index.js +1 -2
  22. package/dist/plugins/multipart/index.d.ts +2 -2
  23. package/dist/plugins/multipart/index.js +2 -3
  24. package/dist/plugins/multipart/multipartPlugin.js +44 -42
  25. package/dist/plugins/multipart/types.js +0 -1
  26. package/dist/plugins/symbolicate/Symbolicator.d.ts +1 -1
  27. package/dist/plugins/symbolicate/Symbolicator.js +172 -190
  28. package/dist/plugins/symbolicate/index.d.ts +3 -3
  29. package/dist/plugins/symbolicate/index.js +3 -4
  30. package/dist/plugins/symbolicate/sybmolicatePlugin.d.ts +1 -1
  31. package/dist/plugins/symbolicate/sybmolicatePlugin.js +29 -40
  32. package/dist/plugins/symbolicate/types.js +0 -1
  33. package/dist/plugins/wss/WebSocketRouter.d.ts +1 -1
  34. package/dist/plugins/wss/WebSocketRouter.js +39 -44
  35. package/dist/plugins/wss/WebSocketServer.d.ts +1 -1
  36. package/dist/plugins/wss/WebSocketServer.js +23 -38
  37. package/dist/plugins/wss/WebSocketServerAdapter.d.ts +1 -1
  38. package/dist/plugins/wss/WebSocketServerAdapter.js +16 -19
  39. package/dist/plugins/wss/index.d.ts +3 -3
  40. package/dist/plugins/wss/index.js +3 -4
  41. package/dist/plugins/wss/servers/WebSocketApiServer.d.ts +1 -1
  42. package/dist/plugins/wss/servers/WebSocketApiServer.js +47 -53
  43. package/dist/plugins/wss/servers/WebSocketDevClientServer.d.ts +1 -1
  44. package/dist/plugins/wss/servers/WebSocketDevClientServer.js +58 -80
  45. package/dist/plugins/wss/servers/WebSocketEventsServer.d.ts +2 -2
  46. package/dist/plugins/wss/servers/WebSocketEventsServer.js +146 -164
  47. package/dist/plugins/wss/servers/WebSocketHMRServer.d.ts +2 -2
  48. package/dist/plugins/wss/servers/WebSocketHMRServer.js +72 -79
  49. package/dist/plugins/wss/servers/WebSocketMessageServer.d.ts +1 -1
  50. package/dist/plugins/wss/servers/WebSocketMessageServer.js +309 -326
  51. package/dist/plugins/wss/types.js +0 -1
  52. package/dist/plugins/wss/wssPlugin.d.ts +8 -8
  53. package/dist/plugins/wss/wssPlugin.js +37 -42
  54. package/dist/types.d.ts +6 -6
  55. package/dist/types.js +8 -18
  56. package/package.json +4 -8
  57. package/CHANGELOG.md +0 -160
  58. package/dist/createServer.js.map +0 -1
  59. package/dist/index.js.map +0 -1
  60. package/dist/plugins/api/apiPlugin.js.map +0 -1
  61. package/dist/plugins/api/index.js.map +0 -1
  62. package/dist/plugins/compiler/compilerPlugin.js.map +0 -1
  63. package/dist/plugins/compiler/index.js.map +0 -1
  64. package/dist/plugins/compiler/types.js.map +0 -1
  65. package/dist/plugins/devtools/devtoolsPlugin.js.map +0 -1
  66. package/dist/plugins/devtools/index.js.map +0 -1
  67. package/dist/plugins/favicon/faviconPlugin.js.map +0 -1
  68. package/dist/plugins/favicon/index.js.map +0 -1
  69. package/dist/plugins/multipart/index.js.map +0 -1
  70. package/dist/plugins/multipart/multipartPlugin.js.map +0 -1
  71. package/dist/plugins/multipart/types.js.map +0 -1
  72. package/dist/plugins/symbolicate/Symbolicator.js.map +0 -1
  73. package/dist/plugins/symbolicate/index.js.map +0 -1
  74. package/dist/plugins/symbolicate/sybmolicatePlugin.js.map +0 -1
  75. package/dist/plugins/symbolicate/types.js.map +0 -1
  76. package/dist/plugins/wss/WebSocketRouter.js.map +0 -1
  77. package/dist/plugins/wss/WebSocketServer.js.map +0 -1
  78. package/dist/plugins/wss/WebSocketServerAdapter.js.map +0 -1
  79. package/dist/plugins/wss/index.js.map +0 -1
  80. package/dist/plugins/wss/servers/WebSocketApiServer.js.map +0 -1
  81. package/dist/plugins/wss/servers/WebSocketDevClientServer.js.map +0 -1
  82. package/dist/plugins/wss/servers/WebSocketEventsServer.js.map +0 -1
  83. package/dist/plugins/wss/servers/WebSocketHMRServer.js.map +0 -1
  84. package/dist/plugins/wss/servers/WebSocketMessageServer.js.map +0 -1
  85. package/dist/plugins/wss/types.js.map +0 -1
  86. package/dist/plugins/wss/wssPlugin.js.map +0 -1
  87. package/dist/types.js.map +0 -1
  88. /package/{dist/img → static}/favicon.ico +0 -0
@@ -1,11 +1,5 @@
1
1
  import { URL } from 'node:url';
2
- import { WebSocketServer } from "../WebSocketServer.js";
3
- /**
4
- * Holds {@link ReactNativeMessage} `id` data.
5
- */
6
- /**
7
- * Message representation used by {@link WebSocketMessageServer}.
8
- */
2
+ import { WebSocketServer } from '../WebSocketServer.js';
9
3
  /**
10
4
  * Class for creating a WebSocket server and sending messages between development server
11
5
  * and the React Native applications.
@@ -15,336 +9,325 @@ import { WebSocketServer } from "../WebSocketServer.js";
15
9
  * @category Development server
16
10
  */
17
11
  export class WebSocketMessageServer extends WebSocketServer {
18
- static PROTOCOL_VERSION = 2;
19
-
20
- /**
21
- * Check if message is a broadcast request.
22
- *
23
- * @param message Message to check.
24
- * @returns True if message is a broadcast request and should be broadcasted
25
- * with {@link sendBroadcast}.
26
- */
27
- static isBroadcast(message) {
28
- return typeof message.method === 'string' && message.id === undefined && message.target === undefined;
29
- }
30
-
31
- /**
32
- * Check if message is a method request.
33
- *
34
- * @param message Message to check.
35
- * @returns True if message is a request.
36
- */
37
- static isRequest(message) {
38
- return typeof message.method === 'string' && typeof message.target === 'string';
39
- }
40
-
41
- /**
42
- * Check if message is a response with results of performing some request.
43
- *
44
- * @param message Message to check.
45
- * @returns True if message is a response.
46
- */
47
- static isResponse(message) {
48
- return typeof message.id === 'object' && typeof message.id.requestId !== 'undefined' && typeof message.id.clientId === 'string' && (message.result !== undefined || message.error !== undefined);
49
- }
50
- clients = new Map();
51
- nextClientId = 0;
52
-
53
- /**
54
- * Create new instance of WebSocketMessageServer and attach it to the given Fastify instance.
55
- * Any logging information, will be passed through standard `fastify.log` API.
56
- *
57
- * @param fastify Fastify instance to attach the WebSocket server to.
58
- */
59
- constructor(fastify) {
60
- super(fastify, '/message');
61
- }
62
-
63
- /**
64
- * Parse stringified message into a {@link ReactNativeMessage}.
65
- *
66
- * @param data Stringified message.
67
- * @param binary Additional binary data if any.
68
- * @returns Parsed message or `undefined` if parsing failed.
69
- */
70
- parseMessage(data, binary) {
71
- if (binary) {
72
- this.fastify.log.error({
73
- msg: 'Failed to parse message - expected text message, got binary'
74
- });
75
- return undefined;
12
+ /**
13
+ * Check if message is a broadcast request.
14
+ *
15
+ * @param message Message to check.
16
+ * @returns True if message is a broadcast request and should be broadcasted
17
+ * with {@link sendBroadcast}.
18
+ */
19
+ static isBroadcast(message) {
20
+ return (typeof message.method === 'string' &&
21
+ message.id === undefined &&
22
+ message.target === undefined);
76
23
  }
77
- try {
78
- const message = JSON.parse(data);
79
- if (message.version === WebSocketMessageServer.PROTOCOL_VERSION.toString()) {
80
- return message;
81
- }
82
- this.fastify.log.error({
83
- msg: 'Received message had wrong protocol version',
84
- message
85
- });
86
- } catch {
87
- this.fastify.log.error({
88
- msg: 'Failed to parse the message as JSON',
89
- data
90
- });
24
+ /**
25
+ * Check if message is a method request.
26
+ *
27
+ * @param message Message to check.
28
+ * @returns True if message is a request.
29
+ */
30
+ static isRequest(message) {
31
+ return (typeof message.method === 'string' && typeof message.target === 'string');
91
32
  }
92
- return undefined;
93
- }
94
-
95
- /**
96
- * Get client's WebSocket connection for given `clientId`.
97
- * Throws if no such client is connected.
98
- *
99
- * @param clientId Id of the client.
100
- * @returns WebSocket connection.
101
- */
102
- getClientSocket(clientId) {
103
- const socket = this.clients.get(clientId);
104
- if (socket === undefined) {
105
- throw new Error(`Could not find client with id "${clientId}"`);
33
+ /**
34
+ * Check if message is a response with results of performing some request.
35
+ *
36
+ * @param message Message to check.
37
+ * @returns True if message is a response.
38
+ */
39
+ static isResponse(message) {
40
+ return (typeof message.id === 'object' &&
41
+ typeof message.id.requestId !== 'undefined' &&
42
+ typeof message.id.clientId === 'string' &&
43
+ (message.result !== undefined || message.error !== undefined));
106
44
  }
107
- return socket;
108
- }
109
-
110
- /**
111
- * Process error by sending an error message to the client whose message caused the error
112
- * to occur.
113
- *
114
- * @param clientId Id of the client whose message caused an error.
115
- * @param message Original message which caused the error.
116
- * @param error Concrete instance of an error that occurred.
117
- */
118
- handleError(clientId, message, error) {
119
- const errorMessage = {
120
- id: message.id,
121
- method: message.method,
122
- target: message.target,
123
- error: message.error === undefined ? 'undefined' : 'defined',
124
- params: message.params === undefined ? 'undefined' : 'defined',
125
- result: message.result === undefined ? 'undefined' : 'defined'
126
- };
127
- if (message.id === undefined) {
128
- this.fastify.log.error({
129
- msg: 'Handling message failed',
130
- clientId,
131
- error,
132
- errorMessage
133
- });
134
- } else {
135
- try {
136
- const socket = this.getClientSocket(clientId);
137
- socket.send(JSON.stringify({
138
- version: WebSocketMessageServer.PROTOCOL_VERSION,
139
- error,
140
- id: message.id
141
- }));
142
- } catch (error) {
143
- this.fastify.log.error('Failed to reply', {
144
- clientId,
145
- error,
146
- errorMessage
147
- });
148
- }
45
+ /**
46
+ * Create new instance of WebSocketMessageServer and attach it to the given Fastify instance.
47
+ * Any logging information, will be passed through standard `fastify.log` API.
48
+ *
49
+ * @param fastify Fastify instance to attach the WebSocket server to.
50
+ */
51
+ constructor(fastify) {
52
+ super(fastify, '/message');
53
+ this.clients = new Map();
54
+ this.nextClientId = 0;
149
55
  }
150
- }
151
-
152
- /**
153
- * Send given request `message` to it's designated client's socket based on `message.target`.
154
- * The target client must be connected, otherwise it will throw an error.
155
- *
156
- * @param clientId Id of the client that requested the forward.
157
- * @param message Message to forward.
158
- */
159
- forwardRequest(clientId, message) {
160
- if (!message.target) {
161
- this.fastify.log.error({
162
- msg: 'Failed to forward request - message.target is missing',
163
- clientId,
164
- message
165
- });
166
- return;
56
+ /**
57
+ * Parse stringified message into a {@link ReactNativeMessage}.
58
+ *
59
+ * @param data Stringified message.
60
+ * @param binary Additional binary data if any.
61
+ * @returns Parsed message or `undefined` if parsing failed.
62
+ */
63
+ parseMessage(data, binary) {
64
+ if (binary) {
65
+ this.fastify.log.error({
66
+ msg: 'Failed to parse message - expected text message, got binary',
67
+ });
68
+ return undefined;
69
+ }
70
+ try {
71
+ const message = JSON.parse(data);
72
+ if (message.version === WebSocketMessageServer.PROTOCOL_VERSION.toString()) {
73
+ return message;
74
+ }
75
+ this.fastify.log.error({
76
+ msg: 'Received message had wrong protocol version',
77
+ message,
78
+ });
79
+ }
80
+ catch {
81
+ this.fastify.log.error({
82
+ msg: 'Failed to parse the message as JSON',
83
+ data,
84
+ });
85
+ }
86
+ return undefined;
167
87
  }
168
- const socket = this.getClientSocket(message.target);
169
- socket.send(JSON.stringify({
170
- version: WebSocketMessageServer.PROTOCOL_VERSION,
171
- method: message.method,
172
- params: message.params,
173
- id: message.id === undefined ? undefined : {
174
- requestId: message.id,
175
- clientId
176
- }
177
- }));
178
- }
179
-
180
- /**
181
- * Send given response `message` to it's designated client's socket based
182
- * on `message.id.clientId`.
183
- * The target client must be connected, otherwise it will throw an error.
184
- *
185
- * @param message Message to forward.
186
- */
187
- forwardResponse(message) {
188
- if (!message.id) {
189
- return;
88
+ /**
89
+ * Get client's WebSocket connection for given `clientId`.
90
+ * Throws if no such client is connected.
91
+ *
92
+ * @param clientId Id of the client.
93
+ * @returns WebSocket connection.
94
+ */
95
+ getClientSocket(clientId) {
96
+ const socket = this.clients.get(clientId);
97
+ if (socket === undefined) {
98
+ throw new Error(`Could not find client with id "${clientId}"`);
99
+ }
100
+ return socket;
190
101
  }
191
- const socket = this.getClientSocket(message.id.clientId);
192
- socket.send(JSON.stringify({
193
- version: WebSocketMessageServer.PROTOCOL_VERSION,
194
- result: message.result,
195
- error: message.error,
196
- id: message.id.requestId
197
- }));
198
- }
199
-
200
- /**
201
- * Process request message targeted towards this {@link WebSocketMessageServer}
202
- * and send back the results.
203
- *
204
- * @param clientId Id of the client who send the message.
205
- * @param message The message to process by the server.
206
- */
207
- processServerRequest(clientId, message) {
208
- let result;
209
- switch (message.method) {
210
- case 'getid':
211
- result = clientId;
212
- break;
213
- case 'getpeers':
214
- {
215
- const output = {};
216
- this.clients.forEach((peerSocket, peerId) => {
217
- if (clientId !== peerId) {
218
- const {
219
- searchParams
220
- } = new URL(peerSocket.upgradeReq?.url || '');
221
- output[peerId] = [...searchParams.entries()].reduce((acc, [key, value]) => {
222
- acc[key] = value;
223
- return acc;
224
- }, {});
102
+ /**
103
+ * Process error by sending an error message to the client whose message caused the error
104
+ * to occur.
105
+ *
106
+ * @param clientId Id of the client whose message caused an error.
107
+ * @param message Original message which caused the error.
108
+ * @param error Concrete instance of an error that occurred.
109
+ */
110
+ handleError(clientId, message, error) {
111
+ const errorMessage = {
112
+ id: message.id,
113
+ method: message.method,
114
+ target: message.target,
115
+ error: message.error === undefined ? 'undefined' : 'defined',
116
+ params: message.params === undefined ? 'undefined' : 'defined',
117
+ result: message.result === undefined ? 'undefined' : 'defined',
118
+ };
119
+ if (message.id === undefined) {
120
+ this.fastify.log.error({
121
+ msg: 'Handling message failed',
122
+ clientId,
123
+ error,
124
+ errorMessage,
125
+ });
126
+ }
127
+ else {
128
+ try {
129
+ const socket = this.getClientSocket(clientId);
130
+ socket.send(JSON.stringify({
131
+ version: WebSocketMessageServer.PROTOCOL_VERSION,
132
+ error,
133
+ id: message.id,
134
+ }));
225
135
  }
226
- });
227
- result = output;
228
- break;
136
+ catch (error) {
137
+ this.fastify.log.error('Failed to reply', {
138
+ clientId,
139
+ error,
140
+ errorMessage,
141
+ });
142
+ }
143
+ }
144
+ }
145
+ /**
146
+ * Send given request `message` to it's designated client's socket based on `message.target`.
147
+ * The target client must be connected, otherwise it will throw an error.
148
+ *
149
+ * @param clientId Id of the client that requested the forward.
150
+ * @param message Message to forward.
151
+ */
152
+ forwardRequest(clientId, message) {
153
+ if (!message.target) {
154
+ this.fastify.log.error({
155
+ msg: 'Failed to forward request - message.target is missing',
156
+ clientId,
157
+ message,
158
+ });
159
+ return;
160
+ }
161
+ const socket = this.getClientSocket(message.target);
162
+ socket.send(JSON.stringify({
163
+ version: WebSocketMessageServer.PROTOCOL_VERSION,
164
+ method: message.method,
165
+ params: message.params,
166
+ id: message.id === undefined
167
+ ? undefined
168
+ : { requestId: message.id, clientId },
169
+ }));
170
+ }
171
+ /**
172
+ * Send given response `message` to it's designated client's socket based
173
+ * on `message.id.clientId`.
174
+ * The target client must be connected, otherwise it will throw an error.
175
+ *
176
+ * @param message Message to forward.
177
+ */
178
+ forwardResponse(message) {
179
+ if (!message.id) {
180
+ return;
229
181
  }
230
- default:
231
- throw new Error(`Cannot process server request - unknown method ${JSON.stringify({
232
- clientId,
233
- message
234
- })}`);
182
+ const socket = this.getClientSocket(message.id.clientId);
183
+ socket.send(JSON.stringify({
184
+ version: WebSocketMessageServer.PROTOCOL_VERSION,
185
+ result: message.result,
186
+ error: message.error,
187
+ id: message.id.requestId,
188
+ }));
235
189
  }
236
- const socket = this.getClientSocket(clientId);
237
- socket.send(JSON.stringify({
238
- version: WebSocketMessageServer.PROTOCOL_VERSION,
239
- result,
240
- id: message.id
241
- }));
242
- }
243
-
244
- /**
245
- * Broadcast given message to all connected clients.
246
- *
247
- * @param broadcasterId Id of the client who is broadcasting.
248
- * @param message Message to broadcast.
249
- */
250
- sendBroadcast(broadcasterId, message) {
251
- const forwarded = {
252
- version: WebSocketMessageServer.PROTOCOL_VERSION,
253
- method: message.method,
254
- params: message.params
255
- };
256
- if (this.clients.size === 0) {
257
- this.fastify.log.warn({
258
- msg: 'No apps connected. ' + `Sending "${message.method}" to all React Native apps failed. ` + 'Make sure your app is running in the simulator or on a phone connected via USB.'
259
- });
190
+ /**
191
+ * Process request message targeted towards this {@link WebSocketMessageServer}
192
+ * and send back the results.
193
+ *
194
+ * @param clientId Id of the client who send the message.
195
+ * @param message The message to process by the server.
196
+ */
197
+ processServerRequest(clientId, message) {
198
+ let result;
199
+ switch (message.method) {
200
+ case 'getid':
201
+ result = clientId;
202
+ break;
203
+ case 'getpeers': {
204
+ const output = {};
205
+ this.clients.forEach((peerSocket, peerId) => {
206
+ if (clientId !== peerId) {
207
+ const { searchParams } = new URL(peerSocket.upgradeReq?.url || '');
208
+ output[peerId] = [...searchParams.entries()].reduce((acc, [key, value]) => {
209
+ acc[key] = value;
210
+ return acc;
211
+ }, {});
212
+ }
213
+ });
214
+ result = output;
215
+ break;
216
+ }
217
+ default:
218
+ throw new Error(`Cannot process server request - unknown method ${JSON.stringify({
219
+ clientId,
220
+ message,
221
+ })}`);
222
+ }
223
+ const socket = this.getClientSocket(clientId);
224
+ socket.send(JSON.stringify({
225
+ version: WebSocketMessageServer.PROTOCOL_VERSION,
226
+ result,
227
+ id: message.id,
228
+ }));
260
229
  }
261
- for (const [clientId, socket] of this.clients) {
262
- if (clientId !== broadcasterId) {
263
- try {
264
- socket.send(JSON.stringify(forwarded));
265
- } catch (error) {
266
- this.fastify.log.error({
267
- msg: 'Failed to send broadcast',
268
- clientId,
269
- error,
270
- forwarded
271
- });
230
+ /**
231
+ * Broadcast given message to all connected clients.
232
+ *
233
+ * @param broadcasterId Id of the client who is broadcasting.
234
+ * @param message Message to broadcast.
235
+ */
236
+ sendBroadcast(broadcasterId, message) {
237
+ const forwarded = {
238
+ version: WebSocketMessageServer.PROTOCOL_VERSION,
239
+ method: message.method,
240
+ params: message.params,
241
+ };
242
+ if (this.clients.size === 0) {
243
+ this.fastify.log.warn({
244
+ msg: 'No apps connected. ' +
245
+ `Sending "${message.method}" to all React Native apps failed. ` +
246
+ 'Make sure your app is running in the simulator or on a phone connected via USB.',
247
+ });
248
+ }
249
+ for (const [clientId, socket] of this.clients) {
250
+ if (clientId !== broadcasterId) {
251
+ try {
252
+ socket.send(JSON.stringify(forwarded));
253
+ }
254
+ catch (error) {
255
+ this.fastify.log.error({
256
+ msg: 'Failed to send broadcast',
257
+ clientId,
258
+ error,
259
+ forwarded,
260
+ });
261
+ }
262
+ }
272
263
  }
273
- }
274
264
  }
275
- }
276
-
277
- /**
278
- * Send method broadcast to all connected clients.
279
- *
280
- * @param method Method name to broadcast.
281
- * @param params Method parameters.
282
- */
283
- broadcast(method, params) {
284
- this.sendBroadcast(undefined, {
285
- method,
286
- params
287
- });
288
- }
289
-
290
- /**
291
- * Process new client's WebSocket connection.
292
- *
293
- * @param socket Incoming WebSocket connection.
294
- * @param request Upgrade request for the connection.
295
- */
296
- onConnection(socket, request) {
297
- const clientId = `client#${this.nextClientId++}`;
298
- const client = socket;
299
- client.upgradeReq = request;
300
- this.clients.set(clientId, client);
301
- this.fastify.log.debug({
302
- msg: 'Message client connected',
303
- clientId
304
- });
305
- const onClose = () => {
306
- this.fastify.log.debug({
307
- msg: 'Message client disconnected',
308
- clientId
309
- });
310
- socket.removeAllListeners();
311
- this.clients.delete(clientId);
312
- };
313
- socket.addEventListener('error', onClose);
314
- socket.addEventListener('close', onClose);
315
- socket.addEventListener('message', event => {
316
- const message = this.parseMessage(event.data.toString(),
317
- // @ts-ignore
318
- event.binary);
319
- if (!message) {
320
- this.fastify.log.error({
321
- msg: 'Received message not matching protocol',
322
- clientId,
323
- message
265
+ /**
266
+ * Send method broadcast to all connected clients.
267
+ *
268
+ * @param method Method name to broadcast.
269
+ * @param params Method parameters.
270
+ */
271
+ broadcast(method, params) {
272
+ this.sendBroadcast(undefined, { method, params });
273
+ }
274
+ /**
275
+ * Process new client's WebSocket connection.
276
+ *
277
+ * @param socket Incoming WebSocket connection.
278
+ * @param request Upgrade request for the connection.
279
+ */
280
+ onConnection(socket, request) {
281
+ const clientId = `client#${this.nextClientId++}`;
282
+ const client = socket;
283
+ client.upgradeReq = request;
284
+ this.clients.set(clientId, client);
285
+ this.fastify.log.debug({ msg: 'Message client connected', clientId });
286
+ const onClose = () => {
287
+ this.fastify.log.debug({ msg: 'Message client disconnected', clientId });
288
+ socket.removeAllListeners();
289
+ this.clients.delete(clientId);
290
+ };
291
+ socket.addEventListener('error', onClose);
292
+ socket.addEventListener('close', onClose);
293
+ socket.addEventListener('message', (event) => {
294
+ const message = this.parseMessage(event.data.toString(),
295
+ // @ts-ignore
296
+ event.binary);
297
+ if (!message) {
298
+ this.fastify.log.error({
299
+ msg: 'Received message not matching protocol',
300
+ clientId,
301
+ message,
302
+ });
303
+ return;
304
+ }
305
+ try {
306
+ if (WebSocketMessageServer.isBroadcast(message)) {
307
+ this.sendBroadcast(clientId, message);
308
+ }
309
+ else if (WebSocketMessageServer.isRequest(message)) {
310
+ if (message.target === 'server') {
311
+ this.processServerRequest(clientId, message);
312
+ }
313
+ else {
314
+ this.forwardRequest(clientId, message);
315
+ }
316
+ }
317
+ else if (WebSocketMessageServer.isResponse(message)) {
318
+ this.forwardResponse(message);
319
+ }
320
+ else {
321
+ throw new Error(`Invalid message, did not match the protocol ${JSON.stringify({
322
+ clientId,
323
+ message,
324
+ })}`);
325
+ }
326
+ }
327
+ catch (error) {
328
+ this.handleError(clientId, message, error);
329
+ }
324
330
  });
325
- return;
326
- }
327
- try {
328
- if (WebSocketMessageServer.isBroadcast(message)) {
329
- this.sendBroadcast(clientId, message);
330
- } else if (WebSocketMessageServer.isRequest(message)) {
331
- if (message.target === 'server') {
332
- this.processServerRequest(clientId, message);
333
- } else {
334
- this.forwardRequest(clientId, message);
335
- }
336
- } else if (WebSocketMessageServer.isResponse(message)) {
337
- this.forwardResponse(message);
338
- } else {
339
- throw new Error(`Invalid message, did not match the protocol ${JSON.stringify({
340
- clientId,
341
- message
342
- })}`);
343
- }
344
- } catch (error) {
345
- this.handleError(clientId, message, error);
346
- }
347
- });
348
- }
331
+ }
349
332
  }
350
- //# sourceMappingURL=WebSocketMessageServer.js.map
333
+ WebSocketMessageServer.PROTOCOL_VERSION = 2;
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=types.js.map