diff --git a/packages/dev-middleware/src/inspector-proxy/Device.js b/packages/dev-middleware/src/inspector-proxy/Device.js index 9666fa222ed927..fba8ecd13ce622 100644 --- a/packages/dev-middleware/src/inspector-proxy/Device.js +++ b/packages/dev-middleware/src/inspector-proxy/Device.js @@ -69,7 +69,7 @@ export default class Device { _deviceSocket: WS; // Stores last list of device's pages. - _pages: Array; + _pages: $ReadOnlyArray; // Stores information about currently connected debugger (if any). _debuggerConnection: ?DebuggerInfo = null; @@ -152,7 +152,7 @@ export default class Device { return this._app; } - getPagesList(): Array { + getPagesList(): $ReadOnlyArray { if (this._lastConnectedReactNativePage) { const reactNativeReloadablePage = { id: REACT_NATIVE_RELOADABLE_PAGE_ID, @@ -216,18 +216,18 @@ export default class Device { pageId: this._debuggerConnection?.pageId ?? null, frontendUserAgent: metadata.userAgent, }); - const handled = this._interceptMessageFromDebugger( + const processedReq = this._interceptMessageFromDebugger( debuggerRequest, debuggerInfo, socket, ); - if (!handled) { + if (processedReq) { this._sendMessageToDevice({ event: 'wrappedEvent', payload: { pageId: this._mapToDevicePageId(pageId), - wrappedEvent: JSON.stringify(debuggerRequest), + wrappedEvent: JSON.stringify(processedReq), }, }); } @@ -545,46 +545,51 @@ export default class Device { req: DebuggerRequest, debuggerInfo: DebuggerInfo, socket: WS, - ): boolean { + ): ?DebuggerRequest { if (req.method === 'Debugger.setBreakpointByUrl') { - this._processDebuggerSetBreakpointByUrl(req, debuggerInfo); + return this._processDebuggerSetBreakpointByUrl(req, debuggerInfo); } else if (req.method === 'Debugger.getScriptSource') { this._processDebuggerGetScriptSource(req, socket); - return true; + return null; } - return false; + return req; } _processDebuggerSetBreakpointByUrl( req: SetBreakpointByUrlRequest, debuggerInfo: DebuggerInfo, - ) { + ): SetBreakpointByUrlRequest { // If we replaced Android emulator's address to localhost we need to change it back. if (debuggerInfo.originalSourceURLAddress != null) { - if (req.params.url != null) { - req.params.url = req.params.url.replace( + const processedReq = {...req, params: {...req.params}}; + if (processedReq.params.url != null) { + processedReq.params.url = processedReq.params.url.replace( 'localhost', debuggerInfo.originalSourceURLAddress, ); if ( - req.params.url && - req.params.url.startsWith(FILE_PREFIX) && + processedReq.params.url && + processedReq.params.url.startsWith(FILE_PREFIX) && debuggerInfo.prependedFilePrefix ) { // Remove fake URL prefix if we modified URL in _processMessageFromDevice. // $FlowFixMe[incompatible-use] - req.params.url = req.params.url.slice(FILE_PREFIX.length); + processedReq.params.url = processedReq.params.url.slice( + FILE_PREFIX.length, + ); } } - if (req.params.urlRegex != null) { - req.params.urlRegex = req.params.urlRegex.replace( + if (processedReq.params.urlRegex != null) { + processedReq.params.urlRegex = processedReq.params.urlRegex.replace( /localhost/g, // $FlowFixMe[incompatible-call] debuggerInfo.originalSourceURLAddress, ); } + return processedReq; } + return req; } _processDebuggerGetScriptSource(req: GetScriptSourceRequest, socket: WS) { diff --git a/packages/dev-middleware/src/inspector-proxy/types.js b/packages/dev-middleware/src/inspector-proxy/types.js index 2d326ded6d44b0..d1374fd634e891 100644 --- a/packages/dev-middleware/src/inspector-proxy/types.js +++ b/packages/dev-middleware/src/inspector-proxy/types.js @@ -12,49 +12,43 @@ // Page information received from the device. New page is created for // each new instance of VM and can appear when user reloads React Native // application. -export type Page = { +export type Page = $ReadOnly<{ id: string, title: string, vm: string, app: string, - ... -}; +}>; // Chrome Debugger Protocol message/event passed between device and debugger. -export type WrappedEvent = { +export type WrappedEvent = $ReadOnly<{ event: 'wrappedEvent', - payload: { + payload: $ReadOnly<{ pageId: string, wrappedEvent: string, - ... - }, - ... -}; + }>, +}>; // Request sent from Inspector Proxy to Device when new debugger is connected // to particular page. -export type ConnectRequest = { +export type ConnectRequest = $ReadOnly<{ event: 'connect', - payload: {pageId: string, ...}, - ... -}; + payload: $ReadOnly<{pageId: string}>, +}>; // Request sent from Inspector Proxy to Device to notify that debugger is // disconnected. -export type DisconnectRequest = { +export type DisconnectRequest = $ReadOnly<{ event: 'disconnect', - payload: {pageId: string, ...}, - ... -}; + payload: $ReadOnly<{pageId: string}>, +}>; // Request sent from Inspector Proxy to Device to get a list of pages. -export type GetPagesRequest = {event: 'getPages', ...}; +export type GetPagesRequest = {event: 'getPages'}; // Response to GetPagesRequest containing a list of page infos. export type GetPagesResponse = { event: 'getPages', - payload: Array, - ... + payload: $ReadOnlyArray, }; // Union type for all possible messages sent from device to Inspector Proxy. @@ -71,7 +65,7 @@ export type MessageToDevice = | DisconnectRequest; // Page description object that is sent in response to /json HTTP request from debugger. -export type PageDescription = { +export type PageDescription = $ReadOnly<{ id: string, description: string, title: string, @@ -79,60 +73,61 @@ export type PageDescription = { devtoolsFrontendUrl: string, type: string, webSocketDebuggerUrl: string, + deviceName: string, + vm: string, // Metadata specific to React Native - reactNative: { + reactNative: $ReadOnly<{ logicalDeviceId: string, - }, - ... -}; -export type JsonPagesListResponse = Array; + }>, +}>; + +export type JsonPagesListResponse = $ReadOnlyArray; // Response to /json/version HTTP request from the debugger specifying browser type and // Chrome protocol version. -export type JsonVersionResponse = { +export type JsonVersionResponse = $ReadOnly<{ Browser: string, 'Protocol-Version': string, - ... -}; +}>; /** * Types were exported from https://github.com/ChromeDevTools/devtools-protocol/blob/master/types/protocol.d.ts */ -export type SetBreakpointByUrlRequest = { +export type SetBreakpointByUrlRequest = $ReadOnly<{ id: number, method: 'Debugger.setBreakpointByUrl', - params: { + params: $ReadOnly<{ lineNumber: number, url?: string, urlRegex?: string, scriptHash?: string, columnNumber?: number, condition?: string, - }, -}; + }>, +}>; -export type GetScriptSourceRequest = { +export type GetScriptSourceRequest = $ReadOnly<{ id: number, method: 'Debugger.getScriptSource', params: { scriptId: string, }, -}; +}>; -export type GetScriptSourceResponse = { +export type GetScriptSourceResponse = $ReadOnly<{ scriptSource: string, /** * Wasm bytecode. */ bytecode?: string, -}; +}>; -export type ErrorResponse = { - error: { +export type ErrorResponse = $ReadOnly<{ + error: $ReadOnly<{ message: string, - }, -}; + }>, +}>; export type DebuggerRequest = | SetBreakpointByUrlRequest