@teardown/cli 1.2.38 → 2.0.41

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 (182) hide show
  1. package/bin/teardown.js +11 -1
  2. package/package.json +77 -57
  3. package/src/cli/commands/init.ts +254 -0
  4. package/src/cli/commands/plugins.ts +93 -0
  5. package/src/cli/commands/prebuild.ts +168 -0
  6. package/src/cli/commands/run.ts +727 -0
  7. package/src/cli/commands/start.ts +87 -0
  8. package/src/cli/commands/validate.ts +62 -0
  9. package/src/cli/index.ts +59 -0
  10. package/src/config/index.ts +45 -0
  11. package/src/config/loader.ts +366 -0
  12. package/src/config/schema.ts +235 -0
  13. package/src/config/types.ts +322 -0
  14. package/src/index.ts +177 -0
  15. package/src/pipeline/cache.ts +179 -0
  16. package/src/pipeline/index.ts +10 -0
  17. package/src/pipeline/stages.ts +692 -0
  18. package/src/plugins/base.ts +370 -0
  19. package/src/plugins/capabilities/biometrics.ts +64 -0
  20. package/src/plugins/capabilities/bluetooth.ts +86 -0
  21. package/src/plugins/capabilities/calendar.ts +57 -0
  22. package/src/plugins/capabilities/camera.ts +77 -0
  23. package/src/plugins/capabilities/contacts.ts +57 -0
  24. package/src/plugins/capabilities/deep-linking.ts +124 -0
  25. package/src/plugins/capabilities/firebase.ts +138 -0
  26. package/src/plugins/capabilities/index.ts +96 -0
  27. package/src/plugins/capabilities/location.ts +87 -0
  28. package/src/plugins/capabilities/photo-library.ts +80 -0
  29. package/src/plugins/capabilities/push-notifications.ts +98 -0
  30. package/src/plugins/capabilities/sign-in-with-apple.ts +53 -0
  31. package/src/plugins/context.ts +220 -0
  32. package/src/plugins/index.ts +26 -0
  33. package/src/plugins/resolver.ts +321 -0
  34. package/src/templates/generator.ts +507 -0
  35. package/src/templates/index.ts +9 -0
  36. package/src/templates/paths.ts +25 -0
  37. package/src/transformers/android/gradle.ts +400 -0
  38. package/src/transformers/android/index.ts +19 -0
  39. package/src/transformers/android/manifest.ts +506 -0
  40. package/src/transformers/index.ts +39 -0
  41. package/src/transformers/ios/entitlements.ts +283 -0
  42. package/src/transformers/ios/index.ts +10 -0
  43. package/src/transformers/ios/pbxproj.ts +267 -0
  44. package/src/transformers/ios/plist.ts +198 -0
  45. package/src/utils/fs.ts +429 -0
  46. package/src/utils/index.ts +21 -0
  47. package/src/utils/logger.ts +203 -0
  48. package/templates/.gitignore +63 -0
  49. package/templates/Gemfile +3 -0
  50. package/templates/android/app/build.gradle.kts +97 -0
  51. package/templates/android/app/proguard-rules.pro +10 -0
  52. package/templates/android/app/src/main/AndroidManifest.xml +26 -0
  53. package/templates/android/app/src/main/java/com/appname/MainActivity.kt +22 -0
  54. package/templates/android/app/src/main/java/com/appname/MainApplication.kt +44 -0
  55. package/templates/android/app/src/main/res/values/strings.xml +3 -0
  56. package/templates/android/app/src/main/res/values/styles.xml +7 -0
  57. package/templates/android/build.gradle.kts +44 -0
  58. package/templates/android/gradle.properties +39 -0
  59. package/templates/android/settings.gradle.kts +12 -0
  60. package/templates/babel.config.js +15 -0
  61. package/templates/index.js +7 -0
  62. package/templates/ios/.xcode.env +11 -0
  63. package/templates/ios/AppName/AppDelegate.swift +25 -0
  64. package/templates/ios/AppName/AppName-Bridging-Header.h +4 -0
  65. package/templates/ios/AppName/AppName.entitlements +6 -0
  66. package/templates/ios/AppName/Images.xcassets/AppIcon.appiconset/Contents.json +35 -0
  67. package/templates/ios/AppName/Images.xcassets/Contents.json +6 -0
  68. package/templates/ios/AppName/Info.plist +49 -0
  69. package/templates/ios/AppName/LaunchScreen.storyboard +38 -0
  70. package/templates/ios/AppName.xcodeproj/project.pbxproj +402 -0
  71. package/templates/ios/AppName.xcodeproj/xcshareddata/xcschemes/AppName.xcscheme +78 -0
  72. package/templates/ios/Podfile +35 -0
  73. package/templates/metro.config.js +41 -0
  74. package/templates/package.json +57 -0
  75. package/templates/react-native.config.js +8 -0
  76. package/templates/src/app/index.tsx +34 -0
  77. package/templates/src/assets/fonts/.gitkeep +1 -0
  78. package/templates/src/assets/images/.gitkeep +1 -0
  79. package/templates/src/components/ui/accordion.tsx +114 -0
  80. package/templates/src/components/ui/avatar.tsx +75 -0
  81. package/templates/src/components/ui/button.tsx +93 -0
  82. package/templates/src/components/ui/card.tsx +120 -0
  83. package/templates/src/components/ui/checkbox.tsx +133 -0
  84. package/templates/src/components/ui/chip.tsx +95 -0
  85. package/templates/src/components/ui/dialog.tsx +134 -0
  86. package/templates/src/components/ui/divider.tsx +67 -0
  87. package/templates/src/components/ui/error-view.tsx +82 -0
  88. package/templates/src/components/ui/form-field.tsx +101 -0
  89. package/templates/src/components/ui/index.ts +100 -0
  90. package/templates/src/components/ui/popover.tsx +92 -0
  91. package/templates/src/components/ui/pressable-feedback.tsx +88 -0
  92. package/templates/src/components/ui/radio-group.tsx +153 -0
  93. package/templates/src/components/ui/scroll-shadow.tsx +108 -0
  94. package/templates/src/components/ui/select.tsx +165 -0
  95. package/templates/src/components/ui/skeleton-group.tsx +97 -0
  96. package/templates/src/components/ui/skeleton.tsx +87 -0
  97. package/templates/src/components/ui/spinner.tsx +87 -0
  98. package/templates/src/components/ui/surface.tsx +95 -0
  99. package/templates/src/components/ui/switch.tsx +124 -0
  100. package/templates/src/components/ui/tabs.tsx +154 -0
  101. package/templates/src/components/ui/text-field.tsx +106 -0
  102. package/templates/src/components/ui/toast.tsx +129 -0
  103. package/templates/src/contexts/.gitkeep +2 -0
  104. package/templates/src/core/clients/api/api.client.ts +113 -0
  105. package/templates/src/core/clients/api/index.ts +1 -0
  106. package/templates/src/core/clients/storage/index.ts +1 -0
  107. package/templates/src/core/clients/storage/storage.client.ts +121 -0
  108. package/templates/src/core/constants/index.ts +19 -0
  109. package/templates/src/core/core.ts +40 -0
  110. package/templates/src/core/index.ts +10 -0
  111. package/templates/src/global.css +87 -0
  112. package/templates/src/hooks/index.ts +6 -0
  113. package/templates/src/hooks/use-debounce.ts +23 -0
  114. package/templates/src/hooks/use-mounted.ts +21 -0
  115. package/templates/src/index.ts +28 -0
  116. package/templates/src/lib/index.ts +5 -0
  117. package/templates/src/lib/utils.ts +115 -0
  118. package/templates/src/modules/.gitkeep +6 -0
  119. package/templates/src/navigation/index.ts +8 -0
  120. package/templates/src/navigation/navigation-provider.tsx +36 -0
  121. package/templates/src/navigation/router.tsx +137 -0
  122. package/templates/src/providers/app.provider.tsx +29 -0
  123. package/templates/src/providers/index.ts +5 -0
  124. package/templates/src/routes/(tabs)/_layout.tsx +42 -0
  125. package/templates/src/routes/(tabs)/explore.tsx +161 -0
  126. package/templates/src/routes/(tabs)/home.tsx +138 -0
  127. package/templates/src/routes/(tabs)/profile.tsx +151 -0
  128. package/templates/src/routes/_layout.tsx +18 -0
  129. package/templates/src/routes/settings.tsx +194 -0
  130. package/templates/src/screens/auth/index.ts +6 -0
  131. package/templates/src/screens/auth/login.tsx +165 -0
  132. package/templates/src/screens/auth/register.tsx +203 -0
  133. package/templates/src/screens/home.tsx +204 -0
  134. package/templates/src/screens/index.ts +17 -0
  135. package/templates/src/screens/profile.tsx +210 -0
  136. package/templates/src/screens/settings.tsx +216 -0
  137. package/templates/src/screens/welcome.tsx +101 -0
  138. package/templates/src/styles/index.ts +103 -0
  139. package/templates/src/types/common.ts +71 -0
  140. package/templates/src/types/index.ts +5 -0
  141. package/templates/tsconfig.json +14 -0
  142. package/README.md +0 -15
  143. package/assets/favicon.ico +0 -0
  144. package/dist/commands/dev/dev.js +0 -55
  145. package/dist/commands/init/init-teardown.js +0 -26
  146. package/dist/index.js +0 -20
  147. package/dist/modules/dev/dev-menu/keyboard-handler.js +0 -138
  148. package/dist/modules/dev/dev-menu/open-debugger-keyboard-handler.js +0 -105
  149. package/dist/modules/dev/dev-server/cdp/cdp.adapter.js +0 -12
  150. package/dist/modules/dev/dev-server/cdp/index.js +0 -18
  151. package/dist/modules/dev/dev-server/cdp/types.js +0 -2
  152. package/dist/modules/dev/dev-server/dev-server-checker.js +0 -72
  153. package/dist/modules/dev/dev-server/dev-server.js +0 -269
  154. package/dist/modules/dev/dev-server/inspector/device.event-reporter.js +0 -165
  155. package/dist/modules/dev/dev-server/inspector/device.js +0 -577
  156. package/dist/modules/dev/dev-server/inspector/inspector.js +0 -204
  157. package/dist/modules/dev/dev-server/inspector/types.js +0 -2
  158. package/dist/modules/dev/dev-server/inspector/wss/servers/debugger-connection.server.js +0 -61
  159. package/dist/modules/dev/dev-server/inspector/wss/servers/device-connection.server.js +0 -64
  160. package/dist/modules/dev/dev-server/plugins/devtools.plugin.js +0 -50
  161. package/dist/modules/dev/dev-server/plugins/favicon.plugin.js +0 -19
  162. package/dist/modules/dev/dev-server/plugins/multipart.plugin.js +0 -62
  163. package/dist/modules/dev/dev-server/plugins/systrace.plugin.js +0 -28
  164. package/dist/modules/dev/dev-server/plugins/types.js +0 -2
  165. package/dist/modules/dev/dev-server/plugins/wss/index.js +0 -19
  166. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-api.server.js +0 -66
  167. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-debugger.server.js +0 -128
  168. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-dev-client.server.js +0 -75
  169. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-events.server.js +0 -198
  170. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-hmr.server.js +0 -120
  171. package/dist/modules/dev/dev-server/plugins/wss/servers/web-socket-message.server.js +0 -357
  172. package/dist/modules/dev/dev-server/plugins/wss/types.js +0 -2
  173. package/dist/modules/dev/dev-server/plugins/wss/web-socket-router.js +0 -57
  174. package/dist/modules/dev/dev-server/plugins/wss/web-socket-server-adapter.js +0 -26
  175. package/dist/modules/dev/dev-server/plugins/wss/web-socket-server.js +0 -46
  176. package/dist/modules/dev/dev-server/plugins/wss/wss.plugin.js +0 -55
  177. package/dist/modules/dev/dev-server/sybmolicate/sybmolicate.plugin.js +0 -36
  178. package/dist/modules/dev/dev-server/sybmolicate/types.js +0 -2
  179. package/dist/modules/dev/terminal/base.terminal.reporter.js +0 -78
  180. package/dist/modules/dev/terminal/terminal.reporter.js +0 -76
  181. package/dist/modules/dev/types.js +0 -2
  182. package/dist/modules/dev/utils/log.js +0 -73
