Skip to content

Commit 1d6b0f1

Browse files
motiz88facebook-github-bot
authored andcommitted
Use exact, read-only types for protocol data structures (facebook#41315)
Summary: Pull Request resolved: facebook#41315 TSIA Changelog: [Internal] Reviewed By: hoxyq Differential Revision: D50980466 fbshipit-source-id: 1f289b868f5c735396fe07899c558e417a4ee2ff
1 parent c90485e commit 1d6b0f1

2 files changed

Lines changed: 58 additions & 58 deletions

File tree

packages/dev-middleware/src/inspector-proxy/Device.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export default class Device {
6969
_deviceSocket: WS;
7070

7171
// Stores last list of device's pages.
72-
_pages: Array<Page>;
72+
_pages: $ReadOnlyArray<Page>;
7373

7474
// Stores information about currently connected debugger (if any).
7575
_debuggerConnection: ?DebuggerInfo = null;
@@ -152,7 +152,7 @@ export default class Device {
152152
return this._app;
153153
}
154154

155-
getPagesList(): Array<Page> {
155+
getPagesList(): $ReadOnlyArray<Page> {
156156
if (this._lastConnectedReactNativePage) {
157157
const reactNativeReloadablePage = {
158158
id: REACT_NATIVE_RELOADABLE_PAGE_ID,
@@ -216,18 +216,18 @@ export default class Device {
216216
pageId: this._debuggerConnection?.pageId ?? null,
217217
frontendUserAgent: metadata.userAgent,
218218
});
219-
const handled = this._interceptMessageFromDebugger(
219+
const processedReq = this._interceptMessageFromDebugger(
220220
debuggerRequest,
221221
debuggerInfo,
222222
socket,
223223
);
224224

225-
if (!handled) {
225+
if (processedReq) {
226226
this._sendMessageToDevice({
227227
event: 'wrappedEvent',
228228
payload: {
229229
pageId: this._mapToDevicePageId(pageId),
230-
wrappedEvent: JSON.stringify(debuggerRequest),
230+
wrappedEvent: JSON.stringify(processedReq),
231231
},
232232
});
233233
}
@@ -545,46 +545,51 @@ export default class Device {
545545
req: DebuggerRequest,
546546
debuggerInfo: DebuggerInfo,
547547
socket: WS,
548-
): boolean {
548+
): ?DebuggerRequest {
549549
if (req.method === 'Debugger.setBreakpointByUrl') {
550-
this._processDebuggerSetBreakpointByUrl(req, debuggerInfo);
550+
return this._processDebuggerSetBreakpointByUrl(req, debuggerInfo);
551551
} else if (req.method === 'Debugger.getScriptSource') {
552552
this._processDebuggerGetScriptSource(req, socket);
553-
return true;
553+
return null;
554554
}
555-
return false;
555+
return req;
556556
}
557557

558558
_processDebuggerSetBreakpointByUrl(
559559
req: SetBreakpointByUrlRequest,
560560
debuggerInfo: DebuggerInfo,
561-
) {
561+
): SetBreakpointByUrlRequest {
562562
// If we replaced Android emulator's address to localhost we need to change it back.
563563
if (debuggerInfo.originalSourceURLAddress != null) {
564-
if (req.params.url != null) {
565-
req.params.url = req.params.url.replace(
564+
const processedReq = {...req, params: {...req.params}};
565+
if (processedReq.params.url != null) {
566+
processedReq.params.url = processedReq.params.url.replace(
566567
'localhost',
567568
debuggerInfo.originalSourceURLAddress,
568569
);
569570

570571
if (
571-
req.params.url &&
572-
req.params.url.startsWith(FILE_PREFIX) &&
572+
processedReq.params.url &&
573+
processedReq.params.url.startsWith(FILE_PREFIX) &&
573574
debuggerInfo.prependedFilePrefix
574575
) {
575576
// Remove fake URL prefix if we modified URL in _processMessageFromDevice.
576577
// $FlowFixMe[incompatible-use]
577-
req.params.url = req.params.url.slice(FILE_PREFIX.length);
578+
processedReq.params.url = processedReq.params.url.slice(
579+
FILE_PREFIX.length,
580+
);
578581
}
579582
}
580-
if (req.params.urlRegex != null) {
581-
req.params.urlRegex = req.params.urlRegex.replace(
583+
if (processedReq.params.urlRegex != null) {
584+
processedReq.params.urlRegex = processedReq.params.urlRegex.replace(
582585
/localhost/g,
583586
// $FlowFixMe[incompatible-call]
584587
debuggerInfo.originalSourceURLAddress,
585588
);
586589
}
590+
return processedReq;
587591
}
592+
return req;
588593
}
589594

590595
_processDebuggerGetScriptSource(req: GetScriptSourceRequest, socket: WS) {

packages/dev-middleware/src/inspector-proxy/types.js

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,43 @@
1212
// Page information received from the device. New page is created for
1313
// each new instance of VM and can appear when user reloads React Native
1414
// application.
15-
export type Page = {
15+
export type Page = $ReadOnly<{
1616
id: string,
1717
title: string,
1818
vm: string,
1919
app: string,
20-
...
21-
};
20+
}>;
2221

2322
// Chrome Debugger Protocol message/event passed between device and debugger.
24-
export type WrappedEvent = {
23+
export type WrappedEvent = $ReadOnly<{
2524
event: 'wrappedEvent',
26-
payload: {
25+
payload: $ReadOnly<{
2726
pageId: string,
2827
wrappedEvent: string,
29-
...
30-
},
31-
...
32-
};
28+
}>,
29+
}>;
3330

3431
// Request sent from Inspector Proxy to Device when new debugger is connected
3532
// to particular page.
36-
export type ConnectRequest = {
33+
export type ConnectRequest = $ReadOnly<{
3734
event: 'connect',
38-
payload: {pageId: string, ...},
39-
...
40-
};
35+
payload: $ReadOnly<{pageId: string}>,
36+
}>;
4137

4238
// Request sent from Inspector Proxy to Device to notify that debugger is
4339
// disconnected.
44-
export type DisconnectRequest = {
40+
export type DisconnectRequest = $ReadOnly<{
4541
event: 'disconnect',
46-
payload: {pageId: string, ...},
47-
...
48-
};
42+
payload: $ReadOnly<{pageId: string}>,
43+
}>;
4944

5045
// Request sent from Inspector Proxy to Device to get a list of pages.
51-
export type GetPagesRequest = {event: 'getPages', ...};
46+
export type GetPagesRequest = {event: 'getPages'};
5247

5348
// Response to GetPagesRequest containing a list of page infos.
5449
export type GetPagesResponse = {
5550
event: 'getPages',
56-
payload: Array<Page>,
57-
...
51+
payload: $ReadOnlyArray<Page>,
5852
};
5953

6054
// Union type for all possible messages sent from device to Inspector Proxy.
@@ -71,68 +65,69 @@ export type MessageToDevice =
7165
| DisconnectRequest;
7266

7367
// Page description object that is sent in response to /json HTTP request from debugger.
74-
export type PageDescription = {
68+
export type PageDescription = $ReadOnly<{
7569
id: string,
7670
description: string,
7771
title: string,
7872
faviconUrl: string,
7973
devtoolsFrontendUrl: string,
8074
type: string,
8175
webSocketDebuggerUrl: string,
76+
deviceName: string,
77+
vm: string,
8278
// Metadata specific to React Native
83-
reactNative: {
79+
reactNative: $ReadOnly<{
8480
logicalDeviceId: string,
85-
},
86-
...
87-
};
88-
export type JsonPagesListResponse = Array<PageDescription>;
81+
}>,
82+
}>;
83+
84+
export type JsonPagesListResponse = $ReadOnlyArray<PageDescription>;
8985

9086
// Response to /json/version HTTP request from the debugger specifying browser type and
9187
// Chrome protocol version.
92-
export type JsonVersionResponse = {
88+
export type JsonVersionResponse = $ReadOnly<{
9389
Browser: string,
9490
'Protocol-Version': string,
95-
...
96-
};
91+
}>;
9792

9893
/**
9994
* Types were exported from https://github.com/ChromeDevTools/devtools-protocol/blob/master/types/protocol.d.ts
10095
*/
10196

102-
export type SetBreakpointByUrlRequest = {
97+
export type SetBreakpointByUrlRequest = $ReadOnly<{
10398
id: number,
10499
method: 'Debugger.setBreakpointByUrl',
105-
params: {
100+
params: $ReadOnly<{
106101
lineNumber: number,
107102
url?: string,
108103
urlRegex?: string,
109104
scriptHash?: string,
110105
columnNumber?: number,
111106
condition?: string,
112-
},
113-
};
107+
}>,
108+
}>;
114109

115-
export type GetScriptSourceRequest = {
110+
export type GetScriptSourceRequest = $ReadOnly<{
116111
id: number,
117112
method: 'Debugger.getScriptSource',
118113
params: {
119114
scriptId: string,
120115
},
121-
};
116+
}>;
122117

123-
export type GetScriptSourceResponse = {
118+
export type GetScriptSourceResponse = $ReadOnly<{
124119
scriptSource: string,
125120
/**
126121
* Wasm bytecode.
127122
*/
128123
bytecode?: string,
129-
};
124+
}>;
130125

131-
export type ErrorResponse = {
132-
error: {
126+
export type ErrorResponse = $ReadOnly<{
127+
error: $ReadOnly<{
133128
message: string,
134-
},
135-
};
129+
}>,
130+
}>;
136131

137132
export type DebuggerRequest =
138133
| SetBreakpointByUrlRequest

0 commit comments

Comments
 (0)