@@ -40,6 +40,18 @@ const debug = require('debug')('Metro:InspectorProxy');
40
40
const PAGES_POLLING_INTERVAL = 1000 ;
41
41
const MIN_MESSAGE_QUEUE_BYTES_TO_REPORT = 2 * 1024 * 1024 ; // 2 MiB
42
42
43
+ const WS_CLOSURE_CODE = {
44
+ NORMAL : 1000 ,
45
+ INTERNAL_ERROR : 1011 ,
46
+ } ;
47
+
48
+ export const WS_CLOSE_REASON = {
49
+ PAGE_NOT_FOUND : 'Debugger Page Not Found' ,
50
+ DEVICE_DISCONNECTED : 'Corresponding Device Disconnected' ,
51
+ RECREATING_DEVICE : 'Recreating Device Connection' ,
52
+ RECREATING_DEBUGGER : 'Recreating Debugger Connection' ,
53
+ } ;
54
+
43
55
// Prefix for script URLs that are alphanumeric IDs. See comment in #processMessageFromDeviceLegacy method for
44
56
// more details.
45
57
const FILE_PREFIX = 'file://' ;
@@ -251,19 +263,22 @@ export default class Device {
251
263
if ( socket === this . #deviceSocket) {
252
264
this . #deviceEventReporter?. logDisconnection ( 'device' ) ;
253
265
// Device disconnected - close debugger connection.
254
- this . #terminateDebuggerConnection( ) ;
266
+ this . #terminateDebuggerConnection(
267
+ WS_CLOSURE_CODE . NORMAL ,
268
+ WS_CLOSE_REASON . DEVICE_DISCONNECTED ,
269
+ ) ;
255
270
clearInterval ( this . #pagesPollingIntervalId) ;
256
271
}
257
272
} ) ;
258
273
}
259
274
260
- #terminateDebuggerConnection( ) {
275
+ #terminateDebuggerConnection( code ?: number , reason ?: string ) {
261
276
const debuggerConnection = this . #debuggerConnection;
262
277
if ( debuggerConnection ) {
263
278
this . #sendDisconnectEventToDevice(
264
279
this . #mapToDevicePageId( debuggerConnection . pageId ) ,
265
280
) ;
266
- debuggerConnection . socket . close ( ) ;
281
+ debuggerConnection . socket . close ( code , reason ) ;
267
282
this . #debuggerConnection = null ;
268
283
}
269
284
}
@@ -286,15 +301,24 @@ export default class Device {
286
301
const oldDebugger = this . #debuggerConnection;
287
302
288
303
if ( this . #app !== deviceOptions . app || this . #name !== deviceOptions . name ) {
289
- this . #deviceSocket. close ( ) ;
290
- this . #terminateDebuggerConnection( ) ;
304
+ this . #deviceSocket. close (
305
+ WS_CLOSURE_CODE . NORMAL ,
306
+ WS_CLOSE_REASON . RECREATING_DEVICE ,
307
+ ) ;
308
+ this . #terminateDebuggerConnection(
309
+ WS_CLOSURE_CODE . NORMAL ,
310
+ WS_CLOSE_REASON . RECREATING_DEVICE ,
311
+ ) ;
291
312
}
292
313
293
314
this.#debuggerConnection = null;
294
315
295
316
if (oldDebugger) {
296
317
oldDebugger . socket . removeAllListeners ( ) ;
297
- this . #deviceSocket. close ( ) ;
318
+ this . #deviceSocket. close (
319
+ WS_CLOSURE_CODE . NORMAL ,
320
+ WS_CLOSE_REASON . RECREATING_DEVICE ,
321
+ ) ;
298
322
this . handleDebuggerConnection ( oldDebugger . socket , oldDebugger . pageId , {
299
323
debuggerRelativeBaseUrl : oldDebugger . debuggerRelativeBaseUrl ,
300
324
userAgent : oldDebugger . userAgent ,
@@ -345,15 +369,21 @@ export default class Device {
345
369
`Got new debugger connection via ${ debuggerRelativeBaseUrl . href } for ` +
346
370
`page ${ pageId } of ${ this . #name} , but no such page exists` ,
347
371
) ;
348
- socket . close ( ) ;
372
+ socket . close (
373
+ WS_CLOSURE_CODE . INTERNAL_ERROR ,
374
+ WS_CLOSE_REASON . PAGE_NOT_FOUND ,
375
+ ) ;
349
376
return ;
350
377
}
351
378
352
379
// Clear any commands we were waiting on.
353
380
this.#deviceEventReporter?.logDisconnection('debugger');
354
381
355
382
// Disconnect current debugger if we already have debugger connected.
356
- this.#terminateDebuggerConnection();
383
+ this.#terminateDebuggerConnection(
384
+ WS_CLOSURE_CODE.NORMAL,
385
+ WS_CLOSE_REASON.RECREATING_DEBUGGER,
386
+ );
357
387
358
388
this.#deviceEventReporter?.logConnection('debugger', {
359
389
pageId ,
0 commit comments