@@ -1,577 +0,0 @@
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.Device = void 0;
7
- const node_fs_1 = __importDefault(require("node:fs"));
8
- const node_path_1 = __importDefault(require("node:path"));
9
- const node_url_1 = require("node:url");
10
- const ws_1 = __importDefault(require("ws"));
11
- const device_event_reporter_1 = require("./device.event-reporter");
12
- const debug = require("debug")("Metro:InspectorProxy");
13
- const PAGES_POLLING_INTERVAL = 1000;
14
- // Constants for host rewriting
15
- const REWRITE_HOSTS_TO_LOCALHOST = [
16
- "127.0.0.1",
17
- "10.0.2.2",
18
- "10.0.3.2",
19
- ];
20
- const FILE_PREFIX = "file://";
21
- const REACT_NATIVE_RELOADABLE_PAGE_ID = "-1";
22
- class Device {
23
- id;
24
- name;
25
- app;
26
- messageFromDeviceQueue = Promise.resolve();
27
- deviceSocket;
28
- pages = new Map();
29
- debuggerConnection = null;
30
- lastConnectedLegacyReactNativePage = null;
31
- isLegacyPageReloading = false;
32
- lastGetPagesMessage = "";
33
- scriptIdToSourcePathMapping = new Map();
34
- projectRoot;
35
- deviceEventReporter;
36
- pagesPollingIntervalId;
37
- createCustomMessageHandler;
38
- connectedPageIds = new Set();
39
- constructor(id, name, app, socket, projectRoot, eventReporter) {
40
- this.id = id;
41
- this.name = name;
42
- this.app = app;
43
- this.deviceSocket = socket;
44
- this.projectRoot = projectRoot;
45
- this.deviceEventReporter =
46
- eventReporter != null
47
- ? new device_event_reporter_1.DeviceEventReporter(eventReporter, {
48
- deviceId: id,
49
- deviceName: name,
50
- appId: app,
51
- })
52
- : null;
53
- this.createCustomMessageHandler = null;
54
- // Setup message handling
55
- this.deviceSocket.on("message", (message) => {
56
- this.messageFromDeviceQueue = this.messageFromDeviceQueue
57
- .then(async () => {
58
- const parsedMessage = JSON.parse(message);
59
- if (parsedMessage.event === "getPages") {
60
- if (message !== this.lastGetPagesMessage) {
61
- debug(`(Debugger) (Proxy) <- (Device), getPages ping has changed: ${message}`);
62
- this.lastGetPagesMessage = message;
63
- }
64
- }
65
- else {
66
- debug(`(Debugger) (Proxy) <- (Device): ${message}`);
67
- }
68
- await this.handleMessageFromDevice(parsedMessage);
69
- })
70
- .catch((error) => {
71
- debug("%O\nHandling device message: %s", error, message);
72
- try {
73
- this.deviceEventReporter?.logProxyMessageHandlingError("device", error, message);
74
- }
75
- catch (loggingError) {
76
- debug("Error logging message handling error to reporter: %O", loggingError);
77
- }
78
- });
79
- });
80
- // Setup polling
81
- this.pagesPollingIntervalId = setInterval(() => this.sendMessageToDevice({ event: "getPages" }), PAGES_POLLING_INTERVAL);
82
- // Handle socket close
83
- this.deviceSocket.on("close", () => {
84
- if (socket === this.deviceSocket) {
85
- this.deviceEventReporter?.logDisconnection("device");
86
- this.terminateDebuggerConnection();
87
- clearInterval(this.pagesPollingIntervalId);
88
- }
89
- });
90
- }
91
- terminateDebuggerConnection() {
92
- const debuggerConnection = this.debuggerConnection;
93
- if (debuggerConnection) {
94
- this.sendDisconnectEventToDevice(this.mapToDevicePageId(debuggerConnection.pageId));
95
- debuggerConnection.socket.close();
96
- this.debuggerConnection = null;
97
- }
98
- }
99
- dangerouslyRecreateDevice(id, name, app, socket, projectRoot, eventReporter) {
100
- invariant(id === this.id, "dangerouslyRecreateDevice() can only be used for the same device ID");
101
- const oldDebugger = this.debuggerConnection;
102
- if (this.app !== app || this.name !== name) {
103
- this.deviceSocket.close();
104
- this.terminateDebuggerConnection();
105
- }
106
- this.debuggerConnection = null;
107
- if (oldDebugger) {
108
- oldDebugger.socket.removeAllListeners();
109
- this.deviceSocket.close();
110
- this.handleDebuggerConnection(oldDebugger.socket, oldDebugger.pageId, {
111
- userAgent: oldDebugger.userAgent,
112
- });
113
- }
114
- this.id = id;
115
- this.name = name;
116
- this.app = app;
117
- this.deviceSocket = socket;
118
- this.projectRoot = projectRoot;
119
- this.deviceEventReporter = eventReporter
120
- ? new device_event_reporter_1.DeviceEventReporter(eventReporter, {
121
- deviceId: id,
122
- deviceName: name,
123
- appId: app,
124
- })
125
- : null;
126
- }
127
- getName() {
128
- return this.name;
129
- }
130
- getApp() {
131
- return this.app;
132
- }
133
- getPagesList() {
134
- if (this.lastConnectedLegacyReactNativePage) {
135
- return [...this.pages.values(), this.createSyntheticPage()];
136
- }
137
- return [...this.pages.values()];
138
- }
139
- handleDebuggerConnection(socket, pageId, metadata) {
140
- const page = pageId === REACT_NATIVE_RELOADABLE_PAGE_ID
141
- ? this.createSyntheticPage()
142
- : this.pages.get(pageId);
143
- if (!page) {
144
- debug(`Got new debugger connection for page ${pageId} of ${this.name}, but no such page exists`);
145
- socket.close();
146
- return;
147
- }
148
- this.deviceEventReporter?.logDisconnection("debugger");
149
- this.terminateDebuggerConnection();
150
- this.deviceEventReporter?.logConnection("debugger", {
151
- pageId,
152
- frontendUserAgent: metadata.userAgent,
153
- });
154
- const debuggerInfo = {
155
- socket,
156
- prependedFilePrefix: false,
157
- pageId,
158
- userAgent: metadata.userAgent,
159
- customHandler: null,
160
- };
161
- this.debuggerConnection = debuggerInfo;
162
- if (this.debuggerConnection && this.createCustomMessageHandler) {
163
- this.debuggerConnection.customHandler = this.createCustomMessageHandler({
164
- page,
165
- debugger: {
166
- userAgent: debuggerInfo.userAgent,
167
- sendMessage: (message) => {
168
- try {
169
- const payload = JSON.stringify(message);
170
- debug(`(Debugger) <- (Proxy) (Device): ${payload}`);
171
- socket.send(payload);
172
- }
173
- catch { }
174
- },
175
- },
176
- device: {
177
- appId: this.app,
178
- id: this.id,
179
- name: this.name,
180
- sendMessage: (message) => {
181
- try {
182
- const payload = JSON.stringify({
183
- event: "wrappedEvent",
184
- payload: {
185
- pageId: this.mapToDevicePageId(pageId),
186
- wrappedEvent: JSON.stringify(message),
187
- },
188
- });
189
- debug(`(Debugger) -> (Proxy) (Device): ${payload}`);
190
- this.deviceSocket.send(payload);
191
- }
192
- catch { }
193
- },
194
- },
195
- });
196
- }
197
- this.sendConnectEventToDevice(this.mapToDevicePageId(pageId));
198
- socket.on("message", (message) => {
199
- debug(`(Debugger) -> (Proxy) (Device): ${message}`);
200
- const debuggerRequest = JSON.parse(message);
201
- this.deviceEventReporter?.logRequest(debuggerRequest, "debugger", {
202
- pageId: this.debuggerConnection?.pageId ?? null,
203
- frontendUserAgent: metadata.userAgent,
204
- prefersFuseboxFrontend: this.isPageFuseboxFrontend(this.debuggerConnection?.pageId ?? null),
205
- });
206
- if (this.debuggerConnection?.customHandler?.handleDebuggerMessage(debuggerRequest) === true) {
207
- return;
208
- }
209
- if (!this.pageHasCapability(page, "nativeSourceCodeFetching")) {
210
- const processedReq = this.interceptClientMessageForSourceFetching(debuggerRequest, debuggerInfo, socket);
211
- if (processedReq) {
212
- this.sendMessageToDevice({
213
- event: "wrappedEvent",
214
- payload: {
215
- pageId: this.mapToDevicePageId(pageId),
216
- wrappedEvent: JSON.stringify(processedReq),
217
- },
218
- });
219
- }
220
- }
221
- });
222
- socket.on("close", () => {
223
- debug(`Debugger for page ${pageId} and ${this.name} disconnected.`);
224
- this.deviceEventReporter?.logDisconnection("debugger");
225
- if (this.debuggerConnection?.socket === socket) {
226
- this.terminateDebuggerConnection();
227
- }
228
- });
229
- const sendFunc = socket.send.bind(socket);
230
- socket.send = (message) => {
231
- debug(`(Debugger) <- (Proxy) (Device): ${message}`);
232
- return sendFunc(message);
233
- };
234
- }
235
- sendConnectEventToDevice(devicePageId) {
236
- if (this.connectedPageIds.has(devicePageId)) {
237
- return;
238
- }
239
- this.connectedPageIds.add(devicePageId);
240
- this.sendMessageToDevice({
241
- event: "connect",
242
- payload: { pageId: devicePageId },
243
- });
244
- }
245
- sendDisconnectEventToDevice(devicePageId) {
246
- if (!this.connectedPageIds.has(devicePageId)) {
247
- return;
248
- }
249
- this.connectedPageIds.delete(devicePageId);
250
- this.sendMessageToDevice({
251
- event: "disconnect",
252
- payload: { pageId: devicePageId },
253
- });
254
- }
255
- pageHasCapability(page, flag) {
256
- return page.capabilities[flag] === true;
257
- }
258
- createSyntheticPage() {
259
- return {
260
- id: REACT_NATIVE_RELOADABLE_PAGE_ID,
261
- title: "React Native Experimental (Improved Chrome Reloads)",
262
- vm: "don't use",
263
- app: this.app,
264
- capabilities: {},
265
- };
266
- }
267
- async handleMessageFromDevice(message) {
268
- if (message.event === "getPages") {
269
- this.pages = new Map(message.payload.map((page) => {
270
- const { capabilities = {}, ...rest } = page;
271
- return [
272
- rest.id,
273
- {
274
- ...rest,
275
- capabilities,
276
- },
277
- ];
278
- }));
279
- for (const page of this.pages.values()) {
280
- if (this.pageHasCapability(page, "nativePageReloads")) {
281
- continue;
282
- }
283
- if (page.title.includes("React")) {
284
- if (page.id !== this.lastConnectedLegacyReactNativePage?.id) {
285
- this.newLegacyReactNativePage(page);
286
- break;
287
- }
288
- }
289
- }
290
- }
291
- else if (message.event === "disconnect") {
292
- const pageId = message.payload.pageId;
293
- const page = this.pages.get(pageId);
294
- if (page && this.pageHasCapability(page, "nativePageReloads")) {
295
- return;
296
- }
297
- const debuggerSocket = this.debuggerConnection?.socket;
298
- if (debuggerSocket && debuggerSocket.readyState === ws_1.default.OPEN) {
299
- if (this.debuggerConnection &&
300
- this.debuggerConnection.pageId !== REACT_NATIVE_RELOADABLE_PAGE_ID) {
301
- debug(`Legacy page ${pageId} is reloading.`);
302
- debuggerSocket.send(JSON.stringify({ method: "reload" }));
303
- }
304
- }
305
- }
306
- else if (message.event === "wrappedEvent") {
307
- if (!this.debuggerConnection) {
308
- return;
309
- }
310
- const debuggerSocket = this.debuggerConnection.socket;
311
- if (!debuggerSocket || debuggerSocket.readyState !== ws_1.default.OPEN) {
312
- return;
313
- }
314
- const parsedPayload = JSON.parse(message.payload.wrappedEvent);
315
- const pageId = this.debuggerConnection.pageId;
316
- if ("id" in parsedPayload) {
317
- this.deviceEventReporter?.logResponse(parsedPayload, "device", {
318
- pageId,
319
- frontendUserAgent: this.debuggerConnection.userAgent,
320
- prefersFuseboxFrontend: this.isPageFuseboxFrontend(pageId),
321
- });
322
- }
323
- if (this.debuggerConnection.customHandler?.handleDeviceMessage(parsedPayload) === true) {
324
- return;
325
- }
326
- await this.processMessageFromDeviceLegacy(parsedPayload, this.debuggerConnection, pageId);
327
- debuggerSocket.send(JSON.stringify(parsedPayload));
328
- }
329
- }
330
- sendMessageToDevice(message) {
331
- try {
332
- if (message.event !== "getPages") {
333
- debug(`(Debugger) (Proxy) -> (Device): ${JSON.stringify(message)}`);
334
- }
335
- this.deviceSocket.send(JSON.stringify(message));
336
- }
337
- catch (error) { }
338
- }
339
- newLegacyReactNativePage(page) {
340
- debug(`React Native page updated to ${page.id}`);
341
- if (!this.debuggerConnection ||
342
- this.debuggerConnection.pageId !== REACT_NATIVE_RELOADABLE_PAGE_ID) {
343
- this.lastConnectedLegacyReactNativePage = page;
344
- return;
345
- }
346
- const oldPageId = this.lastConnectedLegacyReactNativePage?.id;
347
- this.lastConnectedLegacyReactNativePage = page;
348
- this.isLegacyPageReloading = true;
349
- if (oldPageId != null) {
350
- this.sendDisconnectEventToDevice(oldPageId);
351
- }
352
- this.sendConnectEventToDevice(page.id);
353
- const toSend = [
354
- { method: "Runtime.enable", id: 1e9 },
355
- { method: "Debugger.enable", id: 1e9 },
356
- ];
357
- for (const message of toSend) {
358
- const pageId = this.debuggerConnection?.pageId ?? null;
359
- this.deviceEventReporter?.logRequest(message, "proxy", {
360
- pageId,
361
- frontendUserAgent: this.debuggerConnection?.userAgent ?? null,
362
- prefersFuseboxFrontend: this.isPageFuseboxFrontend(pageId),
363
- });
364
- this.sendMessageToDevice({
365
- event: "wrappedEvent",
366
- payload: {
367
- pageId: this.mapToDevicePageId(page.id),
368
- wrappedEvent: JSON.stringify(message),
369
- },
370
- });
371
- }
372
- }
373
- async processMessageFromDeviceLegacy(payload, debuggerInfo, pageId) {
374
- const page = pageId != null ? this.pages.get(pageId) : null;
375
- if ((!page || !this.pageHasCapability(page, "nativeSourceCodeFetching")) &&
376
- payload.method === "Debugger.scriptParsed" &&
377
- // @ts-ignore
378
- payload.params != null) {
379
- // @ts-ignore
380
- const params = payload.params;
381
- if ("sourceMapURL" in params) {
382
- for (const hostToRewrite of REWRITE_HOSTS_TO_LOCALHOST) {
383
- if (params.sourceMapURL.includes(hostToRewrite)) {
384
- // @ts-ignore
385
- payload.params.sourceMapURL = params.sourceMapURL.replace(hostToRewrite, "localhost");
386
- debuggerInfo.originalSourceURLAddress = hostToRewrite;
387
- }
388
- }
389
- const sourceMapURL = this.tryParseHTTPURL(params.sourceMapURL);
390
- if (sourceMapURL) {
391
- try {
392
- const sourceMap = await this.fetchText(sourceMapURL);
393
- // @ts-ignore
394
- payload.params.sourceMapURL = `data:application/json;charset=utf-8;base64,${Buffer.from(sourceMap).toString("base64")}`;
395
- }
396
- catch (exception) {
397
- const exceptionMessage = exception instanceof Error
398
- ? exception.message
399
- : String(exception);
400
- this.sendErrorToDebugger(`Failed to fetch source map ${params.sourceMapURL}: ${exceptionMessage}`);
401
- }
402
- }
403
- }
404
- if ("url" in params) {
405
- for (const hostToRewrite of REWRITE_HOSTS_TO_LOCALHOST) {
406
- if (params?.url?.includes(hostToRewrite)) {
407
- payload.params.url = params.url.replace(hostToRewrite, "localhost");
408
- debuggerInfo.originalSourceURLAddress = hostToRewrite;
409
- }
410
- }
411
- if (payload.params.url.match(/^[0-9a-z]+$/)) {
412
- payload.params.url = FILE_PREFIX + payload.params.url;
413
- debuggerInfo.prependedFilePrefix = true;
414
- }
415
- if (params.scriptId != null) {
416
- this.scriptIdToSourcePathMapping.set(params.scriptId, params.url);
417
- }
418
- }
419
- }
420
- if (payload.method === "Runtime.executionContextCreated" &&
421
- this.isLegacyPageReloading) {
422
- debuggerInfo.socket.send(JSON.stringify({ method: "Runtime.executionContextsCleared" }));
423
- const resumeMessage = { method: "Debugger.resume", id: 0 };
424
- this.deviceEventReporter?.logRequest(resumeMessage, "proxy", {
425
- pageId: this.debuggerConnection?.pageId ?? null,
426
- frontendUserAgent: this.debuggerConnection?.userAgent ?? null,
427
- prefersFuseboxFrontend: this.isPageFuseboxFrontend(this.debuggerConnection?.pageId ?? null),
428
- });
429
- this.sendMessageToDevice({
430
- event: "wrappedEvent",
431
- payload: {
432
- pageId: this.mapToDevicePageId(debuggerInfo.pageId),
433
- wrappedEvent: JSON.stringify(resumeMessage),
434
- },
435
- });
436
- this.isLegacyPageReloading = false;
437
- }
438
- }
439
- interceptClientMessageForSourceFetching(req, debuggerInfo, socket) {
440
- switch (req.method) {
441
- case "Debugger.setBreakpointByUrl":
442
- return this.processDebuggerSetBreakpointByUrl(req, debuggerInfo);
443
- case "Debugger.getScriptSource":
444
- this.processDebuggerGetScriptSource(req, socket);
445
- return null;
446
- default:
447
- return req;
448
- }
449
- }
450
- processDebuggerSetBreakpointByUrl(req, debuggerInfo) {
451
- if (debuggerInfo.originalSourceURLAddress != null) {
452
- const processedReq = { ...req, params: { ...req.params } };
453
- if (processedReq.params.url != null) {
454
- processedReq.params.url = processedReq.params.url.replace("localhost", debuggerInfo.originalSourceURLAddress);
455
- if (processedReq.params.url?.startsWith(FILE_PREFIX) &&
456
- debuggerInfo.prependedFilePrefix) {
457
- processedReq.params.url = processedReq.params.url.slice(FILE_PREFIX.length);
458
- }
459
- }
460
- if (processedReq.params.urlRegex != null) {
461
- processedReq.params.urlRegex = processedReq.params.urlRegex.replace(/localhost/g, debuggerInfo.originalSourceURLAddress);
462
- }
463
- return processedReq;
464
- }
465
- return req;
466
- }
467
- processDebuggerGetScriptSource(req, socket) {
468
- const sendSuccessResponse = (scriptSource) => {
469
- const result = { scriptSource };
470
- const response = {
471
- id: req.id,
472
- result,
473
- };
474
- socket.send(JSON.stringify(response));
475
- const pageId = this.debuggerConnection?.pageId ?? null;
476
- this.deviceEventReporter?.logResponse(response, "proxy", {
477
- pageId,
478
- frontendUserAgent: this.debuggerConnection?.userAgent ?? null,
479
- prefersFuseboxFrontend: this.isPageFuseboxFrontend(pageId),
480
- });
481
- };
482
- const sendErrorResponse = (error) => {
483
- const result = { error: { message: error } };
484
- const response = { id: req.id, result };
485
- socket.send(JSON.stringify(response));
486
- this.sendErrorToDebugger(error);
487
- const pageId = this.debuggerConnection?.pageId ?? null;
488
- this.deviceEventReporter?.logResponse(response, "proxy", {
489
- pageId,
490
- frontendUserAgent: this.debuggerConnection?.userAgent ?? null,
491
- prefersFuseboxFrontend: this.isPageFuseboxFrontend(pageId),
492
- });
493
- };
494
- const pathToSource = this.scriptIdToSourcePathMapping.get(req.params.scriptId);
495
- if (pathToSource != null) {
496
- const httpURL = this.tryParseHTTPURL(pathToSource);
497
- if (httpURL) {
498
- this.fetchText(httpURL).then((text) => sendSuccessResponse(text), (err) => sendErrorResponse(`Failed to fetch source url ${pathToSource}: ${err.message}`));
499
- }
500
- else {
501
- let file;
502
- try {
503
- file = node_fs_1.default.readFileSync(node_path_1.default.resolve(this.projectRoot, pathToSource), "utf8");
504
- sendSuccessResponse(file);
505
- }
506
- catch (err) {
507
- const exceptionMessage = err instanceof Error ? err.message : String(err);
508
- sendErrorResponse(`Failed to fetch source file ${pathToSource}: ${exceptionMessage}`);
509
- }
510
- }
511
- }
512
- }
513
- mapToDevicePageId(pageId) {
514
- if (pageId === REACT_NATIVE_RELOADABLE_PAGE_ID &&
515
- this.lastConnectedLegacyReactNativePage != null) {
516
- return this.lastConnectedLegacyReactNativePage.id;
517
- }
518
- return pageId;
519
- }
520
- tryParseHTTPURL(url) {
521
- let parsedURL = null;
522
- try {
523
- parsedURL = new node_url_1.URL(url);
524
- }
525
- catch { }
526
- const protocol = parsedURL?.protocol;
527
- if (protocol !== "http:" && protocol !== "https:") {
528
- parsedURL = null;
529
- }
530
- return parsedURL;
531
- }
532
- async fetchText(url) {
533
- const response = await fetch(url.toString());
534
- if (!response.ok) {
535
- throw new Error(`HTTP ${response.status} ${response.statusText}`);
536
- }
537
- const text = await response.text();
538
- if (text.length > 350000000) {
539
- throw new Error("file too large to fetch via HTTP");
540
- }
541
- return text;
542
- }
543
- sendErrorToDebugger(message) {
544
- const debuggerSocket = this.debuggerConnection?.socket;
545
- if (debuggerSocket && debuggerSocket.readyState === ws_1.default.OPEN) {
546
- debuggerSocket.send(JSON.stringify({
547
- method: "Runtime.consoleAPICalled",
548
- params: {
549
- args: [
550
- {
551
- type: "string",
552
- value: message,
553
- },
554
- ],
555
- executionContextId: 0,
556
- type: "error",
557
- },
558
- }));
559
- }
560
- }
561
- isPageFuseboxFrontend(pageId) {
562
- const page = pageId == null ? null : this.pages.get(pageId);
563
- if (page == null) {
564
- return null;
565
- }
566
- return this.pageHasCapability(page, "prefersFuseboxFrontend");
567
- }
568
- dangerouslyGetSocket() {
569
- return this.deviceSocket;
570
- }
571
- }
572
- exports.Device = Device;
573
- const invariant = (condition, message) => {
574
- if (!condition) {
575
- throw new Error(message);
576
- }
577
- };