Skip to content

Commit 56174d6

Browse files
authored
feat(vite): choose each device's dev server origin, with the iOS USB tunnel (#6210)
When the project's @nativescript/vite lists the platform under nativescriptCli.devServerFile, the CLI chooses each device's dev server origin and delivers { origin, token } as dev-server.json next to bundle.mjs, per device. First match: NS_HMR_HOST; Android with adb reverse working, 127.0.0.1; an emulator without reverse, 10.0.2.2; an iOS/visionOS simulator, 127.0.0.1; a physical iOS device with an open USB tunnel, the Mac's end of that tunnel (http://[fdXX::2]:5173); otherwise the Mac's LAN IPv4. NS_HTTPS gives an https origin, NS_HMR_NO_ADB_REVERSE skips the reverse and NS_HMR_PREFER_LAN_HOST gives every device the LAN origin. The CLI generates the dev server's WebSocket token once per platform and passes it as NS_HMR_TOKEN. Delivery: written into the installed simulator .app after the sync and after a reinstall; an app/dev-server.json entry in a physical iOS device's sync.zip; on Android, sent from memory over the livesync socket on every full sync (AndroidLivesyncTool.sendFileFromBuffer). A native rebuild or recovery reinstall delivers the file again, and a failed delivery is retried on the device's next sync. USB tunnel: for each physical iOS 17+ / visionOS device on USB, the CLI holds the CoreDevice tunnel with `devicectl device notification observe` and waits (bounded) for it to connect. When a tunnel drops, the CLI opens a new one for a device still on USB (at most once a minute per device), otherwise chooses another origin, rewrites that device's dev-server.json and restarts only its app. Hot updates over the tunnel need @valor/nativescript-websockets 2.0.4 or later. An @nativescript/vite without devServerFile keeps the bundle-baked origin and the NS_ADB_* hand-off, and opens no tunnel.
1 parent b4c2654 commit 56174d6

26 files changed

Lines changed: 3115 additions & 71 deletions

‎lib/bootstrap.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { provideLazy } from "./common/di";
22
import { injector } from "./common/yok";
33
import { registerBuiltInCommand } from "./common/services/command-definition-adapter";
4+
import { DevServerOriginService } from "./contracts/internal/dev-server-origin-service";
5+
import { IOSDeviceTunnelService } from "./contracts/internal/ios-device-tunnel-service";
46
import type { fontsCommandDefinition } from "./commands/fonts";
57
import { AndroidPluginMirrorService } from "./contracts/internal/android-plugin-mirror-service";
68
import { NativeFileHashesMemo } from "./contracts/internal/native-file-hashes-memo";
@@ -694,6 +696,22 @@ injector.require(
694696
"viteHmrPortService",
695697
"./services/bundler/vite-hmr-port-service",
696698
);
699+
injector.register(
700+
provideLazy(
701+
IOSDeviceTunnelService,
702+
() =>
703+
require("./services/ios/ios-device-tunnel-service")
704+
.IOSDeviceTunnelServiceImpl,
705+
),
706+
);
707+
injector.register(
708+
provideLazy(
709+
DevServerOriginService,
710+
() =>
711+
require("./services/bundler/dev-server-origin-service")
712+
.DevServerOriginServiceImpl,
713+
),
714+
);
697715

