@kvbox/server 0.5.38 → 0.5.39
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/dist/kvbox-server.js +57 -10
- package/dist/kvbox-server.js.map +1 -1
- package/package.json +1 -1
package/dist/kvbox-server.js
CHANGED
|
@@ -459,6 +459,39 @@ import { chmod as chmod2, mkdir, readFile as readFile2 } from "node:fs/promises"
|
|
|
459
459
|
import { hostname as osHostname } from "node:os";
|
|
460
460
|
import { request } from "undici";
|
|
461
461
|
|
|
462
|
+
// ../shared/src/country.ts
|
|
463
|
+
var names = new Intl.DisplayNames(["en"], { type: "region", fallback: "none" });
|
|
464
|
+
var NOT_COUNTRIES = /* @__PURE__ */ new Set(["EU", "EZ", "UN", "QO", "XA", "XB", "ZZ"]);
|
|
465
|
+
var ALIASES = { usa: "US", america: "US", uk: "GB", britain: "GB", england: "GB", holland: "NL" };
|
|
466
|
+
var fold = (s) => s.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[^a-z]/g, "");
|
|
467
|
+
var byName;
|
|
468
|
+
function nameIndex() {
|
|
469
|
+
if (byName) return byName;
|
|
470
|
+
byName = new Map(Object.entries(ALIASES));
|
|
471
|
+
for (let a = 65; a <= 90; a++) {
|
|
472
|
+
for (let b = 65; b <= 90; b++) {
|
|
473
|
+
const code = String.fromCharCode(a, b);
|
|
474
|
+
const name = countryName(code);
|
|
475
|
+
if (name !== void 0 && canonical(code) === code) byName.set(fold(name), code);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return byName;
|
|
479
|
+
}
|
|
480
|
+
function canonical(code) {
|
|
481
|
+
return /-([A-Z]{2})$/.exec(Intl.getCanonicalLocales(`und-${code}`)[0] ?? "")?.[1] ?? code;
|
|
482
|
+
}
|
|
483
|
+
function countryName(code) {
|
|
484
|
+
if (!/^[A-Z]{2}$/.test(code) || NOT_COUNTRIES.has(code)) return void 0;
|
|
485
|
+
return names.of(code);
|
|
486
|
+
}
|
|
487
|
+
function parseCountry(input) {
|
|
488
|
+
const t = input.trim();
|
|
489
|
+
const upper = t.toUpperCase();
|
|
490
|
+
const code = countryName(upper) !== void 0 ? canonical(upper) : nameIndex().get(fold(t));
|
|
491
|
+
if (code !== void 0) return code;
|
|
492
|
+
throw new KvboxError(`unknown country "${input}": give its name or its two-letter code, e.g. RO or US`);
|
|
493
|
+
}
|
|
494
|
+
|
|
462
495
|
// ../shared/src/api.ts
|
|
463
496
|
import { z as z3 } from "zod";
|
|
464
497
|
var SizeSchema = z3.enum(SIZE_NAMES);
|
|
@@ -607,6 +640,8 @@ var MachineSchema = z3.object({
|
|
|
607
640
|
* links to, and a volume attaches to, only a box on the same host.
|
|
608
641
|
*/
|
|
609
642
|
host: z3.string().optional(),
|
|
643
|
+
/** The country code of that host (`RO`, `US`), when its admin gave one. Only the control plane fills it in. */
|
|
644
|
+
country: z3.string().optional(),
|
|
610
645
|
/** Seconds without network traffic before the host hibernates the box (#47). Absent: never. */
|
|
611
646
|
autoHibernateSec: z3.number().int().positive().optional(),
|
|
612
647
|
/** A script inside the box that says it is idle (#102, exit 0): auto-hibernate asks it instead of watching the traffic. */
|
|
@@ -704,6 +739,11 @@ var CreateMachineRequest = z3.object({
|
|
|
704
739
|
* control plane reads it; a host has one place to put a box.
|
|
705
740
|
*/
|
|
706
741
|
near: z3.string().optional(),
|
|
742
|
+
/**
|
|
743
|
+
* Place the box on a host in this country (`kvbox new --country romania`): a code or an English name,
|
|
744
|
+
* `@kvbox/shared/country`. Only the control plane reads it.
|
|
745
|
+
*/
|
|
746
|
+
country: z3.string().optional(),
|
|
707
747
|
/** The image to boot (#87, `kvbox new --base postgres`). Absent: the base image. */
|
|
708
748
|
image: z3.string().optional(),
|
|
709
749
|
/** The box's own environment variables (#87, `kvbox new -e NAME=value`), over its owner's. */
|
|
@@ -1086,7 +1126,9 @@ var HostJoinRequestSchema = z3.object({
|
|
|
1086
1126
|
name: z3.string().min(1),
|
|
1087
1127
|
url: z3.string().url(),
|
|
1088
1128
|
fingerprint: z3.string().regex(/^([0-9A-F]{2}:){31}[0-9A-F]{2}$/),
|
|
1089
|
-
publicAddress: z3.string().min(1)
|
|
1129
|
+
publicAddress: z3.string().min(1),
|
|
1130
|
+
/** Where the host is (`kvbox server join --country`): a code or an English name. */
|
|
1131
|
+
country: z3.string().optional()
|
|
1090
1132
|
});
|
|
1091
1133
|
var HostJoinResponseSchema = z3.object({
|
|
1092
1134
|
hostSecret: z3.string(),
|
|
@@ -1098,8 +1140,12 @@ var CloudHostSchema = z3.object({
|
|
|
1098
1140
|
publicAddress: z3.string(),
|
|
1099
1141
|
joinedAt: z3.string(),
|
|
1100
1142
|
lastSeenAt: z3.string().nullable(),
|
|
1101
|
-
capacity: CapacitySchema.nullable()
|
|
1143
|
+
capacity: CapacitySchema.nullable(),
|
|
1144
|
+
/** ISO 3166-1 alpha-2 code (`RO`), null when nobody said where the host is. */
|
|
1145
|
+
country: z3.string().nullable()
|
|
1102
1146
|
});
|
|
1147
|
+
var HostPatchRequest = z3.object({ country: z3.string().nullable() });
|
|
1148
|
+
var CountrySchema = z3.object({ code: z3.string(), name: z3.string() });
|
|
1103
1149
|
|
|
1104
1150
|
// ../shared/src/ssh.ts
|
|
1105
1151
|
import { createHash } from "node:crypto";
|
|
@@ -1211,9 +1257,10 @@ async function joinControlPlane(opts, p = paths(), guessAddress = defaultAdverti
|
|
|
1211
1257
|
}
|
|
1212
1258
|
}
|
|
1213
1259
|
const name = opts.name ?? osHostname();
|
|
1260
|
+
const country = opts.country === void 0 ? void 0 : parseCountry(opts.country);
|
|
1214
1261
|
const { fingerprint } = await ensureTlsCert(p);
|
|
1215
1262
|
const hostUrl = `https://${advertise}:${port}`;
|
|
1216
|
-
const body = { token: opts.token, name, url: hostUrl, fingerprint, publicAddress: advertise };
|
|
1263
|
+
const body = { token: opts.token, name, url: hostUrl, fingerprint, publicAddress: advertise, ...country !== void 0 ? { country } : {} };
|
|
1217
1264
|
const res = await request(`${url}/hosts/join`, {
|
|
1218
1265
|
method: "POST",
|
|
1219
1266
|
headers: { "content-type": "application/json" },
|
|
@@ -1989,9 +2036,9 @@ async function writeBoxEnv(box, vars, p = paths()) {
|
|
|
1989
2036
|
await rename3(tmp, file);
|
|
1990
2037
|
}
|
|
1991
2038
|
async function mergeBoxEnv(box, given, p = paths()) {
|
|
1992
|
-
const
|
|
2039
|
+
const names2 = new Set(given.map((v) => v.name));
|
|
1993
2040
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1994
|
-
const kept = (await readBoxEnv(box, p)).filter((v) => !
|
|
2041
|
+
const kept = (await readBoxEnv(box, p)).filter((v) => !names2.has(v.name));
|
|
1995
2042
|
await writeBoxEnv(box, [...kept, ...given.map((v) => ({ ...v, secret: false, updatedAt: now }))], p);
|
|
1996
2043
|
}
|
|
1997
2044
|
async function copyBoxEnv(from, to) {
|
|
@@ -2107,13 +2154,13 @@ async function listImages(cfg, p = paths()) {
|
|
|
2107
2154
|
out.push({ name: BASE_IMAGE, version: await versionAt(void 0, cfg.rootfs) });
|
|
2108
2155
|
} catch {
|
|
2109
2156
|
}
|
|
2110
|
-
let
|
|
2157
|
+
let names2 = [];
|
|
2111
2158
|
try {
|
|
2112
|
-
|
|
2159
|
+
names2 = (await readdir3(p.images)).sort();
|
|
2113
2160
|
} catch (err) {
|
|
2114
2161
|
if (err.code !== "ENOENT") throw err;
|
|
2115
2162
|
}
|
|
2116
|
-
for (const name of
|
|
2163
|
+
for (const name of names2) {
|
|
2117
2164
|
if (!IMAGE_RE.test(name) || name === BASE_IMAGE) continue;
|
|
2118
2165
|
try {
|
|
2119
2166
|
out.push({ name, version: await versionAt(name, p.image(name).rootfs) });
|
|
@@ -4698,7 +4745,7 @@ function versionOf(srcFileUrl, injected) {
|
|
|
4698
4745
|
}
|
|
4699
4746
|
|
|
4700
4747
|
// src/version.ts
|
|
4701
|
-
var VERSION = versionOf(import.meta.url, true ? "0.5.
|
|
4748
|
+
var VERSION = versionOf(import.meta.url, true ? "0.5.39" : void 0);
|
|
4702
4749
|
|
|
4703
4750
|
// src/caller.ts
|
|
4704
4751
|
import { createHash as createHash2, timingSafeEqual } from "node:crypto";
|
|
@@ -5443,7 +5490,7 @@ program.command("start", { isDefault: true }).description("serve the API on the
|
|
|
5443
5490
|
for (const f of recovery2.failed) console.error(`warning: box ${f.box} was running when it went down and did not come back: ${f.error}`);
|
|
5444
5491
|
}
|
|
5445
5492
|
});
|
|
5446
|
-
program.command("join <control-plane-url>").description("register this host with a control plane (needs a one-time token minted there); it answers the control plane from its next start").requiredOption("--token <token>", "one-time join token from the control plane").option("--advertise <host-or-ip>", "address the control plane reaches this host at (default: this host's default-route address)").option("--name <name>", "how the control plane lists this host (default: hostname)").action(async (url, opts) => {
|
|
5493
|
+
program.command("join <control-plane-url>").description("register this host with a control plane (needs a one-time token minted there); it answers the control plane from its next start").requiredOption("--token <token>", "one-time join token from the control plane").option("--advertise <host-or-ip>", "address the control plane reaches this host at (default: this host's default-route address)").option("--name <name>", "how the control plane lists this host (default: hostname)").option("--country <country>", "where this host is, a name or a code (romania, US); kvbox new --country places boxes by it").action(async (url, opts) => {
|
|
5447
5494
|
const joined = await joinControlPlane({ url, ...opts }, paths());
|
|
5448
5495
|
console.log(`joined ${joined.controlPlane} as host ${joined.hostName}`);
|
|
5449
5496
|
console.log(`it answers the control plane on ${joined.hostUrl} once its server restarts`);
|