@teardown/cli 1.2.15 → 1.2.22
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/bin/teardown.js +3 -0
- package/dist/commands/dev/dev.d.ts +22 -0
- package/dist/commands/dev/dev.js +55 -0
- package/dist/commands/dev/types.d.ts +64 -0
- package/dist/commands/dev/types.js +2 -0
- package/dist/index.js +11 -32
- package/dist/modules/dev/dev-menu/keyboard-handler.d.ts +20 -0
- package/dist/modules/dev/dev-menu/keyboard-handler.js +126 -0
- package/dist/modules/dev/dev-menu/open-debugger-keyboard-handler.d.ts +18 -0
- package/dist/modules/dev/dev-menu/open-debugger-keyboard-handler.js +105 -0
- package/dist/modules/dev/dev-server/dev-server-checker.d.ts +22 -0
- package/dist/modules/dev/dev-server/dev-server-checker.js +72 -0
- package/dist/modules/dev/dev-server/dev-server.d.ts +68 -0
- package/dist/modules/dev/dev-server/dev-server.js +220 -0
- package/dist/modules/dev/dev-server/plugins/devtools.plugin.d.ts +10 -0
- package/dist/modules/dev/dev-server/plugins/devtools.plugin.js +50 -0
- package/dist/modules/dev/dev-server/plugins/favicon.plugin.d.ts +1 -0
- package/dist/modules/dev/dev-server/plugins/favicon.plugin.js +19 -0
- package/dist/modules/dev/dev-server/plugins/multipart.plugin.d.ts +1 -0
- package/dist/modules/dev/dev-server/plugins/multipart.plugin.js +62 -0
- package/dist/modules/dev/dev-server/plugins/symbolicate/index.d.ts +2 -0
- package/dist/modules/dev/dev-server/plugins/symbolicate/index.js +18 -0
- package/dist/modules/dev/dev-server/plugins/symbolicate/sybmolicatePlugin.d.ts +1 -0
- package/dist/modules/dev/dev-server/plugins/symbolicate/sybmolicatePlugin.js +24 -0
- package/dist/modules/dev/dev-server/plugins/symbolicate/types.d.ts +64 -0
- package/dist/modules/dev/dev-server/plugins/symbolicate/types.js +2 -0
- package/dist/modules/dev/dev-server/plugins/types.d.ts +11 -0
- package/dist/modules/dev/dev-server/plugins/types.js +2 -0
- package/dist/modules/dev/dev-server/plugins/wss/index.d.ts +3 -0
- package/dist/modules/dev/dev-server/plugins/wss/index.js +19 -0
- package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-api.server.d.ts +32 -0
- package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-api.server.js +61 -0
- package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-debugger.server.d.ts +63 -0
- package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-debugger.server.js +128 -0
- package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-dev-client.server.d.ts +32 -0
- package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-dev-client.server.js +75 -0
- package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-events.server.d.ts +75 -0
- package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-events.server.js +190 -0
- package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-hmr.server.d.ts +44 -0
- package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-hmr.server.js +112 -0
- package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-message.server.d.ts +139 -0
- package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-message.server.js +352 -0
- package/dist/modules/dev/dev-server/plugins/wss/types.d.ts +6 -0
- package/dist/modules/dev/dev-server/plugins/wss/types.js +2 -0
- package/dist/modules/dev/dev-server/plugins/wss/web-socket-router.d.ts +32 -0
- package/dist/modules/dev/dev-server/plugins/wss/web-socket-router.js +59 -0
- package/dist/modules/dev/dev-server/plugins/wss/web-socket-server-adapter.d.ts +13 -0
- package/dist/modules/dev/dev-server/plugins/wss/web-socket-server-adapter.js +26 -0
- package/dist/modules/dev/dev-server/plugins/wss/web-socket-server.d.ts +39 -0
- package/dist/modules/dev/dev-server/plugins/wss/web-socket-server.js +46 -0
- package/dist/modules/dev/dev-server/plugins/wss/wss.plugin.d.ts +46 -0
- package/dist/modules/dev/dev-server/plugins/wss/wss.plugin.js +57 -0
- package/dist/modules/dev/dev-server/types.d.ts +27 -0
- package/dist/modules/dev/dev-server/types.js +17 -0
- package/dist/modules/dev/terminal/base.terminal.reporter.d.ts +25 -0
- package/dist/modules/dev/terminal/base.terminal.reporter.js +78 -0
- package/dist/modules/dev/terminal/terminal.reporter.d.ts +12 -0
- package/dist/modules/dev/terminal/terminal.reporter.js +71 -0
- package/dist/modules/dev/types.d.ts +20 -0
- package/dist/modules/dev/types.js +2 -0
- package/dist/modules/dev/utils/log.d.ts +23 -0
- package/dist/modules/dev/utils/log.js +73 -0
- package/package.json +40 -17
- /package/dist/{init-teardown.d.ts → commands/init/init-teardown.d.ts} +0 -0
- /package/dist/{init-teardown.js → commands/init/init-teardown.js} +0 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.WebSocketHMRServer = void 0;
|
|
7
|
+
const node_url_1 = require("node:url");
|
|
8
|
+
const web_socket_server_1 = require("../web-socket-server");
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
const HmrServer_1 = __importDefault(require("metro/src/HmrServer"));
|
|
11
|
+
/**
|
|
12
|
+
* Class for creating a WebSocket server for Hot Module Replacement.
|
|
13
|
+
*
|
|
14
|
+
* @category Development server
|
|
15
|
+
*/
|
|
16
|
+
class WebSocketHMRServer extends web_socket_server_1.WebSocketServer {
|
|
17
|
+
options;
|
|
18
|
+
clients = new Map();
|
|
19
|
+
nextClientId = 0;
|
|
20
|
+
hmrServer;
|
|
21
|
+
/**
|
|
22
|
+
* Create new instance of WebSocketHMRServer and attach it to the given Fastify instance.
|
|
23
|
+
* Any logging information, will be passed through standard `fastify.log` API.
|
|
24
|
+
*
|
|
25
|
+
* @param fastify Fastify instance to attach the WebSocket server to.
|
|
26
|
+
* @param delegate HMR delegate instance.
|
|
27
|
+
*/
|
|
28
|
+
constructor(fastify, options) {
|
|
29
|
+
super(fastify, "/hot");
|
|
30
|
+
this.options = options;
|
|
31
|
+
this.hmrServer = new HmrServer_1.default(this.options.metroServer.getBundler(), this.options.metroServer.getCreateModuleId(), this.options.metroConfig);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Send action to all connected HMR clients.
|
|
35
|
+
*
|
|
36
|
+
* @param event Event to send to the clients.
|
|
37
|
+
* @param platform Platform of clients to send the event to.
|
|
38
|
+
* @param clientIds Ids of clients who should receive the event.
|
|
39
|
+
*/
|
|
40
|
+
send(event, platform, clientIds) {
|
|
41
|
+
const data = typeof event === "string" ? event : JSON.stringify(event);
|
|
42
|
+
for (const [key, socket] of this.clients) {
|
|
43
|
+
if (key.platform !== platform ||
|
|
44
|
+
!(clientIds ?? [key.clientId]).includes(key.clientId)) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
socket.send(data);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
this.fastify.log.error({
|
|
52
|
+
msg: "Cannot send action to client",
|
|
53
|
+
event,
|
|
54
|
+
error,
|
|
55
|
+
...key,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Process new WebSocket connection from HMR client.
|
|
62
|
+
*
|
|
63
|
+
* @param socket Incoming HMR client's WebSocket connection.
|
|
64
|
+
*/
|
|
65
|
+
async onConnection(socket, request) {
|
|
66
|
+
const requestUrl = request.url || "";
|
|
67
|
+
const { searchParams } = new node_url_1.URL(requestUrl, "http://localhost");
|
|
68
|
+
const platform = searchParams.get("platform") ?? "unknown";
|
|
69
|
+
if (!platform) {
|
|
70
|
+
this.fastify.log.debug({
|
|
71
|
+
msg: "HMR connection disconnected - missing platform",
|
|
72
|
+
});
|
|
73
|
+
socket.close();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const clientId = `client#${this.nextClientId++}`;
|
|
77
|
+
const client = {
|
|
78
|
+
clientId,
|
|
79
|
+
platform,
|
|
80
|
+
};
|
|
81
|
+
this.clients.set(client, socket);
|
|
82
|
+
this.fastify.log.debug({ msg: "HMR client connected", ...client });
|
|
83
|
+
const onClose = () => {
|
|
84
|
+
this.fastify.log.debug({
|
|
85
|
+
msg: "HMR client disconnected",
|
|
86
|
+
...client,
|
|
87
|
+
});
|
|
88
|
+
this.clients.delete(client);
|
|
89
|
+
};
|
|
90
|
+
socket.addEventListener("error", onClose);
|
|
91
|
+
socket.addEventListener("close", onClose);
|
|
92
|
+
await this.registerHMRClient(socket, requestUrl);
|
|
93
|
+
this.options.onClientConnected(platform, clientId);
|
|
94
|
+
}
|
|
95
|
+
async registerHMRClient(socket, requestUrl) {
|
|
96
|
+
console.log("HMR server registerHMRClient", requestUrl);
|
|
97
|
+
const sendFn = (data) => {
|
|
98
|
+
socket.send(data);
|
|
99
|
+
};
|
|
100
|
+
const hmrClient = await this.hmrServer.onClientConnect(requestUrl, sendFn);
|
|
101
|
+
socket.on("error", (error) => {
|
|
102
|
+
this.hmrServer.onClientError(hmrClient, error);
|
|
103
|
+
});
|
|
104
|
+
socket.on("close", () => {
|
|
105
|
+
this.hmrServer.onClientDisconnect(hmrClient);
|
|
106
|
+
});
|
|
107
|
+
socket.on("message", (data) => {
|
|
108
|
+
this.hmrServer.onClientMessage(hmrClient, data, sendFn);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.WebSocketHMRServer = WebSocketHMRServer;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type { IncomingMessage } from "node:http";
|
|
2
|
+
import type { FastifyInstance } from "fastify";
|
|
3
|
+
import type WebSocket from "ws";
|
|
4
|
+
import { WebSocketServer } from "../web-socket-server";
|
|
5
|
+
/**
|
|
6
|
+
* Holds {@link ReactNativeMessage} `id` data.
|
|
7
|
+
*/
|
|
8
|
+
export interface ReactNativeIdObject {
|
|
9
|
+
requestId: string;
|
|
10
|
+
clientId: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Message representation used by {@link WebSocketMessageServer}.
|
|
14
|
+
*/
|
|
15
|
+
export interface ReactNativeMessage {
|
|
16
|
+
version?: string;
|
|
17
|
+
id?: ReactNativeIdObject;
|
|
18
|
+
method?: string;
|
|
19
|
+
target: string;
|
|
20
|
+
result?: any;
|
|
21
|
+
error?: Error;
|
|
22
|
+
params?: Record<string, any>;
|
|
23
|
+
}
|
|
24
|
+
type WebSocketWithUpgradeReq = WebSocket & {
|
|
25
|
+
upgradeReq?: IncomingMessage;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Class for creating a WebSocket server and sending messages between development server
|
|
29
|
+
* and the React Native applications.
|
|
30
|
+
*
|
|
31
|
+
* Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/messageSocketServer.ts
|
|
32
|
+
*
|
|
33
|
+
* @category Development server
|
|
34
|
+
*/
|
|
35
|
+
export declare class WebSocketMessageServer extends WebSocketServer {
|
|
36
|
+
static readonly PROTOCOL_VERSION = 2;
|
|
37
|
+
/**
|
|
38
|
+
* Check if message is a broadcast request.
|
|
39
|
+
*
|
|
40
|
+
* @param message Message to check.
|
|
41
|
+
* @returns True if message is a broadcast request and should be broadcasted
|
|
42
|
+
* with {@link sendBroadcast}.
|
|
43
|
+
*/
|
|
44
|
+
static isBroadcast(message: Partial<ReactNativeMessage>): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Check if message is a method request.
|
|
47
|
+
*
|
|
48
|
+
* @param message Message to check.
|
|
49
|
+
* @returns True if message is a request.
|
|
50
|
+
*/
|
|
51
|
+
static isRequest(message: Partial<ReactNativeMessage>): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Check if message is a response with results of performing some request.
|
|
54
|
+
*
|
|
55
|
+
* @param message Message to check.
|
|
56
|
+
* @returns True if message is a response.
|
|
57
|
+
*/
|
|
58
|
+
static isResponse(message: Partial<ReactNativeMessage>): boolean;
|
|
59
|
+
private clients;
|
|
60
|
+
private nextClientId;
|
|
61
|
+
/**
|
|
62
|
+
* Create new instance of WebSocketMessageServer and attach it to the given Fastify instance.
|
|
63
|
+
* Any logging information, will be passed through standard `fastify.log` API.
|
|
64
|
+
*
|
|
65
|
+
* @param fastify Fastify instance to attach the WebSocket server to.
|
|
66
|
+
*/
|
|
67
|
+
constructor(fastify: FastifyInstance);
|
|
68
|
+
/**
|
|
69
|
+
* Parse stringified message into a {@link ReactNativeMessage}.
|
|
70
|
+
*
|
|
71
|
+
* @param data Stringified message.
|
|
72
|
+
* @param binary Additional binary data if any.
|
|
73
|
+
* @returns Parsed message or `undefined` if parsing failed.
|
|
74
|
+
*/
|
|
75
|
+
parseMessage(data: string, binary: any): Partial<ReactNativeMessage> | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* Get client's WebSocket connection for given `clientId`.
|
|
78
|
+
* Throws if no such client is connected.
|
|
79
|
+
*
|
|
80
|
+
* @param clientId Id of the client.
|
|
81
|
+
* @returns WebSocket connection.
|
|
82
|
+
*/
|
|
83
|
+
getClientSocket(clientId: string): WebSocketWithUpgradeReq;
|
|
84
|
+
/**
|
|
85
|
+
* Process error by sending an error message to the client whose message caused the error
|
|
86
|
+
* to occur.
|
|
87
|
+
*
|
|
88
|
+
* @param clientId Id of the client whose message caused an error.
|
|
89
|
+
* @param message Original message which caused the error.
|
|
90
|
+
* @param error Concrete instance of an error that occurred.
|
|
91
|
+
*/
|
|
92
|
+
handleError(clientId: string, message: Partial<ReactNativeMessage>, error: Error): void;
|
|
93
|
+
/**
|
|
94
|
+
* Send given request `message` to it's designated client's socket based on `message.target`.
|
|
95
|
+
* The target client must be connected, otherwise it will throw an error.
|
|
96
|
+
*
|
|
97
|
+
* @param clientId Id of the client that requested the forward.
|
|
98
|
+
* @param message Message to forward.
|
|
99
|
+
*/
|
|
100
|
+
forwardRequest(clientId: string, message: Partial<ReactNativeMessage>): void;
|
|
101
|
+
/**
|
|
102
|
+
* Send given response `message` to it's designated client's socket based
|
|
103
|
+
* on `message.id.clientId`.
|
|
104
|
+
* The target client must be connected, otherwise it will throw an error.
|
|
105
|
+
*
|
|
106
|
+
* @param message Message to forward.
|
|
107
|
+
*/
|
|
108
|
+
forwardResponse(message: Partial<ReactNativeMessage>): void;
|
|
109
|
+
/**
|
|
110
|
+
* Process request message targeted towards this {@link WebSocketMessageServer}
|
|
111
|
+
* and send back the results.
|
|
112
|
+
*
|
|
113
|
+
* @param clientId Id of the client who send the message.
|
|
114
|
+
* @param message The message to process by the server.
|
|
115
|
+
*/
|
|
116
|
+
processServerRequest(clientId: string, message: Partial<ReactNativeMessage>): void;
|
|
117
|
+
/**
|
|
118
|
+
* Broadcast given message to all connected clients.
|
|
119
|
+
*
|
|
120
|
+
* @param broadcasterId Id of the client who is broadcasting.
|
|
121
|
+
* @param message Message to broadcast.
|
|
122
|
+
*/
|
|
123
|
+
sendBroadcast(broadcasterId: string | undefined, message: Partial<ReactNativeMessage>): void;
|
|
124
|
+
/**
|
|
125
|
+
* Send method broadcast to all connected clients.
|
|
126
|
+
*
|
|
127
|
+
* @param method Method name to broadcast.
|
|
128
|
+
* @param params Method parameters.
|
|
129
|
+
*/
|
|
130
|
+
broadcast(method: string, params?: Record<string, any>): void;
|
|
131
|
+
/**
|
|
132
|
+
* Process new client's WebSocket connection.
|
|
133
|
+
*
|
|
134
|
+
* @param socket Incoming WebSocket connection.
|
|
135
|
+
* @param request Upgrade request for the connection.
|
|
136
|
+
*/
|
|
137
|
+
onConnection(socket: WebSocket, request: IncomingMessage): void;
|
|
138
|
+
}
|
|
139
|
+
export {};
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WebSocketMessageServer = void 0;
|
|
4
|
+
const node_url_1 = require("node:url");
|
|
5
|
+
const web_socket_server_1 = require("../web-socket-server");
|
|
6
|
+
/**
|
|
7
|
+
* Class for creating a WebSocket server and sending messages between development server
|
|
8
|
+
* and the React Native applications.
|
|
9
|
+
*
|
|
10
|
+
* Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/messageSocketServer.ts
|
|
11
|
+
*
|
|
12
|
+
* @category Development server
|
|
13
|
+
*/
|
|
14
|
+
class WebSocketMessageServer extends web_socket_server_1.WebSocketServer {
|
|
15
|
+
static PROTOCOL_VERSION = 2;
|
|
16
|
+
/**
|
|
17
|
+
* Check if message is a broadcast request.
|
|
18
|
+
*
|
|
19
|
+
* @param message Message to check.
|
|
20
|
+
* @returns True if message is a broadcast request and should be broadcasted
|
|
21
|
+
* with {@link sendBroadcast}.
|
|
22
|
+
*/
|
|
23
|
+
static isBroadcast(message) {
|
|
24
|
+
return (typeof message.method === "string" &&
|
|
25
|
+
message.id === undefined &&
|
|
26
|
+
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
|
+
static isRequest(message) {
|
|
35
|
+
return (typeof message.method === "string" && typeof message.target === "string");
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Check if message is a response with results of performing some request.
|
|
39
|
+
*
|
|
40
|
+
* @param message Message to check.
|
|
41
|
+
* @returns True if message is a response.
|
|
42
|
+
*/
|
|
43
|
+
static isResponse(message) {
|
|
44
|
+
return (typeof message.id === "object" &&
|
|
45
|
+
typeof message.id.requestId !== "undefined" &&
|
|
46
|
+
typeof message.id.clientId === "string" &&
|
|
47
|
+
(message.result !== undefined || message.error !== undefined));
|
|
48
|
+
}
|
|
49
|
+
clients = new Map();
|
|
50
|
+
nextClientId = 0;
|
|
51
|
+
/**
|
|
52
|
+
* Create new instance of WebSocketMessageServer and attach it to the given Fastify instance.
|
|
53
|
+
* Any logging information, will be passed through standard `fastify.log` API.
|
|
54
|
+
*
|
|
55
|
+
* @param fastify Fastify instance to attach the WebSocket server to.
|
|
56
|
+
*/
|
|
57
|
+
constructor(fastify) {
|
|
58
|
+
super(fastify, "/message");
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Parse stringified message into a {@link ReactNativeMessage}.
|
|
62
|
+
*
|
|
63
|
+
* @param data Stringified message.
|
|
64
|
+
* @param binary Additional binary data if any.
|
|
65
|
+
* @returns Parsed message or `undefined` if parsing failed.
|
|
66
|
+
*/
|
|
67
|
+
parseMessage(data, binary) {
|
|
68
|
+
if (binary) {
|
|
69
|
+
this.fastify.log.error({
|
|
70
|
+
msg: "Failed to parse message - expected text message, got binary",
|
|
71
|
+
});
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const message = JSON.parse(data);
|
|
76
|
+
if (message.version === WebSocketMessageServer.PROTOCOL_VERSION.toString()) {
|
|
77
|
+
return message;
|
|
78
|
+
}
|
|
79
|
+
this.fastify.log.error({
|
|
80
|
+
msg: "Received message had wrong protocol version",
|
|
81
|
+
message,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
this.fastify.log.error({
|
|
86
|
+
msg: "Failed to parse the message as JSON",
|
|
87
|
+
data,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Get client's WebSocket connection for given `clientId`.
|
|
94
|
+
* Throws if no such client is connected.
|
|
95
|
+
*
|
|
96
|
+
* @param clientId Id of the client.
|
|
97
|
+
* @returns WebSocket connection.
|
|
98
|
+
*/
|
|
99
|
+
getClientSocket(clientId) {
|
|
100
|
+
const socket = this.clients.get(clientId);
|
|
101
|
+
if (socket === undefined) {
|
|
102
|
+
throw new Error(`Could not find client with id "${clientId}"`);
|
|
103
|
+
}
|
|
104
|
+
return socket;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Process error by sending an error message to the client whose message caused the error
|
|
108
|
+
* to occur.
|
|
109
|
+
*
|
|
110
|
+
* @param clientId Id of the client whose message caused an error.
|
|
111
|
+
* @param message Original message which caused the error.
|
|
112
|
+
* @param error Concrete instance of an error that occurred.
|
|
113
|
+
*/
|
|
114
|
+
handleError(clientId, message, error) {
|
|
115
|
+
const errorMessage = {
|
|
116
|
+
id: message.id,
|
|
117
|
+
method: message.method,
|
|
118
|
+
target: message.target,
|
|
119
|
+
error: message.error === undefined ? "undefined" : "defined",
|
|
120
|
+
params: message.params === undefined ? "undefined" : "defined",
|
|
121
|
+
result: message.result === undefined ? "undefined" : "defined",
|
|
122
|
+
};
|
|
123
|
+
if (message.id === undefined) {
|
|
124
|
+
this.fastify.log.error({
|
|
125
|
+
msg: "Handling message failed",
|
|
126
|
+
clientId,
|
|
127
|
+
error,
|
|
128
|
+
errorMessage,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
try {
|
|
133
|
+
const socket = this.getClientSocket(clientId);
|
|
134
|
+
socket.send(JSON.stringify({
|
|
135
|
+
version: WebSocketMessageServer.PROTOCOL_VERSION,
|
|
136
|
+
error,
|
|
137
|
+
id: message.id,
|
|
138
|
+
}));
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
this.fastify.log.error("Failed to reply", {
|
|
142
|
+
clientId,
|
|
143
|
+
error,
|
|
144
|
+
errorMessage,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Send given request `message` to it's designated client's socket based on `message.target`.
|
|
151
|
+
* The target client must be connected, otherwise it will throw an error.
|
|
152
|
+
*
|
|
153
|
+
* @param clientId Id of the client that requested the forward.
|
|
154
|
+
* @param message Message to forward.
|
|
155
|
+
*/
|
|
156
|
+
forwardRequest(clientId, message) {
|
|
157
|
+
if (!message.target) {
|
|
158
|
+
this.fastify.log.error({
|
|
159
|
+
msg: "Failed to forward request - message.target is missing",
|
|
160
|
+
clientId,
|
|
161
|
+
message,
|
|
162
|
+
});
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const socket = this.getClientSocket(message.target);
|
|
166
|
+
socket.send(JSON.stringify({
|
|
167
|
+
version: WebSocketMessageServer.PROTOCOL_VERSION,
|
|
168
|
+
method: message.method,
|
|
169
|
+
params: message.params,
|
|
170
|
+
id: message.id === undefined
|
|
171
|
+
? undefined
|
|
172
|
+
: { requestId: message.id, clientId },
|
|
173
|
+
}));
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Send given response `message` to it's designated client's socket based
|
|
177
|
+
* on `message.id.clientId`.
|
|
178
|
+
* The target client must be connected, otherwise it will throw an error.
|
|
179
|
+
*
|
|
180
|
+
* @param message Message to forward.
|
|
181
|
+
*/
|
|
182
|
+
forwardResponse(message) {
|
|
183
|
+
console.log("forwardResponse", message);
|
|
184
|
+
if (!message.id) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const socket = this.getClientSocket(message.id.clientId);
|
|
188
|
+
socket.send(JSON.stringify({
|
|
189
|
+
version: WebSocketMessageServer.PROTOCOL_VERSION,
|
|
190
|
+
result: message.result,
|
|
191
|
+
error: message.error,
|
|
192
|
+
id: message.id.requestId,
|
|
193
|
+
}));
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Process request message targeted towards this {@link WebSocketMessageServer}
|
|
197
|
+
* and send back the results.
|
|
198
|
+
*
|
|
199
|
+
* @param clientId Id of the client who send the message.
|
|
200
|
+
* @param message The message to process by the server.
|
|
201
|
+
*/
|
|
202
|
+
processServerRequest(clientId, message) {
|
|
203
|
+
let result;
|
|
204
|
+
switch (message.method) {
|
|
205
|
+
case "getid":
|
|
206
|
+
result = clientId;
|
|
207
|
+
break;
|
|
208
|
+
case "getpeers": {
|
|
209
|
+
const output = {};
|
|
210
|
+
this.clients.forEach((peerSocket, peerId) => {
|
|
211
|
+
if (clientId !== peerId) {
|
|
212
|
+
const { searchParams } = new node_url_1.URL(peerSocket.upgradeReq?.url || "");
|
|
213
|
+
output[peerId] = [...searchParams.entries()].reduce((acc, [key, value]) => {
|
|
214
|
+
acc[key] = value;
|
|
215
|
+
return acc;
|
|
216
|
+
}, {});
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
result = output;
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
default:
|
|
223
|
+
throw new Error(`Cannot process server request - unknown method ${JSON.stringify({
|
|
224
|
+
clientId,
|
|
225
|
+
message,
|
|
226
|
+
})}`);
|
|
227
|
+
}
|
|
228
|
+
const socket = this.getClientSocket(clientId);
|
|
229
|
+
socket.send(JSON.stringify({
|
|
230
|
+
version: WebSocketMessageServer.PROTOCOL_VERSION,
|
|
231
|
+
result,
|
|
232
|
+
id: message.id,
|
|
233
|
+
}));
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Broadcast given message to all connected clients.
|
|
237
|
+
*
|
|
238
|
+
* @param broadcasterId Id of the client who is broadcasting.
|
|
239
|
+
* @param message Message to broadcast.
|
|
240
|
+
*/
|
|
241
|
+
sendBroadcast(broadcasterId, message) {
|
|
242
|
+
const forwarded = {
|
|
243
|
+
version: WebSocketMessageServer.PROTOCOL_VERSION,
|
|
244
|
+
method: message.method,
|
|
245
|
+
params: message.params,
|
|
246
|
+
};
|
|
247
|
+
if (this.clients.size === 0) {
|
|
248
|
+
this.fastify.log.warn({
|
|
249
|
+
msg:
|
|
250
|
+
// biome-ignore lint/style/useTemplate: <explanation>
|
|
251
|
+
"No apps connected. " +
|
|
252
|
+
`Sending "${message.method}" to all React Native apps failed. ` +
|
|
253
|
+
"Make sure your app is running in the simulator or on a phone connected via USB.",
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
for (const [clientId, socket] of this.clients) {
|
|
257
|
+
if (clientId !== broadcasterId) {
|
|
258
|
+
try {
|
|
259
|
+
socket.send(JSON.stringify(forwarded));
|
|
260
|
+
}
|
|
261
|
+
catch (error) {
|
|
262
|
+
this.fastify.log.error({
|
|
263
|
+
msg: "Failed to send broadcast",
|
|
264
|
+
clientId,
|
|
265
|
+
error,
|
|
266
|
+
forwarded,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Send method broadcast to all connected clients.
|
|
274
|
+
*
|
|
275
|
+
* @param method Method name to broadcast.
|
|
276
|
+
* @param params Method parameters.
|
|
277
|
+
*/
|
|
278
|
+
broadcast(method, params) {
|
|
279
|
+
this.sendBroadcast(undefined, { method, params });
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Process new client's WebSocket connection.
|
|
283
|
+
*
|
|
284
|
+
* @param socket Incoming WebSocket connection.
|
|
285
|
+
* @param request Upgrade request for the connection.
|
|
286
|
+
*/
|
|
287
|
+
onConnection(socket, request) {
|
|
288
|
+
const clientId = `client#${this.nextClientId++}`;
|
|
289
|
+
const client = socket;
|
|
290
|
+
client.upgradeReq = request;
|
|
291
|
+
this.clients.set(clientId, client);
|
|
292
|
+
this.fastify.log.debug({ msg: "Message client connected", clientId });
|
|
293
|
+
const cleanup = () => {
|
|
294
|
+
this.fastify.log.debug({ msg: "Cleaning up message client", clientId });
|
|
295
|
+
socket.removeAllListeners();
|
|
296
|
+
this.clients.delete(clientId);
|
|
297
|
+
};
|
|
298
|
+
const onClose = (close) => {
|
|
299
|
+
this.fastify.log.debug({
|
|
300
|
+
msg: ["Message client closed"],
|
|
301
|
+
clientId,
|
|
302
|
+
close,
|
|
303
|
+
});
|
|
304
|
+
cleanup();
|
|
305
|
+
};
|
|
306
|
+
const onError = (error) => {
|
|
307
|
+
this.fastify.log.error({ msg: "Message client error", clientId, error });
|
|
308
|
+
cleanup();
|
|
309
|
+
};
|
|
310
|
+
socket.addEventListener("error", onError);
|
|
311
|
+
socket.addEventListener("close", onClose);
|
|
312
|
+
socket.addEventListener("message", (event) => {
|
|
313
|
+
const message = this.parseMessage(event.data.toString(),
|
|
314
|
+
// @ts-ignore
|
|
315
|
+
event.binary);
|
|
316
|
+
if (!message) {
|
|
317
|
+
this.fastify.log.error({
|
|
318
|
+
msg: "Received message not matching protocol",
|
|
319
|
+
clientId,
|
|
320
|
+
message,
|
|
321
|
+
});
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
try {
|
|
325
|
+
if (WebSocketMessageServer.isBroadcast(message)) {
|
|
326
|
+
this.sendBroadcast(clientId, message);
|
|
327
|
+
}
|
|
328
|
+
else if (WebSocketMessageServer.isRequest(message)) {
|
|
329
|
+
if (message.target === "server") {
|
|
330
|
+
this.processServerRequest(clientId, message);
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
this.forwardRequest(clientId, message);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
else if (WebSocketMessageServer.isResponse(message)) {
|
|
337
|
+
this.forwardResponse(message);
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
throw new Error(`Invalid message, did not match the protocol ${JSON.stringify({
|
|
341
|
+
clientId,
|
|
342
|
+
message,
|
|
343
|
+
})}`);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
catch (error) {
|
|
347
|
+
this.handleError(clientId, message, error);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
exports.WebSocketMessageServer = WebSocketMessageServer;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { FastifyInstance } from "fastify";
|
|
2
|
+
import type { WebSocketServerInterface } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Class for creating a WebSocket router to forward connections to the
|
|
5
|
+
* respective {@link WebSocketServer} as long as the connection is accepted for the upgrade by the
|
|
6
|
+
* server.
|
|
7
|
+
*
|
|
8
|
+
* If the connection is not accepted by any `WebSocketServer`, it will be destroyed to avoid
|
|
9
|
+
* creating handling connections and potentially throwing `ECONNRESET` errors.
|
|
10
|
+
*
|
|
11
|
+
* @category Development server
|
|
12
|
+
*/
|
|
13
|
+
export declare class WebSocketRouter {
|
|
14
|
+
private fastify;
|
|
15
|
+
/** The list of all register WebSocket servers. */
|
|
16
|
+
protected servers: WebSocketServerInterface[];
|
|
17
|
+
/**
|
|
18
|
+
* Create new instance of `WebSocketRouter` and attach it to the given Fastify instance.
|
|
19
|
+
* Any logging information, will be passed through standard `fastify.log` API.
|
|
20
|
+
*
|
|
21
|
+
* @param fastify Fastify instance to attach the WebSocket router to.
|
|
22
|
+
*/
|
|
23
|
+
constructor(fastify: FastifyInstance);
|
|
24
|
+
/**
|
|
25
|
+
* Register a new {@link WebSocketServer}. New connection will now
|
|
26
|
+
* check if the given server will accept them and forward them.
|
|
27
|
+
*
|
|
28
|
+
* @param server WebSocket server to register.
|
|
29
|
+
* @returns The same instance of the WebSocket server after it's been registered.
|
|
30
|
+
*/
|
|
31
|
+
registerServer<T extends WebSocketServerInterface>(server: T): T;
|
|
32
|
+
}
|