@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.
Files changed (65) hide show
  1. package/bin/teardown.js +3 -0
  2. package/dist/commands/dev/dev.d.ts +22 -0
  3. package/dist/commands/dev/dev.js +55 -0
  4. package/dist/commands/dev/types.d.ts +64 -0
  5. package/dist/commands/dev/types.js +2 -0
  6. package/dist/index.js +11 -32
  7. package/dist/modules/dev/dev-menu/keyboard-handler.d.ts +20 -0
  8. package/dist/modules/dev/dev-menu/keyboard-handler.js +126 -0
  9. package/dist/modules/dev/dev-menu/open-debugger-keyboard-handler.d.ts +18 -0
  10. package/dist/modules/dev/dev-menu/open-debugger-keyboard-handler.js +105 -0
  11. package/dist/modules/dev/dev-server/dev-server-checker.d.ts +22 -0
  12. package/dist/modules/dev/dev-server/dev-server-checker.js +72 -0
  13. package/dist/modules/dev/dev-server/dev-server.d.ts +68 -0
  14. package/dist/modules/dev/dev-server/dev-server.js +220 -0
  15. package/dist/modules/dev/dev-server/plugins/devtools.plugin.d.ts +10 -0
  16. package/dist/modules/dev/dev-server/plugins/devtools.plugin.js +50 -0
  17. package/dist/modules/dev/dev-server/plugins/favicon.plugin.d.ts +1 -0
  18. package/dist/modules/dev/dev-server/plugins/favicon.plugin.js +19 -0
  19. package/dist/modules/dev/dev-server/plugins/multipart.plugin.d.ts +1 -0
  20. package/dist/modules/dev/dev-server/plugins/multipart.plugin.js +62 -0
  21. package/dist/modules/dev/dev-server/plugins/symbolicate/index.d.ts +2 -0
  22. package/dist/modules/dev/dev-server/plugins/symbolicate/index.js +18 -0
  23. package/dist/modules/dev/dev-server/plugins/symbolicate/sybmolicatePlugin.d.ts +1 -0
  24. package/dist/modules/dev/dev-server/plugins/symbolicate/sybmolicatePlugin.js +24 -0
  25. package/dist/modules/dev/dev-server/plugins/symbolicate/types.d.ts +64 -0
  26. package/dist/modules/dev/dev-server/plugins/symbolicate/types.js +2 -0
  27. package/dist/modules/dev/dev-server/plugins/types.d.ts +11 -0
  28. package/dist/modules/dev/dev-server/plugins/types.js +2 -0
  29. package/dist/modules/dev/dev-server/plugins/wss/index.d.ts +3 -0
  30. package/dist/modules/dev/dev-server/plugins/wss/index.js +19 -0
  31. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-api.server.d.ts +32 -0
  32. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-api.server.js +61 -0
  33. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-debugger.server.d.ts +63 -0
  34. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-debugger.server.js +128 -0
  35. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-dev-client.server.d.ts +32 -0
  36. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-dev-client.server.js +75 -0
  37. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-events.server.d.ts +75 -0
  38. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-events.server.js +190 -0
  39. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-hmr.server.d.ts +44 -0
  40. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-hmr.server.js +112 -0
  41. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-message.server.d.ts +139 -0
  42. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-message.server.js +352 -0
  43. package/dist/modules/dev/dev-server/plugins/wss/types.d.ts +6 -0
  44. package/dist/modules/dev/dev-server/plugins/wss/types.js +2 -0
  45. package/dist/modules/dev/dev-server/plugins/wss/web-socket-router.d.ts +32 -0
  46. package/dist/modules/dev/dev-server/plugins/wss/web-socket-router.js +59 -0
  47. package/dist/modules/dev/dev-server/plugins/wss/web-socket-server-adapter.d.ts +13 -0
  48. package/dist/modules/dev/dev-server/plugins/wss/web-socket-server-adapter.js +26 -0
  49. package/dist/modules/dev/dev-server/plugins/wss/web-socket-server.d.ts +39 -0
  50. package/dist/modules/dev/dev-server/plugins/wss/web-socket-server.js +46 -0
  51. package/dist/modules/dev/dev-server/plugins/wss/wss.plugin.d.ts +46 -0
  52. package/dist/modules/dev/dev-server/plugins/wss/wss.plugin.js +57 -0
  53. package/dist/modules/dev/dev-server/types.d.ts +27 -0
  54. package/dist/modules/dev/dev-server/types.js +17 -0
  55. package/dist/modules/dev/terminal/base.terminal.reporter.d.ts +25 -0
  56. package/dist/modules/dev/terminal/base.terminal.reporter.js +78 -0
  57. package/dist/modules/dev/terminal/terminal.reporter.d.ts +12 -0
  58. package/dist/modules/dev/terminal/terminal.reporter.js +71 -0
  59. package/dist/modules/dev/types.d.ts +20 -0
  60. package/dist/modules/dev/types.js +2 -0
  61. package/dist/modules/dev/utils/log.d.ts +23 -0
  62. package/dist/modules/dev/utils/log.js +73 -0
  63. package/package.json +40 -17
  64. /package/dist/{init-teardown.d.ts → commands/init/init-teardown.d.ts} +0 -0
  65. /package/dist/{init-teardown.js → commands/init/init-teardown.js} +0 -0
