Skip to content

Commit

Permalink
Add support for launch ID (facebook#43585)
Browse files Browse the repository at this point in the history
Summary:

Changelog: [internal]

Related PR: facebookexperimental/rn-chrome-devtools-frontend#27

Differential Revision: D55164646
  • Loading branch information
EdmondChuiHW authored and facebook-github-bot committed Mar 28, 2024
1 parent 4841373 commit 91807c2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('getDevToolsFrontendUrl', () => {
enableOpenDebuggerRedirect: false,
};

describe('relative: false (default)', () => {
describe('relative: false, launchId: non-null (default)', () => {
test('should return a valid url for all experiments off', async () => {
const actual = getDevToolsFrontendUrl(
experiments,
Expand Down Expand Up @@ -128,4 +128,61 @@ describe('getDevToolsFrontendUrl', () => {
);
});
});

describe('launchId: non-null', () => {
const launchId = 'dG8gdGhlIG1vb24h%21';

test('should return a valid url for all experiments off', async () => {
const actual = getDevToolsFrontendUrl(
experiments,
webSocketDebuggerUrl,
devServerUrl,
{
launchId,
},
);
const url = new URL(actual);
expect(url.pathname).toBe('/debugger-frontend/rn_inspector.html');
expect(url.searchParams.get('ws')).toBe(
'/inspector/debug?device=1a9372c&page=-1',
);
expect(url.searchParams.get('launchId')).toBe(launchId);
});

test('should return a valid url for enableNetworkInspector experiment on', async () => {
const actual = getDevToolsFrontendUrl(
{...experiments, enableNetworkInspector: true, enableNewDebugger: true},
webSocketDebuggerUrl,
devServerUrl,
{
launchId,
},
);
const url = new URL(actual);
expect(url.pathname).toBe('/debugger-frontend/rn_inspector.html');
expect(url.searchParams.get('unstable_enableNetworkPanel')).toBe('true');
expect(url.searchParams.get('ws')).toBe(
'/inspector/debug?device=1a9372c&page=-1',
);
expect(url.searchParams.get('launchId')).toBe(launchId);
});

test('should return a full WS URL if on a different host than the dev server', () => {
const otherWebSocketDebuggerUrl =
'ws://localhost:8082/inspector/debug?device=1a9372c&page=-1';
const actual = getDevToolsFrontendUrl(
experiments,
otherWebSocketDebuggerUrl,
devServerUrl,
{
launchId,
},
);
const url = new URL(actual);
expect(url.searchParams.get('ws')).toBe(
'localhost:8082/inspector/debug?device=1a9372c&page=-1',
);
expect(url.searchParams.get('launchId')).toBe(launchId);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {NextHandleFunction} from 'connect';
import type {IncomingMessage, ServerResponse} from 'http';

import getDevToolsFrontendUrl from '../utils/getDevToolsFrontendUrl';
import crypto from 'crypto';
import url from 'url';

const debuggerInstances = new Map<string, ?LaunchedBrowser>();
Expand Down Expand Up @@ -107,6 +108,8 @@ export default function openDebuggerMiddleware({
return;
}

const launchId = crypto.randomUUID();

try {
switch (launchType) {
case 'launch':
Expand All @@ -122,6 +125,7 @@ export default function openDebuggerMiddleware({
experiments,
target.webSocketDebuggerUrl,
serverBaseUrl,
{launchId},
),
),
);
Expand All @@ -133,7 +137,7 @@ export default function openDebuggerMiddleware({
experiments,
target.webSocketDebuggerUrl,
serverBaseUrl,
{relative: true},
{relative: true, launchId},
),
});
res.end();
Expand Down
4 changes: 4 additions & 0 deletions packages/dev-middleware/src/utils/getDevToolsFrontendUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function getDevToolsFrontendUrl(
devServerUrl: string,
options?: $ReadOnly<{
relative?: boolean,
launchId?: string,
}>,
): string {
const wsParam = getWsParam({
Expand All @@ -38,6 +39,9 @@ export default function getDevToolsFrontendUrl(
if (experiments.enableNetworkInspector) {
searchParams.append('unstable_enableNetworkPanel', 'true');
}
if (options?.launchId != null && options.launchId !== '') {
searchParams.append('launchId', options.launchId);
}

return appUrl + '?' + searchParams.toString();
}
Expand Down

0 comments on commit 91807c2

Please sign in to comment.