698716
injector.require(
699717
"applePortalSessionService",

‎lib/constants.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ export const RSPACK_PLUGIN_NAME = "@nativescript/rspack";
2323
// and tells `@nativescript/vite` where via `NS_VITE_DIST_DIR`; the package's
2424
// own fallback (`.ns-vite-build`) only applies to standalone `vite` runs.
2525
export const VITE_DIST_FOLDER_NAME = ".ns-vite-build";
26+
/**
27+
* Per-device file next to `bundle.mjs` that tells a Vite HMR app the dev
28+
* server's origin and token. Never part of the shared platforms app folder:
29+
* the origin differs between devices.
30+
*/
31+
export const DEV_SERVER_FILE_NAME = "dev-server.json";
2632
export const TNS_CORE_MODULES_WIDGETS_NAME = "tns-core-modules-widgets";
2733
export const UI_MOBILE_BASE_NAME = "@nativescript/ui-mobile-base";
2834
export const TNS_ANDROID_RUNTIME_NAME = "tns-android";
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Contract } from "../../common/di/contract";
2+
3+
export interface IDevServerOriginOptions {
4+
/** Android: `adb reverse tcp:<port> tcp:<port>` is confirmed for the device. */
5+
adbReverse?: boolean;
6+
}
7+
8+
/**
9+
* Decides, per device, the origin an app uses to reach the platform's Vite
10+
* dev server, and the content of the `dev-server.json` file that hands the
11+
* origin and the dev server's token to the app.
12+
*/
13+
@Contract()
14+
export abstract class DevServerOriginService {
15+
/**
16+
* The token of the platform's dev server. Generated on first use and kept
17+
* for the life of the process, so a restarted dev server still accepts the
18+
* files already on the devices.
19+
*/
20+
abstract getToken(platform: string): string;
21+
22+
/**
23+
* Chooses the device's origin from what the CLI knows about how it reaches
24+
* this machine, records it and resolves it. A physical iOS / visionOS
25+
* device uses its USB tunnel only while one is open.
26+
*/
27+
abstract chooseOrigin(
28+
device: Mobile.IDevice,
29+
options?: IDevServerOriginOptions,
30+
): Promise<string>;
31+
32+
/** The device's last chosen origin, if any. */
33+
abstract getOrigin(deviceIdentifier: string): string | undefined;
34+
35+
/**
36+
* The `dev-server.json` content for the device, or null when no origin was
37+
* chosen for it, in which case nothing is delivered.
38+
*/
39+
abstract getDevServerFile(deviceIdentifier: string): string | null;
40+
41+
abstract forget(deviceIdentifier: string): void;
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Contract } from "../../common/di/contract";
2+
3+
/**
4+
* Holds the CoreDevice tunnel of a USB-connected iOS 17+ / visionOS device
5+
* so an app can reach the Vite dev server on the Mac over the cable, without
6+
* Wi-Fi or the Local Network prompt.
7+
*/
8+
@Contract()
9+
export abstract class IOSDeviceTunnelService {
10+
/**
11+
* Opens the device's CoreDevice tunnel and keeps it up until `close` or
12+
* the CLI exits. Resolves the Mac's address on the tunnel, or null when
13+
* the device has no usable USB tunnel. A tunnel gets a new address every
14+
* time it is re-established.
15+
*/
16+
abstract open(device: Mobile.IDevice): Promise<string | null>;
17+
18+
/**
19+
* The Mac's address on the device's tunnel while the tunnel is up.
20+
*/
21+
abstract getHostAddress(deviceIdentifier: string): string | undefined;
22+
23+
abstract close(deviceIdentifier: string): Promise<void>;
24+
25+
/**
26+
* Calls `listener` with the device's identifier when an open tunnel goes
27+
* down without `close`, after `getHostAddress` stopped returning it.
28+
*/
29+
abstract onTunnelLost(listener: (deviceIdentifier: string) => void): void;
30+
}

‎lib/contracts/vite-hmr-port-service.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ export abstract class ViteHmrPortService {
99
* Resolves the port the Vite dev server for `platform` listens on: the
1010
* first free port at or above `NS_HMR_PORT` (default 5173) that no other
1111
* platform in this process holds. Resolved once per platform and stable
12-
* for the life of the process, so the build watcher (which bakes the port
13-
* into `bundle.mjs`), the dev server and the Android `adb reverse` tunnel
14-
* all agree on it.
12+
* for the life of the process, so the dev server, the origin each device
13+
* is given and the Android `adb reverse` tunnel all agree on it.
1514
*
1615
* With `NS_HMR_STRICT_PORT` set, a busy preferred port fails instead of
1716
* moving to the next one.

0 commit comments

Comments
 (0)