@@ -0,0 +1,63 @@
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
+ * Class for creating a WebSocket server and providing a bridge between
7
+ * debugger UI (Remote JS debugger) and the running React Native application.
8
+ *
9
+ * React Native application (aka client) will send and receive messages from the debugger UI
10
+ * which runs inside a browser.
11
+ *
12
+ * @category Development server
13
+ */
14
+ export declare class WebSocketDebuggerServer extends WebSocketServer {
15
+ /**
16
+ * A WebSocket connection with the debugger UI.
17
+ */
18
+ private debuggerSocket;
19
+ /**
20
+ * A WebSocket connection with the client (React Native app).
21
+ */
22
+ private clientSocket;
23
+ /**
24
+ * Create new instance of WebSocketDebuggerServer and attach it to the given Fastify instance.
25
+ * Any logging information, will be passed through standard `fastify.log` API.
26
+ *
27
+ * @param fastify Fastify instance to attach the WebSocket server to.
28
+ */
29
+ constructor(fastify: FastifyInstance);
30
+ /**
31
+ * Check if debugger UI is connected to the WebSocketDebuggerServer.
32
+ */
33
+ isDebuggerConnected(): boolean;
34
+ /**
35
+ * Send a message to a given WebSocket connection.
36
+ *
37
+ * @param socket WebSocket connection to send the message to.
38
+ * @param message Message to send.
39
+ */
40
+ send(socket: WebSocket | undefined, message: string): void;
41
+ /**
42
+ * Process new WebSocket connection. The upgrade request should contain `role` query param
43
+ * for determining the type of the connection.
44
+ *
45
+ * @param socket Incoming WebSocket connection.
46
+ * @param request Upgrade request for the connection.
47
+ */
48
+ onConnection(socket: WebSocket, request: IncomingMessage): void;
49
+ /**
50
+ * Process new WebSocket connection from Debugger UI (Remote JS Debugger).
51
+ * If there's already open connection, the new one gets closed automatically.
52
+ *
53
+ * @param socket Incoming debugger WebSocket connection.
54
+ */
55
+ onDebuggerConnection(socket: WebSocket): void;
56
+ /**
57
+ * Process new WebSocket connection from React Native app (client)
58
+ * and close any previous connection.
59
+ *
60
+ * @param socket Incoming client WebSocket connection.
61
+ */
62
+ onClientConnection(socket: WebSocket): void;
63
+ }
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebSocketDebuggerServer = void 0;
4
+ const web_socket_server_1 = require("../web-socket-server");
5
+ /**
6
+ * Class for creating a WebSocket server and providing a bridge between
7
+ * debugger UI (Remote JS debugger) and the running React Native application.
8
+ *
9
+ * React Native application (aka client) will send and receive messages from the debugger UI
10
+ * which runs inside a browser.
11
+ *
12
+ * @category Development server
13
+ */
14
+ class WebSocketDebuggerServer extends web_socket_server_1.WebSocketServer {
15
+ /**
16
+ * A WebSocket connection with the debugger UI.
17
+ */
18
+ debuggerSocket;
19
+ /**
20
+ * A WebSocket connection with the client (React Native app).
21
+ */
22
+ clientSocket;
23
+ /**
24
+ * Create new instance of WebSocketDebuggerServer and attach it to the given Fastify instance.
25
+ * Any logging information, will be passed through standard `fastify.log` API.
26
+ *
27
+ * @param fastify Fastify instance to attach the WebSocket server to.
28
+ */
29
+ constructor(fastify) {
30
+ super(fastify, "/debugger-proxy");
31
+ }
32
+ /**
33
+ * Check if debugger UI is connected to the WebSocketDebuggerServer.
34
+ */
35
+ isDebuggerConnected() {
36
+ return Boolean(this.debuggerSocket);
37
+ }
38
+ /**
39
+ * Send a message to a given WebSocket connection.
40
+ *
41
+ * @param socket WebSocket connection to send the message to.
42
+ * @param message Message to send.
43
+ */
44
+ send(socket, message) {
45
+ try {
46
+ socket?.send(message);
47
+ }
48
+ catch (error) {
49
+ this.fastify.log.warn({ msg: "Failed to send data to socket", error });
50
+ }
51
+ }
52
+ /**
53
+ * Process new WebSocket connection. The upgrade request should contain `role` query param
54
+ * for determining the type of the connection.
55
+ *
56
+ * @param socket Incoming WebSocket connection.
57
+ * @param request Upgrade request for the connection.
58
+ */
59
+ onConnection(socket, request) {
60
+ console.log("WebSocketDebuggerServer onConnection");
61
+ const { url = "" } = request;
62
+ if (url.indexOf("role=debugger") >= 0) {
63
+ this.fastify.log.info({ msg: "Remote debugger connected" });
64
+ this.onDebuggerConnection(socket);
65
+ }
66
+ else if (url.indexOf("role=client") >= 0) {
67
+ this.fastify.log.info({ msg: "React Native app connected to debugger" });
68
+ this.onClientConnection(socket);
69
+ }
70
+ else {
71
+ socket.close(1011, "Missing role param");
72
+ }
73
+ }
74
+ /**
75
+ * Process new WebSocket connection from Debugger UI (Remote JS Debugger).
76
+ * If there's already open connection, the new one gets closed automatically.
77
+ *
78
+ * @param socket Incoming debugger WebSocket connection.
79
+ */
80
+ onDebuggerConnection(socket) {
81
+ if (this.debuggerSocket) {
82
+ socket.close(1011, "Another debugger is already connected");
83
+ return;
84
+ }
85
+ this.debuggerSocket = socket;
86
+ const onClose = () => {
87
+ this.fastify.log.info({ msg: "Remote debugger disconnected" });
88
+ this.debuggerSocket = undefined;
89
+ if (this.clientSocket) {
90
+ this.clientSocket.removeAllListeners();
91
+ this.clientSocket.close(1011, "Debugger was disconnected");
92
+ }
93
+ };
94
+ this.debuggerSocket.addEventListener("error", onClose);
95
+ this.debuggerSocket.addEventListener("close", onClose);
96
+ this.debuggerSocket.addEventListener("message", ({ data }) => {
97
+ this.send(this.clientSocket, data.toString());
98
+ });
99
+ }
100
+ /**
101
+ * Process new WebSocket connection from React Native app (client)
102
+ * and close any previous connection.
103
+ *
104
+ * @param socket Incoming client WebSocket connection.
105
+ */
106
+ onClientConnection(socket) {
107
+ console.log("WebSocketDebuggerServer onClientConnection");
108
+ if (this.clientSocket) {
109
+ this.clientSocket.removeAllListeners();
110
+ this.clientSocket.close(1011, "Another client is connected");
111
+ this.clientSocket = undefined;
112
+ }
113
+ const onClose = () => {
114
+ this.fastify.log.info({
115
+ msg: "React Native app disconnected from debugger",
116
+ });
117
+ this.clientSocket = undefined;
118
+ this.send(this.debuggerSocket, JSON.stringify({ method: "$disconnected" }));
119
+ };
120
+ this.clientSocket = socket;
121
+ this.clientSocket.addEventListener("error", onClose);
122
+ this.clientSocket.addEventListener("close", onClose);
123
+ this.clientSocket.addEventListener("message", ({ data }) => {
124
+ this.send(this.debuggerSocket, data.toString());
125
+ });
126
+ }
127
+ }
128
+ exports.WebSocketDebuggerServer = WebSocketDebuggerServer;
@@ -0,0 +1,32 @@
1
+ import type { FastifyInstance } from "fastify";
2
+ import type WebSocket from "ws";
3
+ import { WebSocketServer } from "../web-socket-server";
4
+ /**
5
+ * Class for creating a WebSocket server for communication with React Native clients.
6
+ * All client logs - logs from React Native application - are processed here.
7
+ *
8
+ * @category Development server
9
+ */
10
+ export declare class WebSocketDevClientServer extends WebSocketServer {
11
+ private clients;
12
+ private nextClientId;
13
+ /**
14
+ * Create new instance of WebSocketDevClientServer and attach it to the given Fastify instance.
15
+ * Any logging information, will be passed through standard `fastify.log` API.
16
+ *
17
+ * @param fastify Fastify instance to attach the WebSocket server to.
18
+ */
19
+ constructor(fastify: FastifyInstance);
20
+ /**
21
+ * Process client message.
22
+ *
23
+ * @param message Stringified client message.
24
+ */
25
+ processMessage(message: string): void;
26
+ /**
27
+ * Process new WebSocket connection from client application.
28
+ *
29
+ * @param socket Incoming client's WebSocket connection.
30
+ */
31
+ onConnection(socket: WebSocket): void;
32
+ }
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebSocketDevClientServer = void 0;
4
+ const web_socket_server_1 = require("../web-socket-server");
5
+ /**
6
+ * Class for creating a WebSocket server for communication with React Native clients.
7
+ * All client logs - logs from React Native application - are processed here.
8
+ *
9
+ * @category Development server
10
+ */
11
+ class WebSocketDevClientServer extends web_socket_server_1.WebSocketServer {
12
+ clients = new Map();
13
+ nextClientId = 0;
14
+ /**
15
+ * Create new instance of WebSocketDevClientServer and attach it to the given Fastify instance.
16
+ * Any logging information, will be passed through standard `fastify.log` API.
17
+ *
18
+ * @param fastify Fastify instance to attach the WebSocket server to.
19
+ */
20
+ constructor(fastify) {
21
+ super(fastify, "/__client");
22
+ }
23
+ /**
24
+ * Process client message.
25
+ *
26
+ * @param message Stringified client message.
27
+ */
28
+ processMessage(message) {
29
+ console.log("WebSocketDevClientServer processMessage", message);
30
+ const { type, ...body } = JSON.parse(message);
31
+ switch (type) {
32
+ case "client-log":
33
+ if (body.level === "error") {
34
+ this.fastify.log.error({ issuer: "Console", msg: body.data });
35
+ }
36
+ else if (body.level === "warn") {
37
+ this.fastify.log.warn({ issuer: "Console", msg: body.data });
38
+ }
39
+ else if (body.level === "info" || body.level === "log") {
40
+ this.fastify.log.info({ issuer: "Console", msg: body.data });
41
+ }
42
+ else {
43
+ // body.level === 'debug' || body.level === 'trace'
44
+ this.fastify.log.debug({ issuer: "Console", msg: body.data });
45
+ }
46
+ break;
47
+ default:
48
+ this.fastify.log.warn({ msg: "Unknown client message", message });
49
+ }
50
+ }
51
+ /**
52
+ * Process new WebSocket connection from client application.
53
+ *
54
+ * @param socket Incoming client's WebSocket connection.
55
+ */
56
+ onConnection(socket) {
57
+ const clientId = `client#${this.nextClientId++}`;
58
+ console.log("WebSocketDevClientServer onConnection", clientId);
59
+ this.clients.set(clientId, socket);
60
+ this.fastify.log.debug({ msg: "React Native client connected", clientId });
61
+ const onClose = () => {
62
+ this.fastify.log.debug({
63
+ msg: "React Native client disconnected",
64
+ clientId,
65
+ });
66
+ this.clients.delete(clientId);
67
+ };
68
+ socket.addEventListener("error", onClose);
69
+ socket.addEventListener("close", onClose);
70
+ socket.addEventListener("message", (event) => {
71
+ this.processMessage(event.data.toString());
72
+ });
73
+ }
74
+ }
75
+ exports.WebSocketDevClientServer = WebSocketDevClientServer;
@@ -0,0 +1,75 @@
1
+ import type { FastifyInstance } from "fastify";
2
+ import type WebSocket from "ws";
3
+ import { WebSocketServer } from "../web-socket-server";
4
+ import type { WebSocketMessageServer } from "./web-socket-message.server";
5
+ /**
6
+ * {@link WebSocketEventsServer} configuration options.
7
+ */
8
+ export interface WebSocketEventsServerConfig {
9
+ /** Instance of a {@link WebSocketMessageServer} which can be used for broadcasting. */
10
+ webSocketMessageServer: WebSocketMessageServer;
11
+ }
12
+ /**
13
+ * Represents a command that connected clients can send to the {@link WebSocketEventsServer}.
14
+ */
15
+ export interface Command {
16
+ version: number;
17
+ type: "command";
18
+ command: string;
19
+ params?: any;
20
+ }
21
+ /**
22
+ * Represents an event message.
23
+ */
24
+ export interface EventMessage {
25
+ error?: Error | string;
26
+ type?: string;
27
+ data?: any;
28
+ }
29
+ /**
30
+ * Class for creating a WebSocket server to process events and reports.
31
+ *
32
+ * Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/eventsSocketServer.ts
33
+ *
34
+ * @category Development server
35
+ */
36
+ export declare class WebSocketEventsServer extends WebSocketServer {
37
+ private config;
38
+ static readonly PROTOCOL_VERSION = 2;
39
+ private clients;
40
+ private nextClientId;
41
+ /**
42
+ * Create new instance of WebSocketHMRServer and attach it to the given Fastify instance.
43
+ * Any logging information, will be passed through standard `fastify.log` API.
44
+ *
45
+ * @param fastify Fastify instance to attach the WebSocket server to.
46
+ * @param config Configuration object.
47
+ */
48
+ constructor(fastify: FastifyInstance, config: WebSocketEventsServerConfig);
49
+ /**
50
+ * Parse received command message from connected client.
51
+ *
52
+ * @param data Stringified command message to parse.
53
+ * @returns Parsed command or `undefined` if parsing failed.
54
+ */
55
+ parseMessage(data: string): Command | undefined;
56
+ /**
57
+ * Stringify `message` into a format that can be transported as a `string`.
58
+ *
59
+ * @param message Message to serialize.
60
+ * @returns String representation of a `message` or `undefined` if serialization failed.
61
+ */
62
+ serializeMessage(message: EventMessage): string | undefined;
63
+ /**
64
+ * Broadcast event to all connected clients.
65
+ *
66
+ * @param event Event message to broadcast.
67
+ */
68
+ broadcastEvent(event: EventMessage): void;
69
+ /**
70
+ * Process new client's WebSocket connection.
71
+ *
72
+ * @param socket Incoming WebSocket connection.
73
+ */
74
+ onConnection(socket: WebSocket): void;
75
+ }
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.WebSocketEventsServer = void 0;
27
+ const prettyFormat = __importStar(require("pretty-format"));
28
+ const web_socket_server_1 = require("../web-socket-server");
29
+ /**
30
+ * Class for creating a WebSocket server to process events and reports.
31
+ *
32
+ * Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/eventsSocketServer.ts
33
+ *
34
+ * @category Development server
35
+ */
36
+ class WebSocketEventsServer extends web_socket_server_1.WebSocketServer {
37
+ config;
38
+ static PROTOCOL_VERSION = 2;
39
+ clients = new Map();
40
+ nextClientId = 0;
41
+ /**
42
+ * Create new instance of WebSocketHMRServer and attach it to the given Fastify instance.
43
+ * Any logging information, will be passed through standard `fastify.log` API.
44
+ *
45
+ * @param fastify Fastify instance to attach the WebSocket server to.
46
+ * @param config Configuration object.
47
+ */
48
+ constructor(fastify, config) {
49
+ super(fastify, "/events", {
50
+ verifyClient: (({ origin }) => {
51
+ return /^(https?:\/\/localhost|file:\/\/)/.test(origin);
52
+ }),
53
+ });
54
+ this.config = config;
55
+ }
56
+ /**
57
+ * Parse received command message from connected client.
58
+ *
59
+ * @param data Stringified command message to parse.
60
+ * @returns Parsed command or `undefined` if parsing failed.
61
+ */
62
+ parseMessage(data) {
63
+ try {
64
+ const message = JSON.parse(data);
65
+ if (message.version === WebSocketEventsServer.PROTOCOL_VERSION) {
66
+ return message;
67
+ }
68
+ this.fastify.log.error({
69
+ msg: "Received message had wrong protocol version",
70
+ message,
71
+ });
72
+ }
73
+ catch {
74
+ this.fastify.log.error({
75
+ msg: "Failed to parse the message as JSON",
76
+ data,
77
+ });
78
+ }
79
+ return undefined;
80
+ }
81
+ /**
82
+ * Stringify `message` into a format that can be transported as a `string`.
83
+ *
84
+ * @param message Message to serialize.
85
+ * @returns String representation of a `message` or `undefined` if serialization failed.
86
+ */
87
+ serializeMessage(message) {
88
+ let toSerialize = message;
89
+ if (message.error && message.error instanceof Error) {
90
+ toSerialize = {
91
+ ...message,
92
+ error: prettyFormat.default(message.error, {
93
+ escapeString: true,
94
+ highlight: true,
95
+ maxDepth: 3,
96
+ min: true,
97
+ }),
98
+ };
99
+ }
100
+ else if (message && message.type === "client_log") {
101
+ toSerialize = {
102
+ ...message,
103
+ data: message.data.map((item) => typeof item === "string"
104
+ ? item
105
+ : prettyFormat.default(item, {
106
+ escapeString: true,
107
+ highlight: true,
108
+ maxDepth: 3,
109
+ min: true,
110
+ plugins: [prettyFormat.plugins.ReactElement],
111
+ })),
112
+ };
113
+ }
114
+ try {
115
+ return JSON.stringify(toSerialize);
116
+ }
117
+ catch (error) {
118
+ this.fastify.log.error({ msg: "Failed to serialize", error });
119
+ return undefined;
120
+ }
121
+ }
122
+ /**
123
+ * Broadcast event to all connected clients.
124
+ *
125
+ * @param event Event message to broadcast.
126
+ */
127
+ broadcastEvent(event) {
128
+ if (!this.clients.size) {
129
+ return;
130
+ }
131
+ const serialized = this.serializeMessage(event);
132
+ if (!serialized) {
133
+ return;
134
+ }
135
+ for (const [clientId, socket] of this.clients.entries()) {
136
+ try {
137
+ socket.send(serialized);
138
+ }
139
+ catch (error) {
140
+ this.fastify.log.error({
141
+ msg: "Failed to send broadcast to client",
142
+ clientId,
143
+ error,
144
+ _skipBroadcast: true,
145
+ });
146
+ }
147
+ }
148
+ }
149
+ /**
150
+ * Process new client's WebSocket connection.
151
+ *
152
+ * @param socket Incoming WebSocket connection.
153
+ */
154
+ onConnection(socket) {
155
+ const clientId = `client#${this.nextClientId++}`;
156
+ this.clients.set(clientId, socket);
157
+ this.fastify.log.debug({ msg: "Events client connected", clientId });
158
+ const onClose = () => {
159
+ this.fastify.log.debug({ msg: "Events client disconnected", clientId });
160
+ socket.removeAllListeners();
161
+ this.clients.delete(clientId);
162
+ };
163
+ socket.addEventListener("error", onClose);
164
+ socket.addEventListener("close", onClose);
165
+ socket.addEventListener("message", (event) => {
166
+ const message = this.parseMessage(event.data.toString());
167
+ if (!message) {
168
+ return;
169
+ }
170
+ if (message.type === "command") {
171
+ try {
172
+ this.config.webSocketMessageServer.broadcast(message.command, message.params);
173
+ }
174
+ catch (error) {
175
+ this.fastify.log.error({
176
+ msg: "Failed to forward message to clients",
177
+ error,
178
+ });
179
+ }
180
+ }
181
+ else {
182
+ this.fastify.log.error({
183
+ msg: "Unknown message type",
184
+ message,
185
+ });
186
+ }
187
+ });
188
+ }
189
+ }
190
+ exports.WebSocketEventsServer = WebSocketEventsServer;
@@ -0,0 +1,44 @@
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
+ import type { ConfigT } from "metro-config";
6
+ import type MetroServer from "metro/src/Server";
7
+ /**
8
+ * Class for creating a WebSocket server for Hot Module Replacement.
9
+ *
10
+ * @category Development server
11
+ */
12
+ export declare class WebSocketHMRServer extends WebSocketServer {
13
+ private options;
14
+ private clients;
15
+ private nextClientId;
16
+ private hmrServer;
17
+ /**
18
+ * Create new instance of WebSocketHMRServer 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 server to.
22
+ * @param delegate HMR delegate instance.
23
+ */
24
+ constructor(fastify: FastifyInstance, options: {
25
+ metroConfig: ConfigT;
26
+ metroServer: MetroServer;
27
+ onClientConnected: (platform: string, clientId: string) => void;
28
+ });
29
+ /**
30
+ * Send action to all connected HMR clients.
31
+ *
32
+ * @param event Event to send to the clients.
33
+ * @param platform Platform of clients to send the event to.
34
+ * @param clientIds Ids of clients who should receive the event.
35
+ */
36
+ send(event: any, platform: string, clientIds?: string[]): void;
37
+ /**
38
+ * Process new WebSocket connection from HMR client.
39
+ *
40
+ * @param socket Incoming HMR client's WebSocket connection.
41
+ */
42
+ onConnection(socket: WebSocket, request: IncomingMessage): Promise<void>;
43
+ registerHMRClient(socket: WebSocket, requestUrl: string): Promise<void>;
44
+ }