Skip to content

Commit 0088994

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 4442612 commit 0088994

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
@@ -676,6 +676,10 @@ injector.require(
676676
"viteHmrPortService",
677677
"./services/bundler/vite-hmr-port-service",
678678
);
679+
injector.require(
680+
"iOSDeviceTunnelService",
681+
"./services/ios/ios-device-tunnel-service",
682+
);
679683

680684
injector.require(
681685
"applePortalSessionService",

‎lib/contracts/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export { Errors } from "./errors";
4242
export { FileSystem } from "./file-system";
4343
export { HostInfo } from "./host-info";
4444
export { HttpClient } from "./http-client";
45+
export { IOSDeviceTunnelService } from "./ios-device-tunnel-service";
4546
export { Logger } from "./logger";
4647
export { PackageManager } from "./package-manager";
4748
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

3031
export class RunController extends EventEmitter implements IRunController {
@@ -66,6 +67,7 @@ export class RunController extends EventEmitter implements IRunController {
6667
protected $projectDataService: IProjectDataService,
6768
private $staticConfig: Config.IStaticConfig,
6869
private $viteHmrPortService: ViteHmrPortService,
70+
private $iOSDeviceTunnelService: IOSDeviceTunnelService,
6971
) {
7072
super();
7173
}
@@ -167,6 +169,10 @@ export class RunController extends EventEmitter implements IRunController {
167169
_.includes(deviceIdentifiersToRemove, descriptor.identifier),
168170
).map((descriptor) => descriptor.identifier);
169171

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

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

651664
const deviceAction = async (device: Mobile.IDevice) => {
652665
const deviceDescriptor = _.find(
@@ -679,6 +692,12 @@ export class RunController extends EventEmitter implements IRunController {
679692
liveSyncInfo,
680693
"pre-build",
681694
);
695+
await this.openIOSViteHmrTunnel(
696+
device,
697+
projectData,
698+
liveSyncInfo,
699+
sessionDeviceDescriptors,
700+
);
682701

683702
const prepareResultData =
684703
await this.$prepareController.prepare(prepareData);
@@ -819,6 +838,58 @@ export class RunController extends EventEmitter implements IRunController {
819838
);
820839
}
821840

841+
/**
842+
* Serves Vite HMR to a USB-connected iOS / visionOS device over its
843+
* CoreDevice tunnel. One dev server bakes one origin into `bundle.mjs`
844+
* and a tunnel address is reachable only from its own device, so this
845+
* applies only when the device is the session's only target of its
846+
* platform. Must run before `prepare` spawns the Vite children.
847+
*/
848+
private async openIOSViteHmrTunnel(
849+
device: Mobile.IDevice,
850+
projectData: IProjectData,
851+
liveSyncInfo: ILiveSyncInfo,
852+
sessionDeviceDescriptors: ILiveSyncDeviceDescriptor[],
853+
): Promise<void> {
854+
const { platform, identifier } = device.deviceInfo;
855+
if (
856+
!this.$mobileHelper.isApplePlatform(platform) ||
857+
projectData.bundler !== "vite" ||
858+
liveSyncInfo.skipWatcher ||
859+
!liveSyncInfo.useHotModuleReload ||
860+
liveSyncInfo.release ||
861+
process.env.NS_HMR_HOST
862+
) {
863+
return;
864+
}
865+
if (
866+
!this.$bundlerCompilerService.acceptsIPv6DevHost(platform, projectData)
867+
) {
868+
this.$logger.trace(
869+
`Not using a USB tunnel for ${identifier}: the installed @nativescript/vite does not accept an IPv6 dev host.`,
870+
);
871+
return;
872+
}
873+
874+
const platformTargets = sessionDeviceDescriptors.filter((descriptor) => {
875+
const target = this.$devicesService.getDeviceByIdentifier(
876+
descriptor.identifier,
877+
);
878+
return (
879+
!target ||
880+
target.deviceInfo.platform.toLowerCase() === platform.toLowerCase()
881+
);
882+
});
883+
if (platformTargets.length !== 1) {
884+
this.$logger.trace(
885+
`Not using a USB tunnel for ${identifier}: the session has ${platformTargets.length} ${platform} targets.`,
886+
);
887+
return;
888+
}
889+
890+
await this.$iOSDeviceTunnelService.open(device);
891+
}
892+
822893
/**
823894
* Set up `adb reverse tcp:<port> tcp:<port>` for an Android device
824895
* 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,
@@ -121,6 +122,7 @@ export class BundlerCompilerService
121122
private $packageInstallationManager: IPackageInstallationManager, // private $sharedEventBus: ISharedEventBus
122123
private $projectConfigService: IProjectConfigService,
123124
private $viteHmrPortService: ViteHmrPortService,
125+
private $iOSDeviceTunnelService: IOSDeviceTunnelService,
124126
) {
125127
super();
126128
}
@@ -795,19 +797,38 @@ export class BundlerCompilerService
795797
if (process.env[VITE_SINGLE_PROCESS_ENV] === "0") {
796798
return false;
797799
}
800+
return this.getViteSingleProcessPlatforms(projectData).includes(platform);
801+
}
802+
803+
/**
804+
* Older `@nativescript/vite` releases bind the dev server to
805+
* `NS_HMR_HOST` and put an IPv6 literal into URLs unbracketed. The fix
806+
* shipped together with single-process dev, so its platform list is the
807+
* capability marker.
808+
*/
809+
public acceptsIPv6DevHost(
810+
platform: string,
811+
projectData: IProjectData,
812+
): boolean {
813+
return this.getViteSingleProcessPlatforms(projectData).includes(
814+
platform.toLowerCase(),
815+
);
816+
}
817+
818+
private getViteSingleProcessPlatforms(projectData: IProjectData): string[] {
798819
try {
799820
const packageJSONPath = resolvePackageJSONPath(
800821
this.getBundlerPackageName(),
801822
{ paths: [projectData.projectDir] },
802823
);
803824
if (!packageJSONPath) {
804-
return false;
825+
return [];
805826
}
806827
const platforms =
807828
this.$fs.readJson(packageJSONPath)?.nativescriptCli?.singleProcessDev;
808-
return Array.isArray(platforms) && platforms.includes(platform);
829+
return Array.isArray(platforms) ? platforms : [];
809830
} catch {
810-
return false;
831+
return [];
811832
}
812833
}
813834

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

‎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)