Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
45 changes: 45 additions & 0 deletions packages/agent-cdp/src/__tests__/discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
buildTargetId,
decodeTargetSource,
DEFAULT_DISCOVERY_URLS,
dedupeReactNativeTargets,
discoverTargets,
encodeTargetSource,
getDiscoveryUrl,
Expand Down Expand Up @@ -85,6 +86,50 @@ describe("discovery helpers", () => {
});
});

it("keeps only the newest react native target when ids differ only by numeric suffix", () => {
expect(
dedupeReactNativeTargets([
{
id: "react-native:MTI3LjAuMC4xOjgwODE:device-1-1",
rawId: "device-1-1",
title: "Expo",
kind: "react-native",
description: "React Native Bridgeless [C++ connection]",
appId: "host.exp.Exponent",
webSocketDebuggerUrl: "ws://127.0.0.1:8081/inspector/debug?device=device-1&page=1",
sourceUrl: "http://127.0.0.1:8081",
reactNative: {
logicalDeviceId: "device-1",
capabilities: {
nativePageReloads: true,
},
},
},
{
id: "react-native:MTI3LjAuMC4xOjgwODE:device-1-2",
rawId: "device-1-2",
title: "Expo",
kind: "react-native",
description: "React Native Bridgeless [C++ connection]",
appId: "host.exp.Exponent",
webSocketDebuggerUrl: "ws://127.0.0.1:8081/inspector/debug?device=device-1&page=2",
sourceUrl: "http://127.0.0.1:8081",
reactNative: {
logicalDeviceId: "device-1",
capabilities: {
nativePageReloads: true,
},
},
},
]),
).toMatchObject([
{
rawId: "device-1-2",
webSocketDebuggerUrl: "ws://127.0.0.1:8081/inspector/debug?device=device-1&page=2",
},
]);
});

it("keeps ids unique across different source urls", () => {
expect(buildTargetId("chrome", "http://127.0.0.1:9222", "page-1")).not.toBe(
buildTargetId("chrome", "http://127.0.0.1:9229", "page-1"),
Expand Down
87 changes: 62 additions & 25 deletions packages/agent-cdp/src/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,39 @@ export function mapReactNativeTarget(sourceUrl: string, target: ReactNativeJsonT
};
}

function getReactNativeDuplicateKey(target: TargetDescriptor): string | null {
if (target.kind !== "react-native") {
return null;
}

const logicalDeviceId = target.reactNative?.logicalDeviceId;
if (!logicalDeviceId) {
return null;
}

const duplicateSuffix = target.rawId.slice(logicalDeviceId.length + 1);
if (!target.rawId.startsWith(`${logicalDeviceId}-`) || !/^\d+$/.test(duplicateSuffix)) {
return null;
}

return [target.sourceUrl, target.appId || "", logicalDeviceId].join("::");
}

export function dedupeReactNativeTargets(targets: TargetDescriptor[]): TargetDescriptor[] {
const deduped = new Map<string, TargetDescriptor>();

for (const target of targets) {
const duplicateKey = getReactNativeDuplicateKey(target);
const mapKey = duplicateKey || target.id;
if (duplicateKey) {
deduped.delete(mapKey);
}
deduped.set(mapKey, target);
}

return [...deduped.values()];
}

export async function fetchJsonTargets<T>(baseUrl: string): Promise<T[]> {
const response = await fetch(`${normalizeBaseUrl(baseUrl)}/json/list`);
if (!response.ok) {
Expand All @@ -150,33 +183,37 @@ export async function discoverTargets(options: DiscoveryOptions): Promise<Target
const urls = getDiscoveryUrls(options);
if (options.url) {
const targets = await fetchJsonTargets<ReactNativeJsonTarget>(urls[0]);
return targets
.map((target) => {
if (target.reactNative) {
return mapReactNativeTarget(urls[0], target);
}

return mapChromeTarget(urls[0], target);
})
.filter((target): target is TargetDescriptor => target !== null);
return dedupeReactNativeTargets(
targets
.map((target) => {
if (target.reactNative) {
return mapReactNativeTarget(urls[0], target);
}

return mapChromeTarget(urls[0], target);
})
.filter((target): target is TargetDescriptor => target !== null),
);
}

const results = await Promise.allSettled(urls.map((url) => fetchJsonTargets<ReactNativeJsonTarget>(url)));

return results.flatMap((result, index) => {
if (result.status !== "fulfilled") {
return [];
}

const url = urls[index];
return result.value
.map((target) => {
if (target.reactNative) {
return mapReactNativeTarget(url, target);
}

return mapChromeTarget(url, target);
})
.filter((target): target is TargetDescriptor => target !== null);
});
return dedupeReactNativeTargets(
results.flatMap((result, index) => {
if (result.status !== "fulfilled") {
return [];
}

const url = urls[index];
return result.value
.map((target) => {
if (target.reactNative) {
return mapReactNativeTarget(url, target);
}

return mapChromeTarget(url, target);
})
.filter((target): target is TargetDescriptor => target !== null);
}),
);
}
Loading