Skip to content

Commit 1113346

Browse files
committed
feat(ios): serve Vite dev over the USB CoreDevice tunnel
A USB-connected iOS 17+ / visionOS device reaches the Mac over a CoreDevice tunnel (a utun with an fdXX::/64 ULA pair) without Wi-Fi and without the Local Network prompt. The tunnel only exists while a client holds it, and every new tunnel gets a new address. For a Vite HMR session whose only target of a platform is such a device, the run controller now opens the tunnel before prepare: it holds it with `devicectl device notification observe` for the session, reads the device's end from `devicectl list devices`, and takes the Mac's address in the same subnet from the local interfaces. The bundler passes that address to both Vite children as NS_HMR_HOST, so bundle.mjs and the dev server use the tunnel origin. The holder is killed when the device leaves the session or the CLI exits; if it dies on its own the CLI warns that the app lost the dev server. Only an @nativescript/vite that lists the platform under nativescriptCli.singleProcessDev gets the tunnel: older releases bind the dev server to NS_HMR_HOST and put an IPv6 literal into URLs unbracketed. A user-set NS_HMR_HOST, simulators, Wi-Fi or network-paired devices, iOS < 17, a missing devicectl, a tunnel that does not come up and sessions with more than one device of the platform also keep the LAN host from @nativescript/vite, with one trace line.
1 parent 7997eea commit 1113346

11 files changed

Lines changed: 950 additions & 3 deletions

File tree

‎lib/bootstrap.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,10 @@ injector.require(
694694
"viteHmrPortService",
695695
"./services/bundler/vite-hmr-port-service",
696696
);
697+
injector.require(
698+
"iOSDeviceTunnelService",
699+
"./services/ios/ios-device-tunnel-service",
700+
);
697701

698702
injector.require(
699703
"applePortalSessionService",

‎lib/contracts/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export { Errors } from "./errors";
5050
export { FileSystem } from "./file-system";
5151
export { HostInfo } from "./host-info";
5252
export { HttpClient } from "./http-client";
53+
export { IOSDeviceTunnelService } from "./ios-device-tunnel-service";
5354
export { Logger } from "./logger";
5455
export { PackageManager } from "./package-manager";
5556
export { ProjectData } from "./project-data";
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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({ name: "iOSDeviceTunnelService" })
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 tunnel address for the single device of `platform` that has
20+
* an open tunnel, if any.
21+
*/
22+
abstract getHostAddress(platform: string): string | undefined;
23+
24+
abstract close(deviceIdentifier: string): Promise<void>;
25+
}

‎lib/controllers/run-controller.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from "../common/declarations";
2626
import { IInjector } from "../common/definitions/yok";
2727
import { ViteHmrPortService } from "../contracts/vite-hmr-port-service";
28+
import { IOSDeviceTunnelService } from "../contracts/ios-device-tunnel-service";
2829
import { injector } from "../common/yok";
2930
import { isIOSMacDevice } from "../common/mobile/ios/mac/ios-mac-constants";
3031

@@ -67,6 +68,7 @@ export class RunController extends EventEmitter implements IRunController {
6768
protected $projectDataService: IProjectDataService,
6869
private $staticConfig: Config.IStaticConfig,
6970
private $viteHmrPortService: ViteHmrPortService,
71+
private $iOSDeviceTunnelService: IOSDeviceTunnelService,
7072
) {
7173
super();
7274
}
@@ -168,6 +170,10 @@ export class RunController extends EventEmitter implements IRunController {
168170
_.includes(deviceIdentifiersToRemove, descriptor.identifier),
169171
).map((descriptor) => descriptor.identifier);
170172

173+
for (const deviceIdentifier of removedDeviceIdentifiers) {
174+
await this.$iOSDeviceTunnelService.close(deviceIdentifier);
175+
}
176+
171177
// Handle the case when no more devices left for any of the persisted platforms
172178

