Skip to content

Commit d8f085b

Browse files
edusperoniNathanWalker
authored andcommitted
feat(vite): give every device the LAN origin when NS_HMR_PREFER_LAN_HOST is set
In `@nativescript/vite`, `NS_HMR_PREFER_LAN_HOST` meant "use this Mac's LAN address", ahead of adb reverse and the platform defaults. With the origin chosen per device by the CLI, it keeps that meaning: no adb reverse and no USB tunnel are set up, and every device gets the LAN address unless `NS_HMR_HOST` is set. Without a LAN address the usual routes apply. `NS_HMR_NO_ADB_REVERSE` on its own only skips the reverse, so an emulator still gets 10.0.2.2 and a physical device the LAN address.
1 parent 396c3de commit d8f085b

4 files changed

Lines changed: 110 additions & 5 deletions

File tree

‎lib/controllers/run-controller.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -939,13 +939,11 @@ export class RunController extends EventEmitter implements IRunController {
939939
*/
940940
private async chooseDevServerOrigin(device: Mobile.IDevice): Promise<void> {
941941
const { platform, identifier } = device.deviceInfo;
942+
const preferLan = isTruthyEnvFlag(process.env.NS_HMR_PREFER_LAN_HOST);
942943
let adbReverse = false;
943944
let port: number;
944945
if (this.$mobileHelper.isAndroidPlatform(platform)) {
945-
if (
946-
!isTruthyEnvFlag(process.env.NS_HMR_NO_ADB_REVERSE) &&
947-
!isTruthyEnvFlag(process.env.NS_HMR_PREFER_LAN_HOST)
948-
) {
946+
if (!isTruthyEnvFlag(process.env.NS_HMR_NO_ADB_REVERSE) && !preferLan) {
949947
port = await this.$viteHmrPortService.getPort(platform);
950948
try {
951949
adbReverse = await this.ensureAndroidReverse(
@@ -959,7 +957,11 @@ export class RunController extends EventEmitter implements IRunController {
959957
);
960958
}
961959
}
962-
} else if (!device.isEmulator && !process.env.NS_HMR_HOST?.trim()) {
960+
} else if (
961+
!device.isEmulator &&
962+
!preferLan &&
963+
!process.env.NS_HMR_HOST?.trim()
964+
) {
963965
await this.iOSDeviceTunnelService.open(device);
964966
}
965967

‎lib/services/bundler/dev-server-origin-service.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as crypto from "crypto";
22
import * as net from "net";
33
import * as os from "os";
44
import { IDictionary } from "../../common/declarations";
5+
import { isTruthyEnvFlag } from "../../common/helpers";
56
import { inject } from "../../common/di";
67
import {
78
DevServerOriginService,
@@ -20,6 +21,8 @@ export interface IDevServerOriginInputs {
2021
https?: boolean;
2122
/** `NS_HMR_HOST` as the user set it. */
2223
envHost?: string;
24+
/** `NS_HMR_PREFER_LAN_HOST`: the LAN address wins over every route but `NS_HMR_HOST`. */
25+
preferLan?: boolean;
2326
isAndroid: boolean;
2427
isEmulator: boolean;
2528
adbReverse?: boolean;
@@ -52,6 +55,9 @@ export function isDevServerHttps(env: NodeJS.ProcessEnv): boolean {
5255
}
5356

5457
function chooseDevServerHost(inputs: IDevServerOriginInputs): string {
58+
if (inputs.preferLan && inputs.lanAddress) {
59+
return inputs.lanAddress;
60+
}
5561
if (inputs.isAndroid) {
5662
if (inputs.adbReverse) {
5763
return LOOPBACK;
@@ -162,6 +168,7 @@ export class DevServerOriginServiceImpl implements DevServerOriginService {
162168
port: await this.viteHmrPortService.getPort(platform),
163169
https: isDevServerHttps(process.env),
164170
envHost: process.env.NS_HMR_HOST,
171+
preferLan: isTruthyEnvFlag(process.env.NS_HMR_PREFER_LAN_HOST),
165172
isAndroid,
166173
isEmulator: device.isEmulator,
167174
adbReverse: options.adbReverse,

‎test/controllers/run-controller.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ describe("RunController", () => {
319319
buildAction: async () => buildOutputPath,
320320
buildData: <any>{},
321321
};
322+
const androidPhone = <any>{
323+
deviceInfo: { identifier: "R5CT21", platform: "android" },
324+
isEmulator: false,
325+
adb: androidEmulator.adb,
326+
};
327+
const lanAddress = "192.168.1.20";
322328
const hmrLiveSyncInfo = { ...liveSyncInfo, useHotModuleReload: true };
323329
const envNames = [
324330
"NS_HMR_HOST",
@@ -383,6 +389,10 @@ describe("RunController", () => {
383389
device: androidEmulator,
384390
descriptor: androidEmulatorDescriptor,
385391
};
392+
map.R5CT21 = {
393+
device: androidPhone,
394+
descriptor: { ...androidEmulatorDescriptor, identifier: "R5CT21" },
395+
};
386396

387397
const projectDataService = injector.resolve("projectDataService");
388398
const getProjectData =
@@ -434,6 +444,9 @@ describe("RunController", () => {
434444
injector.resolve("analyticsService").trackEventActionInGoogleAnalytics =
435445
async (): Promise<void> => undefined;
436446
const devServerOriginService = injector.get(DevServerOriginService);
447+
(<any>devServerOriginService).getNetworkInterfaces = () => ({
448+
en0: [{ address: lanAddress, family: "IPv4", internal: false }],
449+
});
437450
for (const name of ["iOSLiveSyncService", "androidLiveSyncService"]) {
438451
const liveSyncService = injector.resolve(name);
439452
liveSyncService.fullSync = async (syncInfo: IFullSyncInfo) => {
@@ -478,6 +491,7 @@ describe("RunController", () => {
478491
delete map.mySecondiOSDevice;
479492
delete map.myiOSSimulator;
480493
delete map["emulator-5554"];
494+
delete map.R5CT21;
481495
for (const name of envNames) {
482496
if (previousEnv[name] === undefined) {
483497
delete process.env[name];
@@ -567,6 +581,35 @@ describe("RunController", () => {
567581
);
568582
});
569583

584+
it("gives a physical Android device without adb reverse the LAN origin", async () => {
585+
process.env.NS_HMR_NO_ADB_REVERSE = "1";
586+
587+
await run([androidPhone]);
588+
589+
assert.deepStrictEqual(adbCalls, []);
590+
assert.equal(
591+
JSON.parse(filesAtFullSync.R5CT21).origin,
592+
`http://${lanAddress}:5173`,
593+
);
594+
});
595+
596+
it("gives every device the LAN origin when the user prefers it", async () => {
597+
process.env.NS_HMR_PREFER_LAN_HOST = "1";
598+
599+
await run([androidEmulator, iOSDevice, iOSSimulator]);
600+
601+
assert.deepStrictEqual(adbCalls, []);
602+
assert.deepStrictEqual(openedFor, []);
603+
assert.deepStrictEqual(
604+
_.mapValues(filesAtFullSync, (file) => JSON.parse(file).origin),
605+
{
606+
"emulator-5554": `http://${lanAddress}:5173`,
607+
myiOSDevice: `http://${lanAddress}:5173`,
608+
myiOSSimulator: `http://${lanAddress}:5173`,
609+
},
610+
);
611+
});
612+
570613
it("skips adb reverse when the user opts out", async () => {
571614
process.env.NS_HMR_NO_ADB_REVERSE = "1";
572615

‎test/services/bundler/dev-server-origin-service.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,41 @@ describe("chooseDevServerOrigin", () => {
110110
assert.equal(origin({ lanAddress: undefined }), "http://127.0.0.1:5173");
111111
});
112112

113+
describe("when the user prefers the LAN address", () => {
114+
it("gives it to every device, whatever route it has", () => {
115+
for (const inputs of [
116+
{ isAndroid: true, isEmulator: true, adbReverse: true },
117+
{ isAndroid: true, isEmulator: true },
118+
{ isEmulator: true },
119+
{ tunnelAddress: "fd00::2" },
120+
]) {
121+
assert.equal(
122+
origin({ ...inputs, preferLan: true }),
123+
`http://${lan}:5173`,
124+
);
125+
}
126+
});
127+
128+
it("still yields to NS_HMR_HOST", () => {
129+
assert.equal(
130+
origin({ preferLan: true, envHost: "dev.example.test" }),
131+
"http://dev.example.test:5173",
132+
);
133+
});
134+
135+
it("falls back to the usual route without a LAN address", () => {
136+
assert.equal(
137+
origin({
138+
preferLan: true,
139+
lanAddress: undefined,
140+
isAndroid: true,
141+
isEmulator: true,
142+
}),
143+
"http://10.0.2.2:5173",
144+
);
145+
});
146+
});
147+
113148
it("uses https when the dev server serves it", () => {
114149
assert.equal(
115150
origin({ https: true, tunnelAddress: "fd00::2" }),
@@ -182,6 +217,7 @@ describe("guessLanAddress", () => {
182217
describe("DevServerOriginService", () => {
183218
const previousHost = process.env.NS_HMR_HOST;
184219
const previousHttps = process.env.NS_HTTPS;
220+
const previousPreferLan = process.env.NS_HMR_PREFER_LAN_HOST;
185221
let service: DevServerOriginServiceImpl;
186222
let tunnelAddresses: Record<string, string>;
187223
let interfaces: object;
@@ -200,6 +236,7 @@ describe("DevServerOriginService", () => {
200236
beforeEach(() => {
201237
delete process.env.NS_HMR_HOST;
202238
delete process.env.NS_HTTPS;
239+
delete process.env.NS_HMR_PREFER_LAN_HOST;
203240
tunnelAddresses = {};
204241
warnings = [];
205242
interfaces = {
@@ -247,6 +284,22 @@ describe("DevServerOriginService", () => {
247284
} else {
248285
process.env.NS_HTTPS = previousHttps;
249286
}
287+
if (previousPreferLan === undefined) {
288+
delete process.env.NS_HMR_PREFER_LAN_HOST;
289+
} else {
290+
process.env.NS_HMR_PREFER_LAN_HOST = previousPreferLan;
291+
}
292+
});
293+
294+
it("reads NS_HMR_PREFER_LAN_HOST from the environment", async () => {
295+
process.env.NS_HMR_PREFER_LAN_HOST = "1";
296+
297+
assert.equal(
298+
await service.chooseOrigin(device("emulator-5554", "Android", true), {
299+
adbReverse: true,
300+
}),
301+
`http://${lan}:5174`,
302+
);
250303
});
251304

252305
it("writes an https origin when NS_HTTPS is set", async () => {

0 commit comments

Comments
 (0)