@callstack/repack-dev-server 0.0.0-canary-20240602190113
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 +140 -0
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/dist/createServer.d.ts +13 -0
- package/dist/createServer.js +147 -0
- package/dist/createServer.js.map +1 -0
- package/dist/img/favicon.ico +0 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/api/apiPlugin.d.ts +6 -0
- package/dist/plugins/api/apiPlugin.js +32 -0
- package/dist/plugins/api/apiPlugin.js.map +1 -0
- package/dist/plugins/api/index.d.ts +1 -0
- package/dist/plugins/api/index.js +2 -0
- package/dist/plugins/api/index.js.map +1 -0
- package/dist/plugins/compiler/compilerPlugin.d.ts +7 -0
- package/dist/plugins/compiler/compilerPlugin.js +75 -0
- package/dist/plugins/compiler/compilerPlugin.js.map +1 -0
- package/dist/plugins/compiler/index.d.ts +2 -0
- package/dist/plugins/compiler/index.js +3 -0
- package/dist/plugins/compiler/index.js.map +1 -0
- package/dist/plugins/compiler/types.d.ts +31 -0
- package/dist/plugins/compiler/types.js +2 -0
- package/dist/plugins/compiler/types.js.map +1 -0
- package/dist/plugins/devtools/devtoolsPlugin.d.ts +7 -0
- package/dist/plugins/devtools/devtoolsPlugin.js +47 -0
- package/dist/plugins/devtools/devtoolsPlugin.js.map +1 -0
- package/dist/plugins/devtools/index.d.ts +1 -0
- package/dist/plugins/devtools/index.js +2 -0
- package/dist/plugins/devtools/index.js.map +1 -0
- package/dist/plugins/favicon/faviconPlugin.d.ts +4 -0
- package/dist/plugins/favicon/faviconPlugin.js +17 -0
- package/dist/plugins/favicon/faviconPlugin.js.map +1 -0
- package/dist/plugins/favicon/index.d.ts +1 -0
- package/dist/plugins/favicon/index.js +2 -0
- package/dist/plugins/favicon/index.js.map +1 -0
- package/dist/plugins/multipart/index.d.ts +2 -0
- package/dist/plugins/multipart/index.js +3 -0
- package/dist/plugins/multipart/index.js.map +1 -0
- package/dist/plugins/multipart/multipartPlugin.d.ts +4 -0
- package/dist/plugins/multipart/multipartPlugin.js +55 -0
- package/dist/plugins/multipart/multipartPlugin.js.map +1 -0
- package/dist/plugins/multipart/types.d.ts +12 -0
- package/dist/plugins/multipart/types.js +2 -0
- package/dist/plugins/multipart/types.js.map +1 -0
- package/dist/plugins/symbolicate/Symbolicator.d.ts +46 -0
- package/dist/plugins/symbolicate/Symbolicator.js +208 -0
- package/dist/plugins/symbolicate/Symbolicator.js.map +1 -0
- package/dist/plugins/symbolicate/index.d.ts +3 -0
- package/dist/plugins/symbolicate/index.js +4 -0
- package/dist/plugins/symbolicate/index.js.map +1 -0
- package/dist/plugins/symbolicate/sybmolicatePlugin.d.ts +7 -0
- package/dist/plugins/symbolicate/sybmolicatePlugin.js +44 -0
- package/dist/plugins/symbolicate/sybmolicatePlugin.js.map +1 -0
- package/dist/plugins/symbolicate/types.d.ts +65 -0
- package/dist/plugins/symbolicate/types.js +2 -0
- package/dist/plugins/symbolicate/types.js.map +1 -0
- package/dist/plugins/wss/WebSocketRouter.d.ts +32 -0
- package/dist/plugins/wss/WebSocketRouter.js +57 -0
- package/dist/plugins/wss/WebSocketRouter.js.map +1 -0
- package/dist/plugins/wss/WebSocketServer.d.ts +38 -0
- package/dist/plugins/wss/WebSocketServer.js +45 -0
- package/dist/plugins/wss/WebSocketServer.js.map +1 -0
- package/dist/plugins/wss/WebSocketServerAdapter.d.ts +16 -0
- package/dist/plugins/wss/WebSocketServerAdapter.js +22 -0
- package/dist/plugins/wss/WebSocketServerAdapter.js.map +1 -0
- package/dist/plugins/wss/index.d.ts +3 -0
- package/dist/plugins/wss/index.js +4 -0
- package/dist/plugins/wss/index.js.map +1 -0
- package/dist/plugins/wss/servers/WebSocketApiServer.d.ts +32 -0
- package/dist/plugins/wss/servers/WebSocketApiServer.js +62 -0
- package/dist/plugins/wss/servers/WebSocketApiServer.js.map +1 -0
- package/dist/plugins/wss/servers/WebSocketDebuggerServer.d.ts +64 -0
- package/dist/plugins/wss/servers/WebSocketDebuggerServer.js +142 -0
- package/dist/plugins/wss/servers/WebSocketDebuggerServer.js.map +1 -0
- package/dist/plugins/wss/servers/WebSocketDevClientServer.d.ts +32 -0
- package/dist/plugins/wss/servers/WebSocketDevClientServer.js +86 -0
- package/dist/plugins/wss/servers/WebSocketDevClientServer.js.map +1 -0
- package/dist/plugins/wss/servers/WebSocketEventsServer.d.ts +75 -0
- package/dist/plugins/wss/servers/WebSocketEventsServer.js +180 -0
- package/dist/plugins/wss/servers/WebSocketEventsServer.js.map +1 -0
- package/dist/plugins/wss/servers/WebSocketHMRServer.d.ts +38 -0
- package/dist/plugins/wss/servers/WebSocketHMRServer.js +89 -0
- package/dist/plugins/wss/servers/WebSocketHMRServer.js.map +1 -0
- package/dist/plugins/wss/servers/WebSocketMessageServer.d.ts +140 -0
- package/dist/plugins/wss/servers/WebSocketMessageServer.js +350 -0
- package/dist/plugins/wss/servers/WebSocketMessageServer.js.map +1 -0
- package/dist/plugins/wss/types.d.ts +25 -0
- package/dist/plugins/wss/types.js +2 -0
- package/dist/plugins/wss/types.js.map +1 -0
- package/dist/plugins/wss/wssPlugin.d.ts +31 -0
- package/dist/plugins/wss/wssPlugin.js +56 -0
- package/dist/plugins/wss/wssPlugin.js.map +1 -0
- package/dist/types.d.ts +165 -0
- package/dist/types.js +24 -0
- package/dist/types.js.map +1 -0
- package/package.json +83 -0
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { URL } from 'url';
|
|
2
|
+
import { WebSocketServer } from "../WebSocketServer.js";
|
|
3
|
+
/**
|
|
4
|
+
* Holds {@link ReactNativeMessage} `id` data.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Message representation used by {@link WebSocketMessageServer}.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Class for creating a WebSocket server and sending messages between development server
|
|
11
|
+
* and the React Native applications.
|
|
12
|
+
*
|
|
13
|
+
* Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/messageSocketServer.ts
|
|
14
|
+
*
|
|
15
|
+
* @category Development server
|
|
16
|
+
*/
|
|
17
|
+
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;
|
|
76
|
+
}
|
|
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 (e) {
|
|
87
|
+
this.fastify.log.error({
|
|
88
|
+
msg: 'Failed to parse the message as JSON',
|
|
89
|
+
data
|
|
90
|
+
});
|
|
91
|
+
}
|
|
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}"`);
|
|
106
|
+
}
|
|
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
|
+
}
|
|
149
|
+
}
|
|
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;
|
|
167
|
+
}
|
|
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;
|
|
190
|
+
}
|
|
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,
|
|
223
|
+
[key]: value
|
|
224
|
+
}), {});
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
result = output;
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
default:
|
|
231
|
+
throw new Error(`Cannot process server request - unknown method ${JSON.stringify({
|
|
232
|
+
clientId,
|
|
233
|
+
message
|
|
234
|
+
})}`);
|
|
235
|
+
}
|
|
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
|
+
});
|
|
260
|
+
}
|
|
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
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
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
|
+
let 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
|
|
324
|
+
});
|
|
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
|
+
}
|
|
349
|
+
}
|
|
350
|
+
//# sourceMappingURL=WebSocketMessageServer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebSocketMessageServer.js","names":["URL","WebSocketServer","WebSocketMessageServer","PROTOCOL_VERSION","isBroadcast","message","method","id","undefined","target","isRequest","isResponse","requestId","clientId","result","error","clients","Map","nextClientId","constructor","fastify","parseMessage","data","binary","log","msg","JSON","parse","version","toString","e","getClientSocket","socket","get","Error","handleError","errorMessage","params","send","stringify","forwardRequest","forwardResponse","processServerRequest","output","forEach","peerSocket","peerId","searchParams","upgradeReq","url","entries","reduce","acc","key","value","sendBroadcast","broadcasterId","forwarded","size","warn","broadcast","onConnection","request","client","set","debug","onClose","removeAllListeners","delete","addEventListener","event"],"sources":["../../../../src/plugins/wss/servers/WebSocketMessageServer.ts"],"sourcesContent":["import type { IncomingMessage } from 'http';\nimport { URL } from 'url';\nimport type { FastifyInstance } from 'fastify';\nimport WebSocket from 'ws';\nimport { WebSocketServer } from '../WebSocketServer';\n\n/**\n * Holds {@link ReactNativeMessage} `id` data.\n */\nexport interface ReactNativeIdObject {\n requestId: string;\n clientId: string;\n}\n\n/**\n * Message representation used by {@link WebSocketMessageServer}.\n */\nexport interface ReactNativeMessage {\n version?: string;\n id?: ReactNativeIdObject;\n method?: string;\n target: string;\n result?: any;\n error?: Error;\n params?: Record<string, any>;\n}\n\ntype WebSocketWithUpgradeReq = WebSocket & { upgradeReq?: IncomingMessage };\n\n/**\n * Class for creating a WebSocket server and sending messages between development server\n * and the React Native applications.\n *\n * Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/messageSocketServer.ts\n *\n * @category Development server\n */\nexport class WebSocketMessageServer extends WebSocketServer {\n static readonly PROTOCOL_VERSION = 2;\n\n /**\n * Check if message is a broadcast request.\n *\n * @param message Message to check.\n * @returns True if message is a broadcast request and should be broadcasted\n * with {@link sendBroadcast}.\n */\n static isBroadcast(message: Partial<ReactNativeMessage>) {\n return (\n typeof message.method === 'string' &&\n message.id === undefined &&\n message.target === undefined\n );\n }\n\n /**\n * Check if message is a method request.\n *\n * @param message Message to check.\n * @returns True if message is a request.\n */\n static isRequest(message: Partial<ReactNativeMessage>) {\n return (\n typeof message.method === 'string' && typeof message.target === 'string'\n );\n }\n\n /**\n * Check if message is a response with results of performing some request.\n *\n * @param message Message to check.\n * @returns True if message is a response.\n */\n static isResponse(message: Partial<ReactNativeMessage>) {\n return (\n typeof message.id === 'object' &&\n typeof message.id.requestId !== 'undefined' &&\n typeof message.id.clientId === 'string' &&\n (message.result !== undefined || message.error !== undefined)\n );\n }\n\n private clients = new Map<string, WebSocketWithUpgradeReq>();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketMessageServer and attach it to the given Fastify instance.\n * Any logging information, will be passed through standard `fastify.log` API.\n *\n * @param fastify Fastify instance to attach the WebSocket server to.\n */\n constructor(fastify: FastifyInstance) {\n super(fastify, '/message');\n }\n\n /**\n * Parse stringified message into a {@link ReactNativeMessage}.\n *\n * @param data Stringified message.\n * @param binary Additional binary data if any.\n * @returns Parsed message or `undefined` if parsing failed.\n */\n parseMessage(\n data: string,\n binary: any\n ): Partial<ReactNativeMessage> | undefined {\n if (binary) {\n this.fastify.log.error({\n msg: 'Failed to parse message - expected text message, got binary',\n });\n return undefined;\n }\n try {\n const message = JSON.parse(data) as Partial<ReactNativeMessage>;\n if (\n message.version === WebSocketMessageServer.PROTOCOL_VERSION.toString()\n ) {\n return message;\n }\n this.fastify.log.error({\n msg: 'Received message had wrong protocol version',\n message,\n });\n } catch (e) {\n this.fastify.log.error({\n msg: 'Failed to parse the message as JSON',\n data,\n });\n }\n return undefined;\n }\n\n /**\n * Get client's WebSocket connection for given `clientId`.\n * Throws if no such client is connected.\n *\n * @param clientId Id of the client.\n * @returns WebSocket connection.\n */\n getClientSocket(clientId: string) {\n const socket = this.clients.get(clientId);\n if (socket === undefined) {\n throw new Error(`Could not find client with id \"${clientId}\"`);\n }\n return socket;\n }\n\n /**\n * Process error by sending an error message to the client whose message caused the error\n * to occur.\n *\n * @param clientId Id of the client whose message caused an error.\n * @param message Original message which caused the error.\n * @param error Concrete instance of an error that occurred.\n */\n handleError(\n clientId: string,\n message: Partial<ReactNativeMessage>,\n error: Error\n ) {\n const errorMessage = {\n id: message.id,\n method: message.method,\n target: message.target,\n error: message.error === undefined ? 'undefined' : 'defined',\n params: message.params === undefined ? 'undefined' : 'defined',\n result: message.result === undefined ? 'undefined' : 'defined',\n };\n\n if (message.id === undefined) {\n this.fastify.log.error({\n msg: 'Handling message failed',\n clientId,\n error,\n errorMessage,\n });\n } else {\n try {\n const socket = this.getClientSocket(clientId);\n socket.send(\n JSON.stringify({\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n error,\n id: message.id,\n })\n );\n } catch (error) {\n this.fastify.log.error('Failed to reply', {\n clientId,\n error,\n errorMessage,\n });\n }\n }\n }\n\n /**\n * Send given request `message` to it's designated client's socket based on `message.target`.\n * The target client must be connected, otherwise it will throw an error.\n *\n * @param clientId Id of the client that requested the forward.\n * @param message Message to forward.\n */\n forwardRequest(clientId: string, message: Partial<ReactNativeMessage>) {\n if (!message.target) {\n this.fastify.log.error({\n msg: 'Failed to forward request - message.target is missing',\n clientId,\n message,\n });\n return;\n }\n\n const socket = this.getClientSocket(message.target);\n socket.send(\n JSON.stringify({\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n method: message.method,\n params: message.params,\n id:\n message.id === undefined\n ? undefined\n : { requestId: message.id, clientId },\n })\n );\n }\n\n /**\n * Send given response `message` to it's designated client's socket based\n * on `message.id.clientId`.\n * The target client must be connected, otherwise it will throw an error.\n *\n * @param message Message to forward.\n */\n forwardResponse(message: Partial<ReactNativeMessage>) {\n if (!message.id) {\n return;\n }\n\n const socket = this.getClientSocket(message.id.clientId);\n socket.send(\n JSON.stringify({\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n result: message.result,\n error: message.error,\n id: message.id.requestId,\n })\n );\n }\n\n /**\n * Process request message targeted towards this {@link WebSocketMessageServer}\n * and send back the results.\n *\n * @param clientId Id of the client who send the message.\n * @param message The message to process by the server.\n */\n processServerRequest(clientId: string, message: Partial<ReactNativeMessage>) {\n let result: string | Record<string, Record<string, string>>;\n\n switch (message.method) {\n case 'getid':\n result = clientId;\n break;\n case 'getpeers': {\n const output: Record<string, Record<string, string>> = {};\n this.clients.forEach((peerSocket, peerId) => {\n if (clientId !== peerId) {\n const { searchParams } = new URL(peerSocket.upgradeReq?.url || '');\n output[peerId] = [...searchParams.entries()].reduce(\n (acc, [key, value]) => ({\n ...acc,\n [key]: value,\n }),\n {}\n );\n }\n });\n result = output;\n break;\n }\n default:\n throw new Error(\n `Cannot process server request - unknown method ${JSON.stringify({\n clientId,\n message,\n })}`\n );\n }\n\n const socket = this.getClientSocket(clientId);\n socket.send(\n JSON.stringify({\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n result,\n id: message.id,\n })\n );\n }\n\n /**\n * Broadcast given message to all connected clients.\n *\n * @param broadcasterId Id of the client who is broadcasting.\n * @param message Message to broadcast.\n */\n sendBroadcast(\n broadcasterId: string | undefined,\n message: Partial<ReactNativeMessage>\n ) {\n const forwarded = {\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n method: message.method,\n params: message.params,\n };\n\n if (this.clients.size === 0) {\n this.fastify.log.warn({\n msg:\n 'No apps connected. ' +\n `Sending \"${message.method}\" to all React Native apps failed. ` +\n 'Make sure your app is running in the simulator or on a phone connected via USB.',\n });\n }\n\n for (const [clientId, socket] of this.clients) {\n if (clientId !== broadcasterId) {\n try {\n socket.send(JSON.stringify(forwarded));\n } catch (error) {\n this.fastify.log.error({\n msg: 'Failed to send broadcast',\n clientId,\n error,\n forwarded,\n });\n }\n }\n }\n }\n\n /**\n * Send method broadcast to all connected clients.\n *\n * @param method Method name to broadcast.\n * @param params Method parameters.\n */\n broadcast(method: string, params?: Record<string, any>) {\n this.sendBroadcast(undefined, { method, params });\n }\n\n /**\n * Process new client's WebSocket connection.\n *\n * @param socket Incoming WebSocket connection.\n * @param request Upgrade request for the connection.\n */\n onConnection(socket: WebSocket, request: IncomingMessage) {\n const clientId = `client#${this.nextClientId++}`;\n let client: WebSocketWithUpgradeReq = socket;\n client.upgradeReq = request;\n this.clients.set(clientId, client);\n this.fastify.log.debug({ msg: 'Message client connected', clientId });\n\n const onClose = () => {\n this.fastify.log.debug({ msg: 'Message client disconnected', clientId });\n socket.removeAllListeners();\n this.clients.delete(clientId);\n };\n\n socket.addEventListener('error', onClose);\n socket.addEventListener('close', onClose);\n socket.addEventListener('message', (event) => {\n const message = this.parseMessage(\n event.data.toString(),\n // @ts-ignore\n event.binary\n );\n\n if (!message) {\n this.fastify.log.error({\n msg: 'Received message not matching protocol',\n clientId,\n message,\n });\n return;\n }\n\n try {\n if (WebSocketMessageServer.isBroadcast(message)) {\n this.sendBroadcast(clientId, message);\n } else if (WebSocketMessageServer.isRequest(message)) {\n if (message.target === 'server') {\n this.processServerRequest(clientId, message);\n } else {\n this.forwardRequest(clientId, message);\n }\n } else if (WebSocketMessageServer.isResponse(message)) {\n this.forwardResponse(message);\n } else {\n throw new Error(\n `Invalid message, did not match the protocol ${JSON.stringify({\n clientId,\n message,\n })}`\n );\n }\n } catch (error) {\n this.handleError(clientId, message, error as Error);\n }\n });\n }\n}\n"],"mappings":"AACA,SAASA,GAAG,QAAQ,KAAK;AAAC,SAGjBC,eAAe;AAExB;AACA;AACA;AAMA;AACA;AACA;AAaA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,sBAAsB,SAASD,eAAe,CAAC;EAC1D,OAAgBE,gBAAgB,GAAG,CAAC;;EAEpC;AACF;AACA;AACA;AACA;AACA;AACA;EACE,OAAOC,WAAWA,CAACC,OAAoC,EAAE;IACvD,OACE,OAAOA,OAAO,CAACC,MAAM,KAAK,QAAQ,IAClCD,OAAO,CAACE,EAAE,KAAKC,SAAS,IACxBH,OAAO,CAACI,MAAM,KAAKD,SAAS;EAEhC;;EAEA;AACF;AACA;AACA;AACA;AACA;EACE,OAAOE,SAASA,CAACL,OAAoC,EAAE;IACrD,OACE,OAAOA,OAAO,CAACC,MAAM,KAAK,QAAQ,IAAI,OAAOD,OAAO,CAACI,MAAM,KAAK,QAAQ;EAE5E;;EAEA;AACF;AACA;AACA;AACA;AACA;EACE,OAAOE,UAAUA,CAACN,OAAoC,EAAE;IACtD,OACE,OAAOA,OAAO,CAACE,EAAE,KAAK,QAAQ,IAC9B,OAAOF,OAAO,CAACE,EAAE,CAACK,SAAS,KAAK,WAAW,IAC3C,OAAOP,OAAO,CAACE,EAAE,CAACM,QAAQ,KAAK,QAAQ,KACtCR,OAAO,CAACS,MAAM,KAAKN,SAAS,IAAIH,OAAO,CAACU,KAAK,KAAKP,SAAS,CAAC;EAEjE;EAEQQ,OAAO,GAAG,IAAIC,GAAG,CAAkC,CAAC;EACpDC,YAAY,GAAG,CAAC;;EAExB;AACF;AACA;AACA;AACA;AACA;EACEC,WAAWA,CAACC,OAAwB,EAAE;IACpC,KAAK,CAACA,OAAO,EAAE,UAAU,CAAC;EAC5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
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
/// <reference types="node" />
|
|
4
|
+
import { IncomingMessage } from 'node:http';
|
|
5
|
+
import { Socket } from 'node:net';
|
|
6
|
+
/**
|
|
7
|
+
* Delegate with implementation for HMR-specific functions.
|
|
8
|
+
*/
|
|
9
|
+
export interface HmrDelegate {
|
|
10
|
+
/** Get URI under which HMR server will be running, e.g: `/hmr` */
|
|
11
|
+
getUriPath: () => string;
|
|
12
|
+
/**
|
|
13
|
+
* Callback for when the new HMR client is connected.
|
|
14
|
+
*
|
|
15
|
+
* Useful for running initial synchronization or any other side effect.
|
|
16
|
+
*
|
|
17
|
+
* @param platform Platform of the connected client.
|
|
18
|
+
* @param clientId Id of the connected client.
|
|
19
|
+
*/
|
|
20
|
+
onClientConnected: (platform: string, clientId: string) => void;
|
|
21
|
+
}
|
|
22
|
+
export interface WebSocketServerInterface {
|
|
23
|
+
shouldUpgrade(pathname: string): boolean;
|
|
24
|
+
upgrade(request: IncomingMessage, socket: Socket, head: Buffer): void;
|
|
25
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","names":[],"sources":["../../../src/plugins/wss/types.ts"],"sourcesContent":["import { IncomingMessage } from 'node:http';\nimport { Socket } from 'node:net';\n\n/**\n * Delegate with implementation for HMR-specific functions.\n */\nexport interface HmrDelegate {\n /** Get URI under which HMR server will be running, e.g: `/hmr` */\n getUriPath: () => string;\n\n /**\n * Callback for when the new HMR client is connected.\n *\n * Useful for running initial synchronization or any other side effect.\n *\n * @param platform Platform of the connected client.\n * @param clientId Id of the connected client.\n */\n onClientConnected: (platform: string, clientId: string) => void;\n}\n\nexport interface WebSocketServerInterface {\n shouldUpgrade(pathname: string): boolean;\n upgrade(request: IncomingMessage, socket: Socket, head: Buffer): void;\n}\n"],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { FastifyInstance } from 'fastify';
|
|
2
|
+
import type { Server } from '../../types';
|
|
3
|
+
import { WebSocketDebuggerServer } from './servers/WebSocketDebuggerServer';
|
|
4
|
+
import { WebSocketDevClientServer } from './servers/WebSocketDevClientServer';
|
|
5
|
+
import { WebSocketMessageServer } from './servers/WebSocketMessageServer';
|
|
6
|
+
import { WebSocketEventsServer } from './servers/WebSocketEventsServer';
|
|
7
|
+
import { WebSocketApiServer } from './servers/WebSocketApiServer';
|
|
8
|
+
import { WebSocketHMRServer } from './servers/WebSocketHMRServer';
|
|
9
|
+
import { WebSocketRouter } from './WebSocketRouter';
|
|
10
|
+
import { WebSocketServerAdapter } from './WebSocketServerAdapter';
|
|
11
|
+
declare module 'fastify' {
|
|
12
|
+
interface FastifyInstance {
|
|
13
|
+
wss: {
|
|
14
|
+
debuggerServer: WebSocketDebuggerServer;
|
|
15
|
+
devClientServer: WebSocketDevClientServer;
|
|
16
|
+
messageServer: WebSocketMessageServer;
|
|
17
|
+
eventsServer: WebSocketEventsServer;
|
|
18
|
+
apiServer: WebSocketApiServer;
|
|
19
|
+
hmrServer: WebSocketHMRServer;
|
|
20
|
+
deviceConnectionServer: WebSocketServerAdapter;
|
|
21
|
+
debuggerConnectionServer: WebSocketServerAdapter;
|
|
22
|
+
router: WebSocketRouter;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
declare function wssPlugin(instance: FastifyInstance, { options, delegate, }: {
|
|
27
|
+
options: Server.Options;
|
|
28
|
+
delegate: Server.Delegate;
|
|
29
|
+
}): Promise<void>;
|
|
30
|
+
declare const _default: typeof wssPlugin;
|
|
31
|
+
export default _default;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import fastifyPlugin from 'fastify-plugin';
|
|
2
|
+
import { WebSocketDebuggerServer } from "./servers/WebSocketDebuggerServer.js";
|
|
3
|
+
import { WebSocketDevClientServer } from "./servers/WebSocketDevClientServer.js";
|
|
4
|
+
import { WebSocketMessageServer } from "./servers/WebSocketMessageServer.js";
|
|
5
|
+
import { WebSocketEventsServer } from "./servers/WebSocketEventsServer.js";
|
|
6
|
+
import { WebSocketApiServer } from "./servers/WebSocketApiServer.js";
|
|
7
|
+
import { WebSocketHMRServer } from "./servers/WebSocketHMRServer.js";
|
|
8
|
+
import { WebSocketRouter } from "./WebSocketRouter.js";
|
|
9
|
+
import { WebSocketServerAdapter } from "./WebSocketServerAdapter.js";
|
|
10
|
+
/**
|
|
11
|
+
* Defined in @react-native/dev-middleware
|
|
12
|
+
* Reference: https://github.com/facebook/react-native/blob/main/packages/dev-middleware/src/inspector-proxy/InspectorProxy.js
|
|
13
|
+
*/
|
|
14
|
+
const WS_DEVICE_URL = '/inspector/device';
|
|
15
|
+
const WS_DEBUGGER_URL = '/inspector/debug';
|
|
16
|
+
async function wssPlugin(instance, {
|
|
17
|
+
options,
|
|
18
|
+
delegate
|
|
19
|
+
}) {
|
|
20
|
+
const router = new WebSocketRouter(instance);
|
|
21
|
+
const debuggerServer = new WebSocketDebuggerServer(instance);
|
|
22
|
+
const devClientServer = new WebSocketDevClientServer(instance);
|
|
23
|
+
const messageServer = new WebSocketMessageServer(instance);
|
|
24
|
+
const eventsServer = new WebSocketEventsServer(instance, {
|
|
25
|
+
webSocketMessageServer: messageServer
|
|
26
|
+
});
|
|
27
|
+
const apiServer = new WebSocketApiServer(instance);
|
|
28
|
+
const hmrServer = new WebSocketHMRServer(instance, delegate.hmr);
|
|
29
|
+
|
|
30
|
+
// @react-native/dev-middleware servers
|
|
31
|
+
const deviceConnectionServer = new WebSocketServerAdapter(instance, WS_DEVICE_URL, options.endpoints?.[WS_DEVICE_URL]);
|
|
32
|
+
const debuggerConnectionServer = new WebSocketServerAdapter(instance, WS_DEBUGGER_URL, options.endpoints?.[WS_DEBUGGER_URL]);
|
|
33
|
+
router.registerServer(debuggerServer);
|
|
34
|
+
router.registerServer(devClientServer);
|
|
35
|
+
router.registerServer(messageServer);
|
|
36
|
+
router.registerServer(eventsServer);
|
|
37
|
+
router.registerServer(apiServer);
|
|
38
|
+
router.registerServer(hmrServer);
|
|
39
|
+
router.registerServer(deviceConnectionServer);
|
|
40
|
+
router.registerServer(debuggerConnectionServer);
|
|
41
|
+
instance.decorate('wss', {
|
|
42
|
+
debuggerServer,
|
|
43
|
+
devClientServer,
|
|
44
|
+
messageServer,
|
|
45
|
+
eventsServer,
|
|
46
|
+
apiServer,
|
|
47
|
+
hmrServer,
|
|
48
|
+
deviceConnectionServer,
|
|
49
|
+
debuggerConnectionServer,
|
|
50
|
+
router
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
export default fastifyPlugin(wssPlugin, {
|
|
54
|
+
name: 'wss-plugin'
|
|
55
|
+
});
|
|
56
|
+
//# sourceMappingURL=wssPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wssPlugin.js","names":["fastifyPlugin","WebSocketDebuggerServer","WebSocketDevClientServer","WebSocketMessageServer","WebSocketEventsServer","WebSocketApiServer","WebSocketHMRServer","WebSocketRouter","WebSocketServerAdapter","WS_DEVICE_URL","WS_DEBUGGER_URL","wssPlugin","instance","options","delegate","router","debuggerServer","devClientServer","messageServer","eventsServer","webSocketMessageServer","apiServer","hmrServer","hmr","deviceConnectionServer","endpoints","debuggerConnectionServer","registerServer","decorate","name"],"sources":["../../../src/plugins/wss/wssPlugin.ts"],"sourcesContent":["import type { FastifyInstance } from 'fastify';\nimport fastifyPlugin from 'fastify-plugin';\nimport type { Server } from '../../types';\nimport { WebSocketDebuggerServer } from './servers/WebSocketDebuggerServer';\nimport { WebSocketDevClientServer } from './servers/WebSocketDevClientServer';\nimport { WebSocketMessageServer } from './servers/WebSocketMessageServer';\nimport { WebSocketEventsServer } from './servers/WebSocketEventsServer';\nimport { WebSocketApiServer } from './servers/WebSocketApiServer';\nimport { WebSocketHMRServer } from './servers/WebSocketHMRServer';\nimport { WebSocketRouter } from './WebSocketRouter';\nimport { WebSocketServerAdapter } from './WebSocketServerAdapter';\n\ndeclare module 'fastify' {\n interface FastifyInstance {\n wss: {\n debuggerServer: WebSocketDebuggerServer;\n devClientServer: WebSocketDevClientServer;\n messageServer: WebSocketMessageServer;\n eventsServer: WebSocketEventsServer;\n apiServer: WebSocketApiServer;\n hmrServer: WebSocketHMRServer;\n deviceConnectionServer: WebSocketServerAdapter;\n debuggerConnectionServer: WebSocketServerAdapter;\n router: WebSocketRouter;\n };\n }\n}\n\n/**\n * Defined in @react-native/dev-middleware\n * Reference: https://github.com/facebook/react-native/blob/main/packages/dev-middleware/src/inspector-proxy/InspectorProxy.js\n */\nconst WS_DEVICE_URL = '/inspector/device';\nconst WS_DEBUGGER_URL = '/inspector/debug';\n\nasync function wssPlugin(\n instance: FastifyInstance,\n {\n options,\n delegate,\n }: {\n options: Server.Options;\n delegate: Server.Delegate;\n }\n) {\n const router = new WebSocketRouter(instance);\n\n const debuggerServer = new WebSocketDebuggerServer(instance);\n const devClientServer = new WebSocketDevClientServer(instance);\n const messageServer = new WebSocketMessageServer(instance);\n const eventsServer = new WebSocketEventsServer(instance, {\n webSocketMessageServer: messageServer,\n });\n const apiServer = new WebSocketApiServer(instance);\n const hmrServer = new WebSocketHMRServer(instance, delegate.hmr);\n\n // @react-native/dev-middleware servers\n const deviceConnectionServer = new WebSocketServerAdapter(\n instance,\n WS_DEVICE_URL,\n options.endpoints?.[WS_DEVICE_URL]\n );\n\n const debuggerConnectionServer = new WebSocketServerAdapter(\n instance,\n WS_DEBUGGER_URL,\n options.endpoints?.[WS_DEBUGGER_URL]\n );\n\n router.registerServer(debuggerServer);\n router.registerServer(devClientServer);\n router.registerServer(messageServer);\n router.registerServer(eventsServer);\n router.registerServer(apiServer);\n router.registerServer(hmrServer);\n router.registerServer(deviceConnectionServer);\n router.registerServer(debuggerConnectionServer);\n\n instance.decorate('wss', {\n debuggerServer,\n devClientServer,\n messageServer,\n eventsServer,\n apiServer,\n hmrServer,\n deviceConnectionServer,\n debuggerConnectionServer,\n router,\n });\n}\n\nexport default fastifyPlugin(wssPlugin, {\n name: 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