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