Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/dev-middleware/src/__tests__/ServerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import connect from 'connect';
import http from 'http';
import https from 'https';
import * as selfsigned from 'selfsigned';
import url from 'url';

type CreateDevMiddlewareOptions = Parameters<typeof createDevMiddleware>[0];
type CreateServerOptions = {
Expand Down Expand Up @@ -109,7 +108,7 @@ export async function createServer(options: CreateServerOptions): Promise<{
});
app.use(middleware);
httpServer.on('upgrade', (request, socket, head) => {
const {pathname} = url.parse(request.url);
const {pathname} = new URL(request.url, 'http://example.com');
if (pathname != null && websocketEndpoints[pathname]) {
websocketEndpoints[pathname].handleUpgrade(
request,
Expand Down
33 changes: 20 additions & 13 deletions packages/dev-middleware/src/inspector-proxy/InspectorProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import Device from './Device';
import EventLoopPerfTracker from './EventLoopPerfTracker';
import InspectorProxyHeartbeat from './InspectorProxyHeartbeat';
import nullthrows from 'nullthrows';
import url from 'url';
import WS from 'ws';

const debug = require('debug')('Metro:InspectorProxy');
Expand Down Expand Up @@ -205,7 +204,7 @@ export default class InspectorProxy implements InspectorProxyQueries {
response: ServerResponse,
next: (?Error) => unknown,
) {
const pathname = url.parse(request.url).pathname;
const pathname = new URL(request.url, 'http://example.com').pathname;
if (
pathname === PAGES_LIST_JSON_URL ||
pathname === PAGES_LIST_JSON_URL_2
Expand Down Expand Up @@ -336,12 +335,11 @@ export default class InspectorProxy implements InspectorProxyQueries {
const wssTimestamp = Date.now();

const fallbackDeviceId = String(this.#deviceCounter++);

const query = url.parse(req.url || '', true).query || {};
const deviceId = query.device || fallbackDeviceId;
const deviceName = query.name || 'Unknown';
const appName = query.app || 'Unknown';
const isProfilingBuild = query.profiling === 'true';
const query = tryParseQueryParams(req.url);
const deviceId = query?.get('device') || fallbackDeviceId;
const deviceName = query?.get('name') || 'Unknown';
const appName = query?.get('app') || 'Unknown';
const isProfilingBuild = query?.get('profiling') === 'true';

try {
const deviceRelativeBaseUrl =
Expand Down Expand Up @@ -504,9 +502,9 @@ export default class InspectorProxy implements InspectorProxyQueries {
wss.on('connection', async (socket: WS, req) => {
const wssTimestamp = Date.now();

const query = url.parse(req.url || '', true).query || {};
const deviceId = query.device;
const pageId = query.page;
const query = tryParseQueryParams(req.url);
const deviceId = query?.get('device') || null;
const pageId = query?.get('page') || null;
const debuggerRelativeBaseUrl =
getBaseUrlFromRequest(req) ?? this.#serverBaseUrl;
const device: Device | void = deviceId
Expand Down Expand Up @@ -593,7 +591,8 @@ export default class InspectorProxy implements InspectorProxyQueries {

device.handleDebuggerConnection(socket, pageId, {
debuggerRelativeBaseUrl,
userAgent: req.headers['user-agent'] ?? query.userAgent ?? null,
userAgent:
req.headers['user-agent'] ?? query?.get('userAgent') ?? null,
});

socket.on('close', (code: number, reason: string) => {
Expand All @@ -619,7 +618,7 @@ export default class InspectorProxy implements InspectorProxyQueries {
"Connection failed to be established with DevTools for app='%s' on device='%s' and device id='%s' with error:",
device?.getApp() || 'unknown',
device?.getName() || 'unknown',
deviceId,
deviceId || 'unknown',
error,
);
socket.close(INTERNAL_ERROR_CODE, error?.toString() ?? 'Unknown error');
Expand All @@ -634,3 +633,11 @@ export default class InspectorProxy implements InspectorProxyQueries {
return wss;
}
}

function tryParseQueryParams(urlString: string): ?URLSearchParams {
try {
return new URL(urlString, 'http://example.com').searchParams;
} catch {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import type {IncomingMessage, ServerResponse} from 'http';

import getDevToolsFrontendUrl from '../utils/getDevToolsFrontendUrl';
import {createHash} from 'crypto';
import url from 'url';

const LEGACY_SYNTHETIC_PAGE_TITLE =
'React Native Experimental (Improved Chrome Reloads)';
Expand Down Expand Up @@ -74,7 +73,7 @@ export default function openDebuggerMiddleware({
req.method === 'POST' ||
(experiments.enableOpenDebuggerRedirect && req.method === 'GET')
) {
const parsedUrl = url.parse(req.url, true);
const {searchParams} = new URL(req.url, 'http://example.com');

const query: {
/** @deprecated Will only match legacy Hermes targets */
Expand All @@ -86,7 +85,7 @@ export default function openDebuggerMiddleware({
target?: string,
panel?: string,
...
} = parsedUrl.query;
} = Object.fromEntries(searchParams);

const targets = inspectorProxy
.getPageDescriptions({requestorRelativeBaseUrl: new URL(serverBaseUrl)})
Expand Down
Loading