173179
for (let i = 0; i < liveSyncProcessInfo.platforms.length; i++) {
@@ -648,6 +654,13 @@ export class RunController extends EventEmitter implements IRunController {
648654
platform: string;
649655
isEmulator: boolean;
650656
}> = {};
657+
// Snapshot before any device action: a device whose sync fails leaves
658+
// the session but must still count as a target of its platform.
659+
const sessionDeviceDescriptors = [
660+
...this.$liveSyncProcessDataService.getDeviceDescriptors(
661+
projectData.projectDir,
662+
),
663+
];
651664

652665
const deviceAction = async (device: Mobile.IDevice) => {
653666
const deviceDescriptor = _.find(
@@ -680,6 +693,12 @@ export class RunController extends EventEmitter implements IRunController {
680693
liveSyncInfo,
681694
"pre-build",
682695
);
696+
await this.openIOSViteHmrTunnel(
697+
device,
698+
projectData,
699+
liveSyncInfo,
700+
sessionDeviceDescriptors,
701+
);
683702

684703
const prepareResultData =
685704
await this.$prepareController.prepare(prepareData);
@@ -821,6 +840,58 @@ export class RunController extends EventEmitter implements IRunController {
821840
);
822841
}
823842

843+
/**
844+
* Serves Vite HMR to a USB-connected iOS / visionOS device over its
845+
* CoreDevice tunnel. One dev server bakes one origin into `bundle.mjs`
846+
* and a tunnel address is reachable only from its own device, so this
847+
* applies only when the device is the session's only target of its
848+
* platform. Must run before `prepare` spawns the Vite children.
849+
*/
850+
private async openIOSViteHmrTunnel(
851+
device: Mobile.IDevice,
852+
projectData: IProjectData,
853+
liveSyncInfo: ILiveSyncInfo,
854+
sessionDeviceDescriptors: ILiveSyncDeviceDescriptor[],
855+
): Promise<void> {
856+
const { platform, identifier } = device.deviceInfo;
857+
if (
858+
!this.$mobileHelper.isApplePlatform(platform) ||
859+
projectData.bundler !== "vite" ||
860+
liveSyncInfo.skipWatcher ||
861+
!liveSyncInfo.useHotModuleReload ||
862+
liveSyncInfo.release ||
863+
process.env.NS_HMR_HOST
864+
) {
865+
return;
866+
}
867+
if (
868+
!this.$bundlerCompilerService.acceptsIPv6DevHost(platform, projectData)
869+
) {
870+
this.$logger.trace(
871+
`Not using a USB tunnel for ${identifier}: the installed @nativescript/vite does not accept an IPv6 dev host.`,
872+
);
873+
return;
874+
}
875+
876+
const platformTargets = sessionDeviceDescriptors.filter((descriptor) => {
877+
const target = this.$devicesService.getDeviceByIdentifier(
878+
descriptor.identifier,
879+
);
880+
return (
881+
!target ||
882+
target.deviceInfo.platform.toLowerCase() === platform.toLowerCase()
883+
);
884+
});
885+
if (platformTargets.length !== 1) {
886+
this.$logger.trace(
887+
`Not using a USB tunnel for ${identifier}: the session has ${platformTargets.length} ${platform} targets.`,
888+
);
889+
return;
890+
}
891+
892+
await this.$iOSDeviceTunnelService.open(device);
893+
}
894+
824895
/**
825896
* Set up `adb reverse tcp:<port> tcp:<port>` for an Android device
826897
* when the project bundles with Vite in HMR/watch mode, then export

‎lib/services/bundler/bundler-compiler-service.ts‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
} from "../../common/declarations";
3636
import { ICleanupService } from "../../definitions/cleanup-service";
3737
import { ViteHmrPortService } from "../../contracts/vite-hmr-port-service";
38+
import { IOSDeviceTunnelService } from "../../contracts/ios-device-tunnel-service";
3839
import { injector } from "../../common/yok";
3940
import {
4041
resolvePackagePath,
@@ -122,6 +123,7 @@ export class BundlerCompilerService
122123
private $packageInstallationManager: IPackageInstallationManager, // private $sharedEventBus: ISharedEventBus
123124
private $projectConfigService: IProjectConfigService,
124125
private $viteHmrPortService: ViteHmrPortService,
126+
private $iOSDeviceTunnelService: IOSDeviceTunnelService,
125127
) {
126128
super();
127129
}
@@ -796,19 +798,38 @@ export class BundlerCompilerService
796798
if (process.env[VITE_SINGLE_PROCESS_ENV] === "0") {
797799
return false;
798800
}
801+
return this.getViteSingleProcessPlatforms(projectData).includes(platform);
802+
}
803+
804+
/**
805+
* Older `@nativescript/vite` releases bind the dev server to
806+
* `NS_HMR_HOST` and put an IPv6 literal into URLs unbracketed. The fix
807+
* shipped together with single-process dev, so its platform list is the
808+
* capability marker.
809+
*/
810+
public acceptsIPv6DevHost(
811+
platform: string,
812+
projectData: IProjectData,
813+
): boolean {
814+
return this.getViteSingleProcessPlatforms(projectData).includes(
815+
platform.toLowerCase(),
816+
);
817+
}
818+
819+
private getViteSingleProcessPlatforms(projectData: IProjectData): string[] {
799820
try {
800821
const packageJSONPath = resolvePackageJSONPath(
801822
this.getBundlerPackageName(),
802823
{ paths: [projectData.projectDir] },
803824
);
804825
if (!packageJSONPath) {
805-
return false;
826+
return [];
806827
}
807828
const platforms =
808829
this.$fs.readJson(packageJSONPath)?.nativescriptCli?.singleProcessDev;
809-
return Array.isArray(platforms) && platforms.includes(platform);
830+
return Array.isArray(platforms) ? platforms : [];
810831
} catch {
811-
return false;
832+
return [];
812833
}
813834
}
814835

@@ -832,6 +853,8 @@ export class BundlerCompilerService
832853
* sessions — the dev-server port. The port is resolved here, once, and
833854
* handed to `@nativescript/vite` as `NS_HMR_PORT`, so the URLs baked into
834855
* `bundle.mjs`, the server's bind and the `adb reverse` tunnel all match.
856+
* When the platform's only device has a USB tunnel open, its Mac address
857+
* becomes `NS_HMR_HOST`, unless the user set one.
835858
*/
836859
private async getViteChildEnv(
837860
platform: string,
@@ -844,6 +867,10 @@ export class BundlerCompilerService
844867
env.NS_HMR_PORT = String(
845868
await this.$viteHmrPortService.getPort(platform),
846869
);
870+
const tunnelHost = this.$iOSDeviceTunnelService.getHostAddress(platform);
871+
if (tunnelHost && !process.env.NS_HMR_HOST) {
872+
env.NS_HMR_HOST = tunnelHost;
873+
}
847874
}
848875
return env;
849876
}

‎lib/services/bundler/bundler.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ declare global {
3030
prepareData: IPrepareData,
3131
): Promise<void>;
3232
stopBundlerCompiler(platform: string): Promise<void>;
33+
/**
34+
* Whether the installed `@nativescript/vite` accepts a bare IPv6
35+
* address as `NS_HMR_HOST` for `platform`.
36+
*/
37+
acceptsIPv6DevHost(platform: string, projectData: IProjectData): boolean;
3338
}
3439

3540
/**

0 commit comments

Comments
 (0)