@callstack/repack-dev-server 1.0.0-next.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 (86) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +32 -0
  3. package/dist/createServer.d.ts +13 -0
  4. package/dist/createServer.js +111 -0
  5. package/dist/createServer.js.map +1 -0
  6. package/dist/index.d.ts +2 -0
  7. package/dist/index.js +3 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/plugins/compiler/compilerPlugin.d.ts +6 -0
  10. package/dist/plugins/compiler/compilerPlugin.js +85 -0
  11. package/dist/plugins/compiler/compilerPlugin.js.map +1 -0
  12. package/dist/plugins/compiler/index.d.ts +2 -0
  13. package/dist/plugins/compiler/index.js +3 -0
  14. package/dist/plugins/compiler/index.js.map +1 -0
  15. package/dist/plugins/compiler/types.d.ts +31 -0
  16. package/dist/plugins/compiler/types.js +2 -0
  17. package/dist/plugins/compiler/types.js.map +1 -0
  18. package/dist/plugins/devtools/devtoolsPlugin.d.ts +6 -0
  19. package/dist/plugins/devtools/devtoolsPlugin.js +109 -0
  20. package/dist/plugins/devtools/devtoolsPlugin.js.map +1 -0
  21. package/dist/plugins/devtools/index.d.ts +1 -0
  22. package/dist/plugins/devtools/index.js +2 -0
  23. package/dist/plugins/devtools/index.js.map +1 -0
  24. package/dist/plugins/multipart/index.d.ts +2 -0
  25. package/dist/plugins/multipart/index.js +3 -0
  26. package/dist/plugins/multipart/index.js.map +1 -0
  27. package/dist/plugins/multipart/multipartPlugin.d.ts +3 -0
  28. package/dist/plugins/multipart/multipartPlugin.js +68 -0
  29. package/dist/plugins/multipart/multipartPlugin.js.map +1 -0
  30. package/dist/plugins/multipart/types.d.ts +12 -0
  31. package/dist/plugins/multipart/types.js +2 -0
  32. package/dist/plugins/multipart/types.js.map +1 -0
  33. package/dist/plugins/symbolicate/Symbolicator.d.ts +46 -0
  34. package/dist/plugins/symbolicate/Symbolicator.js +227 -0
  35. package/dist/plugins/symbolicate/Symbolicator.js.map +1 -0
  36. package/dist/plugins/symbolicate/index.d.ts +3 -0
  37. package/dist/plugins/symbolicate/index.js +4 -0
  38. package/dist/plugins/symbolicate/index.js.map +1 -0
  39. package/dist/plugins/symbolicate/sybmolicatePlugin.d.ts +6 -0
  40. package/dist/plugins/symbolicate/sybmolicatePlugin.js +46 -0
  41. package/dist/plugins/symbolicate/sybmolicatePlugin.js.map +1 -0
  42. package/dist/plugins/symbolicate/types.d.ts +65 -0
  43. package/dist/plugins/symbolicate/types.js +2 -0
  44. package/dist/plugins/symbolicate/types.js.map +1 -0
  45. package/dist/plugins/wss/WebSocketRouter.d.ts +32 -0
  46. package/dist/plugins/wss/WebSocketRouter.js +61 -0
  47. package/dist/plugins/wss/WebSocketRouter.js.map +1 -0
  48. package/dist/plugins/wss/WebSocketServer.d.ts +37 -0
  49. package/dist/plugins/wss/WebSocketServer.js +49 -0
  50. package/dist/plugins/wss/WebSocketServer.js.map +1 -0
  51. package/dist/plugins/wss/index.d.ts +3 -0
  52. package/dist/plugins/wss/index.js +4 -0
  53. package/dist/plugins/wss/index.js.map +1 -0
  54. package/dist/plugins/wss/servers/HermesInspectorProxy.d.ts +24 -0
  55. package/dist/plugins/wss/servers/HermesInspectorProxy.js +138 -0
  56. package/dist/plugins/wss/servers/HermesInspectorProxy.js.map +1 -0
  57. package/dist/plugins/wss/servers/WebSocketDashboardServer.d.ts +34 -0
  58. package/dist/plugins/wss/servers/WebSocketDashboardServer.js +70 -0
  59. package/dist/plugins/wss/servers/WebSocketDashboardServer.js.map +1 -0
  60. package/dist/plugins/wss/servers/WebSocketDebuggerServer.d.ts +64 -0
  61. package/dist/plugins/wss/servers/WebSocketDebuggerServer.js +156 -0
  62. package/dist/plugins/wss/servers/WebSocketDebuggerServer.js.map +1 -0
  63. package/dist/plugins/wss/servers/WebSocketDevClientServer.d.ts +32 -0
  64. package/dist/plugins/wss/servers/WebSocketDevClientServer.js +95 -0
  65. package/dist/plugins/wss/servers/WebSocketDevClientServer.js.map +1 -0
  66. package/dist/plugins/wss/servers/WebSocketEventsServer.d.ts +75 -0
  67. package/dist/plugins/wss/servers/WebSocketEventsServer.js +187 -0
  68. package/dist/plugins/wss/servers/WebSocketEventsServer.js.map +1 -0
  69. package/dist/plugins/wss/servers/WebSocketHMRServer.d.ts +38 -0
  70. package/dist/plugins/wss/servers/WebSocketHMRServer.js +103 -0
  71. package/dist/plugins/wss/servers/WebSocketHMRServer.js.map +1 -0
  72. package/dist/plugins/wss/servers/WebSocketMessageServer.d.ts +140 -0
  73. package/dist/plugins/wss/servers/WebSocketMessageServer.js +380 -0
  74. package/dist/plugins/wss/servers/WebSocketMessageServer.js.map +1 -0
  75. package/dist/plugins/wss/types.d.ts +16 -0
  76. package/dist/plugins/wss/types.js +2 -0
  77. package/dist/plugins/wss/types.js.map +1 -0
  78. package/dist/plugins/wss/wssPlugin.d.ts +29 -0
  79. package/dist/plugins/wss/wssPlugin.js +47 -0
  80. package/dist/plugins/wss/wssPlugin.js.map +1 -0
  81. package/dist/types.d.ts +127 -0
  82. package/dist/types.js +24 -0
  83. package/dist/types.js.map +1 -0
  84. package/package.json +75 -0
  85. package/vendor/metro-inspector-proxy/src/Device.cjs +1 -0
  86. package/vendor/metro-inspector-proxy/src/Device.js +1 -0
