-
Notifications
You must be signed in to change notification settings - Fork 24.6k
/
Copy pathDevice.js
1100 lines (990 loc) · 35.5 KB
/
Device.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/
import type {EventReporter} from '../types/EventReporter';
import type {
CDPClientMessage,
CDPRequest,
CDPResponse,
CDPServerMessage,
} from './cdp-types/messages';
import type {
CreateCustomMessageHandlerFn,
CustomMessageHandler,
} from './CustomMessageHandler';
import type {
MessageFromDevice,
MessageToDevice,
Page,
TargetCapabilityFlags,
} from './types';
import CDPMessagesLogging from './CDPMessagesLogging';
import DeviceEventReporter from './DeviceEventReporter';
import * as fs from 'fs';
import invariant from 'invariant';
import * as path from 'path';
import WS from 'ws';
const debug = require('debug')('Metro:InspectorProxy');
const PAGES_POLLING_INTERVAL = 1000;
const MIN_MESSAGE_QUEUE_BYTES_TO_REPORT = 2 * 1024 * 1024; // 2 MiB
const WS_CLOSURE_CODE = {
NORMAL: 1000,
INTERNAL_ERROR: 1011,
};
export const WS_CLOSE_REASON = {
PAGE_NOT_FOUND: 'Debugger Page Not Found',
DEVICE_DISCONNECTED: 'Corresponding Device Disconnected',
RECREATING_DEVICE: 'Recreating Device Connection',
RECREATING_DEBUGGER: 'Recreating Debugger Connection',
};
// Prefix for script URLs that are alphanumeric IDs. See comment in #processMessageFromDeviceLegacy method for
// more details.
const FILE_PREFIX = 'file://';
let fuseboxConsoleNoticeLogged = false;
type DebuggerConnection = {
// Debugger web socket connection
socket: WS,
prependedFilePrefix: boolean,
pageId: string,
userAgent: string | null,
customHandler: ?CustomMessageHandler,
debuggerRelativeBaseUrl: URL,
};
const REACT_NATIVE_RELOADABLE_PAGE_ID = '-1';
export type DeviceOptions = $ReadOnly<{
id: string,
name: string,
app: string,
socket: WS,
projectRoot: string,
eventReporter: ?EventReporter,
createMessageMiddleware: ?CreateCustomMessageHandlerFn,
deviceRelativeBaseUrl: URL,
serverRelativeBaseUrl: URL,
isProfilingBuild: boolean,
}>;
/**
* Device class represents single device connection to Inspector Proxy. Each device
* can have multiple inspectable pages.
*/
export default class Device {
// ID of the device.
#id: string;
// Name of the device.
#name: string;
// Package name of the app.
#app: string;
// Stores socket connection between Inspector Proxy and device.
#deviceSocket: WS;
// Stores the most recent listing of device's pages, keyed by the `id` field.
#pages: $ReadOnlyMap<string, Page> = new Map();
// Stores information about currently connected debugger (if any).
#debuggerConnection: ?DebuggerConnection = null;
// Last known Page ID of the React Native page.
// This is used by debugger connections that don't have PageID specified
// (and will interact with the latest React Native page).
#lastConnectedLegacyReactNativePage: ?Page = null;
// Whether we are in the middle of a reload in the REACT_NATIVE_RELOADABLE_PAGE.
#isLegacyPageReloading: boolean = false;
// The previous "GetPages" message, for deduplication in debug logs.
#lastGetPagesMessage: string = '';
// Mapping built from scriptParsed events and used to fetch file content in `Debugger.getScriptSource`.
#scriptIdToSourcePathMapping: Map<string, string> = new Map();
// Root of the project used for relative to absolute source path conversion.
#projectRoot: string;
#deviceEventReporter: ?DeviceEventReporter;
#pagesPollingIntervalId: ReturnType<typeof setInterval>;
// The device message middleware factory function allowing implementers to handle unsupported CDP messages.
#createCustomMessageHandler: ?CreateCustomMessageHandlerFn;
#connectedPageIds: Set<string> = new Set();
// A base HTTP(S) URL to this server, reachable from the device. Derived from
// the http request that created the connection.
#deviceRelativeBaseUrl: URL;
// A base HTTP(S) URL to the server, relative to this server.
#serverRelativeBaseUrl: URL;
// Logging reporting batches of cdp messages
#cdpMessagesLogging: CDPMessagesLogging;
constructor(deviceOptions: DeviceOptions) {
this.#dangerouslyConstruct(deviceOptions);
}
#dangerouslyConstruct({
id,
name,
app,
socket,
projectRoot,
eventReporter,
createMessageMiddleware,
serverRelativeBaseUrl,
deviceRelativeBaseUrl,
isProfilingBuild,
}: DeviceOptions) {
this.#cdpMessagesLogging = new CDPMessagesLogging();
this.#id = id;
this.#name = name;
this.#app = app;
this.#deviceSocket = socket;
this.#projectRoot = projectRoot;
this.#serverRelativeBaseUrl = serverRelativeBaseUrl;
this.#deviceRelativeBaseUrl = deviceRelativeBaseUrl;
this.#deviceEventReporter = eventReporter
? new DeviceEventReporter(eventReporter, {
deviceId: id,
deviceName: name,
appId: app,
})
: null;
this.#createCustomMessageHandler = createMessageMiddleware;
if (isProfilingBuild) {
this.#deviceEventReporter?.logProfilingTargetRegistered();
}
// $FlowFixMe[incompatible-call]
this.#deviceSocket.on('message', (message: string) => {
try {
const parsedMessage = JSON.parse(message);
if (parsedMessage.event === 'getPages') {
// There's a 'getPages' message every second, so only show them if they change
if (message !== this.#lastGetPagesMessage) {
debug('Device getPages ping has changed: %s', message);
this.#lastGetPagesMessage = message;
}
} else {
this.#cdpMessagesLogging.log('DeviceToProxy', message);
}
this.#handleMessageFromDevice(parsedMessage);
} catch (error) {
debug('%O\nHandling device message: %s', error, message);
try {
this.#deviceEventReporter?.logProxyMessageHandlingError(
'device',
error,
message,
);
} catch (loggingError) {
debug(
'Error logging message handling error to reporter: %O',
loggingError,
);
}
}
});
// Sends 'getPages' request to device every PAGES_POLLING_INTERVAL milliseconds.
this.#pagesPollingIntervalId = setInterval(
() => this.#sendMessageToDevice({event: 'getPages'}),
PAGES_POLLING_INTERVAL,
);
this.#deviceSocket.on('close', () => {
if (socket === this.#deviceSocket) {
this.#deviceEventReporter?.logDisconnection('device');
// Device disconnected - close debugger connection.
this.#terminateDebuggerConnection(
WS_CLOSURE_CODE.NORMAL,
WS_CLOSE_REASON.DEVICE_DISCONNECTED,
);
clearInterval(this.#pagesPollingIntervalId);
}
});
}
#terminateDebuggerConnection(code?: number, reason?: string) {
const debuggerConnection = this.#debuggerConnection;
if (debuggerConnection) {
this.#sendDisconnectEventToDevice(
this.#mapToDevicePageId(debuggerConnection.pageId),
);
debuggerConnection.socket.close(code, reason);
this.#debuggerConnection = null;
}
}
/**
* Used to recreate the device connection if there is a device ID collision.
* 1. Checks if the same device is attempting to reconnect for the same app.
* 2. If not, close both the device and debugger socket.
* 3. If the debugger connection can be reused, close the device socket only.
*
* This hack attempts to allow users to reload the app, either as result of a
* crash, or manually reloading, without having to restart the debugger.
*/
dangerouslyRecreateDevice(deviceOptions: DeviceOptions) {
invariant(
deviceOptions.id === this.#id,
'dangerouslyRecreateDevice() can only be used for the same device ID',
);
const oldDebugger = this.#debuggerConnection;
if (this.#app !== deviceOptions.app || this.#name !== deviceOptions.name) {
this.#deviceSocket.close(
WS_CLOSURE_CODE.NORMAL,
WS_CLOSE_REASON.RECREATING_DEVICE,
);
this.#terminateDebuggerConnection(
WS_CLOSURE_CODE.NORMAL,
WS_CLOSE_REASON.RECREATING_DEVICE,
);
}
this.#debuggerConnection = null;
if (oldDebugger) {
oldDebugger.socket.removeAllListeners();
this.#deviceSocket.close(
WS_CLOSURE_CODE.NORMAL,
WS_CLOSE_REASON.RECREATING_DEVICE,
);
this.handleDebuggerConnection(oldDebugger.socket, oldDebugger.pageId, {
debuggerRelativeBaseUrl: oldDebugger.debuggerRelativeBaseUrl,
userAgent: oldDebugger.userAgent,
});
}
this.#dangerouslyConstruct(deviceOptions);
}
getName(): string {
return this.#name;
}
getApp(): string {
return this.#app;
}
getPagesList(): $ReadOnlyArray<Page> {
if (this.#lastConnectedLegacyReactNativePage) {
return [...this.#pages.values(), this.#createSyntheticPage()];
} else {
return [...this.#pages.values()];
}
}
// Handles new debugger connection to this device:
// 1. Sends connect event to device
// 2. Forwards all messages from the debugger to device as wrappedEvent
// 3. Sends disconnect event to device when debugger connection socket closes.
handleDebuggerConnection(
socket: WS,
pageId: string,
{
debuggerRelativeBaseUrl,
userAgent,
}: $ReadOnly<{
debuggerRelativeBaseUrl: URL,
userAgent: string | null,
}>,
) {
const page: ?Page =
pageId === REACT_NATIVE_RELOADABLE_PAGE_ID
? this.#createSyntheticPage()
: this.#pages.get(pageId);
if (!page) {
debug(
`Got new debugger connection via ${debuggerRelativeBaseUrl.href} for ` +
`page ${pageId} of ${this.#name}, but no such page exists`,
);
socket.close(
WS_CLOSURE_CODE.INTERNAL_ERROR,
WS_CLOSE_REASON.PAGE_NOT_FOUND,
);
return;
}
// Clear any commands we were waiting on.
this.#deviceEventReporter?.logDisconnection('debugger');
// Disconnect current debugger if we already have debugger connected.
this.#terminateDebuggerConnection(
WS_CLOSURE_CODE.NORMAL,
WS_CLOSE_REASON.RECREATING_DEBUGGER,
);
this.#deviceEventReporter?.logConnection('debugger', {
pageId,
frontendUserAgent: userAgent,
});
const debuggerInfo = {
socket,
prependedFilePrefix: false,
pageId,
userAgent: userAgent,
customHandler: null,
debuggerRelativeBaseUrl,
};
this.#debuggerConnection = debuggerInfo;
debug(
`Got new debugger connection via ${debuggerRelativeBaseUrl.href} for ` +
`page ${pageId} of ${this.#name}`,
);
if (this.#debuggerConnection && this.#createCustomMessageHandler) {
this.#debuggerConnection.customHandler = this.#createCustomMessageHandler(
{
page,
debugger: {
userAgent: debuggerInfo.userAgent,
sendMessage: message => {
try {
const payload = JSON.stringify(message);
this.#cdpMessagesLogging.log('ProxyToDebugger', payload);
socket.send(payload);
} catch {}
},
},
device: {
appId: this.#app,
id: this.#id,
name: this.#name,
sendMessage: message => {
try {
const payload = JSON.stringify({
event: 'wrappedEvent',
payload: {
pageId: this.#mapToDevicePageId(pageId),
wrappedEvent: JSON.stringify(message),
},
});
this.#cdpMessagesLogging.log('DebuggerToProxy', payload);
this.#deviceSocket.send(payload);
} catch {}
},
},
},
);
if (this.#debuggerConnection.customHandler) {
debug('Created new custom message handler for debugger connection');
} else {
debug(
'Skipping new custom message handler for debugger connection, factory function returned null',
);
}
}
this.#sendConnectEventToDevice(this.#mapToDevicePageId(pageId));
// $FlowFixMe[incompatible-call]
socket.on('message', (message: string) => {
this.#cdpMessagesLogging.log('DebuggerToProxy', message);
const debuggerRequest = JSON.parse(message);
this.#deviceEventReporter?.logRequest(debuggerRequest, 'debugger', {
pageId: this.#debuggerConnection?.pageId ?? null,
frontendUserAgent: userAgent,
prefersFuseboxFrontend: this.#isPageFuseboxFrontend(
this.#debuggerConnection?.pageId,
),
});
let processedReq = debuggerRequest;
if (
this.#debuggerConnection?.customHandler?.handleDebuggerMessage(
debuggerRequest,
) === true
) {
return;
}
if (!this.#pageHasCapability(page, 'nativeSourceCodeFetching')) {
processedReq = this.#interceptClientMessageForSourceFetching(
debuggerRequest,
debuggerInfo,
socket,
);
}
if (processedReq) {
this.#sendMessageToDevice({
event: 'wrappedEvent',
payload: {
pageId: this.#mapToDevicePageId(pageId),
wrappedEvent: JSON.stringify(processedReq),
},
});
}
});
socket.on('close', () => {
debug(`Debugger for page ${pageId} and ${this.#name} disconnected.`);
this.#deviceEventReporter?.logDisconnection('debugger');
if (this.#debuggerConnection?.socket === socket) {
this.#terminateDebuggerConnection();
}
});
const cdpMessagesLogging = this.#cdpMessagesLogging;
// $FlowFixMe[method-unbinding]
const sendFunc = socket.send;
// $FlowFixMe[cannot-write]
socket.send = function (message: string) {
cdpMessagesLogging.log('ProxyToDebugger', message);
return sendFunc.call(socket, message);
};
}
#sendConnectEventToDevice(devicePageId: string) {
if (this.#connectedPageIds.has(devicePageId)) {
return;
}
this.#connectedPageIds.add(devicePageId);
this.#sendMessageToDevice({
event: 'connect',
payload: {pageId: devicePageId},
});
}
#sendDisconnectEventToDevice(devicePageId: string) {
if (!this.#connectedPageIds.has(devicePageId)) {
return;
}
this.#connectedPageIds.delete(devicePageId);
this.#sendMessageToDevice({
event: 'disconnect',
payload: {pageId: devicePageId},
});
}
/**
* Returns `true` if a page supports the given target capability flag.
*/
#pageHasCapability(page: Page, flag: $Keys<TargetCapabilityFlags>): boolean {
return page.capabilities[flag] === true;
}
/**
* Returns the synthetic "React Native Experimental (Improved Chrome Reloads)" page.
*/
#createSyntheticPage(): Page {
return {
id: REACT_NATIVE_RELOADABLE_PAGE_ID,
title: 'React Native Experimental (Improved Chrome Reloads)',
vm: "don't use",
app: this.#app,
capabilities: {},
};
}
// Handles messages received from device:
// 1. For getPages responses updates local #pages list.
// 2. All other messages are forwarded to debugger as wrappedEvent.
//
// In the future more logic will be added to this method for modifying
// some of the messages (like updating messages with source maps and file
// locations).
#handleMessageFromDevice(message: MessageFromDevice) {
if (message.event === 'getPages') {
// Preserve ordering - getPages guarantees addition order.
this.#pages = new Map(
message.payload.map(({capabilities, ...page}) => [
page.id,
{
...page,
capabilities: capabilities ?? {},
},
]),
);
if (message.payload.length !== this.#pages.size) {
const duplicateIds = new Set<string>();
const idsSeen = new Set<string>();
for (const page of message.payload) {
if (!idsSeen.has(page.id)) {
idsSeen.add(page.id);
} else {
duplicateIds.add(page.id);
}
}
debug(
`Received duplicate page IDs from device: ${[...duplicateIds].join(
', ',
)}`,
);
}
// Check if device has a new legacy React Native page.
// There is usually no more than 2-3 pages per device so this operation
// is not expensive.
// TODO(hypuk): It is better for VM to send update event when new page is
// created instead of manually checking this on every getPages result.
for (const page of this.#pages.values()) {
if (this.#pageHasCapability(page, 'nativePageReloads')) {
this.#logFuseboxConsoleNotice();
continue;
}
if (page.title.includes('React')) {
if (page.id !== this.#lastConnectedLegacyReactNativePage?.id) {
this.#newLegacyReactNativePage(page);
break;
}
}
}
} else if (message.event === 'disconnect') {
// Device sends disconnect events only when page is reloaded or
// if debugger socket was disconnected.
const pageId = message.payload.pageId;
// TODO(moti): Handle null case explicitly, e.g. swallow disconnect events
// for unknown pages.
const page: ?Page = this.#pages.get(pageId);
if (page != null && this.#pageHasCapability(page, 'nativePageReloads')) {
return;
}
const debuggerSocket = this.#debuggerConnection
? this.#debuggerConnection.socket
: null;
if (debuggerSocket && debuggerSocket.readyState === WS.OPEN) {
if (
this.#debuggerConnection != null &&
this.#debuggerConnection.pageId !== REACT_NATIVE_RELOADABLE_PAGE_ID
) {
debug(`Legacy page ${pageId} is reloading.`);
debuggerSocket.send(JSON.stringify({method: 'reload'}));
}
}
} else if (message.event === 'wrappedEvent') {
if (this.#debuggerConnection == null) {
return;
}
// FIXME: Is it possible that we received message for pageID that does not
// correspond to current debugger connection?
// TODO(moti): yes, fix multi-debugger case
const debuggerSocket = this.#debuggerConnection.socket;
if (debuggerSocket == null || debuggerSocket.readyState !== WS.OPEN) {
// TODO(hypuk): Send error back to device?
return;
}
const parsedPayload = JSON.parse(message.payload.wrappedEvent);
const pageId = this.#debuggerConnection?.pageId ?? null;
if ('id' in parsedPayload) {
this.#deviceEventReporter?.logResponse(parsedPayload, 'device', {
pageId,
frontendUserAgent: this.#debuggerConnection?.userAgent ?? null,
prefersFuseboxFrontend: this.#isPageFuseboxFrontend(pageId),
});
}
const debuggerConnection = this.#debuggerConnection;
if (debuggerConnection != null) {
if (
debuggerConnection.customHandler?.handleDeviceMessage(
parsedPayload,
) === true
) {
return;
}
this.#processMessageFromDeviceLegacy(
parsedPayload,
debuggerConnection,
pageId,
);
const messageToSend = JSON.stringify(parsedPayload);
debuggerSocket.send(messageToSend);
} else {
debuggerSocket.send(message.payload.wrappedEvent);
}
}
}
// Sends single message to device.
#sendMessageToDevice(message: MessageToDevice) {
try {
const messageToSend = JSON.stringify(message);
if (message.event !== 'getPages') {
this.#cdpMessagesLogging.log('ProxyToDevice', messageToSend);
}
this.#deviceSocket.send(messageToSend);
} catch (error) {}
}
// We received new React Native Page ID.
#newLegacyReactNativePage(page: Page) {
debug(`React Native page updated to ${page.id}`);
if (
this.#debuggerConnection == null ||
this.#debuggerConnection.pageId !== REACT_NATIVE_RELOADABLE_PAGE_ID
) {
// We can just remember new page ID without any further actions if no
// debugger is currently attached or attached debugger is not
// "Reloadable React Native" connection.
this.#lastConnectedLegacyReactNativePage = page;
return;
}
const oldPageId = this.#lastConnectedLegacyReactNativePage?.id;
this.#lastConnectedLegacyReactNativePage = page;
this.#isLegacyPageReloading = true;
// We already had a debugger connected to React Native page and a
// new one appeared - in this case we need to emulate execution context
// detroy and resend Debugger.enable and Runtime.enable commands to new
// page.
if (oldPageId != null) {
this.#sendDisconnectEventToDevice(oldPageId);
}
this.#sendConnectEventToDevice(page.id);
const toSend = [
{method: 'Runtime.enable', id: 1e9},
{method: 'Debugger.enable', id: 1e9},
];
for (const message of toSend) {
const pageId = this.#debuggerConnection?.pageId ?? null;
this.#deviceEventReporter?.logRequest(message, 'proxy', {
pageId,
frontendUserAgent: this.#debuggerConnection?.userAgent ?? null,
prefersFuseboxFrontend: this.#isPageFuseboxFrontend(pageId),
});
this.#sendMessageToDevice({
event: 'wrappedEvent',
payload: {
pageId: this.#mapToDevicePageId(page.id),
wrappedEvent: JSON.stringify(message),
},
});
}
}
/**
* Given a URL from the debugger frontend, returns the equivalent URL
* reachable from the device.
*/
#debuggerRelativeToDeviceRelativeUrl(
debuggerRelativeUrl: URL,
{debuggerRelativeBaseUrl}: DebuggerConnection,
): URL {
const deviceRelativeUrl = new URL(debuggerRelativeUrl.href);
if (debuggerRelativeUrl.origin === debuggerRelativeBaseUrl.origin) {
deviceRelativeUrl.hostname = this.#deviceRelativeBaseUrl.hostname;
deviceRelativeUrl.port = this.#deviceRelativeBaseUrl.port;
deviceRelativeUrl.protocol = this.#deviceRelativeBaseUrl.protocol;
}
return deviceRelativeUrl;
}
/**
* Given a URL from the device, returns the equivalent URL reachable from
* the debugger frontend.
*/
#deviceRelativeUrlToDebuggerRelativeUrl(
deviceRelativeUrl: URL,
{debuggerRelativeBaseUrl}: DebuggerConnection,
): URL {
const debuggerRelativeUrl = new URL(deviceRelativeUrl.href);
if (deviceRelativeUrl.origin === this.#deviceRelativeBaseUrl.origin) {
debuggerRelativeUrl.hostname = debuggerRelativeBaseUrl.hostname;
debuggerRelativeUrl.port = debuggerRelativeBaseUrl.port;
debuggerRelativeUrl.protocol = debuggerRelativeUrl.protocol;
}
return debuggerRelativeUrl;
}
/**
* Given a URL from the device, returns the equivalent URL reachable from
* this proxy.
*/
#deviceRelativeUrlToServerRelativeUrl(deviceRelativeUrl: URL): URL {
const debuggerRelativeUrl = new URL(deviceRelativeUrl.href);
if (deviceRelativeUrl.origin === this.#deviceRelativeBaseUrl.origin) {
debuggerRelativeUrl.hostname = this.#serverRelativeBaseUrl.hostname;
debuggerRelativeUrl.port = this.#serverRelativeBaseUrl.port;
debuggerRelativeUrl.protocol = this.#serverRelativeBaseUrl.protocol;
}
return debuggerRelativeUrl;
}
// Allows to make changes in incoming message from device.
#processMessageFromDeviceLegacy(
payload: CDPServerMessage,
debuggerInfo: DebuggerConnection,
pageId: ?string,
) {
// TODO(moti): Handle null case explicitly, or ideally associate a copy
// of the page metadata object with the connection so this can never be
// null.
const page: ?Page = pageId != null ? this.#pages.get(pageId) : null;
// Replace Android addresses for scriptParsed event.
if (
(!page || !this.#pageHasCapability(page, 'nativeSourceCodeFetching')) &&
payload.method === 'Debugger.scriptParsed' &&
payload.params != null
) {
const params = payload.params;
if ('sourceMapURL' in params) {
const sourceMapURL = this.#tryParseHTTPURL(params.sourceMapURL);
if (sourceMapURL) {
// Rewrite device-relative URLs to de debugger-relative URLs for the
// frontend.
payload.params.sourceMapURL =
this.#deviceRelativeUrlToDebuggerRelativeUrl(
sourceMapURL,
debuggerInfo,
).href;
}
}
if ('url' in params) {
let serverRelativeUrl = params.url;
const parsedUrl = this.#tryParseHTTPURL(params.url);
if (parsedUrl) {
// Rewrite device-relative URLs pointing to the server so that they're
// reachable from the frontend.
payload.params.url = this.#deviceRelativeUrlToDebuggerRelativeUrl(
parsedUrl,
debuggerInfo,
).href;
// Determine the server-relative URL.
serverRelativeUrl =
this.#deviceRelativeUrlToServerRelativeUrl(parsedUrl).href;
}
// Chrome doesn't download source maps if URL param is not a valid
// URL. Some frameworks pass alphanumeric script ID instead of URL which causes
// Chrome to not download source maps. In this case we want to prepend script ID
// with 'file://' prefix.
if (payload.params.url.match(/^[0-9a-z]+$/)) {
payload.params.url = FILE_PREFIX + payload.params.url;
debuggerInfo.prependedFilePrefix = true;
}
if ('scriptId' in params && params.scriptId != null) {
// Set a server-relative URL to locally fetch source by script ID
// on Debugger.getScriptSource.
this.#scriptIdToSourcePathMapping.set(
params.scriptId,
serverRelativeUrl,
);
}
}
}
if (
payload.method === 'Runtime.executionContextCreated' &&
this.#isLegacyPageReloading
) {
// The new context is ready. First notify Chrome that we've reloaded so
// it'll resend its breakpoints. If we do this earlier, we may not be
// ready to receive them.
debuggerInfo.socket.send(
JSON.stringify({method: 'Runtime.executionContextsCleared'}),
);
// The VM starts in a paused mode. Ask it to resume.
// Note that if setting breakpoints in early initialization functions,
// there's a currently race condition between these functions executing
// and Chrome re-applying the breakpoints due to the message above.
//
// This is not an issue in VSCode/Nuclide where the IDE knows to resume
// at its convenience.
const resumeMessage = {method: 'Debugger.resume', id: 0};
this.#deviceEventReporter?.logRequest(resumeMessage, 'proxy', {
pageId: this.#debuggerConnection?.pageId ?? null,
frontendUserAgent: this.#debuggerConnection?.userAgent ?? null,
prefersFuseboxFrontend: this.#isPageFuseboxFrontend(
this.#debuggerConnection?.pageId,
),
});
this.#sendMessageToDevice({
event: 'wrappedEvent',
payload: {
pageId: this.#mapToDevicePageId(debuggerInfo.pageId),
wrappedEvent: JSON.stringify(resumeMessage),
},
});
this.#isLegacyPageReloading = false;
}
}
/**
* Intercept an incoming message from a connected debugger. Returns either an
* original/replacement CDP message object, or `null` (will forward nothing
* to the target).
*/
#interceptClientMessageForSourceFetching(
req: CDPClientMessage,
debuggerInfo: DebuggerConnection,
socket: WS,
): CDPClientMessage | null {
switch (req.method) {
case 'Debugger.setBreakpointByUrl':
return this.#processDebuggerSetBreakpointByUrl(req, debuggerInfo);
case 'Debugger.getScriptSource':
// Sends response to debugger via side-effect
this.#processDebuggerGetScriptSource(req, socket);
return null;
case 'Network.loadNetworkResource':
// If we're rewriting URLs (to frontend-relative), we don't want to
// pass these URLs to the device, since it may try to fetch, return a
// CDP *result* (not error) with a network failure, and CDT
// will *not* then fall back to fetching locally.
//
// Instead, take the absence of a nativeSourceCodeFetching
// capability as a signal to never pass a loadNetworkResource request
// to the device. By returning a CDP error, the frontend should fetch.
const result = {
error: {
code: -32601, // Method not found
message:
'[inspector-proxy]: Page lacks nativeSourceCodeFetching capability.',
},
};
const response = {id: req.id, result};
socket.send(JSON.stringify(response));
const pageId = this.#debuggerConnection?.pageId ?? null;
this.#deviceEventReporter?.logResponse(response, 'proxy', {
pageId,
frontendUserAgent: this.#debuggerConnection?.userAgent ?? null,
prefersFuseboxFrontend: this.#isPageFuseboxFrontend(pageId),
});
return null;
default:
return req;
}
}
#processDebuggerSetBreakpointByUrl(
req: CDPRequest<'Debugger.setBreakpointByUrl'>,
debuggerInfo: DebuggerConnection,
): CDPRequest<'Debugger.setBreakpointByUrl'> {
// If we replaced Android emulator's address to localhost we need to change it back.
const {debuggerRelativeBaseUrl, prependedFilePrefix} = debuggerInfo;
const processedReq = {...req, params: {...req.params}};
if (processedReq.params.url != null) {
const originalUrlParam = processedReq.params.url;
const httpUrl = this.#tryParseHTTPURL(originalUrlParam);
if (httpUrl) {
processedReq.params.url = this.#debuggerRelativeToDeviceRelativeUrl(
httpUrl,
debuggerInfo,
).href;
} else if (
originalUrlParam.startsWith(FILE_PREFIX) &&
prependedFilePrefix
) {
// Remove fake URL prefix if we modified URL in #processMessageFromDeviceLegacy.
processedReq.params.url = originalUrlParam.slice(FILE_PREFIX.length);
}
}
// Retain special case rewriting of localhost to device-relative IPs
// within regex patterns. We don't rewrite the protocol here because
// these patterns typically come from CDT reinterpreting the source URL
// `file://host/path` into the regex `host/path|file://host/path`. See:
//
// https://github.com/ChromeDevTools/devtools-frontend/blob/f913cc6d76f2e2639c05b11ba673fc880b5490dd/front_end/core/sdk/DebuggerModel.ts#L505
//
// This has always been fragile and probably unnecessary - we don't set
// `file://` source URLs. It can be removed when we drop support for
// legacy targets, if not sooner.
if (
// Android's stock emulator and other emulators such as genymotion use a
// standard localhost alias.
new Set(['10.0.2.2', '10.0.3.2']).has(
this.#deviceRelativeBaseUrl.hostname,
) &&
debuggerRelativeBaseUrl.hostname === 'localhost' &&
processedReq.params.urlRegex != null
) {
processedReq.params.urlRegex = processedReq.params.urlRegex.replaceAll(
'localhost',
// regex-escape IPv4
this.#deviceRelativeBaseUrl.hostname.replaceAll('.', '\\.'),
);
}
return processedReq;
}
#processDebuggerGetScriptSource(
req: CDPRequest<'Debugger.getScriptSource'>,
socket: WS,
): void {
const sendSuccessResponse = (scriptSource: string) => {
const result = {scriptSource};
const response: CDPResponse<'Debugger.getScriptSource'> = {
id: req.id,
result,
};
socket.send(JSON.stringify(response));
const pageId = this.#debuggerConnection?.pageId ?? null;
this.#deviceEventReporter?.logResponse(response, 'proxy', {
pageId,
frontendUserAgent: this.#debuggerConnection?.userAgent ?? null,
prefersFuseboxFrontend: this.#isPageFuseboxFrontend(pageId),
});
};
const sendErrorResponse = (error: string) => {
// Tell the client that the request failed
const result = {error: {message: error}};
const response = {id: req.id, result};
socket.send(JSON.stringify(response));
// Send to the console as well, so the user can see it
this.#sendErrorToDebugger(error);
const pageId = this.#debuggerConnection?.pageId ?? null;
this.#deviceEventReporter?.logResponse(response, 'proxy', {
pageId,
frontendUserAgent: this.#debuggerConnection?.userAgent ?? null,
prefersFuseboxFrontend: this.#isPageFuseboxFrontend(pageId),
});
};
const pathToSource = this.#scriptIdToSourcePathMapping.get(
req.params.scriptId,
);
if (pathToSource != null) {
const httpURL = this.#tryParseHTTPURL(pathToSource);
if (httpURL) {
// URL is server-relatve, so we should be able to fetch it from here.
this.#fetchText(httpURL).then(
text => sendSuccessResponse(text),
err =>
sendErrorResponse(
`Failed to fetch source url ${pathToSource}: ${err.message}`,
),
);
} else {
let file;
try {
file = fs.readFileSync(
path.resolve(this.#projectRoot, pathToSource),