@@ -27,7 +27,6 @@ import Device from './Device';
2727import EventLoopPerfTracker from './EventLoopPerfTracker' ;
2828import InspectorProxyHeartbeat from './InspectorProxyHeartbeat' ;
2929import nullthrows from 'nullthrows' ;
30- import url from 'url' ;
3130import WS from 'ws' ;
3231
3332const debug = require ( 'debug' ) ( 'Metro:InspectorProxy' ) ;
@@ -205,7 +204,7 @@ export default class InspectorProxy implements InspectorProxyQueries {
205204 response : ServerResponse ,
206205 next : ( ?Error ) = > unknown ,
207206 ) {
208- const pathname = url . parse ( request . url ) . pathname ;
207+ const pathname = new URL ( request . url , 'http://example.com' ) . pathname ;
209208 if (
210209 pathname === PAGES_LIST_JSON_URL ||
211210 pathname === PAGES_LIST_JSON_URL_2
@@ -336,12 +335,11 @@ export default class InspectorProxy implements InspectorProxyQueries {
336335 const wssTimestamp = Date . now ( ) ;
337336
338337 const fallbackDeviceId = String ( this . #deviceCounter++ ) ;
339-
340- const query = url . parse ( req . url || '' , true ) . query || { } ;
341- const deviceId = query . device || fallbackDeviceId ;
342- const deviceName = query . name || 'Unknown' ;
343- const appName = query . app || 'Unknown' ;
344- const isProfilingBuild = query . profiling === 'true' ;
338+ const query = tryParseQueryParams ( req . url ) ;
339+ const deviceId = query ?. get ( 'device' ) || fallbackDeviceId ;
340+ const deviceName = query ?. get ( 'name' ) || 'Unknown' ;
341+ const appName = query ?. get ( 'app' ) || 'Unknown' ;
342+ const isProfilingBuild = query ?. get ( 'profiling' ) === 'true' ;
345343
346344 try {
347345 const deviceRelativeBaseUrl =
@@ -504,9 +502,9 @@ export default class InspectorProxy implements InspectorProxyQueries {
504502 wss . on ( 'connection' , async ( socket : WS , req ) => {
505503 const wssTimestamp = Date . now ( ) ;
506504
507- const query = url . parse ( req . url || '' , true ) . query || { } ;
508- const deviceId = query . device ;
509- const pageId = query . page ;
505+ const query = tryParseQueryParams ( req . url ) ;
506+ const deviceId = query ?. get ( ' device' ) || null ;
507+ const pageId = query ?. get ( ' page' ) || null ;
510508 const debuggerRelativeBaseUrl =
511509 getBaseUrlFromRequest ( req ) ?? this . #serverBaseUrl;
512510 const device : Device | void = deviceId
@@ -593,7 +591,8 @@ export default class InspectorProxy implements InspectorProxyQueries {
593591
594592 device . handleDebuggerConnection ( socket , pageId , {
595593 debuggerRelativeBaseUrl,
596- userAgent : req . headers [ 'user-agent' ] ?? query . userAgent ?? null ,
594+ userAgent :
595+ req . headers [ 'user-agent' ] ?? query ?. get ( 'userAgent' ) ?? null ,
597596 } ) ;
598597
599598 socket . on ( 'close' , ( code : number , reason : string ) => {
@@ -619,7 +618,7 @@ export default class InspectorProxy implements InspectorProxyQueries {
619618 "Connection failed to be established with DevTools for app='%s' on device='%s' and device id='%s' with error:" ,
620619 device ?. getApp ( ) || 'unknown' ,
621620 device ?. getName ( ) || 'unknown' ,
622- deviceId ,
621+ deviceId || 'unknown' ,
623622 error ,
624623 ) ;
625624 socket . close ( INTERNAL_ERROR_CODE , error ?. toString ( ) ?? 'Unknown error' ) ;
@@ -634,3 +633,11 @@ export default class InspectorProxy implements InspectorProxyQueries {
634633 return wss ;
635634 }
636635}
636+
637+ function tryParseQueryParams ( urlString : string ) : ?URLSearchParams {
638+ try {
639+ return new URL ( urlString , 'http://example.com' ) . searchParams ;
640+ } catch {
641+ return null ;
642+ }
643+ }
0 commit comments