@react-native/dev-middleware 0.77.0-nightly-20241121-550b0c0ed → 0.77.0-nightly-20241123-1fed2cfde

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.
@@ -37,7 +37,10 @@ declare class Device {
37
37
  handleDebuggerConnection(
38
38
  socket: WS,
39
39
  pageId: string,
40
- metadata: Readonly<{ userAgent: string | null }>
40
+ $$PARAM_2$$: Readonly<{
41
+ debuggerRelativeBaseUrl: URL;
42
+ userAgent: string | null;
43
+ }>
41
44
  ): void;
42
45
  dangerouslyGetSocket(): WS;
43
46
  }
@@ -39,11 +39,6 @@ function _interopRequireDefault(e) {
39
39
  }
40
40
  const debug = require("debug")("Metro:InspectorProxy");
41
41
  const PAGES_POLLING_INTERVAL = 1000;
42
- const REWRITE_HOSTS_TO_LOCALHOST = new Set([
43
- "127.0.0.1",
44
- "10.0.2.2",
45
- "10.0.3.2",
46
- ]);
47
42
  const FILE_PREFIX = "file://";
48
43
  const REACT_NATIVE_RELOADABLE_PAGE_ID = "-1";
49
44
  class Device {
@@ -167,6 +162,7 @@ class Device {
167
162
  oldDebugger.socket.removeAllListeners();
168
163
  this.#deviceSocket.close();
169
164
  this.handleDebuggerConnection(oldDebugger.socket, oldDebugger.pageId, {
165
+ debuggerRelativeBaseUrl: oldDebugger.debuggerRelativeBaseUrl,
170
166
  userAgent: oldDebugger.userAgent,
171
167
  });
172
168
  }
@@ -185,16 +181,19 @@ class Device {
185
181
  return [...this.#pages.values()];
186
182
  }
187
183
  }
188
- handleDebuggerConnection(socket, pageId, metadata) {
184
+ handleDebuggerConnection(
185
+ socket,
186
+ pageId,
187
+ { debuggerRelativeBaseUrl, userAgent }
188
+ ) {
189
189
  const page =
190
190
  pageId === REACT_NATIVE_RELOADABLE_PAGE_ID
191
191
  ? this.#createSyntheticPage()
192
192
  : this.#pages.get(pageId);
193
193
  if (!page) {
194
194
  debug(
195
- `Got new debugger connection for page ${pageId} of ${
196
- this.#name
197
- }, but no such page exists`
195
+ `Got new debugger connection via ${debuggerRelativeBaseUrl.href} for ` +
196
+ `page ${pageId} of ${this.#name}, but no such page exists`
198
197
  );
199
198
  socket.close();
200
199
  return;
@@ -203,17 +202,21 @@ class Device {
203
202
  this.#terminateDebuggerConnection();
204
203
  this.#deviceEventReporter?.logConnection("debugger", {
205
204
  pageId,
206
- frontendUserAgent: metadata.userAgent,
205
+ frontendUserAgent: userAgent,
207
206
  });
208
207
  const debuggerInfo = {
209
208
  socket,
210
209
  prependedFilePrefix: false,
211
210
  pageId,
212
- userAgent: metadata.userAgent,
211
+ userAgent: userAgent,
213
212
  customHandler: null,
213
+ debuggerRelativeBaseUrl,
214
214
  };
215
215
  this.#debuggerConnection = debuggerInfo;
216
- debug(`Got new debugger connection for page ${pageId} of ${this.#name}`);
216
+ debug(
217
+ `Got new debugger connection via ${debuggerRelativeBaseUrl.href} for ` +
218
+ `page ${pageId} of ${this.#name}`
219
+ );
217
220
  if (this.#debuggerConnection && this.#createCustomMessageHandler) {
218
221
  this.#debuggerConnection.customHandler = this.#createCustomMessageHandler(
219
222
  {
@@ -262,7 +265,7 @@ class Device {
262
265
  const debuggerRequest = JSON.parse(message);
263
266
  this.#deviceEventReporter?.logRequest(debuggerRequest, "debugger", {
264
267
  pageId: this.#debuggerConnection?.pageId ?? null,
265
- frontendUserAgent: metadata.userAgent,
268
+ frontendUserAgent: userAgent,
266
269
  prefersFuseboxFrontend: this.#isPageFuseboxFrontend(
267
270
  this.#debuggerConnection?.pageId
268
271
  ),
@@ -492,6 +495,39 @@ class Device {
492
495
  });
493
496
  }
494
497
  }
498
+ #debuggerRelativeToDeviceRelativeUrl(
499
+ debuggerRelativeUrl,
500
+ { debuggerRelativeBaseUrl }
501
+ ) {
502
+ const deviceRelativeUrl = new URL(debuggerRelativeUrl.href);
503
+ if (debuggerRelativeUrl.origin === debuggerRelativeBaseUrl.origin) {
504
+ deviceRelativeUrl.hostname = this.#deviceRelativeBaseUrl.hostname;
505
+ deviceRelativeUrl.port = this.#deviceRelativeBaseUrl.port;
506
+ deviceRelativeUrl.protocol = this.#deviceRelativeBaseUrl.protocol;
507
+ }
508
+ return deviceRelativeUrl;
509
+ }
510
+ #deviceRelativeUrlToDebuggerRelativeUrl(
511
+ deviceRelativeUrl,
512
+ { debuggerRelativeBaseUrl }
513
+ ) {
514
+ const debuggerRelativeUrl = new URL(deviceRelativeUrl.href);
515
+ if (deviceRelativeUrl.origin === this.#deviceRelativeBaseUrl.origin) {
516
+ debuggerRelativeUrl.hostname = debuggerRelativeBaseUrl.hostname;
517
+ debuggerRelativeUrl.port = debuggerRelativeBaseUrl.port;
518
+ debuggerRelativeUrl.protocol = debuggerRelativeUrl.protocol;
519
+ }
520
+ return debuggerRelativeUrl;
521
+ }
522
+ #deviceRelativeUrlToServerRelativeUrl(deviceRelativeUrl) {
523
+ const debuggerRelativeUrl = new URL(deviceRelativeUrl.href);
524
+ if (deviceRelativeUrl.origin === this.#deviceRelativeBaseUrl.origin) {
525
+ debuggerRelativeUrl.hostname = this.#serverRelativeBaseUrl.hostname;
526
+ debuggerRelativeUrl.port = this.#serverRelativeBaseUrl.port;
527
+ debuggerRelativeUrl.protocol = this.#serverRelativeBaseUrl.protocol;
528
+ }
529
+ return debuggerRelativeUrl;
530
+ }
495
531
  async #processMessageFromDeviceLegacy(payload, debuggerInfo, pageId) {