@@ -0,0 +1,380 @@
1
+ import { URL } from 'url';
2
+ import { WebSocketServer } from "../WebSocketServer.js";
3
+ /**
4
+ * Holds {@link ReactNativeMessage} `id` data.
5
+ */
6
+
7
+ /**
8
+ * Class for creating a WebSocket server and sending messages between development server
9
+ * and the React Native applications.
10
+ *
11
+ * Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/messageSocketServer.ts
12
+ *
13
+ * @category Development server
14
+ */
15
+ export class WebSocketMessageServer extends WebSocketServer {
16
+ static PROTOCOL_VERSION = 2;
17
+ /**
18
+ * Check if message is a broadcast request.
19
+ *
20
+ * @param message Message to check.
21
+ * @returns True if message is a broadcast request and should be broadcasted
22
+ * with {@link sendBroadcast}.
23
+ */
24
+
25
+ static isBroadcast(message) {
26
+ return typeof message.method === 'string' && message.id === undefined && message.target === undefined;
27
+ }
28
+ /**
29
+ * Check if message is a method request.
30
+ *
31
+ * @param message Message to check.
32
+ * @returns True if message is a request.
33
+ */
34
+
35
+
36
+ static isRequest(message) {
37
+ return typeof message.method === 'string' && typeof message.target === 'string';
38
+ }
39
+ /**
40
+ * Check if message is a response with results of performing some request.
41
+ *
42
+ * @param message Message to check.
43
+ * @returns True if message is a response.
44
+ */
45
+
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
+
51
+ clients = new Map();
52
+ nextClientId = 0;
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
+
60
+ constructor(fastify) {
61
+ super(fastify, '/message');
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
+
71
+
72
+ parseMessage(data, binary) {
73
+ if (binary) {
74
+ this.fastify.log.error({
75
+ msg: 'Failed to parse message - expected text message, got binary'
76
+ });
77
+ return undefined;
78
+ }
79
+
80
+ try {
81
+ const message = JSON.parse(data);
82
+
83
+ if (message.version === WebSocketMessageServer.PROTOCOL_VERSION.toString()) {
84
+ return message;
85
+ }
86
+
87
+ this.fastify.log.error({
88
+ msg: 'Received message had wrong protocol version',
89
+ message
90
+ });
91
+ } catch (e) {
92
+ this.fastify.log.error({
93
+ msg: 'Failed to parse the message as JSON',
94
+ data
95
+ });
96
+ }
97
+
98
+ return undefined;
99
+ }
100
+ /**
101
+ * Get client's WebSocket connection for given `clientId`.
102
+ * Throws if no such client is connected.
103
+ *
104
+ * @param clientId Id of the client.
105
+ * @returns WebSocket connection.
106
+ */
107
+
108
+
109
+ getClientSocket(clientId) {
110
+ const socket = this.clients.get(clientId);
111
+
112
+ if (socket === undefined) {
113
+ throw new Error(`Could not find client with id "${clientId}"`);
114
+ }
115
+
116
+ return socket;
117
+ }
118
+ /**
119
+ * Process error by sending an error message to the client whose message caused the error
120
+ * to occur.
121
+ *
122
+ * @param clientId Id of the client whose message caused an error.
123
+ * @param message Original message which caused the error.
124
+ * @param error Concrete instance of an error that occurred.
125
+ */
126
+
127
+
128
+ handleError(clientId, message, error) {
129
+ const errorMessage = {
130
+ id: message.id,
131
+ method: message.method,
132
+ target: message.target,
133
+ error: message.error === undefined ? 'undefined' : 'defined',
134
+ params: message.params === undefined ? 'undefined' : 'defined',
135
+ result: message.result === undefined ? 'undefined' : 'defined'
136
+ };
137
+
138
+ if (message.id === undefined) {
139
+ this.fastify.log.error({
140
+ msg: 'Handling message failed',
141
+ clientId,
142
+ error,
143
+ errorMessage
144
+ });
145
+ } else {
146
+ try {
147
+ const socket = this.getClientSocket(clientId);
148
+ socket.send(JSON.stringify({
149
+ version: WebSocketMessageServer.PROTOCOL_VERSION,
150
+ error,
151
+ id: message.id
152
+ }));
153
+ } catch (error) {
154
+ this.fastify.log.error('Failed to reply', {
155
+ clientId,
156
+ error,
157
+ errorMessage
158
+ });
159
+ }
160
+ }
161
+ }
162
+ /**
163
+ * Send given request `message` to it's designated client's socket based on `message.target`.
164
+ * The target client must be connected, otherwise it will throw an error.
165
+ *
166
+ * @param clientId Id of the client that requested the forward.
167
+ * @param message Message to forward.
168
+ */
169
+
170
+
171
+ forwardRequest(clientId, message) {
172
+ if (!message.target) {
173
+ this.fastify.log.error({
174
+ msg: 'Failed to forward request - message.target is missing',
175
+ clientId,
176
+ message
177
+ });
178
+ return;
179
+ }
180
+
181
+ const socket = this.getClientSocket(message.target);
182
+ socket.send(JSON.stringify({
183
+ version: WebSocketMessageServer.PROTOCOL_VERSION,
184
+ method: message.method,
185
+ params: message.params,
186
+ id: message.id === undefined ? undefined : {
187
+ requestId: message.id,
188
+ clientId
189
+ }
190
+ }));
191
+ }
192
+ /**
193
+ * Send given response `message` to it's designated client's socket based
194
+ * on `message.id.clientId`.
195
+ * The target client must be connected, otherwise it will throw an error.
196
+ *
197
+ * @param message Message to forward.
198
+ */
199
+
200
+
201
+ forwardResponse(message) {
202
+ if (!message.id) {
203
+ return;
204
+ }
205
+
206
+ const socket = this.getClientSocket(message.id.clientId);
207
+ socket.send(JSON.stringify({
208
+ version: WebSocketMessageServer.PROTOCOL_VERSION,
209
+ result: message.result,
210
+ error: message.error,
211
+ id: message.id.requestId
212
+ }));
213
+ }
214
+ /**
215
+ * Process request message targeted towards this {@link WebSocketMessageServer}
216
+ * and send back the results.
217
+ *
218
+ * @param clientId Id of the client who send the message.
219
+ * @param message The message to process by the server.
220
+ */
221
+
222
+
223
+ processServerRequest(clientId, message) {
224
+ let result;
225
+
226
+ switch (message.method) {
227
+ case 'getid':
228
+ result = clientId;
229
+ break;
230
+
231
+ case 'getpeers':
232
+ {
233
+ const output = {};
234
+ this.clients.forEach((peerSocket, peerId) => {
235
+ if (clientId !== peerId) {
236
+ var _peerSocket$upgradeRe;
237
+
238
+ const {
239
+ searchParams
240
+ } = new URL(((_peerSocket$upgradeRe = peerSocket.upgradeReq) === null || _peerSocket$upgradeRe === void 0 ? void 0 : _peerSocket$upgradeRe.url) || '');
241
+ output[peerId] = [...searchParams.entries()].reduce((acc, [key, value]) => ({ ...acc,
242
+ [key]: value
243
+ }), {});
244
+ }
245
+ });
246
+ result = output;
247
+ break;
248
+ }
249
+
250
+ default:
251
+ throw new Error(`Cannot process server request - unknown method ${JSON.stringify({
252
+ clientId,
253
+ message
254
+ })}`);
255
+ }
256
+
257
+ const socket = this.getClientSocket(clientId);
258
+ socket.send(JSON.stringify({
259
+ version: WebSocketMessageServer.PROTOCOL_VERSION,
260
+ result,
261
+ id: message.id
262
+ }));
263
+ }
264
+ /**
265
+ * Broadcast given message to all connected clients.
266
+ *
267
+ * @param broadcasterId Id of the client who is broadcasting.
268
+ * @param message Message to broadcast.
269
+ */
270
+
271
+
272
+ sendBroadcast(broadcasterId, message) {
273
+ const forwarded = {
274
+ version: WebSocketMessageServer.PROTOCOL_VERSION,
275
+ method: message.method,
276
+ params: message.params
277
+ };
278
+
279
+ if (this.clients.size === 0) {
280
+ this.fastify.log.warn({
281
+ 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.'
282
+ });
283
+ }
284
+
285
+ for (const [clientId, socket] of this.clients) {
286
+ if (clientId !== broadcasterId) {
287
+ try {
288
+ socket.send(JSON.stringify(forwarded));
289
+ } catch (error) {
290
+ this.fastify.log.error({
291
+ msg: 'Failed to send broadcast',
292
+ clientId,
293
+ error,
294
+ forwarded
295
+ });
296
+ }
297
+ }
298
+ }
299
+ }
300
+ /**
301
+ * Send method broadcast to all connected clients.
302
+ *
303
+ * @param method Method name to broadcast.
304
+ * @param params Method parameters.
305
+ */
306
+
307
+
308
+ broadcast(method, params) {
309
+ this.sendBroadcast(undefined, {
310
+ method,
311
+ params
312
+ });
313
+ }
314
+ /**
315
+ * Process new client's WebSocket connection.
316
+ *
317
+ * @param socket Incoming WebSocket connection.
318
+ * @param request Upgrade request for the connection.
319
+ */
320
+
321
+
322
+ onConnection(socket, request) {
323
+ const clientId = `client#${this.nextClientId++}`;
324
+ let client = socket;
325
+ client.upgradeReq = request;
326
+ this.clients.set(clientId, client);
327
+ this.fastify.log.debug({
328
+ msg: 'Message client connected',
329
+ clientId
330
+ });
331
+
332
+ const onClose = () => {
333
+ this.fastify.log.debug({
334
+ msg: 'Message client disconnected',
335
+ clientId
336
+ });
337
+ socket.removeAllListeners();
338
+ this.clients.delete(clientId);
339
+ };
340
+
341
+ socket.addEventListener('error', onClose);
342
+ socket.addEventListener('close', onClose);
343
+ socket.addEventListener('message', event => {
344
+ const message = this.parseMessage(event.data.toString(), // @ts-ignore
345
+ event.binary);
346
+
347
+ if (!message) {
348
+ this.fastify.log.error({
349
+ msg: 'Received message not matching protocol',
350
+ clientId,
351
+ message
352
+ });
353
+ return;
354
+ }
355
+
356
+ try {
357
+ if (WebSocketMessageServer.isBroadcast(message)) {
358
+ this.sendBroadcast(clientId, message);
359
+ } else if (WebSocketMessageServer.isRequest(message)) {
360
+ if (message.target === 'server') {
361
+ this.processServerRequest(clientId, message);
362
+ } else {
363
+ this.forwardRequest(clientId, message);
364
+ }
365
+ } else if (WebSocketMessageServer.isResponse(message)) {
366
+ this.forwardResponse(message);
367
+ } else {
368
+ throw new Error(`Invalid message, did not match the protocol ${JSON.stringify({
369
+ clientId,
370
+ message
371
+ })}`);
372
+ }
373
+ } catch (error) {
374
+ this.handleError(clientId, message, error);
375
+ }
376
+ });
377
+ }
378
+
379
+ }
380
+ //# 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,GAAT,QAAoB,KAApB;SAGSC,e;AAET;AACA;AACA;;AAqBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,sBAAN,SAAqCD,eAArC,CAAqD;EAC1B,OAAhBE,gBAAgB,GAAG,CAAH;EAEhC;AACF;AACA;AACA;AACA;AACA;AACA;;EACoB,OAAXC,WAAW,CAACC,OAAD,EAAuC;IACvD,OACE,OAAOA,OAAO,CAACC,MAAf,KAA0B,QAA1B,IACAD,OAAO,CAACE,EAAR,KAAeC,SADf,IAEAH,OAAO,CAACI,MAAR,KAAmBD,SAHrB;EAKD;EAED;AACF;AACA;AACA;AACA;AACA;;;EACkB,OAATE,SAAS,CAACL,OAAD,EAAuC;IACrD,OACE,OAAOA,OAAO,CAACC,MAAf,KAA0B,QAA1B,IAAsC,OAAOD,OAAO,CAACI,MAAf,KAA0B,QADlE;EAGD;EAED;AACF;AACA;AACA;AACA;AACA;;;EACmB,OAAVE,UAAU,CAACN,OAAD,EAAuC;IACtD,OACE,OAAOA,OAAO,CAACE,EAAf,KAAsB,QAAtB,IACA,OAAOF,OAAO,CAACE,EAAR,CAAWK,SAAlB,KAAgC,WADhC,IAEA,OAAOP,OAAO,CAACE,EAAR,CAAWM,QAAlB,KAA+B,QAF/B,KAGCR,OAAO,CAACS,MAAR,KAAmBN,SAAnB,IAAgCH,OAAO,CAACU,KAAR,KAAkBP,SAHnD,CADF;EAMD;;EAEOQ,OAAO,GAAG,IAAIC,GAAJ,EAAH;EACPC,YAAY,GAAG,CAAH;EAEpB;AACF;AACA;AACA;AACA;AACA;;EACEC,WAAW,CAACC,OAAD,EAA2B;IACpC,MAAMA,OAAN,EAAe,UAAf;EACD;EAED;AACF;AACA;AACA;AACA;AACA;AACA;;;EACEC,YAAY,CACVC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,KAAK,CAAChD,IAAN,CAAWO,QAAX,EADc,EAEd;MACAyC,KAAK,CAAC/C,MAHQ,CAAhB;;MAMA,IAAI,CAAClB,OAAL,EAAc;QACZ,KAAKe,OAAL,CAAaI,GAAb,CAAiBT,KAAjB,CAAuB;UACrBU,GAAG,EAAE,wCADgB;UAErBZ,QAFqB;UAGrBR;QAHqB,CAAvB;QAKA;MACD;;MAED,IAAI;QACF,IAAIH,sBAAsB,CAACE,WAAvB,CAAmCC,OAAnC,CAAJ,EAAiD;UAC/C,KAAKkD,aAAL,CAAmB1C,QAAnB,EAA6BR,OAA7B;QACD,CAFD,MAEO,IAAIH,sBAAsB,CAACQ,SAAvB,CAAiCL,OAAjC,CAAJ,EAA+C;UACpD,IAAIA,OAAO,CAACI,MAAR,KAAmB,QAAvB,EAAiC;YAC/B,KAAKiC,oBAAL,CAA0B7B,QAA1B,EAAoCR,OAApC;UACD,CAFD,MAEO;YACL,KAAKmC,cAAL,CAAoB3B,QAApB,EAA8BR,OAA9B;UACD;QACF,CANM,MAMA,IAAIH,sBAAsB,CAACS,UAAvB,CAAkCN,OAAlC,CAAJ,EAAgD;UACrD,KAAKoC,eAAL,CAAqBpC,OAArB;QACD,CAFM,MAEA;UACL,MAAM,IAAI6B,KAAJ,CACH,+CAA8CR,IAAI,CAACa,SAAL,CAAe;YAC5D1B,QAD4D;YAE5DR;UAF4D,CAAf,CAG5C,EAJC,CAAN;QAMD;MACF,CAnBD,CAmBE,OAAOU,KAAP,EAAc;QACd,KAAKoB,WAAL,CAAiBtB,QAAjB,EAA2BR,OAA3B,EAAoCU,KAApC;MACD;IACF,CAtCD;EAuCD;;AAtXyD"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Delegate with implementation for HMR-specific functions.
3
+ */
4
+ export interface HmrDelegate {
5
+ /** Get URI under which HMR server will be running, e.g: `/hmr` */
6
+ getUriPath: () => string;
7
+ /**
8
+ * Callback for when the new HMR client is connected.
9
+ *
10
+ * Useful for running initial synchronization or any other side effect.
11
+ *
12
+ * @param platform Platform of the connected client.
13
+ * @param clientId Id of the connected client.
14
+ */
15
+ onClientConnected: (platform: string, clientId: string) => void;
16
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","names":[],"sources":["../../../src/plugins/wss/types.ts"],"sourcesContent":["/**\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"],"mappings":""}
@@ -0,0 +1,29 @@
1
+ /// <reference types="node" />
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 { HermesInspectorProxy } from './servers/HermesInspectorProxy';
8
+ import { WebSocketDashboardServer } from './servers/WebSocketDashboardServer';
9
+ import { WebSocketHMRServer } from './servers/WebSocketHMRServer';
10
+ import { WebSocketRouter } from './WebSocketRouter';
11
+ declare module 'fastify' {
12
+ interface FastifyInstance {
13
+ wss: {
14
+ debuggerServer: WebSocketDebuggerServer;
15
+ devClientServer: WebSocketDevClientServer;
16
+ messageServer: WebSocketMessageServer;
17
+ eventsServer: WebSocketEventsServer;
18
+ hermesInspectorProxy: HermesInspectorProxy;
19
+ dashboardServer: WebSocketDashboardServer;
20
+ hmrServer: WebSocketHMRServer;
21
+ router: WebSocketRouter;
22
+ };
23
+ }
24
+ }
25
+ declare const _default: import("fastify").FastifyPluginAsync<{
26
+ options: Server.Options;
27
+ delegate: Server.Delegate;
28
+ }, import("http").Server>;
29
+ export default _default;
@@ -0,0 +1,47 @@
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 { HermesInspectorProxy } from "./servers/HermesInspectorProxy.js";
7
+ import { WebSocketDashboardServer } from "./servers/WebSocketDashboardServer.js";
8
+ import { WebSocketHMRServer } from "./servers/WebSocketHMRServer.js";
9
+ import { WebSocketRouter } from "./WebSocketRouter.js";
10
+
11
+ async function wssPlugin(instance, {
12
+ options,
13
+ delegate
14
+ }) {
15
+ const router = new WebSocketRouter(instance);
16
+ const debuggerServer = new WebSocketDebuggerServer(instance);
17
+ const devClientServer = new WebSocketDevClientServer(instance);
18
+ const messageServer = new WebSocketMessageServer(instance);
19
+ const eventsServer = new WebSocketEventsServer(instance, {
20
+ webSocketMessageServer: messageServer
21
+ });
22
+ const hermesInspectorProxy = new HermesInspectorProxy(instance, options);
23
+ const dashboardServer = new WebSocketDashboardServer(instance);
24
+ const hmrServer = new WebSocketHMRServer(instance, delegate.hmr);
25
+ router.registerServer(debuggerServer);
26
+ router.registerServer(devClientServer);
27
+ router.registerServer(messageServer);
28
+ router.registerServer(eventsServer);
29
+ router.registerServer(hermesInspectorProxy);
30
+ router.registerServer(dashboardServer);
31
+ router.registerServer(hmrServer);
32
+ instance.decorate('wss', {
33
+ debuggerServer,
34
+ devClientServer,
35
+ messageServer,
36
+ eventsServer,
37
+ hermesInspectorProxy,
38
+ dashboardServer,
39
+ hmrServer,
40
+ router
41
+ });
42
+ }
43
+
44
+ export default fastifyPlugin(wssPlugin, {
45
+ name: 'wss-plugin'
46
+ });
47
+ //# sourceMappingURL=wssPlugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wssPlugin.js","names":["fastifyPlugin","WebSocketDebuggerServer","WebSocketDevClientServer","WebSocketMessageServer","WebSocketEventsServer","HermesInspectorProxy","WebSocketDashboardServer","WebSocketHMRServer","WebSocketRouter","wssPlugin","instance","options","delegate","router","debuggerServer","devClientServer","messageServer","eventsServer","webSocketMessageServer","hermesInspectorProxy","dashboardServer","hmrServer","hmr","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 { HermesInspectorProxy } from './servers/HermesInspectorProxy';\nimport { WebSocketDashboardServer } from './servers/WebSocketDashboardServer';\nimport { WebSocketHMRServer } from './servers/WebSocketHMRServer';\nimport { WebSocketRouter } from './WebSocketRouter';\n\ndeclare module 'fastify' {\n interface FastifyInstance {\n wss: {\n debuggerServer: WebSocketDebuggerServer;\n devClientServer: WebSocketDevClientServer;\n messageServer: WebSocketMessageServer;\n eventsServer: WebSocketEventsServer;\n hermesInspectorProxy: HermesInspectorProxy;\n dashboardServer: WebSocketDashboardServer;\n hmrServer: WebSocketHMRServer;\n router: WebSocketRouter;\n };\n }\n}\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 hermesInspectorProxy = new HermesInspectorProxy(instance, options);\n const dashboardServer = new WebSocketDashboardServer(instance);\n const hmrServer = new WebSocketHMRServer(instance, delegate.hmr);\n\n router.registerServer(debuggerServer);\n router.registerServer(devClientServer);\n router.registerServer(messageServer);\n router.registerServer(eventsServer);\n router.registerServer(hermesInspectorProxy);\n router.registerServer(dashboardServer);\n router.registerServer(hmrServer);\n\n instance.decorate('wss', {\n debuggerServer,\n devClientServer,\n messageServer,\n eventsServer,\n hermesInspectorProxy,\n dashboardServer,\n hmrServer,\n router,\n });\n}\n\nexport default fastifyPlugin(wssPlugin, {\n name: 'wss-plugin',\n});\n"],"mappings":"AACA,OAAOA,aAAP,MAA0B,gBAA1B;SAESC,uB;SACAC,wB;SACAC,sB;SACAC,qB;SACAC,oB;SACAC,wB;SACAC,kB;SACAC,e;;AAiBT,eAAeC,SAAf,CACEC,QADF,EAEE;EACEC,OADF;EAEEC;AAFF,CAFF,EASE;EACA,MAAMC,MAAM,GAAG,IAAIL,eAAJ,CAAoBE,QAApB,CAAf;EAEA,MAAMI,cAAc,GAAG,IAAIb,uBAAJ,CAA4BS,QAA5B,CAAvB;EACA,MAAMK,eAAe,GAAG,IAAIb,wBAAJ,CAA6BQ,QAA7B,CAAxB;EACA,MAAMM,aAAa,GAAG,IAAIb,sBAAJ,CAA2BO,QAA3B,CAAtB;EACA,MAAMO,YAAY,GAAG,IAAIb,qBAAJ,CAA0BM,QAA1B,EAAoC;IACvDQ,sBAAsB,EAAEF;EAD+B,CAApC,CAArB;EAGA,MAAMG,oBAAoB,GAAG,IAAId,oBAAJ,CAAyBK,QAAzB,EAAmCC,OAAnC,CAA7B;EACA,MAAMS,eAAe,GAAG,IAAId,wBAAJ,CAA6BI,QAA7B,CAAxB;EACA,MAAMW,SAAS,GAAG,IAAId,kBAAJ,CAAuBG,QAAvB,EAAiCE,QAAQ,CAACU,GAA1C,CAAlB;EAEAT,MAAM,CAACU,cAAP,CAAsBT,cAAtB;EACAD,MAAM,CAACU,cAAP,CAAsBR,eAAtB;EACAF,MAAM,CAACU,cAAP,CAAsBP,aAAtB;EACAH,MAAM,CAACU,cAAP,CAAsBN,YAAtB;EACAJ,MAAM,CAACU,cAAP,CAAsBJ,oBAAtB;EACAN,MAAM,CAACU,cAAP,CAAsBH,eAAtB;EACAP,MAAM,CAACU,cAAP,CAAsBF,SAAtB;EAEAX,QAAQ,CAACc,QAAT,CAAkB,KAAlB,EAAyB;IACvBV,cADuB;IAEvBC,eAFuB;IAGvBC,aAHuB;IAIvBC,YAJuB;IAKvBE,oBALuB;IAMvBC,eANuB;IAOvBC,SAPuB;IAQvBR;EARuB,CAAzB;AAUD;;AAED,eAAeb,aAAa,CAACS,SAAD,EAAY;EACtCgB,IAAI,EAAE;AADgC,CAAZ,CAA5B"}
@@ -0,0 +1,127 @@
1
+ import { FastifyLoggerInstance } from 'fastify';
2
+ import type { CompilerDelegate } from './plugins/compiler';
3
+ import type { SymbolicatorDelegate } from './plugins/symbolicate';
4
+ import type { HmrDelegate } from './plugins/wss';
5
+ export type { CompilerDelegate } from './plugins/compiler';
6
+ export type { SymbolicatorDelegate, ReactNativeStackFrame, InputStackFrame, StackFrame, CodeFrame, SymbolicatorResults, } from './plugins/symbolicate';
7
+ export type { HmrDelegate } from './plugins/wss';
8
+ export declare namespace Server {
9
+ /** Development server configuration. */
10
+ interface Config {
11
+ /** Development server options to configure e.g: `port`, `host` etc. */
12
+ options: Options;
13
+ /** Function to create a delegate, which implements crucial functionalities. */
14
+ delegate: (context: DelegateContext) => Delegate;
15
+ }
16
+ /** Development server options. */
17
+ interface Options {
18
+ /** Root directory of the project. */
19
+ rootDir: string;
20
+ /** Port under which to run the development server. */
21
+ port: number;
22
+ /**
23
+ * Hostname or IP address under which to run the development server.
24
+ * When left unspecified, it will listen on all available network interfaces, similarly to listening on '0.0.0.0'.
25
+ */
26
+ host?: string;
27
+ /** Options for running the server as HTTPS. If `undefined`, the server will run as HTTP. */
28
+ https?: {
29
+ /** Path to certificate when running server as HTTPS. */
30
+ cert?: string;
31
+ /** Path to certificate key when running server as HTTPS. */
32
+ key?: string;
33
+ };
34
+ }
35
+ /**
36
+ * A complete delegate with implementations for all server functionalities.
37
+ */
38
+ interface Delegate {
39
+ /** A compiler delegate. */
40
+ compiler: CompilerDelegate;
41
+ /** A symbolicator delegate. */
42
+ symbolicator: SymbolicatorDelegate;
43
+ /** A logger delegate. */
44
+ logger: LoggerDelegate;
45
+ /** An HMR delegate. */
46
+ hmr: HmrDelegate;
47
+ /** An messages delegate. */
48
+ messages: MessagesDelegate;
49
+ }
50
+ /**
51
+ * A delegate context used in `delegate` builder in {@link Config}.
52
+ *
53
+ * Allows to emit logs, notify about compilation events and broadcast events to connected clients.
54
+ */
55
+ interface DelegateContext {
56
+ /** A logger instance, useful for emitting logs from the delegate. */
57
+ log: FastifyLoggerInstance;
58
+ /** Send notification about compilation start for given `platform`. */
59
+ notifyBuildStart: (platform: string) => void;
60
+ /** Send notification about compilation end for given `platform`. */
61
+ notifyBuildEnd: (platform: string) => void;
62
+ /**
63
+ * Broadcast arbitrary event to all connected HMR clients for given `platform`.
64
+ *
65
+ * @param event Arbitrary event to broadcast.
66
+ * @param platform Platform of the clients to which broadcast should be sent.
67
+ * @param clientIds Ids of the client to which broadcast should be sent.
68
+ * If `undefined` the broadcast will be sent to all connected clients for the given `platform`.
69
+ */
70
+ broadcastToHmrClients: <E = any>(event: E, platform: string, clientIds?: string[]) => void;
71
+ /**
72
+ * Broadcast arbitrary method-like event to all connected message clients.
73
+ *
74
+ * @param event Arbitrary method-like event to broadcast.
75
+ */
76
+ broadcastToMessageClients: <E extends {
77
+ method: string;
78
+ params?: Record<string, any>;
79
+ }>(event: E) => void;
80
+ }
81
+ /**
82
+ * Delegate with implementation for logging functions.
83
+ */
84
+ interface LoggerDelegate {
85
+ /**
86
+ * Callback for when a new log is emitted.
87
+ *
88
+ * @param log An object with log data.
89
+ */
90
+ onMessage: (log: any) => void;
91
+ }
92
+ /**
93
+ * Delegate with implementation for messages used in route handlers.
94
+ */
95
+ interface MessagesDelegate {
96
+ /** Get message to send as a reply for `GET /` route. */
97
+ getHello: () => string;
98
+ /** Get message to send as a reply for `GET /status` route. */
99
+ getStatus: () => string;
100
+ }
101
+ }
102
+ /** Representation of the compilation progress. */
103
+ export interface ProgressData {
104
+ /** Number of modules built. */
105
+ completed: number;
106
+ /** Total number of modules detect as part of compilation. */
107
+ total: number;
108
+ }
109
+ /**
110
+ * Type representing a function to send the progress.
111
+ *
112
+ * Used by {@link CompilerDelegate} in `getAsset` function to send the compilation
113
+ * progress to the client who requested the asset.
114
+ */
115
+ export declare type SendProgress = (data: ProgressData) => void;
116
+ /**
117
+ * Internal types. Do not use.
118
+ *
119
+ * @internal
120
+ */
121
+ export declare namespace Internal {
122
+ enum EventTypes {
123
+ BuildStart = "BuildStart",
124
+ BuildEnd = "BuildEnd",
125
+ HmrEvent = "HmrEvent"
126
+ }
127
+ }