Skip to content

Commit e820949

Browse files
committed
refactor: split client and Metro URL ownership (#2146)
1 parent 81a9cb2 commit e820949

8 files changed

Lines changed: 36 additions & 12 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test } from 'vitest';
22
import assert from 'node:assert/strict';
3-
import { normalizeBaseUrl } from '../url.ts';
3+
import { normalizeBaseUrl } from './base-url.ts';
44

55
test('normalizeBaseUrl trims trailing slashes without changing other characters', () => {
66
assert.equal(normalizeBaseUrl('https://example.test'), 'https://example.test');

‎src/client/base-url.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function normalizeBaseUrl(input: string): string {
2+
let end = input.length;
3+
while (end > 0 && input.charCodeAt(end - 1) === 47) {
4+
end -= 1;
5+
}
6+
return end === input.length ? input : input.slice(0, end);
7+
}

‎src/client/client-companion-tunnel-worker.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import type {
2525
MetroTunnelRequestMessage as MetroCompanionRequest,
2626
MetroTunnelResponseMessage,
2727
} from '../metro/metro.ts';
28-
import { normalizeBaseUrl } from '../utils/url.ts';
28+
import { normalizeBaseUrl } from './base-url.ts';
2929

3030
const COMPANION_REGISTER_TIMEOUT_MS = 5_000;
3131
const COMPANION_REGISTER_MAX_RETRY_DELAY_MS = 60_000;

‎src/client/client-companion-tunnel.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
ENV_COMPANION_TUNNEL_STATE_PATH,
1818
ENV_COMPANION_TUNNEL_UNREGISTER_PATH,
1919
} from './client-companion-tunnel-contract.ts';
20-
import { normalizeBaseUrl } from '../utils/url.ts';
20+
import { normalizeBaseUrl } from './base-url.ts';
2121
import { runCmdDetached } from '@agent-device/host-kit/command';
2222
import {
2323
isProcessAlive,

‎src/metro/bundle-url.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { test } from 'vitest';
2+
import assert from 'node:assert/strict';
3+
import { buildBundleUrl } from './bundle-url.ts';
4+
5+
test('buildBundleUrl normalizes the base URL and adds Metro query parameters', () => {
6+
assert.equal(
7+
buildBundleUrl('https://example.test///', 'ios'),
8+
'https://example.test/index.bundle?platform=ios&dev=true&minify=false',
9+
);
10+
});
11+
12+
test('buildBundleUrl preserves the URL path and serializes a custom entry path', () => {
13+
assert.equal(
14+
buildBundleUrl(
15+
'https://example.test:8081/api/metro/runtimes/runtime-1/',
16+
'android',
17+
'.expo/.virtual-metro-entry.bundle',
18+
),
19+
'https://example.test:8081/api/metro/runtimes/runtime-1/.expo/.virtual-metro-entry.bundle?platform=android&dev=true&minify=false',
20+
);
21+
});
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
export function normalizeBaseUrl(input: string): string {
2-
let end = input.length;
3-
while (end > 0 && input.charCodeAt(end - 1) === 47) {
4-
end -= 1;
5-
}
6-
return end === input.length ? input : input.slice(0, end);
7-
}
1+
import { normalizeBaseUrl } from '../client/base-url.ts';
82

93
export function buildBundleUrl(
104
baseUrl: string,

‎src/metro/client-metro.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import {
2929
readProjectPackageJson,
3030
type PackageJsonShape,
3131
} from '../utils/project-runtime.ts';
32-
import { buildBundleUrl, normalizeBaseUrl } from '../utils/url.ts';
32+
import { normalizeBaseUrl } from '../client/base-url.ts';
33+
import { buildBundleUrl } from './bundle-url.ts';
3334
import {
3435
EXPO_VIRTUAL_ENTRY_BUNDLE_PATH,
3536
parsePort,

‎src/sdk/metro.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export { buildBundleUrl, normalizeBaseUrl } from '../utils/url.ts';
1+
export { normalizeBaseUrl } from '../client/base-url.ts';
2+
export { buildBundleUrl } from '../metro/bundle-url.ts';
23
export type {
34
MetroBridgeDescriptor,
45
MetroTunnelRequestMessage,

0 commit comments

Comments
 (0)