@kvbox/server 0.5.29 → 0.5.30

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.
@@ -170,7 +170,8 @@ var PricingSchema = z.object({
170
170
  ramGibHour: z.number().nonnegative(),
171
171
  diskGibHour: z.number().nonnegative(),
172
172
  volumeHour: z.number().nonnegative().default(0),
173
- volumeGibHour: z.number().nonnegative().default(0)
173
+ volumeGibHour: z.number().nonnegative().default(0),
174
+ diskIncludedGib: z.number().int().nonnegative().default(0)
174
175
  });
175
176
  var HOUR_MS = 36e5;
176
177
  var round = (n, places) => Math.round(n * 10 ** places) / 10 ** places;
@@ -182,6 +183,7 @@ function computeUsage(events, w) {
182
183
  let vcpuHours = 0;
183
184
  let ramGibHours = 0;
184
185
  let diskGibHours = 0;
186
+ const included = w.pricing?.diskIncludedGib ?? 0;
185
187
  sorted.forEach((e, i) => {
186
188
  const start = Math.max(Date.parse(e.at), from);
187
189
  const next = sorted[i + 1];
@@ -191,7 +193,7 @@ function computeUsage(events, w) {
191
193
  ms[e.state] += end - start;
192
194
  if (e.state === "running") vcpuHours += h * e.vcpu;
193
195
  if (e.state === "running" || e.state === "standby") ramGibHours += h * e.memMib / 1024;
194
- diskGibHours += h * e.diskGib;
196
+ diskGibHours += h * Math.max(0, e.diskGib - included);
195
197
  });
196
198
  const totals = {
197
199
  seconds: {
@@ -489,6 +491,21 @@ var DomainSchema = z3.object({
489
491
  records: z3.array(z3.object({ type: z3.enum(["A", "TXT", "CNAME"]), name: z3.string(), value: z3.string() })),
490
492
  createdAt: z3.string()
491
493
  });
494
+ var WakeHookSchema = z3.object({
495
+ id: z3.string(),
496
+ owner: z3.string(),
497
+ box: z3.string(),
498
+ url: z3.string(),
499
+ createdBy: z3.string().nullable(),
500
+ createdAt: z3.string(),
501
+ lastCalledAt: z3.string().nullable()
502
+ });
503
+ var CreatedWakeHookSchema = WakeHookSchema.extend({ secret: z3.string() });
504
+ var WakeHookCallSchema = z3.object({
505
+ box: z3.string(),
506
+ action: z3.enum(["wake", "resume", "none"]),
507
+ reason: z3.string().optional()
508
+ });
492
509
  var MachineSchema = z3.object({
493
510
  /** Host-unique short id (the tap is `kv-<id>`). Missing only on a box made before ids that has not restarted since. */
494
511
  id: z3.string(),
@@ -533,6 +550,8 @@ var MachineSchema = z3.object({
533
550
  volumes: z3.array(z3.object({ name: z3.string(), sizeGib: z3.number().int(), mountPath: z3.string() })).optional(),
534
551
  /** Boxes that may reach this one, each on one port (#98). Absent: none. */
535
552
  links: z3.array(BoxLinkSchema).optional(),
553
+ /** How to reach what the box serves, its secrets masked (#87): `postgres://postgres:****@db.kvbox:5432/postgres`. */
554
+ url: z3.string().optional(),
536
555
  /**
537
556
  * Only in what `POST /machines` answers, for an image that declares one (#87): how to reach what it
538
557
  * serves, e.g. `postgres://postgres:<password>@db.kvbox:5432/postgres`. Holds the box's variables.
@@ -1386,6 +1405,8 @@ var MachineMetaSchema = z4.object({
1386
1405
  base: z4.string(),
1387
1406
  /** The image (#87), when not the base one: assets/images/<image>/rootfs-<base>.ext4. `vm update` keeps it. */
1388
1407
  image: z4.string().optional(),
1408
+ /** The image's connect line with its secrets masked and `${BOX}` unfilled (#87, `maskedUrl`). Absent: the image declares none. */
1409
+ url: z4.string().optional(),
1389
1410
  /**
1390
1411
  * Snapshots taken since this VM started (boot or restore). Firecracker resets its dirty-page bitmap at
1391
1412
  * each one, so when this is > 0 the next diff is only standalone merged onto `mem.base`; if that file
@@ -1846,8 +1867,11 @@ var BoxEnvFileSchema = z5.object({
1846
1867
  vars: z5.array(z5.object({ name: z5.string(), value: z5.string(), secret: z5.boolean(), updatedAt: z5.string() }))
1847
1868
  });
1848
1869
  async function readBoxEnv(box, p = paths()) {
1870
+ return readBoxEnvFile(p.machine(box.owner, box.name).env);
1871
+ }
1872
+ async function readBoxEnvFile(file) {
1849
1873
  try {
1850
- return BoxEnvFileSchema.parse(JSON.parse(await readFile5(p.machine(box.owner, box.name).env, "utf8"))).vars;
1874
+ return BoxEnvFileSchema.parse(JSON.parse(await readFile5(file, "utf8"))).vars;
1851
1875
  } catch (err) {
1852
1876
  if (err.code === "ENOENT") return [];
1853
1877
  throw err;
@@ -2073,6 +2097,15 @@ function imageEnv(manifest, box, given, inherited = []) {
2073
2097
  const connect3 = manifest.connect === void 0 ? void 0 : expand(manifest.connect, connectVars);
2074
2098
  return { vars: [...given, ...added], generated, secrets: [...generated, ...secrets], connect: connect3 };
2075
2099
  }
2100
+ var MASK = "****";
2101
+ function maskedUrl(manifest, vars) {
2102
+ if (manifest.connect === void 0) return void 0;
2103
+ const encode = /^[a-z][a-z0-9+.-]*:\/\//i.test(manifest.connect);
2104
+ const generated = new Set(manifest.generate ?? []);
2105
+ const shown2 = new Map(vars.map((v) => [v.name, v.secret || generated.has(v.name) ? MASK : encode ? encodeURIComponent(v.value) : v.value]));
2106
+ return manifest.connect.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (all, name) => name === "BOX" ? all : shown2.get(name) ?? all);
2107
+ }
2108
+ var urlFor = (url, name) => url.replaceAll("${BOX}", name);
2076
2109
  function namesIn(text) {
2077
2110
  return [...text.matchAll(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g)].map((m) => m[1]);
2078
2111
  }
@@ -2865,6 +2898,7 @@ function toMachine(meta, p = paths()) {
2865
2898
  ...diskStats(p.machine(meta.owner, meta.name).overlay),
2866
2899
  base: meta.base,
2867
2900
  ...meta.image !== void 0 ? { image: meta.image } : {},
2901
+ ...meta.url !== void 0 ? { url: urlFor(meta.url, meta.name) } : {},
2868
2902
  createdAt: meta.createdAt,
2869
2903
  source: meta.source,
2870
2904
  status: statusOf(meta, p),
@@ -3067,14 +3101,15 @@ async function createMachine(req, owner, p = paths()) {
3067
3101
  const env = imageEnv(manifest, name, req.env ?? [], (await readOwnerEnv(owner, p).catch(() => null))?.vars ?? []);
3068
3102
  const now = (/* @__PURE__ */ new Date()).toISOString();
3069
3103
  const autoHibernateSec = autoHibernateFor(req, cfg, manifest);
3104
+ const vars = env.vars.map((v) => ({ ...v, secret: env.secrets.includes(v.name), updatedAt: now }));
3070
3105
  const machine = await bootInto(
3071
3106
  { id, name, owner, ip, mac: macFor(ip), vcpu: size.vcpu, memMib: size.memMib, base: base.version, image, sshKeys },
3072
3107
  // The image is shared read-only by every box; only the box's own writes land in its overlay.
3073
3108
  async (m) => {
3074
3109
  await createOverlay(m.overlay, size.diskGib);
3075
- if (env.vars.length > 0) await writeBoxEnv({ owner, name }, env.vars.map((v) => ({ ...v, secret: env.secrets.includes(v.name), updatedAt: now })), p);
3110
+ if (vars.length > 0) await writeBoxEnv({ owner, name }, vars, p);
3076
3111
  },
3077
- { source: "base", autoHibernateSec },
3112
+ { source: "base", autoHibernateSec, url: maskedUrl(manifest, vars) },
3078
3113
  cfg,
3079
3114
  p
3080
3115
  );
@@ -3152,6 +3187,8 @@ async function restoreFrom(from, owner, req, cfg, p) {
3152
3187
  const id = newBoxId(idsInUse(existing));
3153
3188
  const ip = nextFreeIp(cfg.subnet, [...existing.map((m) => m.ip), cfg.gateway]);
3154
3189
  const s = p.snapshot(from.owner, from.name);
3190
+ const given = new Set((req.env ?? []).map((v) => v.name));
3191
+ const vars = [...(await readBoxEnvFile(s.env)).filter((v) => !given.has(v.name)), ...(req.env ?? []).map((v) => ({ ...v, secret: false }))];
3155
3192
  return restoreInto(
3156
3193
  { id, name, owner, ip, mac: macFor(ip), vcpu: snap.vcpu, memMib: snap.memMib, base: snap.base, image: snap.image, sshKeys },
3157
3194
  async (m) => {
@@ -3162,7 +3199,7 @@ async function restoreFrom(from, owner, req, cfg, p) {
3162
3199
  await placeholderDrives(m.dir, snap.volumes ?? []);
3163
3200
  return { vmstate: s.vmstate, mem: s.mem, label: shown(from), volumes: snap.volumes };
3164
3201
  },
3165
- { source: `snapshot:${refKey(from)}`, autoHibernateSec: autoHibernateFor(req, cfg, manifest) },
3202
+ { source: `snapshot:${refKey(from)}`, autoHibernateSec: autoHibernateFor(req, cfg, manifest), url: maskedUrl(manifest, vars) },
3166
3203
  cfg,
3167
3204
  p
3168
3205
  );
@@ -3241,7 +3278,7 @@ var forkMachine = locked(async function forkMachine2(ref, owner, req, p = paths(
3241
3278
  const ip = nextFreeIp(cfg.subnet, [...existing.map((m) => m.ip), cfg.gateway]);
3242
3279
  const box = { id, name, owner, ip, mac: macFor(ip), ...shape, base: src.base, image: src.image, sshKeys };
3243
3280
  const srcEnv = p.machine(src.owner, src.name).env;
3244
- const origin = { source: `fork:${refKey(src)}`, autoHibernateSec: (req.autoHibernateSec ?? src.autoHibernateSec) || void 0 };
3281
+ const origin = { source: `fork:${refKey(src)}`, autoHibernateSec: (req.autoHibernateSec ?? src.autoHibernateSec) || void 0, url: src.url };
3245
3282
  if (!warm) {
3246
3283
  return bootInto(
3247
3284
  box,
@@ -4143,7 +4180,7 @@ function versionOf(srcFileUrl, injected) {
4143
4180
  }
4144
4181
 
4145
4182
  // src/version.ts
4146
- var VERSION = versionOf(import.meta.url, true ? "0.5.29" : void 0);
4183
+ var VERSION = versionOf(import.meta.url, true ? "0.5.30" : void 0);
4147
4184
 
4148
4185
  // src/caller.ts
4149
4186
  import { createHash as createHash2, timingSafeEqual } from "node:crypto";