Skip to content

Commit db2c44a

Browse files
motiz88facebook-github-bot
authored andcommitted
Make inspector-proxy protocol types mutable by default (facebook#45021)
Summary: Pull Request resolved: facebook#45021 Changelog: [Internal] TSIA, hoisted from an upcoming refactor of `Device` in inspector-proxy. Reviewed By: hoxyq Differential Revision: D58723496 fbshipit-source-id: af272b08bc280d681bd7e1adb60d2a411a549864
1 parent 64d5de7 commit db2c44a

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,6 @@ export default class Device {
642642
if ('sourceMapURL' in params) {
643643
for (const hostToRewrite of REWRITE_HOSTS_TO_LOCALHOST) {
644644
if (params.sourceMapURL.includes(hostToRewrite)) {
645-
// $FlowFixMe[cannot-write]
646645
payload.params.sourceMapURL = params.sourceMapURL.replace(
647646
hostToRewrite,
648647
'localhost',
@@ -660,7 +659,6 @@ export default class Device {
660659
// message to the debug client.
661660
try {
662661
const sourceMap = await this.#fetchText(sourceMapURL);
663-
// $FlowFixMe[cannot-write]
664662
payload.params.sourceMapURL =
665663
'data:application/json;charset=utf-8;base64,' +
666664
Buffer.from(sourceMap).toString('base64');
@@ -674,7 +672,6 @@ export default class Device {
674672
if ('url' in params) {
675673
for (const hostToRewrite of REWRITE_HOSTS_TO_LOCALHOST) {
676674
if (params.url.includes(hostToRewrite)) {
677-
// $FlowFixMe[cannot-write]
678675
payload.params.url = params.url.replace(hostToRewrite, 'localhost');
679676
debuggerInfo.originalSourceURLAddress = hostToRewrite;
680677
}
@@ -685,7 +682,6 @@ export default class Device {
685682
// Chrome to not download source maps. In this case we want to prepend script ID
686683
// with 'file://' prefix.
687684
if (payload.params.url.match(/^[0-9a-z]+$/)) {
688-
// $FlowFixMe[cannot-write]
689685
payload.params.url = FILE_PREFIX + payload.params.url;
690686
debuggerInfo.prependedFilePrefix = true;
691687
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import type {EventReporter} from '../types/EventReporter';
1212
import type {CDPResponse} from './cdp-types/messages';
13+
import type {DeepReadOnly} from './types';
1314

1415
import TTLCache from '@isaacs/ttlcache';
1516

@@ -77,7 +78,7 @@ class DeviceEventReporter {
7778
}
7879

7980
logResponse(
80-
res: CDPResponse<>,
81+
res: DeepReadOnly<CDPResponse<>>,
8182
origin: 'device' | 'proxy',
8283
metadata: ResponseMetadata,
8384
): void {

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,36 @@
99
* @oncall react_native
1010
*/
1111

12+
import type {JSONSerializable} from '../types';
1213
import type {Commands, Events} from './protocol';
1314

1415
// Note: A CDP event is a JSON-RPC notification with no `id` member.
15-
export type CDPEvent<TEvent: $Keys<Events> = 'unknown'> = $ReadOnly<{
16+
export type CDPEvent<TEvent: $Keys<Events> = 'unknown'> = {
1617
method: TEvent,
1718
params: Events[TEvent],
18-
}>;
19+
};
1920

20-
export type CDPRequest<TCommand: $Keys<Commands> = 'unknown'> = $ReadOnly<{
21+
export type CDPRequest<TCommand: $Keys<Commands> = 'unknown'> = {
2122
method: TCommand,
2223
params: Commands[TCommand]['paramsType'],
2324
id: number,
24-
}>;
25+
};
2526

2627
export type CDPResponse<TCommand: $Keys<Commands> = 'unknown'> =
27-
| $ReadOnly<{
28+
| {
2829
result: Commands[TCommand]['resultType'],
2930
id: number,
30-
}>
31-
| $ReadOnly<{
31+
}
32+
| {
3233
error: CDPRequestError,
3334
id: number,
34-
}>;
35+
};
3536

36-
export type CDPRequestError = $ReadOnly<{
37+
export type CDPRequestError = {
3738
code: number,
3839
message: string,
39-
data?: mixed,
40-
}>;
40+
data?: JSONSerializable,
41+
};
4142

4243
export type CDPClientMessage =
4344
| CDPRequest<'Debugger.getScriptSource'>

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111

1212
// Adapted from https://github.com/ChromeDevTools/devtools-protocol/blob/master/types/protocol.d.ts
1313

14+
import type {JSONSerializable} from '../types';
15+
1416
type integer = number;
1517

1618
export interface Debugger {
17-
GetScriptSourceParams: $ReadOnly<{
19+
GetScriptSourceParams: {
1820
/**
1921
* Id of the script to get source for.
2022
*/
2123
scriptId: string,
22-
}>;
24+
};
2325

24-
GetScriptSourceResult: $ReadOnly<{
26+
GetScriptSourceResult: {
2527
/**
2628
* Script source (empty in case of Wasm bytecode).
2729
*/
@@ -31,9 +33,9 @@ export interface Debugger {
3133
* Wasm bytecode. (Encoded as a base64 string when passed over JSON)
3234
*/
3335
bytecode?: string,
34-
}>;
36+
};
3537

36-
SetBreakpointByUrlParams: $ReadOnly<{
38+
SetBreakpointByUrlParams: {
3739
/**
3840
* Line number to set breakpoint at.
3941
*/
@@ -65,9 +67,9 @@ export interface Debugger {
6567
* breakpoint if this expression evaluates to true.
6668
*/
6769
condition?: string,
68-
}>;
70+
};
6971

70-
ScriptParsedEvent: $ReadOnly<{
72+
ScriptParsedEvent: {
7173
/**
7274
* Identifier of the script parsed.
7375
*/
@@ -82,12 +84,12 @@ export interface Debugger {
8284
* URL of source map associated with script (if any).
8385
*/
8486
sourceMapURL: string,
85-
}>;
87+
};
8688
}
8789

8890
export type Events = {
8991
'Debugger.scriptParsed': Debugger['ScriptParsedEvent'],
90-
[method: string]: mixed,
92+
[method: string]: JSONSerializable,
9193
};
9294

9395
export type Commands = {
@@ -100,7 +102,7 @@ export type Commands = {
100102
resultType: void,
101103
},
102104
[method: string]: {
103-
paramsType: mixed,
104-
resultType: mixed,
105+
paramsType: JSONSerializable,
106+
resultType: JSONSerializable,
105107
},
106108
};

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,10 @@ export type JSONSerializable =
146146
| null
147147
| $ReadOnlyArray<JSONSerializable>
148148
| {+[string]: JSONSerializable};
149+
150+
export type DeepReadOnly<T> =
151+
T extends $ReadOnlyArray<infer V>
152+
? $ReadOnlyArray<DeepReadOnly<V>>
153+
: T extends {...}
154+
? {+[K in keyof T]: DeepReadOnly<T[K]>}
155+
: T;

0 commit comments

Comments
 (0)