@teardown/cli 1.2.33 → 1.2.35

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 (40) hide show
  1. package/dist/commands/dev/dev.js +55 -0
  2. package/dist/commands/init/init-teardown.js +26 -0
  3. package/dist/index.js +20 -0
  4. package/dist/modules/dev/dev-menu/keyboard-handler.js +138 -0
  5. package/dist/modules/dev/dev-menu/open-debugger-keyboard-handler.js +105 -0
  6. package/dist/modules/dev/dev-server/cdp/cdp.adapter.js +12 -0
  7. package/dist/modules/dev/dev-server/cdp/index.js +18 -0
  8. package/dist/modules/dev/dev-server/cdp/types.js +2 -0
  9. package/dist/modules/dev/dev-server/dev-server-checker.js +72 -0
  10. package/dist/modules/dev/dev-server/dev-server.js +269 -0
  11. package/dist/modules/dev/dev-server/inspector/device.event-reporter.js +165 -0
  12. package/dist/modules/dev/dev-server/inspector/device.js +577 -0
  13. package/dist/modules/dev/dev-server/inspector/inspector.js +204 -0
  14. package/dist/modules/dev/dev-server/inspector/types.js +2 -0
  15. package/dist/modules/dev/dev-server/inspector/wss/servers/debugger-connection.server.js +61 -0
  16. package/dist/modules/dev/dev-server/inspector/wss/servers/device-connection.server.js +64 -0
  17. package/dist/modules/dev/dev-server/plugins/devtools.plugin.js +50 -0
  18. package/dist/modules/dev/dev-server/plugins/favicon.plugin.js +19 -0
  19. package/dist/modules/dev/dev-server/plugins/multipart.plugin.js +62 -0
  20. package/dist/modules/dev/dev-server/plugins/systrace.plugin.js +28 -0
  21. package/dist/modules/dev/dev-server/plugins/types.js +2 -0
  22. package/dist/modules/dev/dev-server/plugins/wss/index.js +19 -0
  23. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-api.server.js +66 -0
  24. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-debugger.server.js +128 -0
  25. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-dev-client.server.js +75 -0
  26. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-events.server.js +198 -0
  27. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-hmr.server.js +120 -0
  28. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-message.server.js +357 -0
  29. package/dist/modules/dev/dev-server/plugins/wss/types.js +2 -0
  30. package/dist/modules/dev/dev-server/plugins/wss/web-socket-router.js +57 -0
  31. package/dist/modules/dev/dev-server/plugins/wss/web-socket-server-adapter.js +26 -0
  32. package/dist/modules/dev/dev-server/plugins/wss/web-socket-server.js +46 -0
  33. package/dist/modules/dev/dev-server/plugins/wss/wss.plugin.js +55 -0
  34. package/dist/modules/dev/dev-server/sybmolicate/sybmolicate.plugin.js +36 -0
  35. package/dist/modules/dev/dev-server/sybmolicate/types.js +2 -0
  36. package/dist/modules/dev/terminal/base.terminal.reporter.js +78 -0
  37. package/dist/modules/dev/terminal/terminal.reporter.js +76 -0
  38. package/dist/modules/dev/types.js +2 -0
  39. package/dist/modules/dev/utils/log.js +73 -0
  40. package/package.json +1 -1
@@ -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,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,198 @@
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, clientId) {
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({
116
+ ...toSerialize,
117
+ clientId,
118
+ });
119
+ }
120
+ catch (error) {
121
+ this.fastify.log.error({ msg: "Failed to serialize", error });
122
+ return undefined;
123
+ }
124
+ }
125
+ /**
126
+ * Broadcast event to all connected clients.
127
+ *
128
+ * @param event Event message to broadcast.
129
+ */
130
+ broadcastEvent(event) {
131
+ if (!this.clients.size) {
132
+ return;
133
+ }
134
+ const serialized = this.serializeMessage(event);
135
+ if (!serialized) {
136
+ console.log("Failed to serialize event");
137
+ return;
138
+ }
139
+ for (const [clientId, socket] of this.clients.entries()) {
140
+ try {
141
+ const clientMessage = this.serializeMessage(event, clientId);
142
+ if (!clientMessage) {
143
+ continue;
144
+ }
145
+ socket.send(clientMessage);
146
+ }
147
+ catch (error) {
148
+ this.fastify.log.error({
149
+ msg: "Failed to send broadcast to client",
150
+ clientId,
151
+ error,
152
+ _skipBroadcast: true,
153
+ });
154
+ }
155
+ }
156
+ }
157
+ /**
158
+ * Process new client's WebSocket connection.
159
+ *
160
+ * @param socket Incoming WebSocket connection.
161
+ */
162
+ onConnection(socket) {
163
+ const clientId = `client#${this.nextClientId++}`;
164
+ this.clients.set(clientId, socket);
165
+ this.fastify.log.debug({ msg: "Events client connected", clientId });
166
+ const onClose = () => {
167
+ this.fastify.log.debug({ msg: "Events client disconnected", clientId });
168
+ socket.removeAllListeners();
169
+ this.clients.delete(clientId);
170
+ };
171
+ socket.addEventListener("error", onClose);
172
+ socket.addEventListener("close", onClose);
173
+ socket.addEventListener("message", (event) => {
174
+ const message = this.parseMessage(event.data.toString());
175
+ if (!message) {
176
+ return;
177
+ }
178
+ if (message.type === "command") {
179
+ try {
180
+ this.config.webSocketMessageServer.broadcast(message.command, message.params);
181
+ }
182
+ catch (error) {
183
+ this.fastify.log.error({
184
+ msg: "Failed to forward message to clients",
185
+ error,
186
+ });
187
+ }
188
+ }
189
+ else {
190
+ this.fastify.log.error({
191
+ msg: "Unknown message type",
192
+ message,
193
+ });
194
+ }
195
+ });
196
+ }
197
+ }
198
+ exports.WebSocketEventsServer = WebSocketEventsServer;
@@ -0,0 +1,120 @@
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
+ const sendFn = (...args) => {
97
+ // @ts-ignore
98
+ socket.send(...args);
99
+ };
100
+ const hmrClient = await this.hmrServer.onClientConnect(requestUrl, sendFn);
101
+ socket.on("error", (error) => {
102
+ this.fastify.log.error({
103
+ msg: "HMR client error",
104
+ error,
105
+ });
106
+ this.hmrServer.onClientError(hmrClient, error);
107
+ });
108
+ socket.on("close", () => {
109
+ this.fastify.log.debug({
110
+ msg: "HMR client disconnected",
111
+ ...hmrClient,
112
+ });
113
+ this.hmrServer.onClientDisconnect(hmrClient);
114
+ });
115
+ socket.on("message", (data) => {
116
+ this.hmrServer.onClientMessage(hmrClient, data, sendFn);
117
+ });
118
+ }
119
+ }
120
+ exports.WebSocketHMRServer = WebSocketHMRServer;