Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow client-side device identifiers in inspector proxy #991

Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion packages/metro-inspector-proxy/src/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const REACT_NATIVE_RELOADABLE_PAGE_ID = '-1';
*/
class Device {
// ID of the device.
_id: number;
_id: string;

// Name of the device.
_name: string;
Expand Down Expand Up @@ -134,6 +134,10 @@ class Device {
return this._name;
}

getApp(): string {
return this._app;
}

getPagesList(): Array<Page> {
if (this._lastConnectedReactNativePage) {
const reactNativeReloadablePage = {
Expand Down Expand Up @@ -212,6 +216,35 @@ class Device {
};
}

// Handles cleaning up a duplicate device connection, by client-side device ID.
// 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 allows users to reload the app, either as result of a crash, or manually
// reloading, without having to restart the debugger.
huntie marked this conversation as resolved.
Show resolved Hide resolved
handleDuplicateDeviceConnection(newDevice: Device) {
if (
this._app !== newDevice.getApp() ||
this._name !== newDevice.getName()
) {
this._deviceSocket.close();
this._debuggerConnection?.socket.close();
}

const oldDebugger = this._debuggerConnection;
this._debuggerConnection = null;

if (oldDebugger) {
oldDebugger.socket.removeAllListeners();
this._deviceSocket.close();
newDevice.handleDebuggerConnection(
oldDebugger.socket,
oldDebugger.pageId,
);
}
}

// Handles messages received from device:
// 1. For getPages responses updates local _pages list.
// 2. All other messages are forwarded to debugger as wrappedEvent.
Expand Down
29 changes: 22 additions & 7 deletions packages/metro-inspector-proxy/src/InspectorProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class InspectorProxy {
_projectRoot: string;

// Maps device ID to Device instance.
_devices: Map<number, Device>;
_devices: Map<string, Device>;

// Internal counter for device IDs -- just gets incremented for each new device.
_deviceCounter: number = 0;
Expand Down Expand Up @@ -111,7 +111,7 @@ class InspectorProxy {
// Converts page information received from device into PageDescription object
// that is sent to debugger.
_buildPageDescription(
deviceId: number,
deviceId: string,
device: Device,
page: Page,
): PageDescription {
Expand Down Expand Up @@ -162,16 +162,31 @@ class InspectorProxy {
// $FlowFixMe[value-as-type]
wss.on('connection', async (socket: WS, req) => {
try {
const fallbackDeviceId = String(this._deviceCounter++);

const query = url.parse(req.url || '', true).query || {};
const deviceId = query.device || fallbackDeviceId;
const deviceName = query.name || 'Unknown';
const appName = query.app || 'Unknown';
const deviceId = this._deviceCounter++;
this._devices.set(

const oldDevice = this._devices.get(deviceId);
const newDevice = new Device(
deviceId,
new Device(deviceId, deviceName, appName, socket, this._projectRoot),
deviceName,
appName,
socket,
this._projectRoot,
);

debug(`Got new connection: device=${deviceName}, app=${appName}`);
if (oldDevice) {
oldDevice.handleDuplicateDeviceConnection(newDevice);
}

this._devices.set(deviceId, newDevice);

debug(
`Got new connection: device=${deviceName}, app=${appName}, id=${deviceId}`,
byCedric marked this conversation as resolved.
Show resolved Hide resolved
);

socket.on('close', () => {
this._devices.delete(deviceId);
Expand Down Expand Up @@ -206,7 +221,7 @@ class InspectorProxy {
throw new Error('Incorrect URL - must provide device and page IDs');
}

const device = this._devices.get(parseInt(deviceId, 10));
const device = this._devices.get(deviceId);
if (device == null) {
throw new Error('Unknown device with ID ' + deviceId);
}
Expand Down