@kvbox/server 0.5.37 → 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 +66 -11
- 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);
|
|
@@ -602,6 +635,13 @@ var MachineSchema = z3.object({
|
|
|
602
635
|
ports: z3.array(PortForwardSchema).optional(),
|
|
603
636
|
/** Whether the owner slug is a user or an org. Only the control plane knows; it fills it in on `GET /machines`. */
|
|
604
637
|
ownerKind: z3.enum(["user", "org"]).optional(),
|
|
638
|
+
/**
|
|
639
|
+
* The name of the host the box runs on. Only the control plane fills it in, on `GET /machines`: a box
|
|
640
|
+
* links to, and a volume attaches to, only a box on the same host.
|
|
641
|
+
*/
|
|
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(),
|
|
605
645
|
/** Seconds without network traffic before the host hibernates the box (#47). Absent: never. */
|
|
606
646
|
autoHibernateSec: z3.number().int().positive().optional(),
|
|
607
647
|
/** A script inside the box that says it is idle (#102, exit 0): auto-hibernate asks it instead of watching the traffic. */
|
|
@@ -629,7 +669,9 @@ var VolumeSchema = z3.object({
|
|
|
629
669
|
attachedTo: z3.string().nullable(),
|
|
630
670
|
createdAt: z3.string(),
|
|
631
671
|
/** Whether the owner slug is a user or an org. Only the control plane knows; it fills it in on `GET /volumes`. */
|
|
632
|
-
ownerKind: z3.enum(["user", "org"]).optional()
|
|
672
|
+
ownerKind: z3.enum(["user", "org"]).optional(),
|
|
673
|
+
/** The name of the host the volume lives on; only the control plane fills it in. It attaches only to a box there. */
|
|
674
|
+
host: z3.string().optional()
|
|
633
675
|
});
|
|
634
676
|
var MAX_VOLUME_GIB = 4096;
|
|
635
677
|
var VOLUME_NAME_RE = /^[a-z0-9][a-z0-9-]{0,11}$/;
|
|
@@ -697,6 +739,11 @@ var CreateMachineRequest = z3.object({
|
|
|
697
739
|
* control plane reads it; a host has one place to put a box.
|
|
698
740
|
*/
|
|
699
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(),
|
|
700
747
|
/** The image to boot (#87, `kvbox new --base postgres`). Absent: the base image. */
|
|
701
748
|
image: z3.string().optional(),
|
|
702
749
|
/** The box's own environment variables (#87, `kvbox new -e NAME=value`), over its owner's. */
|
|
@@ -778,6 +825,7 @@ var CapacitySchema = z3.object({
|
|
|
778
825
|
*/
|
|
779
826
|
images: z3.array(z3.object({ name: z3.string(), version: z3.string() })).optional()
|
|
780
827
|
});
|
|
828
|
+
var ImageSchema = z3.object({ name: z3.string(), version: z3.string().nullable() });
|
|
781
829
|
var RecoveryReportSchema = z3.object({
|
|
782
830
|
at: z3.string(),
|
|
783
831
|
booted: z3.array(z3.string()),
|
|
@@ -1078,7 +1126,9 @@ var HostJoinRequestSchema = z3.object({
|
|
|
1078
1126
|
name: z3.string().min(1),
|
|
1079
1127
|
url: z3.string().url(),
|
|
1080
1128
|
fingerprint: z3.string().regex(/^([0-9A-F]{2}:){31}[0-9A-F]{2}$/),
|
|
1081
|
-
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()
|
|
1082
1132
|
});
|
|
1083
1133
|
var HostJoinResponseSchema = z3.object({
|
|
1084
1134
|
hostSecret: z3.string(),
|
|
@@ -1090,8 +1140,12 @@ var CloudHostSchema = z3.object({
|
|
|
1090
1140
|
publicAddress: z3.string(),
|
|
1091
1141
|
joinedAt: z3.string(),
|
|
1092
1142
|
lastSeenAt: z3.string().nullable(),
|
|
1093
|
-
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()
|
|
1094
1146
|
});
|
|
1147
|
+
var HostPatchRequest = z3.object({ country: z3.string().nullable() });
|
|
1148
|
+
var CountrySchema = z3.object({ code: z3.string(), name: z3.string() });
|
|
1095
1149
|
|
|
1096
1150
|
// ../shared/src/ssh.ts
|
|
1097
1151
|
import { createHash } from "node:crypto";
|
|
@@ -1203,9 +1257,10 @@ async function joinControlPlane(opts, p = paths(), guessAddress = defaultAdverti
|
|
|
1203
1257
|
}
|
|
1204
1258
|
}
|
|
1205
1259
|
const name = opts.name ?? osHostname();
|
|
1260
|
+
const country = opts.country === void 0 ? void 0 : parseCountry(opts.country);
|
|
1206
1261
|
const { fingerprint } = await ensureTlsCert(p);
|
|
1207
1262
|
const hostUrl = `https://${advertise}:${port}`;
|
|
1208
|
-
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 } : {} };
|
|
1209
1264
|
const res = await request(`${url}/hosts/join`, {
|
|
1210
1265
|
method: "POST",
|
|
1211
1266
|
headers: { "content-type": "application/json" },
|
|
@@ -1981,9 +2036,9 @@ async function writeBoxEnv(box, vars, p = paths()) {
|
|
|
1981
2036
|
await rename3(tmp, file);
|
|
1982
2037
|
}
|
|
1983
2038
|
async function mergeBoxEnv(box, given, p = paths()) {
|
|
1984
|
-
const
|
|
2039
|
+
const names2 = new Set(given.map((v) => v.name));
|
|
1985
2040
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1986
|
-
const kept = (await readBoxEnv(box, p)).filter((v) => !
|
|
2041
|
+
const kept = (await readBoxEnv(box, p)).filter((v) => !names2.has(v.name));
|
|
1987
2042
|
await writeBoxEnv(box, [...kept, ...given.map((v) => ({ ...v, secret: false, updatedAt: now }))], p);
|
|
1988
2043
|
}
|
|
1989
2044
|
async function copyBoxEnv(from, to) {
|
|
@@ -2099,13 +2154,13 @@ async function listImages(cfg, p = paths()) {
|
|
|
2099
2154
|
out.push({ name: BASE_IMAGE, version: await versionAt(void 0, cfg.rootfs) });
|
|
2100
2155
|
} catch {
|
|
2101
2156
|
}
|
|
2102
|
-
let
|
|
2157
|
+
let names2 = [];
|
|
2103
2158
|
try {
|
|
2104
|
-
|
|
2159
|
+
names2 = (await readdir3(p.images)).sort();
|
|
2105
2160
|
} catch (err) {
|
|
2106
2161
|
if (err.code !== "ENOENT") throw err;
|
|
2107
2162
|
}
|
|
2108
|
-
for (const name of
|
|
2163
|
+
for (const name of names2) {
|
|
2109
2164
|
if (!IMAGE_RE.test(name) || name === BASE_IMAGE) continue;
|
|
2110
2165
|
try {
|
|
2111
2166
|
out.push({ name, version: await versionAt(name, p.image(name).rootfs) });
|
|
@@ -4690,7 +4745,7 @@ function versionOf(srcFileUrl, injected) {
|
|
|
4690
4745
|
}
|
|
4691
4746
|
|
|
4692
4747
|
// src/version.ts
|
|
4693
|
-
var VERSION = versionOf(import.meta.url, true ? "0.5.
|
|
4748
|
+
var VERSION = versionOf(import.meta.url, true ? "0.5.39" : void 0);
|
|
4694
4749
|
|
|
4695
4750
|
// src/caller.ts
|
|
4696
4751
|
import { createHash as createHash2, timingSafeEqual } from "node:crypto";
|
|
@@ -5435,7 +5490,7 @@ program.command("start", { isDefault: true }).description("serve the API on the
|
|
|
5435
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}`);
|
|
5436
5491
|
}
|
|
5437
5492
|
});
|
|
5438
|
-
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) => {
|
|
5439
5494
|
const joined = await joinControlPlane({ url, ...opts }, paths());
|
|
5440
5495
|
console.log(`joined ${joined.controlPlane} as host ${joined.hostName}`);
|
|
5441
5496
|
console.log(`it answers the control plane on ${joined.hostUrl} once its server restarts`);
|