balm-core 6.0.0-alpha.8 → 6.0.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/dist/index.cjs +173 -49
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +18 -1
- package/dist/index.d.ts +18 -1
- package/dist/index.js +171 -49
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# balm-core
|
|
2
2
|
|
|
3
|
+
## 6.0.0-alpha.9
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Prevent IPv4/IPv6 dev server port split-brain and automatically advance to the next free port when occupied
|
|
8
|
+
- Probe both IPv4 (`127.0.0.1`, `0.0.0.0`) and IPv6 (`::1`, `::`) in `isPortAvailable` / `resolveAvailablePort` so concurrent `host: true` (`0.0.0.0`) and default (`localhost` / `::1`) dev servers no longer hijack the same `localhost:3000` URL on macOS
|
|
9
|
+
- Automatically switch to the next unused port on collision with a clear warning log and handle `EADDRINUSE` race conditions before attaching WebSocket live reload
|
|
10
|
+
- Add `server.strictPort` config option and `--strictPort` CLI flag to fail fast instead of incrementing ports
|
|
11
|
+
|
|
3
12
|
## 6.0.0-alpha.8
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -78,10 +78,12 @@ __export(index_exports, {
|
|
|
78
78
|
isFunction: () => isFunction,
|
|
79
79
|
isManifestFile: () => isManifestFile,
|
|
80
80
|
isObject: () => isObject,
|
|
81
|
+
isPortAvailable: () => isPortAvailable,
|
|
81
82
|
isString: () => isString,
|
|
82
83
|
localResolve: () => localResolve,
|
|
83
84
|
localWorkspace: () => localWorkspace,
|
|
84
85
|
logger: () => logger,
|
|
86
|
+
resolveAvailablePort: () => resolveAvailablePort,
|
|
85
87
|
resolveConfig: () => resolveConfig,
|
|
86
88
|
resolveDefaultEntry: () => resolveDefaultEntry,
|
|
87
89
|
resolveServerHost: () => resolveServerHost,
|
|
@@ -251,6 +253,7 @@ function createDefaultConfig(workspace = process.cwd()) {
|
|
|
251
253
|
const server = {
|
|
252
254
|
host: null,
|
|
253
255
|
port: 3e3,
|
|
256
|
+
strictPort: false,
|
|
254
257
|
open: true,
|
|
255
258
|
proxy: false,
|
|
256
259
|
proxyOptions: false,
|
|
@@ -2636,6 +2639,7 @@ var FtpTask = class extends BaseTask {
|
|
|
2636
2639
|
|
|
2637
2640
|
// src/tasks/server.ts
|
|
2638
2641
|
var import_node_http = __toESM(require("http"), 1);
|
|
2642
|
+
var import_node_net = __toESM(require("net"), 1);
|
|
2639
2643
|
var import_node_crypto2 = __toESM(require("crypto"), 1);
|
|
2640
2644
|
var import_node_fs9 = __toESM(require("fs"), 1);
|
|
2641
2645
|
var import_node_os = __toESM(require("os"), 1);
|
|
@@ -3256,9 +3260,9 @@ function getNetworkAddresses() {
|
|
|
3256
3260
|
const addresses = [];
|
|
3257
3261
|
const interfaces = import_node_os.default.networkInterfaces();
|
|
3258
3262
|
for (const entries of Object.values(interfaces)) {
|
|
3259
|
-
for (const
|
|
3260
|
-
if (
|
|
3261
|
-
addresses.push(
|
|
3263
|
+
for (const net2 of entries || []) {
|
|
3264
|
+
if (net2.family === "IPv4" && !net2.internal) {
|
|
3265
|
+
addresses.push(net2.address);
|
|
3262
3266
|
}
|
|
3263
3267
|
}
|
|
3264
3268
|
}
|
|
@@ -3279,6 +3283,84 @@ function resolveServerHost(rawHost, envHost = process.env.HOST) {
|
|
|
3279
3283
|
isWildcard: host === "0.0.0.0" || host === "::"
|
|
3280
3284
|
};
|
|
3281
3285
|
}
|
|
3286
|
+
var LOCAL_OR_WILDCARD_HOSTS = /* @__PURE__ */ new Set([
|
|
3287
|
+
"localhost",
|
|
3288
|
+
"127.0.0.1",
|
|
3289
|
+
"::1",
|
|
3290
|
+
"0.0.0.0",
|
|
3291
|
+
"::"
|
|
3292
|
+
]);
|
|
3293
|
+
var IGNORABLE_SECONDARY_PROBE_ERRORS = /* @__PURE__ */ new Set([
|
|
3294
|
+
"EADDRNOTAVAIL",
|
|
3295
|
+
"EAFNOSUPPORT",
|
|
3296
|
+
"EINVAL",
|
|
3297
|
+
"ENOTFOUND"
|
|
3298
|
+
]);
|
|
3299
|
+
function probePortOnHost(port, host, ignoreUnsupportedFamily) {
|
|
3300
|
+
return new Promise((resolve) => {
|
|
3301
|
+
const tester = import_node_net.default.createServer();
|
|
3302
|
+
tester.unref();
|
|
3303
|
+
tester.once("error", (err) => {
|
|
3304
|
+
if (ignoreUnsupportedFamily && err.code && IGNORABLE_SECONDARY_PROBE_ERRORS.has(err.code)) {
|
|
3305
|
+
resolve(true);
|
|
3306
|
+
return;
|
|
3307
|
+
}
|
|
3308
|
+
resolve(false);
|
|
3309
|
+
});
|
|
3310
|
+
tester.once("listening", () => {
|
|
3311
|
+
tester.close(() => resolve(true));
|
|
3312
|
+
});
|
|
3313
|
+
tester.listen({ port, host, exclusive: true });
|
|
3314
|
+
});
|
|
3315
|
+
}
|
|
3316
|
+
async function isPortAvailable(port, host) {
|
|
3317
|
+
if (port === 0) return true;
|
|
3318
|
+
if (LOCAL_OR_WILDCARD_HOSTS.has(host)) {
|
|
3319
|
+
const primaryProbes = [
|
|
3320
|
+
{ host, optional: false },
|
|
3321
|
+
{ host: "127.0.0.1", optional: host !== "127.0.0.1" },
|
|
3322
|
+
{ host: "0.0.0.0", optional: host !== "0.0.0.0" },
|
|
3323
|
+
{ host: "::1", optional: host !== "::1" },
|
|
3324
|
+
{ host: "::", optional: host !== "::" }
|
|
3325
|
+
];
|
|
3326
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3327
|
+
for (const probe of primaryProbes) {
|
|
3328
|
+
if (seen.has(probe.host)) continue;
|
|
3329
|
+
seen.add(probe.host);
|
|
3330
|
+
const free = await probePortOnHost(port, probe.host, probe.optional);
|
|
3331
|
+
if (!free) {
|
|
3332
|
+
return false;
|
|
3333
|
+
}
|
|
3334
|
+
}
|
|
3335
|
+
return true;
|
|
3336
|
+
}
|
|
3337
|
+
return probePortOnHost(port, host, false);
|
|
3338
|
+
}
|
|
3339
|
+
async function resolveAvailablePort(requestedPort, host, options = {}) {
|
|
3340
|
+
if (requestedPort === 0) {
|
|
3341
|
+
return 0;
|
|
3342
|
+
}
|
|
3343
|
+
const strictPort = Boolean(options.strictPort);
|
|
3344
|
+
const maxAttempts = options.maxAttempts ?? 100;
|
|
3345
|
+
const maxPort = Math.min(65535, requestedPort + maxAttempts);
|
|
3346
|
+
for (let candidate = requestedPort; candidate <= maxPort; candidate++) {
|
|
3347
|
+
if (await isPortAvailable(candidate, host)) {
|
|
3348
|
+
return candidate;
|
|
3349
|
+
}
|
|
3350
|
+
if (strictPort) {
|
|
3351
|
+
const err2 = new Error(
|
|
3352
|
+
`Port ${requestedPort} is already in use (strictPort is enabled).`
|
|
3353
|
+
);
|
|
3354
|
+
err2.code = "EADDRINUSE";
|
|
3355
|
+
throw err2;
|
|
3356
|
+
}
|
|
3357
|
+
}
|
|
3358
|
+
const err = new Error(
|
|
3359
|
+
`No available port found in range ${requestedPort}-${maxPort} for host "${host}".`
|
|
3360
|
+
);
|
|
3361
|
+
err.code = "EADDRINUSE";
|
|
3362
|
+
throw err;
|
|
3363
|
+
}
|
|
3282
3364
|
var ServerTask = class extends BaseTask {
|
|
3283
3365
|
watcher = null;
|
|
3284
3366
|
wsServer = null;
|
|
@@ -3364,59 +3446,99 @@ var ServerTask = class extends BaseTask {
|
|
|
3364
3446
|
const app = createDevServerApp(config, {
|
|
3365
3447
|
ensureBuildReady: () => this.ensureBuildReady(config)
|
|
3366
3448
|
});
|
|
3367
|
-
const
|
|
3449
|
+
const requestedPort = config.server.port ?? 3e3;
|
|
3450
|
+
const strictPort = Boolean(config.server.strictPort);
|
|
3368
3451
|
const { host, isWildcard } = resolveServerHost(config.server.host);
|
|
3369
3452
|
const server = import_node_http.default.createServer(app);
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3453
|
+
const maxAttempts = 100;
|
|
3454
|
+
const maxPort = requestedPort === 0 ? 0 : Math.min(65535, requestedPort + maxAttempts);
|
|
3455
|
+
const listenOnPort = (candidatePort) => new Promise((resolveListen, rejectListen) => {
|
|
3456
|
+
const onError = (err) => {
|
|
3457
|
+
server.removeListener("listening", onListening);
|
|
3458
|
+
rejectListen(err);
|
|
3459
|
+
};
|
|
3460
|
+
const onListening = () => {
|
|
3461
|
+
server.removeListener("error", onError);
|
|
3373
3462
|
const address = server.address();
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
console.log("");
|
|
3377
|
-
logger.success("server", import_picocolors4.default.bold("BalmJS Dev Server is ready!"));
|
|
3378
|
-
console.log(
|
|
3379
|
-
` ${import_picocolors4.default.green("\u279C")} ${import_picocolors4.default.bold("Local:")} ${import_picocolors4.default.cyan(`http://${localHost}:${activePort}/`)}`
|
|
3463
|
+
resolveListen(
|
|
3464
|
+
address && typeof address === "object" ? address.port : candidatePort
|
|
3380
3465
|
);
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3466
|
+
};
|
|
3467
|
+
server.once("error", onError);
|
|
3468
|
+
server.once("listening", onListening);
|
|
3469
|
+
server.listen(candidatePort, host);
|
|
3470
|
+
});
|
|
3471
|
+
let activePort = await resolveAvailablePort(requestedPort, host, {
|
|
3472
|
+
strictPort,
|
|
3473
|
+
maxAttempts
|
|
3474
|
+
});
|
|
3475
|
+
while (true) {
|
|
3476
|
+
try {
|
|
3477
|
+
activePort = await listenOnPort(activePort);
|
|
3478
|
+
break;
|
|
3479
|
+
} catch (err) {
|
|
3480
|
+
if (err?.code === "EADDRINUSE" && !strictPort && activePort > 0 && activePort < maxPort) {
|
|
3481
|
+
activePort = await resolveAvailablePort(activePort + 1, host, {
|
|
3482
|
+
strictPort: false,
|
|
3483
|
+
maxAttempts: maxPort - activePort
|
|
3484
|
+
});
|
|
3485
|
+
continue;
|
|
3486
|
+
}
|
|
3487
|
+
this.watcher?.stop();
|
|
3488
|
+
throw err;
|
|
3489
|
+
}
|
|
3490
|
+
}
|
|
3491
|
+
this.attachLiveReloadServer(server);
|
|
3492
|
+
const localHost = isWildcard ? "localhost" : host;
|
|
3493
|
+
console.log("");
|
|
3494
|
+
if (requestedPort !== 0 && activePort !== requestedPort) {
|
|
3495
|
+
logger.warn(
|
|
3496
|
+
"server",
|
|
3497
|
+
`Port ${requestedPort} is in use, automatically switched to ${activePort}`
|
|
3498
|
+
);
|
|
3499
|
+
}
|
|
3500
|
+
logger.success("server", import_picocolors4.default.bold("BalmJS Dev Server is ready!"));
|
|
3501
|
+
console.log(
|
|
3502
|
+
` ${import_picocolors4.default.green("\u279C")} ${import_picocolors4.default.bold("Local:")} ${import_picocolors4.default.cyan(`http://${localHost}:${activePort}/`)}`
|
|
3503
|
+
);
|
|
3504
|
+
if (isWildcard) {
|
|
3505
|
+
const networkIps = getNetworkAddresses();
|
|
3506
|
+
if (networkIps.length > 0) {
|
|
3507
|
+
for (const ip of networkIps) {
|
|
3395
3508
|
console.log(
|
|
3396
|
-
` ${import_picocolors4.default.
|
|
3509
|
+
` ${import_picocolors4.default.green("\u279C")} ${import_picocolors4.default.bold("Network:")} ${import_picocolors4.default.cyan(`http://${ip}:${activePort}/`)}`
|
|
3397
3510
|
);
|
|
3398
3511
|
}
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3407
|
-
|
|
3408
|
-
|
|
3409
|
-
|
|
3410
|
-
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3512
|
+
} else {
|
|
3513
|
+
console.log(
|
|
3514
|
+
` ${import_picocolors4.default.green("\u279C")} ${import_picocolors4.default.bold("Network:")} ${import_picocolors4.default.cyan(`http://127.0.0.1:${activePort}/`)}`
|
|
3515
|
+
);
|
|
3516
|
+
}
|
|
3517
|
+
} else if (host === "localhost" || host === "127.0.0.1" || host === "::1") {
|
|
3518
|
+
console.log(
|
|
3519
|
+
` ${import_picocolors4.default.dim("\u279C Network: use --host or server.host = true to expose")}`
|
|
3520
|
+
);
|
|
3521
|
+
}
|
|
3522
|
+
console.log("");
|
|
3523
|
+
if (config.env.isTest) {
|
|
3524
|
+
this.watcher?.stop();
|
|
3525
|
+
this.wsServer?.close();
|
|
3526
|
+
await new Promise((resolveClose) => server.close(() => resolveClose()));
|
|
3527
|
+
return;
|
|
3528
|
+
}
|
|
3529
|
+
return new Promise((_resolve) => {
|
|
3530
|
+
const cleanup = () => {
|
|
3531
|
+
this.watcher?.stop();
|
|
3532
|
+
this.wsServer?.close();
|
|
3533
|
+
server.close();
|
|
3534
|
+
};
|
|
3535
|
+
process.once("SIGINT", () => {
|
|
3536
|
+
cleanup();
|
|
3537
|
+
process.exit(0);
|
|
3538
|
+
});
|
|
3539
|
+
process.once("SIGTERM", () => {
|
|
3540
|
+
cleanup();
|
|
3541
|
+
process.exit(0);
|
|
3420
3542
|
});
|
|
3421
3543
|
});
|
|
3422
3544
|
}
|
|
@@ -3723,10 +3845,12 @@ var index_default = balm;
|
|
|
3723
3845
|
isFunction,
|
|
3724
3846
|
isManifestFile,
|
|
3725
3847
|
isObject,
|
|
3848
|
+
isPortAvailable,
|
|
3726
3849
|
isString,
|
|
3727
3850
|
localResolve,
|
|
3728
3851
|
localWorkspace,
|
|
3729
3852
|
logger,
|
|
3853
|
+
resolveAvailablePort,
|
|
3730
3854
|
resolveConfig,
|
|
3731
3855
|
resolveDefaultEntry,
|
|
3732
3856
|
resolveServerHost,
|