Skip to content

Commit f4d2d39

Browse files
vzaidmanfacebook-github-bot
authored andcommitted
add dev middleware ws socket close reasons (#50071)
Summary: Pull Request resolved: #50071 Pull Request resolved: #50070 Changelog: [General][Internal] Send code and close reasons when we close the connection to any websockets from the dev middleware This should improve the debuggability of our code. Reviewed By: robhogan Differential Revision: D71314509 fbshipit-source-id: 1d6fc57a8601bcea78e95a87d423c7c46c51e799
1 parent a802be0 commit f4d2d39

File tree

1 file changed

+38
-8
lines changed
  • packages/dev-middleware/src/inspector-proxy

1 file changed

+38
-8
lines changed

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

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ const debug = require('debug')('Metro:InspectorProxy');
4040
const PAGES_POLLING_INTERVAL = 1000;
4141
const MIN_MESSAGE_QUEUE_BYTES_TO_REPORT = 2 * 1024 * 1024; // 2 MiB
4242

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+
4355
// Prefix for script URLs that are alphanumeric IDs. See comment in #processMessageFromDeviceLegacy method for
4456
// more details.
4557
const FILE_PREFIX = 'file://';
@@ -251,19 +263,22 @@ export default class Device {
251263
if (socket === this.#deviceSocket) {
252264
this.#deviceEventReporter?.logDisconnection('device');
253265
// Device disconnected - close debugger connection.
254-
this.#terminateDebuggerConnection();
266+
this.#terminateDebuggerConnection(
267+
WS_CLOSURE_CODE.NORMAL,
268+
WS_CLOSE_REASON.DEVICE_DISCONNECTED,
269+
);
255270
clearInterval(this.#pagesPollingIntervalId);
256271
}
257272
});
258273
}
259274

260-
#terminateDebuggerConnection() {
275+
#terminateDebuggerConnection(code?: number, reason?: string) {
261276
const debuggerConnection = this.#debuggerConnection;
262277
if (debuggerConnection) {
263278
this.#sendDisconnectEventToDevice(
264279
this.#mapToDevicePageId(debuggerConnection.pageId),
265280
);
266-
debuggerConnection.socket.close();
281+
debuggerConnection.socket.close(code, reason);
267282
this.#debuggerConnection = null;
268283
}
269284
}
@@ -286,15 +301,24 @@ export default class Device {
286301
const oldDebugger = this.#debuggerConnection;
287302

288303
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+
);
291312
}
292313

293314
this.#debuggerConnection = null;
294315

295316
if (oldDebugger) {
296317
oldDebugger.socket.removeAllListeners();
297-
this.#deviceSocket.close();
318+
this.#deviceSocket.close(
319+
WS_CLOSURE_CODE.NORMAL,
320+
WS_CLOSE_REASON.RECREATING_DEVICE,
321+
);
298322
this.handleDebuggerConnection(oldDebugger.socket, oldDebugger.pageId, {
299323
debuggerRelativeBaseUrl: oldDebugger.debuggerRelativeBaseUrl,
300324
userAgent: oldDebugger.userAgent,
@@ -345,15 +369,21 @@ export default class Device {
345369
`Got new debugger connection via ${debuggerRelativeBaseUrl.href} for ` +
346370
`page ${pageId} of ${this.#name}, but no such page exists`,
347371
);
348-
socket.close();
372+
socket.close(
373+
WS_CLOSURE_CODE.INTERNAL_ERROR,
374+
WS_CLOSE_REASON.PAGE_NOT_FOUND,
375+
);
349376
return;
350377
}
351378

352379
// Clear any commands we were waiting on.
353380
this.#deviceEventReporter?.logDisconnection('debugger');
354381

355382
// 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+
);
357387

358388
this.#deviceEventReporter?.logConnection('debugger', {
359389
pageId,

0 commit comments

Comments
 (0)