@callstack/repack-dev-server 1.1.0 → 4.0.0-rc.1

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 (82) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/LICENSE +21 -0
  3. package/dist/createServer.d.ts +1 -1
  4. package/dist/createServer.js +15 -17
  5. package/dist/createServer.js.map +1 -1
  6. package/dist/plugins/api/apiPlugin.js +3 -9
  7. package/dist/plugins/api/apiPlugin.js.map +1 -1
  8. package/dist/plugins/api/index.js.map +1 -1
  9. package/dist/plugins/compiler/compilerPlugin.d.ts +4 -3
  10. package/dist/plugins/compiler/compilerPlugin.js +8 -18
  11. package/dist/plugins/compiler/compilerPlugin.js.map +1 -1
  12. package/dist/plugins/compiler/index.js.map +1 -1
  13. package/dist/plugins/devtools/devtoolsPlugin.d.ts +4 -3
  14. package/dist/plugins/devtools/devtoolsPlugin.js +6 -8
  15. package/dist/plugins/devtools/devtoolsPlugin.js.map +1 -1
  16. package/dist/plugins/devtools/index.js.map +1 -1
  17. package/dist/plugins/favicon/faviconPlugin.d.ts +3 -2
  18. package/dist/plugins/favicon/faviconPlugin.js +7 -5
  19. package/dist/plugins/favicon/faviconPlugin.js.map +1 -1
  20. package/dist/plugins/favicon/index.js.map +1 -1
  21. package/dist/plugins/multipart/index.js.map +1 -1
  22. package/dist/plugins/multipart/multipartPlugin.d.ts +3 -2
  23. package/dist/plugins/multipart/multipartPlugin.js +1 -14
  24. package/dist/plugins/multipart/multipartPlugin.js.map +1 -1
  25. package/dist/plugins/symbolicate/Symbolicator.js +13 -32
  26. package/dist/plugins/symbolicate/Symbolicator.js.map +1 -1
  27. package/dist/plugins/symbolicate/index.js.map +1 -1
  28. package/dist/plugins/symbolicate/sybmolicatePlugin.d.ts +4 -3
  29. package/dist/plugins/symbolicate/sybmolicatePlugin.js +2 -4
  30. package/dist/plugins/symbolicate/sybmolicatePlugin.js.map +1 -1
  31. package/dist/plugins/wss/WebSocketRouter.js +2 -6
  32. package/dist/plugins/wss/WebSocketRouter.js.map +1 -1
  33. package/dist/plugins/wss/WebSocketServer.js +2 -5
  34. package/dist/plugins/wss/WebSocketServer.js.map +1 -1
  35. package/dist/plugins/wss/index.js.map +1 -1
  36. package/dist/plugins/wss/servers/HermesInspectorProxy.js +1 -19
  37. package/dist/plugins/wss/servers/HermesInspectorProxy.js.map +1 -1
  38. package/dist/plugins/wss/servers/WebSocketApiServer.js +5 -11
  39. package/dist/plugins/wss/servers/WebSocketApiServer.js.map +1 -1
  40. package/dist/plugins/wss/servers/WebSocketDebuggerServer.js +6 -20
  41. package/dist/plugins/wss/servers/WebSocketDebuggerServer.js.map +1 -1
  42. package/dist/plugins/wss/servers/WebSocketDevClientServer.js +3 -12
  43. package/dist/plugins/wss/servers/WebSocketDevClientServer.js.map +1 -1
  44. package/dist/plugins/wss/servers/WebSocketEventsServer.js +18 -25
  45. package/dist/plugins/wss/servers/WebSocketEventsServer.js.map +1 -1
  46. package/dist/plugins/wss/servers/WebSocketHMRServer.js +3 -13
  47. package/dist/plugins/wss/servers/WebSocketHMRServer.js.map +1 -1
  48. package/dist/plugins/wss/servers/WebSocketMessageServer.d.ts +1 -1
  49. package/dist/plugins/wss/servers/WebSocketMessageServer.js +21 -51
  50. package/dist/plugins/wss/servers/WebSocketMessageServer.js.map +1 -1
  51. package/dist/plugins/wss/wssPlugin.d.ts +4 -3
  52. package/dist/plugins/wss/wssPlugin.js +0 -2
  53. package/dist/plugins/wss/wssPlugin.js.map +1 -1
  54. package/dist/types.d.ts +1 -1
  55. package/dist/types.js +9 -34
  56. package/dist/types.js.map +1 -1
  57. package/package.json +26 -29
  58. package/docs/README.md +0 -28
  59. package/docs/enums/Internal.EventTypes.md +0 -41
  60. package/docs/functions/createServer.md +0 -21
  61. package/docs/interfaces/CodeFrame.md +0 -48
  62. package/docs/interfaces/CompilerDelegate.md +0 -83
  63. package/docs/interfaces/HmrDelegate.md +0 -51
  64. package/docs/interfaces/InputStackFrame.md +0 -76
  65. package/docs/interfaces/ProgressData.md +0 -34
  66. package/docs/interfaces/ReactNativeStackFrame.md +0 -58
  67. package/docs/interfaces/Server.Api.Asset.md +0 -36
  68. package/docs/interfaces/Server.Api.CompilationStats.md +0 -9
  69. package/docs/interfaces/Server.Api.Delegate.md +0 -75
  70. package/docs/interfaces/Server.Config.md +0 -49
  71. package/docs/interfaces/Server.Delegate.md +0 -88
  72. package/docs/interfaces/Server.DelegateContext.md +0 -134
  73. package/docs/interfaces/Server.LoggerDelegate.md +0 -33
  74. package/docs/interfaces/Server.MessagesDelegate.md +0 -44
  75. package/docs/interfaces/Server.Options.md +0 -70
  76. package/docs/interfaces/StackFrame.md +0 -85
  77. package/docs/interfaces/SymbolicatorDelegate.md +0 -77
  78. package/docs/interfaces/SymbolicatorResults.md +0 -30
  79. package/docs/modules/Internal.md +0 -11
  80. package/docs/modules/Server.Api.md +0 -11
  81. package/docs/modules/Server.md +0 -16
  82. package/docs/types/SendProgress.md +0 -26