496
532
  const page = pageId != null ? this.#pages.get(pageId) : null;
497
533
  if (
@@ -503,21 +539,15 @@ class Device {
503
539
  if ("sourceMapURL" in params) {
504
540
  const sourceMapURL = this.#tryParseHTTPURL(params.sourceMapURL);
505
541
  if (sourceMapURL) {
506
- const serverRelativeUrl = new URL(sourceMapURL.href);
507
- if (
508
- sourceMapURL.origin === this.#deviceRelativeBaseUrl.origin &&
509
- REWRITE_HOSTS_TO_LOCALHOST.has(this.#deviceRelativeBaseUrl.hostname)
510
- ) {
511
- const debuggerRelativeURL = new URL(sourceMapURL.href);
512
- debuggerRelativeURL.hostname = "localhost";
513
- serverRelativeUrl.host = this.#serverRelativeBaseUrl.host;
514
- serverRelativeUrl.protocol = this.#serverRelativeBaseUrl.protocol;
515
- debuggerInfo.originalSourceURLAddress =
516
- this.#deviceRelativeBaseUrl.hostname;
517
- payload.params.sourceMapURL = debuggerRelativeURL.href;
518
- }
542
+ payload.params.sourceMapURL =
543
+ this.#deviceRelativeUrlToDebuggerRelativeUrl(
544
+ sourceMapURL,
545
+ debuggerInfo
546
+ ).href;
519
547
  try {
520
- const sourceMap = await this.#fetchText(serverRelativeUrl);
548
+ const sourceMap = await this.#fetchText(
549
+ this.#deviceRelativeUrlToServerRelativeUrl(sourceMapURL)
550
+ );
521
551
  payload.params.sourceMapURL =
522
552
  "data:application/json;charset=utf-8;base64," +
523
553
  Buffer.from(sourceMap).toString("base64");
@@ -529,21 +559,15 @@ class Device {
529
559
  }
530
560
  }
531
561
  if ("url" in params) {
532
- const originalParamsUrl = params.url;
533
- let serverRelativeUrl = originalParamsUrl;
534
- const parsedUrl = this.#tryParseHTTPURL(originalParamsUrl);
535
- if (
536
- parsedUrl &&
537
- parsedUrl.origin === this.#deviceRelativeBaseUrl.origin &&
538
- REWRITE_HOSTS_TO_LOCALHOST.has(this.#deviceRelativeBaseUrl.hostname)
539
- ) {
540
- parsedUrl.hostname = "localhost";
541
- payload.params.url = parsedUrl.href;
542
- debuggerInfo.originalSourceURLAddress =
543
- this.#deviceRelativeBaseUrl.hostname;
544
- parsedUrl.host = this.#serverRelativeBaseUrl.host;
545
- parsedUrl.protocol = this.#serverRelativeBaseUrl.protocol;
546
- serverRelativeUrl = parsedUrl.href;
562
+ let serverRelativeUrl = params.url;
563
+ const parsedUrl = this.#tryParseHTTPURL(params.url);
564
+ if (parsedUrl) {
565
+ payload.params.url = this.#deviceRelativeUrlToDebuggerRelativeUrl(
566
+ parsedUrl,
567
+ debuggerInfo
568
+ ).href;
569
+ serverRelativeUrl =
570
+ this.#deviceRelativeUrlToServerRelativeUrl(parsedUrl).href;
547
571
  }
548
572
  if (payload.params.url.match(/^[0-9a-z]+$/)) {
549
573
  payload.params.url = FILE_PREFIX + payload.params.url;
@@ -618,37 +642,41 @@ class Device {
618
642
  }
619
643
  }
620
644
  #processDebuggerSetBreakpointByUrl(req, debuggerInfo) {
621
- if (debuggerInfo.originalSourceURLAddress != null) {
622
- const processedReq = {
623
- ...req,
624
- params: {
625
- ...req.params,
626
- },
627
- };
628
- if (processedReq.params.url != null) {
629
- processedReq.params.url = processedReq.params.url.replace(
630
- "localhost",
631
- debuggerInfo.originalSourceURLAddress
632
- );
633
- if (
634
- processedReq.params.url &&
635
- processedReq.params.url.startsWith(FILE_PREFIX) &&
636
- debuggerInfo.prependedFilePrefix
637
- ) {
638
- processedReq.params.url = processedReq.params.url.slice(
639
- FILE_PREFIX.length
640
- );
641
- }
642
- }
643
- if (processedReq.params.urlRegex != null) {
644
- processedReq.params.urlRegex = processedReq.params.urlRegex.replace(
645
- /localhost/g,
646
- debuggerInfo.originalSourceURLAddress
647
- );
645
+ const { debuggerRelativeBaseUrl, prependedFilePrefix } = debuggerInfo;
646
+ const processedReq = {
647
+ ...req,
648
+ params: {
649
+ ...req.params,
650
+ },
651
+ };
652
+ if (processedReq.params.url != null) {
653
+ const originalUrlParam = processedReq.params.url;
654
+ const httpUrl = this.#tryParseHTTPURL(originalUrlParam);
655
+ if (httpUrl) {
656
+ processedReq.params.url = this.#debuggerRelativeToDeviceRelativeUrl(
657
+ httpUrl,
658
+ debuggerInfo
659
+ ).href;
660
+ } else if (
661
+ originalUrlParam.startsWith(FILE_PREFIX) &&
662
+ prependedFilePrefix
663
+ ) {
664
+ processedReq.params.url = originalUrlParam.slice(FILE_PREFIX.length);
648
665
  }
649
- return processedReq;
650
666
  }
651
- return req;
667
+ if (
668
+ new Set(["10.0.2.2", "10.0.3.2"]).has(
669
+ this.#deviceRelativeBaseUrl.hostname
670
+ ) &&
671
+ debuggerRelativeBaseUrl.hostname === "localhost" &&
672
+ processedReq.params.urlRegex != null
673
+ ) {
674
+ processedReq.params.urlRegex = processedReq.params.urlRegex.replaceAll(
675
+ "localhost",
676
+ this.#deviceRelativeBaseUrl.hostname.replaceAll(".", "\\.")
677
+ );
678
+ }
679
+ return processedReq;
652
680
  }
653
681
  #processDebuggerGetScriptSource(req, socket) {
654
682
  const sendSuccessResponse = (scriptSource) => {
@@ -40,7 +40,8 @@ declare export default class Device {
40
40
  handleDebuggerConnection(
41
41
  socket: WS,
42
42
  pageId: string,
43
- metadata: $ReadOnly<{
43
+ $ReadOnly<{
44
+ debuggerRelativeBaseUrl: URL,
44
45
  userAgent: string | null,
45
46
  }>
46
47
  ): void;
@@ -190,6 +190,8 @@ class InspectorProxy {
190
190
  const query = _url.default.parse(req.url || "", true).query || {};
191
191
  const deviceId = query.device;
192
192
  const pageId = query.page;
193
+ const debuggerRelativeBaseUrl =
194
+ (0, _getBaseUrlFromRequest.default)(req) ?? this.#serverBaseUrl;
193
195
  if (deviceId == null || pageId == null) {
194
196
  throw new Error("Incorrect URL - must provide device and page IDs");
195
197
  }
@@ -199,6 +201,7 @@ class InspectorProxy {
199
201
  }
200
202
  this.#startHeartbeat(socket, DEBUGGER_HEARTBEAT_INTERVAL_MS);
201
203
  device.handleDebuggerConnection(socket, pageId, {
204
+ debuggerRelativeBaseUrl,
202
205
  userAgent: req.headers["user-agent"] ?? query.userAgent ?? null,
203
206
  });
204
207
  } catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-native/dev-middleware",
3
- "version": "0.77.0-nightly-20241121-550b0c0ed",
3
+ "version": "0.77.0-nightly-20241123-1fed2cfde",
4
4
  "description": "Dev server middleware for React Native",
5
5
  "keywords": [
6
6
  "react-native",
@@ -23,7 +23,7 @@
23
23
  ],
24
24
  "dependencies": {
25
25
  "@isaacs/ttlcache": "^1.4.1",
26
- "@react-native/debugger-frontend": "0.77.0-nightly-20241121-550b0c0ed",
26
+ "@react-native/debugger-frontend": "0.77.0-nightly-20241123-1fed2cfde",
27
27
  "chrome-launcher": "^0.15.2",
28
28
  "chromium-edge-launcher": "^0.2.0",
29
29
  "connect": "^3.6.5",