@kvbox/server 0.5.38 → 0.5.40
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 +76 -12
- package/dist/kvbox-server.js.map +1 -1
- package/package.json +1 -1
package/dist/kvbox-server.js
CHANGED
|
@@ -185,6 +185,7 @@ var PricingSchema = z.object({
|
|
|
185
185
|
volumeGibHour: z.number().nonnegative().default(0),
|
|
186
186
|
diskIncludedGib: z.number().int().nonnegative().default(0)
|
|
187
187
|
});
|
|
188
|
+
var PriceListSchema = PricingSchema.extend({ welcomeCredit: z.number().nonnegative() });
|
|
188
189
|
var HOUR_MS = 36e5;
|
|
189
190
|
var round = (n, places) => Math.round(n * 10 ** places) / 10 ** places;
|
|
190
191
|
function computeUsage(events, w) {
|
|
@@ -459,6 +460,39 @@ import { chmod as chmod2, mkdir, readFile as readFile2 } from "node:fs/promises"
|
|
|
459
460
|
import { hostname as osHostname } from "node:os";
|
|
460
461
|
import { request } from "undici";
|
|
461
462
|
|
|
463
|
+
// ../shared/src/country.ts
|
|
464
|
+
var names = new Intl.DisplayNames(["en"], { type: "region", fallback: "none" });
|
|
465
|
+
var NOT_COUNTRIES = /* @__PURE__ */ new Set(["EU", "EZ", "UN", "QO", "XA", "XB", "ZZ"]);
|
|
466
|
+
var ALIASES = { usa: "US", america: "US", uk: "GB", britain: "GB", england: "GB", holland: "NL" };
|
|
467
|
+
var fold = (s) => s.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[^a-z]/g, "");
|
|
468
|
+
var byName;
|
|
469
|
+
function nameIndex() {
|
|
470
|
+
if (byName) return byName;
|
|
471
|
+
byName = new Map(Object.entries(ALIASES));
|
|
472
|
+
for (let a = 65; a <= 90; a++) {
|
|
473
|
+
for (let b = 65; b <= 90; b++) {
|
|
474
|
+
const code = String.fromCharCode(a, b);
|
|
475
|
+
const name = countryName(code);
|
|
476
|
+
if (name !== void 0 && canonical(code) === code) byName.set(fold(name), code);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
return byName;
|
|
480
|
+
}
|
|
481
|
+
function canonical(code) {
|
|
482
|
+
return /-([A-Z]{2})$/.exec(Intl.getCanonicalLocales(`und-${code}`)[0] ?? "")?.[1] ?? code;
|
|
483
|
+
}
|
|
484
|
+
function countryName(code) {
|
|
485
|
+
if (!/^[A-Z]{2}$/.test(code) || NOT_COUNTRIES.has(code)) return void 0;
|
|
486
|
+
return names.of(code);
|
|
487
|
+
}
|
|
488
|
+
function parseCountry(input) {
|
|
489
|
+
const t = input.trim();
|
|
490
|
+
const upper = t.toUpperCase();
|
|
491
|
+
const code = countryName(upper) !== void 0 ? canonical(upper) : nameIndex().get(fold(t));
|
|
492
|
+
if (code !== void 0) return code;
|
|
493
|
+
throw new KvboxError(`unknown country "${input}": give its name or its two-letter code, e.g. RO or US`);
|
|
494
|
+
}
|
|
495
|
+
|
|
462
496
|
// ../shared/src/api.ts
|
|
463
497
|
import { z as z3 } from "zod";
|
|
464
498
|
var SizeSchema = z3.enum(SIZE_NAMES);
|
|
@@ -607,6 +641,8 @@ var MachineSchema = z3.object({
|
|
|
607
641
|
* links to, and a volume attaches to, only a box on the same host.
|
|
608
642
|
*/
|
|
609
643
|
host: z3.string().optional(),
|
|
644
|
+
/** The country code of that host (`RO`, `US`), when its admin gave one. Only the control plane fills it in. */
|
|
645
|
+
country: z3.string().optional(),
|
|
610
646
|
/** Seconds without network traffic before the host hibernates the box (#47). Absent: never. */
|
|
611
647
|
autoHibernateSec: z3.number().int().positive().optional(),
|
|
612
648
|
/** A script inside the box that says it is idle (#102, exit 0): auto-hibernate asks it instead of watching the traffic. */
|
|
@@ -704,6 +740,11 @@ var CreateMachineRequest = z3.object({
|
|
|
704
740
|
* control plane reads it; a host has one place to put a box.
|
|
705
741
|
*/
|
|
706
742
|
near: z3.string().optional(),
|
|
743
|
+
/**
|
|
744
|
+
* Place the box on a host in this country (`kvbox new --country romania`): a code or an English name,
|
|
745
|
+
* `@kvbox/shared/country`. Only the control plane reads it.
|
|
746
|
+
*/
|
|
747
|
+
country: z3.string().optional(),
|
|
707
748
|
/** The image to boot (#87, `kvbox new --base postgres`). Absent: the base image. */
|
|
708
749
|
image: z3.string().optional(),
|
|
709
750
|
/** The box's own environment variables (#87, `kvbox new -e NAME=value`), over its owner's. */
|
|
@@ -932,8 +973,11 @@ var VolumeEventsResponseSchema = z3.object({
|
|
|
932
973
|
events: z3.array(VolumeEventRowSchema),
|
|
933
974
|
next: z3.number().int()
|
|
934
975
|
});
|
|
976
|
+
var API_ERROR_CODES = ["account-inactive", "org-inactive", "out-of-credit"];
|
|
977
|
+
var ApiErrorCodeSchema = z3.enum(API_ERROR_CODES);
|
|
935
978
|
var ApiErrorSchema = z3.object({
|
|
936
|
-
error: z3.string()
|
|
979
|
+
error: z3.string(),
|
|
980
|
+
code: ApiErrorCodeSchema.optional()
|
|
937
981
|
});
|
|
938
982
|
var ACTIVATED_BY = ["card", "admin"];
|
|
939
983
|
var ActivatedBySchema = z3.enum(ACTIVATED_BY);
|
|
@@ -1086,7 +1130,9 @@ var HostJoinRequestSchema = z3.object({
|
|
|
1086
1130
|
name: z3.string().min(1),
|
|
1087
1131
|
url: z3.string().url(),
|
|
1088
1132
|
fingerprint: z3.string().regex(/^([0-9A-F]{2}:){31}[0-9A-F]{2}$/),
|
|
1089
|
-
publicAddress: z3.string().min(1)
|
|
1133
|
+
publicAddress: z3.string().min(1),
|
|
1134
|
+
/** Where the host is (`kvbox server join --country`): a code or an English name. */
|
|
1135
|
+
country: z3.string().optional()
|
|
1090
1136
|
});
|
|
1091
1137
|
var HostJoinResponseSchema = z3.object({
|
|
1092
1138
|
hostSecret: z3.string(),
|
|
@@ -1098,8 +1144,12 @@ var CloudHostSchema = z3.object({
|
|
|
1098
1144
|
publicAddress: z3.string(),
|
|
1099
1145
|
joinedAt: z3.string(),
|
|
1100
1146
|
lastSeenAt: z3.string().nullable(),
|
|
1101
|
-
capacity: CapacitySchema.nullable()
|
|
1147
|
+
capacity: CapacitySchema.nullable(),
|
|
1148
|
+
/** ISO 3166-1 alpha-2 code (`RO`), null when nobody said where the host is. */
|
|
1149
|
+
country: z3.string().nullable()
|
|
1102
1150
|
});
|
|
1151
|
+
var HostPatchRequest = z3.object({ country: z3.string().nullable() });
|
|
1152
|
+
var CountrySchema = z3.object({ code: z3.string(), name: z3.string() });
|
|
1103
1153
|
|
|
1104
1154
|
// ../shared/src/ssh.ts
|
|
1105
1155
|
import { createHash } from "node:crypto";
|
|
@@ -1211,9 +1261,10 @@ async function joinControlPlane(opts, p = paths(), guessAddress = defaultAdverti
|
|
|
1211
1261
|
}
|
|
1212
1262
|
}
|
|
1213
1263
|
const name = opts.name ?? osHostname();
|
|
1264
|
+
const country = opts.country === void 0 ? void 0 : parseCountry(opts.country);
|
|
1214
1265
|
const { fingerprint } = await ensureTlsCert(p);
|
|
1215
1266
|
const hostUrl = `https://${advertise}:${port}`;
|
|
1216
|
-
const body = { token: opts.token, name, url: hostUrl, fingerprint, publicAddress: advertise };
|
|
1267
|
+
const body = { token: opts.token, name, url: hostUrl, fingerprint, publicAddress: advertise, ...country !== void 0 ? { country } : {} };
|
|
1217
1268
|
const res = await request(`${url}/hosts/join`, {
|
|
1218
1269
|
method: "POST",
|
|
1219
1270
|
headers: { "content-type": "application/json" },
|
|
@@ -1989,9 +2040,9 @@ async function writeBoxEnv(box, vars, p = paths()) {
|
|
|
1989
2040
|
await rename3(tmp, file);
|
|
1990
2041
|
}
|
|
1991
2042
|
async function mergeBoxEnv(box, given, p = paths()) {
|
|
1992
|
-
const
|
|
2043
|
+
const names2 = new Set(given.map((v) => v.name));
|
|
1993
2044
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1994
|
-
const kept = (await readBoxEnv(box, p)).filter((v) => !
|
|
2045
|
+
const kept = (await readBoxEnv(box, p)).filter((v) => !names2.has(v.name));
|
|
1995
2046
|
await writeBoxEnv(box, [...kept, ...given.map((v) => ({ ...v, secret: false, updatedAt: now }))], p);
|
|
1996
2047
|
}
|
|
1997
2048
|
async function copyBoxEnv(from, to) {
|
|
@@ -2107,13 +2158,13 @@ async function listImages(cfg, p = paths()) {
|
|
|
2107
2158
|
out.push({ name: BASE_IMAGE, version: await versionAt(void 0, cfg.rootfs) });
|
|
2108
2159
|
} catch {
|
|
2109
2160
|
}
|
|
2110
|
-
let
|
|
2161
|
+
let names2 = [];
|
|
2111
2162
|
try {
|
|
2112
|
-
|
|
2163
|
+
names2 = (await readdir3(p.images)).sort();
|
|
2113
2164
|
} catch (err) {
|
|
2114
2165
|
if (err.code !== "ENOENT") throw err;
|
|
2115
2166
|
}
|
|
2116
|
-
for (const name of
|
|
2167
|
+
for (const name of names2) {
|
|
2117
2168
|
if (!IMAGE_RE.test(name) || name === BASE_IMAGE) continue;
|
|
2118
2169
|
try {
|
|
2119
2170
|
out.push({ name, version: await versionAt(name, p.image(name).rootfs) });
|
|
@@ -4424,7 +4475,7 @@ async function withTries(what, fn, signal) {
|
|
|
4424
4475
|
}
|
|
4425
4476
|
|
|
4426
4477
|
// src/core/relay.ts
|
|
4427
|
-
import { chmod as chmod3, mkdir as mkdir8, rm as rm5, writeFile as writeFile7 } from "node:fs/promises";
|
|
4478
|
+
import { access as access2, chmod as chmod3, mkdir as mkdir8, rm as rm5, writeFile as writeFile7 } from "node:fs/promises";
|
|
4428
4479
|
|
|
4429
4480
|
// ../shared/src/relay.ts
|
|
4430
4481
|
import { z as z7 } from "zod";
|
|
@@ -4496,6 +4547,13 @@ async function useRelay(joinString, p = paths()) {
|
|
|
4496
4547
|
await saveConfig({ ...cfg, relay }, p);
|
|
4497
4548
|
return relay;
|
|
4498
4549
|
}
|
|
4550
|
+
async function restoreRelay(p = paths()) {
|
|
4551
|
+
const cfg = await loadConfig(p);
|
|
4552
|
+
if (!cfg.relay) return null;
|
|
4553
|
+
await access2(p.relayConf);
|
|
4554
|
+
await netSetup(cfg, ["relay", "up", p.relayConf, String(cfg.controlPlane?.port ?? DEFAULT_HTTPS_PORT)]);
|
|
4555
|
+
return cfg.relay;
|
|
4556
|
+
}
|
|
4499
4557
|
async function dropRelay(p = paths()) {
|
|
4500
4558
|
const cfg = await loadConfig(p);
|
|
4501
4559
|
const relay = cfg.relay;
|
|
@@ -4698,7 +4756,7 @@ function versionOf(srcFileUrl, injected) {
|
|
|
4698
4756
|
}
|
|
4699
4757
|
|
|
4700
4758
|
// src/version.ts
|
|
4701
|
-
var VERSION = versionOf(import.meta.url, true ? "0.5.
|
|
4759
|
+
var VERSION = versionOf(import.meta.url, true ? "0.5.40" : void 0);
|
|
4702
4760
|
|
|
4703
4761
|
// src/caller.ts
|
|
4704
4762
|
import { createHash as createHash2, timingSafeEqual } from "node:crypto";
|
|
@@ -5422,6 +5480,12 @@ program.command("start", { isDefault: true }).description("serve the API on the
|
|
|
5422
5480
|
} catch (err) {
|
|
5423
5481
|
console.error(`warning: host network not ready (${err.message}); run: kvbox-server net up`);
|
|
5424
5482
|
}
|
|
5483
|
+
try {
|
|
5484
|
+
const relay = await restoreRelay(paths());
|
|
5485
|
+
if (relay) console.log(`relay ${relay.name}: the control plane may reach this host through the tunnel again`);
|
|
5486
|
+
} catch (err) {
|
|
5487
|
+
console.error(`warning: the control plane may not reach this host through its relay (${err.message}); run: kvbox server relay <join string>`);
|
|
5488
|
+
}
|
|
5425
5489
|
let dnsWarned = false;
|
|
5426
5490
|
const bindDns = () => {
|
|
5427
5491
|
loadConfig(paths()).then(async (cfg) => {
|
|
@@ -5443,7 +5507,7 @@ program.command("start", { isDefault: true }).description("serve the API on the
|
|
|
5443
5507
|
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
5508
|
}
|
|
5445
5509
|
});
|
|
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) => {
|
|
5510
|
+
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
5511
|
const joined = await joinControlPlane({ url, ...opts }, paths());
|
|
5448
5512
|
console.log(`joined ${joined.controlPlane} as host ${joined.hostName}`);
|
|
5449
5513
|
console.log(`it answers the control plane on ${joined.hostUrl} once its server restarts`);
|