@@ -1,6 +1,14 @@
1
1
  import * as prettyFormat from 'pretty-format';
2
2
  import { WebSocketServer } from "../WebSocketServer.js";
3
-
3
+ /**
4
+ * {@link WebSocketEventsServer} configuration options.
5
+ */
6
+ /**
7
+ * Represents a command that connected clients can send to the {@link WebSocketEventsServer}.
8
+ */
9
+ /**
10
+ * Represents an event message.
11
+ */
4
12
  /**
5
13
  * Class for creating a WebSocket server to process events and reports.
6
14
  *
@@ -12,6 +20,7 @@ export class WebSocketEventsServer extends WebSocketServer {
12
20
  static PROTOCOL_VERSION = 2;
13
21
  clients = new Map();
14
22
  nextClientId = 0;
23
+
15
24
  /**
16
25
  * Create new instance of WebSocketHMRServer and attach it to the given Fastify instance.
17
26
  * Any logging information, will be passed through standard `fastify.log` API.
@@ -19,7 +28,6 @@ export class WebSocketEventsServer extends WebSocketServer {
19
28
  * @param fastify Fastify instance to attach the WebSocket server to.
20
29
  * @param config Configuration object.
21
30
  */
22
-
23
31
  constructor(fastify, config) {
24
32
  super(fastify, '/events', {
25
33
  verifyClient: ({
@@ -30,22 +38,19 @@ export class WebSocketEventsServer extends WebSocketServer {
30
38
  });
31
39
  this.config = config;
32
40
  }
41
+
33
42
  /**
34
43
  * Parse received command message from connected client.
35
44
  *
36
45
  * @param data Stringified command message to parse.
37
46
  * @returns Parsed command or `undefined` if parsing failed.
38
47
  */
39
-
40
-
41
48
  parseMessage(data) {
42
49
  try {
43
50
  const message = JSON.parse(data);
44
-
45
51
  if (message.version === WebSocketEventsServer.PROTOCOL_VERSION) {
46
52
  return message;
47
53
  }
48
-
49
54
  this.fastify.log.error({
50
55
  msg: 'Received message had wrong protocol version',
51
56
  message
@@ -56,22 +61,20 @@ export class WebSocketEventsServer extends WebSocketServer {
56
61
  data
57
62
  });
58
63
  }
59
-
60
64
  return undefined;
61
65
  }
66
+
62
67
  /**
63
68
  * Stringify `message` into a format that can be transported as a `string`.
64
69
  *
65
70
  * @param message Message to serialize.
66
71
  * @returns String representation of a `message` or `undefined` if serialization failed.
67
72
  */
68
-
69
-
70
73
  serializeMessage(message) {
71
74
  let toSerialize = message;
72
-
73
75
  if (message.error && message.error instanceof Error) {
74
- toSerialize = { ...message,
76
+ toSerialize = {
77
+ ...message,
75
78
  error: prettyFormat.default(message.error, {
76
79
  escapeString: true,
77
80
  highlight: true,
@@ -80,7 +83,8 @@ export class WebSocketEventsServer extends WebSocketServer {
80
83
  })
81
84
  };
82
85
  } else if (message && message.type === 'client_log') {
83
- toSerialize = { ...message,
86
+ toSerialize = {
87
+ ...message,
84
88
  data: message.data.map(item => typeof item === 'string' ? item : prettyFormat.default(item, {
85
89
  escapeString: true,
86
90
  highlight: true,
@@ -90,7 +94,6 @@ export class WebSocketEventsServer extends WebSocketServer {
90
94
  }))
91
95
  };
92
96
  }
93
-
94
97
  try {
95
98
  return JSON.stringify(toSerialize);
96
99
  } catch (error) {
@@ -101,24 +104,20 @@ export class WebSocketEventsServer extends WebSocketServer {
101
104
  return undefined;
102
105
  }
103
106
  }
107
+
104
108
  /**
105
109
  * Broadcast event to all connected clients.
106
110
  *
107
111
  * @param event Event message to broadcast.
108
112
  */
109
-
110
-
111
113
  broadcastEvent(event) {
112
114
  if (!this.clients.size) {
113
115
  return;
114
116
  }
115
-
116
117
  const serialized = this.serializeMessage(event);
117
-
118
118
  if (!serialized) {
119
119
  return;
120
120
  }
121
-
122
121
  for (const [clientId, socket] of this.clients.entries()) {
123
122
  try {
124
123
  socket.send(serialized);
@@ -132,13 +131,12 @@ export class WebSocketEventsServer extends WebSocketServer {
132
131
  }
133
132
  }
134
133
  }
134
+
135
135
  /**
136
136
  * Process new client's WebSocket connection.
137
137
  *
138
138
  * @param socket Incoming WebSocket connection.
139
139
  */
140
-
141
-
142
140
  onConnection(socket) {
143
141
  const clientId = `client#${this.nextClientId++}`;
144
142
  this.clients.set(clientId, socket);
@@ -146,7 +144,6 @@ export class WebSocketEventsServer extends WebSocketServer {
146
144
  msg: 'Events client connected',
147
145
  clientId
148
146
  });
149
-
150
147
  const onClose = () => {
151
148
  this.fastify.log.debug({
152
149
  msg: 'Events client disconnected',
@@ -155,16 +152,13 @@ export class WebSocketEventsServer extends WebSocketServer {
155
152
  socket.removeAllListeners();
156
153
  this.clients.delete(clientId);
157
154
  };
158
-
159
155
  socket.addEventListener('error', onClose);
160
156
  socket.addEventListener('close', onClose);
161
157
  socket.addEventListener('message', event => {
162
158
  const message = this.parseMessage(event.data.toString());
163
-
164
159
  if (!message) {
165
160
  return;
166
161
  }
167
-
168
162
  if (message.type === 'command') {
169
163
  try {
170
164
  this.config.webSocketMessageServer.broadcast(message.command, message.params);
@@ -182,6 +176,5 @@ export class WebSocketEventsServer extends WebSocketServer {
182
176
  }
183
177
  });
184
178
  }
185
-
186
179
  }
187
180
  //# sourceMappingURL=WebSocketEventsServer.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"WebSocketEventsServer.js","names":["prettyFormat","WebSocketServer","WebSocketEventsServer","PROTOCOL_VERSION","clients","Map","nextClientId","constructor","fastify","config","verifyClient","origin","test","parseMessage","data","message","JSON","parse","version","log","error","msg","undefined","serializeMessage","toSerialize","Error","default","escapeString","highlight","maxDepth","min","type","map","item","plugins","ReactElement","stringify","broadcastEvent","event","size","serialized","clientId","socket","entries","send","_skipBroadcast","onConnection","set","debug","onClose","removeAllListeners","delete","addEventListener","toString","webSocketMessageServer","broadcast","command","params"],"sources":["../../../../src/plugins/wss/servers/WebSocketEventsServer.ts"],"sourcesContent":["import WebSocket from 'ws';\nimport * as prettyFormat from 'pretty-format';\nimport type { FastifyInstance } from 'fastify';\nimport { WebSocketServer } from '../WebSocketServer';\nimport { WebSocketMessageServer } from './WebSocketMessageServer';\n\n/**\n * {@link WebSocketEventsServer} configuration options.\n */\nexport interface WebSocketEventsServerConfig {\n /** Instance of a {@link WebSocketMessageServer} which can be used for broadcasting. */\n webSocketMessageServer: WebSocketMessageServer;\n}\n\n/**\n * Represents a command that connected clients can send to the {@link WebSocketEventsServer}.\n */\nexport interface Command {\n version: number;\n type: 'command';\n command: string;\n params?: any;\n}\n\n/**\n * Represents an event message.\n */\nexport interface EventMessage {\n error?: Error | string;\n type?: string;\n data?: any;\n}\n\n/**\n * Class for creating a WebSocket server to process events and reports.\n *\n * Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/eventsSocketServer.ts\n *\n * @category Development server\n */\nexport class WebSocketEventsServer extends WebSocketServer {\n static readonly PROTOCOL_VERSION = 2;\n\n private clients = new Map<string, WebSocket>();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketHMRServer 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 * @param config Configuration object.\n */\n constructor(\n fastify: FastifyInstance,\n private config: WebSocketEventsServerConfig\n ) {\n super(fastify, '/events', {\n verifyClient: (({ origin }) => {\n return /^(https?:\\/\\/localhost|file:\\/\\/)/.test(origin);\n }) as WebSocket.VerifyClientCallbackSync,\n });\n }\n\n /**\n * Parse received command message from connected client.\n *\n * @param data Stringified command message to parse.\n * @returns Parsed command or `undefined` if parsing failed.\n */\n parseMessage(data: string): Command | undefined {\n try {\n const message = JSON.parse(data);\n if (message.version === WebSocketEventsServer.PROTOCOL_VERSION) {\n return message;\n }\n this.fastify.log.error({\n msg: 'Received message had wrong protocol version',\n message,\n });\n } catch {\n this.fastify.log.error({\n msg: 'Failed to parse the message as JSON',\n data,\n });\n }\n\n return undefined;\n }\n\n /**\n * Stringify `message` into a format that can be transported as a `string`.\n *\n * @param message Message to serialize.\n * @returns String representation of a `message` or `undefined` if serialization failed.\n */\n serializeMessage(message: EventMessage) {\n let toSerialize = message;\n if (message.error && message.error instanceof Error) {\n toSerialize = {\n ...message,\n error: prettyFormat.default(message.error, {\n escapeString: true,\n highlight: true,\n maxDepth: 3,\n min: true,\n }),\n };\n } else if (message && message.type === 'client_log') {\n toSerialize = {\n ...message,\n data: message.data.map((item: any) =>\n typeof item === 'string'\n ? item\n : prettyFormat.default(item, {\n escapeString: true,\n highlight: true,\n maxDepth: 3,\n min: true,\n plugins: [prettyFormat.plugins.ReactElement],\n })\n ),\n };\n }\n try {\n return JSON.stringify(toSerialize);\n } catch (error) {\n this.fastify.log.error({ msg: 'Failed to serialize', error });\n return undefined;\n }\n }\n\n /**\n * Broadcast event to all connected clients.\n *\n * @param event Event message to broadcast.\n */\n broadcastEvent(event: EventMessage) {\n if (!this.clients.size) {\n return;\n }\n\n const serialized = this.serializeMessage(event);\n if (!serialized) {\n return;\n }\n\n for (const [clientId, socket] of this.clients.entries()) {\n try {\n socket.send(serialized);\n } catch (error) {\n this.fastify.log.error({\n msg: 'Failed to send broadcast to client',\n clientId,\n error,\n _skipBroadcast: true,\n });\n }\n }\n }\n\n /**\n * Process new client's WebSocket connection.\n *\n * @param socket Incoming WebSocket connection.\n */\n onConnection(socket: WebSocket) {\n const clientId = `client#${this.nextClientId++}`;\n this.clients.set(clientId, socket);\n this.fastify.log.debug({ msg: 'Events client connected', clientId });\n\n const onClose = () => {\n this.fastify.log.debug({ msg: 'Events 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(event.data.toString());\n\n if (!message) {\n return;\n }\n\n if (message.type === 'command') {\n try {\n this.config.webSocketMessageServer.broadcast(\n message.command,\n message.params\n );\n } catch (error) {\n this.fastify.log.error({\n msg: 'Failed to forward message to clients',\n error,\n });\n }\n } else {\n this.fastify.log.error({\n msg: 'Unknown message type',\n message,\n });\n }\n });\n }\n}\n"],"mappings":"AACA,OAAO,KAAKA,YAAZ,MAA8B,eAA9B;SAESC,e;;AA8BT;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,qBAAN,SAAoCD,eAApC,CAAoD;EACzB,OAAhBE,gBAAgB,GAAG,CAAH;EAExBC,OAAO,GAAG,IAAIC,GAAJ,EAAH;EACPC,YAAY,GAAG,CAAH;EAEpB;AACF;AACA;AACA;AACA;AACA;AACA;;EACEC,WAAW,CACTC,OADS,EAEDC,MAFC,EAGT;IACA,MAAMD,OAAN,EAAe,SAAf,EAA0B;MACxBE,YAAY,EAAG,CAAC;QAAEC;MAAF,CAAD,KAAgB;QAC7B,OAAO,oCAAoCC,IAApC,CAAyCD,MAAzC,CAAP;MACD;IAHuB,CAA1B;IADA,KADQF,MACR,GADQA,MACR;EAMD;EAED;AACF;AACA;AACA;AACA;AACA;;;EACEI,YAAY,CAACC,IAAD,EAAoC;IAC9C,IAAI;MACF,MAAMC,OAAO,GAAGC,IAAI,CAACC,KAAL,CAAWH,IAAX,CAAhB;;MACA,IAAIC,OAAO,CAACG,OAAR,KAAoBhB,qBAAqB,CAACC,gBAA9C,EAAgE;QAC9D,OAAOY,OAAP;MACD;;MACD,KAAKP,OAAL,CAAaW,GAAb,CAAiBC,KAAjB,CAAuB;QACrBC,GAAG,EAAE,6CADgB;QAErBN;MAFq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P,MAAM,CAACU,gBAAP,CAAwB,OAAxB,EAAiCH,OAAjC;IACAP,MAAM,CAACU,gBAAP,CAAwB,SAAxB,EAAoCd,KAAD,IAAW;MAC5C,MAAMvB,OAAO,GAAG,KAAKF,YAAL,CAAkByB,KAAK,CAACxB,IAAN,CAAWuC,QAAX,EAAlB,CAAhB;;MAEA,IAAI,CAACtC,OAAL,EAAc;QACZ;MACD;;MAED,IAAIA,OAAO,CAACgB,IAAR,KAAiB,SAArB,EAAgC;QAC9B,IAAI;UACF,KAAKtB,MAAL,CAAY6C,sBAAZ,CAAmCC,SAAnC,CACExC,OAAO,CAACyC,OADV,EAEEzC,OAAO,CAAC0C,MAFV;QAID,CALD,CAKE,OAAOrC,KAAP,EAAc;UACd,KAAKZ,OAAL,CAAaW,GAAb,CAAiBC,KAAjB,CAAuB;YACrBC,GAAG,EAAE,sCADgB;YAErBD;UAFqB,CAAvB;QAID;MACF,CAZD,MAYO;QACL,KAAKZ,OAAL,CAAaW,GAAb,CAAiBC,KAAjB,CAAuB;UACrBC,GAAG,EAAE,sBADgB;UAErBN;QAFqB,CAAvB;MAID;IACF,CAzBD;EA0BD;;AArKwD"}
1
+ {"version":3,"file":"WebSocketEventsServer.js","names":["prettyFormat","WebSocketServer","WebSocketEventsServer","PROTOCOL_VERSION","clients","Map","nextClientId","constructor","fastify","config","verifyClient","origin","test","parseMessage","data","message","JSON","parse","version","log","error","msg","undefined","serializeMessage","toSerialize","Error","default","escapeString","highlight","maxDepth","min","type","map","item","plugins","ReactElement","stringify","broadcastEvent","event","size","serialized","clientId","socket","entries","send","_skipBroadcast","onConnection","set","debug","onClose","removeAllListeners","delete","addEventListener","toString","webSocketMessageServer","broadcast","command","params"],"sources":["../../../../src/plugins/wss/servers/WebSocketEventsServer.ts"],"sourcesContent":["import WebSocket from 'ws';\nimport * as prettyFormat from 'pretty-format';\nimport type { FastifyInstance } from 'fastify';\nimport { WebSocketServer } from '../WebSocketServer';\nimport { WebSocketMessageServer } from './WebSocketMessageServer';\n\n/**\n * {@link WebSocketEventsServer} configuration options.\n */\nexport interface WebSocketEventsServerConfig {\n /** Instance of a {@link WebSocketMessageServer} which can be used for broadcasting. */\n webSocketMessageServer: WebSocketMessageServer;\n}\n\n/**\n * Represents a command that connected clients can send to the {@link WebSocketEventsServer}.\n */\nexport interface Command {\n version: number;\n type: 'command';\n command: string;\n params?: any;\n}\n\n/**\n * Represents an event message.\n */\nexport interface EventMessage {\n error?: Error | string;\n type?: string;\n data?: any;\n}\n\n/**\n * Class for creating a WebSocket server to process events and reports.\n *\n * Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/eventsSocketServer.ts\n *\n * @category Development server\n */\nexport class WebSocketEventsServer extends WebSocketServer {\n static readonly PROTOCOL_VERSION = 2;\n\n private clients = new Map<string, WebSocket>();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketHMRServer 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 * @param config Configuration object.\n */\n constructor(\n fastify: FastifyInstance,\n private config: WebSocketEventsServerConfig\n ) {\n super(fastify, '/events', {\n verifyClient: (({ origin }) => {\n return /^(https?:\\/\\/localhost|file:\\/\\/)/.test(origin);\n }) as WebSocket.VerifyClientCallbackSync,\n });\n }\n\n /**\n * Parse received command message from connected client.\n *\n * @param data Stringified command message to parse.\n * @returns Parsed command or `undefined` if parsing failed.\n */\n parseMessage(data: string): Command | undefined {\n try {\n const message = JSON.parse(data);\n if (message.version === WebSocketEventsServer.PROTOCOL_VERSION) {\n return message;\n }\n this.fastify.log.error({\n msg: 'Received message had wrong protocol version',\n message,\n });\n } catch {\n this.fastify.log.error({\n msg: 'Failed to parse the message as JSON',\n data,\n });\n }\n\n return undefined;\n }\n\n /**\n * Stringify `message` into a format that can be transported as a `string`.\n *\n * @param message Message to serialize.\n * @returns String representation of a `message` or `undefined` if serialization failed.\n */\n serializeMessage(message: EventMessage) {\n let toSerialize = message;\n if (message.error && message.error instanceof Error) {\n toSerialize = {\n ...message,\n error: prettyFormat.default(message.error, {\n escapeString: true,\n highlight: true,\n maxDepth: 3,\n min: true,\n }),\n };\n } else if (message && message.type === 'client_log') {\n toSerialize = {\n ...message,\n data: message.data.map((item: any) =>\n typeof item === 'string'\n ? item\n : prettyFormat.default(item, {\n escapeString: true,\n highlight: true,\n maxDepth: 3,\n min: true,\n plugins: [prettyFormat.plugins.ReactElement],\n })\n ),\n };\n }\n try {\n return JSON.stringify(toSerialize);\n } catch (error) {\n this.fastify.log.error({ msg: 'Failed to serialize', error });\n return undefined;\n }\n }\n\n /**\n * Broadcast event to all connected clients.\n *\n * @param event Event message to broadcast.\n */\n broadcastEvent(event: EventMessage) {\n if (!this.clients.size) {\n return;\n }\n\n const serialized = this.serializeMessage(event);\n if (!serialized) {\n return;\n }\n\n for (const [clientId, socket] of this.clients.entries()) {\n try {\n socket.send(serialized);\n } catch (error) {\n this.fastify.log.error({\n msg: 'Failed to send broadcast to client',\n clientId,\n error,\n _skipBroadcast: true,\n });\n }\n }\n }\n\n /**\n * Process new client's WebSocket connection.\n *\n * @param socket Incoming WebSocket connection.\n */\n onConnection(socket: WebSocket) {\n const clientId = `client#${this.nextClientId++}`;\n this.clients.set(clientId, socket);\n this.fastify.log.debug({ msg: 'Events client connected', clientId });\n\n const onClose = () => {\n this.fastify.log.debug({ msg: 'Events 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(event.data.toString());\n\n if (!message) {\n return;\n }\n\n if (message.type === 'command') {\n try {\n this.config.webSocketMessageServer.broadcast(\n message.command,\n message.params\n );\n } catch (error) {\n this.fastify.log.error({\n msg: 'Failed to forward message to clients',\n error,\n });\n }\n } else {\n this.fastify.log.error({\n msg: 'Unknown message type',\n message,\n });\n }\n });\n }\n}\n"],"mappings":"AACA,O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ACgB,IAAI,KAAK,SAAS,EAAE;QAC9B,IAAI;UACF,IAAI,CAACtB,MAAM,CAAC6C,sBAAsB,CAACC,SAAS,CAC1CxC,OAAO,CAACyC,OAAO,EACfzC,OAAO,CAAC0C,MACV,CAAC;QACH,CAAC,CAAC,OAAOrC,KAAK,EAAE;UACd,IAAI,CAACZ,OAAO,CAACW,GAAG,CAACC,KAAK,CAAC;YACrBC,GAAG,EAAE,sCAAsC;YAC3CD;UACF,CAAC,CAAC;QACJ;MACF,CAAC,MAAM;QACL,IAAI,CAACZ,OAAO,CAACW,GAAG,CAACC,KAAK,CAAC;UACrBC,GAAG,EAAE,sBAAsB;UAC3BN;QACF,CAAC,CAAC;MACJ;IACF,CAAC,CAAC;EACJ;AACF"}
@@ -5,10 +5,10 @@ import { WebSocketServer } from "../WebSocketServer.js";
5
5
  *
6
6
  * @category Development server
7
7
  */
8
-
9
8
  export class WebSocketHMRServer extends WebSocketServer {
10
9
  clients = new Map();
11
10
  nextClientId = 0;
11
+
12
12
  /**
13
13
  * Create new instance of WebSocketHMRServer and attach it to the given Fastify instance.
14
14
  * Any logging information, will be passed through standard `fastify.log` API.
@@ -16,11 +16,11 @@ export class WebSocketHMRServer extends WebSocketServer {
16
16
  * @param fastify Fastify instance to attach the WebSocket server to.
17
17
  * @param delegate HMR delegate instance.
18
18
  */
19
-
20
19
  constructor(fastify, delegate) {
21
20
  super(fastify, delegate.getUriPath());
22
21
  this.delegate = delegate;
23
22
  }
23
+
24
24
  /**
25
25
  * Send action to all connected HMR clients.
26
26
  *
@@ -28,16 +28,12 @@ export class WebSocketHMRServer extends WebSocketServer {
28
28
  * @param platform Platform of clients to send the event to.
29
29
  * @param clientIds Ids of clients who should receive the event.
30
30
  */
31
-
32
-
33
31
  send(event, platform, clientIds) {
34
32
  const data = typeof event === 'string' ? event : JSON.stringify(event);
35
-
36
33
  for (const [key, socket] of this.clients) {
37
34
  if (key.platform !== platform || !(clientIds ?? [key.clientId]).includes(key.clientId)) {
38
35
  return;
39
36
  }
40
-
41
37
  try {
42
38
  socket.send(data);
43
39
  } catch (error) {
@@ -50,19 +46,17 @@ export class WebSocketHMRServer extends WebSocketServer {
50
46
  }
51
47
  }
52
48
  }
49
+
53
50
  /**
54
51
  * Process new WebSocket connection from HMR client.
55
52
  *
56
53
  * @param socket Incoming HMR client's WebSocket connection.
57
54
  */
58
-
59
-
60
55
  onConnection(socket, request) {
61
56
  const {
62
57
  searchParams
63
58
  } = new URL(request.url || '', 'http://localhost');
64
59
  const platform = searchParams.get('platform');
65
-
66
60
  if (!platform) {
67
61
  this.fastify.log.info({
68
62
  msg: 'HMR connection disconnected - missing platform'
@@ -70,7 +64,6 @@ export class WebSocketHMRServer extends WebSocketServer {
70
64
  socket.close();
71
65
  return;
72
66
  }
73
-
74
67
  const clientId = `client#${this.nextClientId++}`;
75
68
  this.clients.set({
76
69
  clientId,
@@ -81,7 +74,6 @@ export class WebSocketHMRServer extends WebSocketServer {
81
74
  clientId,
82
75
  platform
83
76
  });
84
-
85
77
  const onClose = () => {
86
78
  this.fastify.log.info({
87
79
  msg: 'HMR client disconnected',
@@ -93,11 +85,9 @@ export class WebSocketHMRServer extends WebSocketServer {
93
85
  platform
94
86
  });
95
87
  };
96
-
97
88
  socket.addEventListener('error', onClose);
98
89
  socket.addEventListener('close', onClose);
99
90
  this.delegate.onClientConnected(platform, clientId);
100
91
  }
101
-
102
92
  }
103
93
  //# sourceMappingURL=WebSocketHMRServer.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"WebSocketHMRServer.js","names":["URL","WebSocketServer","WebSocketHMRServer","clients","Map","nextClientId","constructor","fastify","delegate","getUriPath","send","event","platform","clientIds","data","JSON","stringify","key","socket","clientId","includes","error","log","msg","onConnection","request","searchParams","url","get","info","close","set","onClose","delete","addEventListener","onClientConnected"],"sources":["../../../../src/plugins/wss/servers/WebSocketHMRServer.ts"],"sourcesContent":["import { URL } from 'url';\nimport { IncomingMessage } from 'http';\nimport { FastifyInstance } from 'fastify';\nimport WebSocket from 'ws';\nimport { HmrDelegate } from '../types';\nimport { WebSocketServer } from '../WebSocketServer';\n\n/**\n * Class for creating a WebSocket server for Hot Module Replacement.\n *\n * @category Development server\n */\nexport class WebSocketHMRServer extends WebSocketServer {\n private clients = new Map<\n { clientId: string; platform: string },\n WebSocket\n >();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketHMRServer 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 * @param delegate HMR delegate instance.\n */\n constructor(fastify: FastifyInstance, private delegate: HmrDelegate) {\n super(fastify, delegate.getUriPath());\n }\n\n /**\n * Send action to all connected HMR clients.\n *\n * @param event Event to send to the clients.\n * @param platform Platform of clients to send the event to.\n * @param clientIds Ids of clients who should receive the event.\n */\n send(event: any, platform: string, clientIds?: string[]) {\n const data = typeof event === 'string' ? event : JSON.stringify(event);\n\n for (const [key, socket] of this.clients) {\n if (\n key.platform !== platform ||\n !(clientIds ?? [key.clientId]).includes(key.clientId)\n ) {\n return;\n }\n\n try {\n socket.send(data);\n } catch (error) {\n this.fastify.log.error({\n msg: 'Cannot send action to client',\n event,\n error,\n ...key,\n });\n }\n }\n }\n\n /**\n * Process new WebSocket connection from HMR client.\n *\n * @param socket Incoming HMR client's WebSocket connection.\n */\n onConnection(socket: WebSocket, request: IncomingMessage) {\n const { searchParams } = new URL(request.url || '', 'http://localhost');\n const platform = searchParams.get('platform');\n\n if (!platform) {\n this.fastify.log.info({\n msg: 'HMR connection disconnected - missing platform',\n });\n socket.close();\n return;\n }\n\n const clientId = `client#${this.nextClientId++}`;\n this.clients.set({ clientId, platform }, socket);\n\n this.fastify.log.info({ msg: 'HMR client connected', clientId, platform });\n\n const onClose = () => {\n this.fastify.log.info({\n msg: 'HMR client disconnected',\n clientId,\n platform,\n });\n this.clients.delete({ clientId, platform });\n };\n\n socket.addEventListener('error', onClose);\n socket.addEventListener('close', onClose);\n\n this.delegate.onClientConnected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ACE,GAAR,IAAe,EAAvB,EAA2B,kBAA3B,CAAzB;IACA,MAAMf,QAAQ,GAAGc,YAAY,CAACE,GAAb,CAAiB,UAAjB,CAAjB;;IAEA,IAAI,CAAChB,QAAL,EAAe;MACb,KAAKL,OAAL,CAAae,GAAb,CAAiBO,IAAjB,CAAsB;QACpBN,GAAG,EAAE;MADe,CAAtB;MAGAL,MAAM,CAACY,KAAP;MACA;IACD;;IAED,MAAMX,QAAQ,GAAI,UAAS,KAAKd,YAAL,EAAoB,EAA/C;IACA,KAAKF,OAAL,CAAa4B,GAAb,CAAiB;MAAEZ,QAAF;MAAYP;IAAZ,CAAjB,EAAyCM,MAAzC;IAEA,KAAKX,OAAL,CAAae,GAAb,CAAiBO,IAAjB,CAAsB;MAAEN,GAAG,EAAE,sBAAP;MAA+BJ,QAA/B;MAAyCP;IAAzC,CAAtB;;IAEA,MAAMoB,OAAO,GAAG,MAAM;MACpB,KAAKzB,OAAL,CAAae,GAAb,CAAiBO,IAAjB,CAAsB;QACpBN,GAAG,EAAE,yBADe;QAEpBJ,QAFoB;QAGpBP;MAHoB,CAAtB;MAKA,KAAKT,OAAL,CAAa8B,MAAb,CAAoB;QAAEd,QAAF;QAAYP;MAAZ,CAApB;IACD,CAPD;;IASAM,MAAM,CAACgB,gBAAP,CAAwB,OAAxB,EAAiCF,OAAjC;IACAd,MAAM,CAACgB,gBAAP,CAAwB,OAAxB,EAAiCF,OAAjC;IAEA,KAAKxB,QAAL,CAAc2B,iBAAd,CAAgCvB,QAAhC,EAA0CO,QAA1C;EACD;;AApFqD"}
1
+ {"version":3,"file":"WebSocketHMRServer.js","names":["URL","WebSocketServer","WebSocketHMRServer","clients","Map","nextClientId","constructor","fastify","delegate","getUriPath","send","event","platform","clientIds","data","JSON","stringify","key","socket","clientId","includes","error","log","msg","onConnection","request","searchParams","url","get","info","close","set","onClose","delete","addEventListener","onClientConnected"],"sources":["../../../../src/plugins/wss/servers/WebSocketHMRServer.ts"],"sourcesContent":["import { URL } from 'url';\nimport { IncomingMessage } from 'http';\nimport { FastifyInstance } from 'fastify';\nimport WebSocket from 'ws';\nimport { HmrDelegate } from '../types';\nimport { WebSocketServer } from '../WebSocketServer';\n\n/**\n * Class for creating a WebSocket server for Hot Module Replacement.\n *\n * @category Development server\n */\nexport class WebSocketHMRServer extends WebSocketServer {\n private clients = new Map<\n { clientId: string; platform: string },\n WebSocket\n >();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketHMRServer 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 * @param delegate HMR delegate instance.\n */\n constructor(\n fastify: FastifyInstance,\n private delegate: HmrDelegate\n ) {\n super(fastify, delegate.getUriPath());\n }\n\n /**\n * Send action to all connected HMR clients.\n *\n * @param event Event to send to the clients.\n * @param platform Platform of clients to send the event to.\n * @param clientIds Ids of clients who should receive the event.\n */\n send(event: any, platform: string, clientIds?: string[]) {\n const data = typeof event === 'string' ? event : JSON.stringify(event);\n\n for (const [key, socket] of this.clients) {\n if (\n key.platform !== platform ||\n !(clientIds ?? [key.clientId]).includes(key.clientId)\n ) {\n return;\n }\n\n try {\n socket.send(data);\n } catch (error) {\n this.fastify.log.error({\n msg: 'Cannot send action to client',\n event,\n error,\n ...key,\n });\n }\n }\n }\n\n /**\n * Process new WebSocket connection from HMR client.\n *\n * @param socket Incoming HMR client's WebSocket connection.\n */\n onConnection(socket: WebSocket, request: IncomingMessage) {\n const { searchParams } = new URL(request.url || '', 'http://localhost');\n const platform = searchParams.get('platform');\n\n if (!platform) {\n this.fastify.log.info({\n msg: 'HMR connection disconnected - missing platform',\n });\n socket.close();\n return;\n }\n\n const clientId = `client#${this.nextClientId++}`;\n this.clients.set({ clientId, platform }, socket);\n\n this.fastify.log.info({ msg: 'HMR client connected', clientId, platform });\n\n const onClose = () => {\n this.fastify.log.info({\n msg: 'HMR client disconnected',\n clientId,\n platform,\n });\n this.clients.delete({ clientId, platform });\n };\n\n socket.addEventListener('error', onClose);\n socket.addEventListener('close', onClose);\n\n this.delegate.onClientConnected(platform, clientId);\n }\n}\n"],"mappings":"AAAA,SAASA,GAAG,QAAQ,KAAK;AAAC,SAKjBC,eAAe;AAExB;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,kBAAkB,SAASD,eAAe,CAAC;EAC9CE,OAAO,GAAG,IAAIC,GAAG,CAGvB,CAAC;EACKC,YAAY,GAAG,CAAC;;EAExB;AACF;AACA;AACA;AACA;AACA;AACA;EACEC,WAAWA,CACTC,OAAwB,EAChBC,QAAqB,EAC7B;IACA,KAAK,CAACD,OAAO,EAAEC,QAAQ,CAACC,UAAU,CAAC,CAAC,CAAC;IAAC,KAF9BD,QAAqB,GAArBA,QAAqB;EAG/B;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;EACEE,IAAIA,CAACC,KAAU,EAAEC,QAAgB,EAAEC,SAAoB,EAAE;IACvD,MAAMC,IAAI,GAAG,OAAOH,KAAK,KAAK,QAAQ,GAAGA,KAAK,GAAGI,IAAI,CAACC,SAAS,CAACL,KAAK,CAAC;IAEtE,KAAK,MAAM,CAACM,GAAG,EAAEC,MAAM,CAAC,IAAI,IAAI,CAACf,OAAO,EAAE;MACxC,IACEc,GAAG,CAACL,QAAQ,KAAKA,QAAQ,IACzB,CAAC,CAACC,SAAS,IAAI,CAACI,GAAG,CAACE,QAAQ,CAAC,EAAEC,QAAQ,CAACH,GAAG,CAACE,QAAQ,CAAC,EACrD;QACA;MACF;MAEA,IAAI;QACFD,MAAM,CAACR,IAAI,CAACI,IAAI,CAAC;MACnB,CAAC,CAAC,OAAOO,KAAK,EAAE;QACd,IAAI,CAACd,OAAO,CAACe,GAAG,CAACD,KAAK,CAAC;UACrBE,GAAG,EAAE,8BAA8B;UACnCZ,KAAK;UACLU,KAAK;UACL,GAAGJ;QACL,CAAC,CAAC;MACJ;IACF;EACF;;EAEA;AACF;AACA;AACA;AACA;EACEO,YAAYA,CAACN,MAAiB,EAAEO,OAAwB,EAAE;IACxD,MAAM;MAAEC;IAAa,CAAC,GAAG,IAAI1B,GAAG,CAACyB,OAAO,CAACE,GAAG,IAAI,EAAE,EAAE,kBAAkB,CAAC;IACvE,MAAMf,QAAQ,GAAGc,YAAY,CAACE,GAAG,CAAC,UAAU,CAAC;IAE7C,IAAI,CAAChB,QAAQ,EAAE;MACb,IAAI,CAACL,OAAO,CAACe,GAAG,CAACO,IAAI,CAAC;QACpBN,GAAG,EAAE;MACP,CAAC,CAAC;MACFL,MAAM,CAACY,KAAK,CAAC,CAAC;MACd;IACF;IAEA,MAAMX,QAAQ,GAAI,UAAS,IAAI,CAACd,YAAY,EAAG,EAAC;IAChD,IAAI,CAACF,OAAO,CAAC4B,GAAG,CAAC;MAAEZ,QAAQ;MAAEP;IAAS,CAAC,EAAEM,MAAM,CAAC;IAEhD,IAAI,CAACX,OAAO,CAACe,GAAG,CAACO,IAAI,CAAC;MAAEN,GAAG,EAAE,sBAAsB;MAAEJ,QAAQ;MAAEP;IAAS,CAAC,CAAC;IAE1E,MAAMoB,OAAO,GAAGA,CAAA,KAAM;MACpB,IAAI,CAACzB,OAAO,CAACe,GAAG,CAACO,IAAI,CAAC;QACpBN,GAAG,EAAE,yBAAyB;QAC9BJ,QAAQ;QACRP;MACF,CAAC,CAAC;MACF,IAAI,CAACT,OAAO,CAAC8B,MAAM,CAAC;QAAEd,QAAQ;QAAEP;MAAS,CAAC,CAAC;IAC7C,CAAC;IAEDM,MAAM,CAACgB,gBAAgB,CAAC,OAAO,EAAEF,OAAO,CAAC;IACzCd,MAAM,CAACgB,gBAAgB,CAAC,OAAO,EAAEF,OAAO,CAAC;IAEzC,IAAI,CAACxB,QAAQ,CAAC2B,iBAAiB,CAACvB,QAAQ,EAAEO,QAAQ,CAAC;EACrD;AACF"}
@@ -22,7 +22,7 @@ export interface ReactNativeMessage {
22
22
  error?: Error;
23
23
  params?: Record<string, any>;
24
24
  }
25
- declare type WebSocketWithUpgradeReq = WebSocket & {
25
+ type WebSocketWithUpgradeReq = WebSocket & {
26
26
  upgradeReq?: IncomingMessage;
27
27
  };
28
28
  /**