@finalai/sandbox-router-final 0.2.2 → 0.2.4

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.
@@ -13,7 +13,7 @@ const lifecycleFields = (lifecycle, defaultWallClockS = DEFAULT_WALL_CLOCK_S) =>
13
13
  idlePolicy: idle == null ? void 0 : IDLE_POLICIES[idle.action],
14
14
  autoResume: lifecycle.autoResume,
15
15
  wallClockLimitS: max === void 0 ? defaultWallClockS : max === null ? null : void 0,
16
- maxLifetime: max == null ? null : { after_s: max.afterS, action: max.action, from: MaxLifetimeFrom.Start }
16
+ maxLifetime: max == null ? null : { after_s: max.afterS, action: max.action, from: MaxLifetimeFrom.Create }
17
17
  };
18
18
  };
19
19
  const IDLE_ACTIONS = {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/lifecycle.ts"],
4
- "sourcesContent": ["import { IdleAction, MaxAction, type LifecycleIface } from '@finalai/sandbox-router/provider';\nimport { IdlePolicy, MaxLifetimeFrom, type MaxLifetimeBodyIface } from \"./vm/resources.mjs\";\n\n// A lifecycle in the platform's terms. The idle policy answers to idleness. A max lifetime stops\n// or deletes the sandbox a time after its latest start (create being the first), in any state,\n// and replaces the wall-clock limit; without one, the wall-clock limit applies the idle policy to\n// a sandbox that has run that long since it last started or resumed.\n\n// This adapter's default for a lifecycle that leaves `max` out: a wall-clock limit of one year,\n// which create caps at the plan's maximum (30 days by default). The platform's own default is a\n// day.\nexport const DEFAULT_WALL_CLOCK_S = 365 * 24 * 60 * 60;\n\nconst IDLE_POLICIES: Record<IdleAction, IdlePolicy> = {\n [IdleAction.Pause]: IdlePolicy.Pause,\n [IdleAction.Stop]: IdlePolicy.Stop,\n [IdleAction.Delete]: IdlePolicy.Delete,\n};\n\nexport interface LifecycleFieldsIface {\n // Null: no idle timeout. This adapter has none by default (the platform's is five minutes).\n idleTimeoutS: number | null;\n // Undefined: the platform's default (pause).\n idlePolicy: IdlePolicy | undefined;\n // Undefined: the platform's default (wakes).\n autoResume: boolean | undefined;\n // Null: none, for organizations entitled to run without one. Undefined: a max lifetime\n // replaces it.\n wallClockLimitS: number | null | undefined;\n // Undefined: none to set (create) or keep; null removes one.\n maxLifetime: MaxLifetimeBodyIface | null;\n}\n\nexport const lifecycleFields = (\n lifecycle: LifecycleIface,\n defaultWallClockS: number = DEFAULT_WALL_CLOCK_S,\n): LifecycleFieldsIface => {\n const { idle, max } = lifecycle;\n return {\n idleTimeoutS: idle?.afterS ?? null,\n idlePolicy: idle == null ? undefined : IDLE_POLICIES[idle.action],\n autoResume: lifecycle.autoResume,\n wallClockLimitS: max === undefined ? defaultWallClockS : max === null ? null : undefined,\n maxLifetime:\n max == null ? null : { after_s: max.afterS, action: max.action, from: MaxLifetimeFrom.Start },\n };\n};\n\nconst IDLE_ACTIONS: Record<IdlePolicy, IdleAction> = {\n [IdlePolicy.Pause]: IdleAction.Pause,\n [IdlePolicy.Stop]: IdleAction.Stop,\n [IdlePolicy.Delete]: IdleAction.Delete,\n};\n\nconst MAX_ACTIONS: Partial<Record<IdlePolicy, MaxAction>> = {\n [IdlePolicy.Stop]: MaxAction.Stop,\n [IdlePolicy.Delete]: MaxAction.Delete,\n};\n\nexport interface LifecycleStateIface {\n // As set; null when the platform's default applies or the timer is off.\n idleTimeoutS: number | null;\n idleTimeoutDisabled: boolean;\n idlePolicy: IdlePolicy;\n autoResume: boolean;\n // Its own lifetime (a create's time to live reads as a delete from create).\n maxLifetime: { afterS: number; action: 'stop' | 'delete' } | null;\n wallClockLimitS: number | null;\n // Set while running unless the wall-clock limit is off or replaced.\n wallClockDeadlineAt: Date | null;\n running: boolean;\n}\n\n// The lifecycle in force, as far as the API shows it. A field at the platform's default is left\n// out (the API does not say what the default is), and so is a wall-clock limit that pauses,\n// which has no router equivalent. A lifetime counted from create is reported as a maximum like\n// one counted from the latest start. The wall-clock limit is reported as kept, capped at the\n// plan's maximum.\nexport const lifecycleOf = (state: LifecycleStateIface): LifecycleIface => {\n const idle = state.idleTimeoutDisabled\n ? null\n : state.idleTimeoutS == null\n ? undefined\n : { afterS: state.idleTimeoutS, action: IDLE_ACTIONS[state.idlePolicy] };\n const wallClockAction = MAX_ACTIONS[state.idlePolicy];\n const max =\n state.maxLifetime != null\n ? {\n afterS: state.maxLifetime.afterS,\n action: state.maxLifetime.action === 'stop' ? MaxAction.Stop : MaxAction.Delete,\n }\n : state.wallClockLimitS != null\n ? wallClockAction == null\n ? undefined\n : { afterS: state.wallClockLimitS, action: wallClockAction }\n : state.running && state.wallClockDeadlineAt == null\n ? null\n : undefined;\n return {\n ...(idle === undefined ? {} : { idle }),\n ...(max === undefined ? {} : { max }),\n autoResume: state.autoResume,\n };\n};\n"],
5
- "mappings": "AAAA,SAAS,YAAY,iBAAsC;AAC3D,SAAS,YAAY,uBAAkD;AAUhE,MAAM,uBAAuB,MAAM,KAAK,KAAK;AAEpD,MAAM,gBAAgD;AAAA,EACpD,CAAC,WAAW,KAAK,GAAG,WAAW;AAAA,EAC/B,CAAC,WAAW,IAAI,GAAG,WAAW;AAAA,EAC9B,CAAC,WAAW,MAAM,GAAG,WAAW;AAClC;AAgBO,MAAM,kBAAkB,CAC7B,WACA,oBAA4B,yBACH;AACzB,QAAM,EAAE,MAAM,IAAI,IAAI;AACtB,SAAO;AAAA,IACL,cAAc,MAAM,UAAU;AAAA,IAC9B,YAAY,QAAQ,OAAO,SAAY,cAAc,KAAK,MAAM;AAAA,IAChE,YAAY,UAAU;AAAA,IACtB,iBAAiB,QAAQ,SAAY,oBAAoB,QAAQ,OAAO,OAAO;AAAA,IAC/E,aACE,OAAO,OAAO,OAAO,EAAE,SAAS,IAAI,QAAQ,QAAQ,IAAI,QAAQ,MAAM,gBAAgB,MAAM;AAAA,EAChG;AACF;AAEA,MAAM,eAA+C;AAAA,EACnD,CAAC,WAAW,KAAK,GAAG,WAAW;AAAA,EAC/B,CAAC,WAAW,IAAI,GAAG,WAAW;AAAA,EAC9B,CAAC,WAAW,MAAM,GAAG,WAAW;AAClC;AAEA,MAAM,cAAsD;AAAA,EAC1D,CAAC,WAAW,IAAI,GAAG,UAAU;AAAA,EAC7B,CAAC,WAAW,MAAM,GAAG,UAAU;AACjC;AAqBO,MAAM,cAAc,CAAC,UAA+C;AACzE,QAAM,OAAO,MAAM,sBACf,OACA,MAAM,gBAAgB,OACpB,SACA,EAAE,QAAQ,MAAM,cAAc,QAAQ,aAAa,MAAM,UAAU,EAAE;AAC3E,QAAM,kBAAkB,YAAY,MAAM,UAAU;AACpD,QAAM,MACJ,MAAM,eAAe,OACjB;AAAA,IACE,QAAQ,MAAM,YAAY;AAAA,IAC1B,QAAQ,MAAM,YAAY,WAAW,SAAS,UAAU,OAAO,UAAU;AAAA,EAC3E,IACA,MAAM,mBAAmB,OACvB,mBAAmB,OACjB,SACA,EAAE,QAAQ,MAAM,iBAAiB,QAAQ,gBAAgB,IAC3D,MAAM,WAAW,MAAM,uBAAuB,OAC5C,OACA;AACV,SAAO;AAAA,IACL,GAAI,SAAS,SAAY,CAAC,IAAI,EAAE,KAAK;AAAA,IACrC,GAAI,QAAQ,SAAY,CAAC,IAAI,EAAE,IAAI;AAAA,IACnC,YAAY,MAAM;AAAA,EACpB;AACF;",
4
+ "sourcesContent": ["import { IdleAction, MaxAction, type LifecycleIface } from '@finalai/sandbox-router/provider';\nimport { IdlePolicy, MaxLifetimeFrom, type MaxLifetimeBodyIface } from \"./vm/resources.mjs\";\n\n// A lifecycle in the platform's terms. The idle policy answers to idleness. A max lifetime stops\n// or deletes the sandbox a time after its create, in any state,\n// and replaces the wall-clock limit; without one, the wall-clock limit applies the idle policy to\n// a sandbox that has run that long since it last started or resumed.\n\n// This adapter's default for a lifecycle that leaves `max` out: a wall-clock limit of one year,\n// which create caps at the plan's maximum (30 days by default). The platform's own default is a\n// day.\nexport const DEFAULT_WALL_CLOCK_S = 365 * 24 * 60 * 60;\n\nconst IDLE_POLICIES: Record<IdleAction, IdlePolicy> = {\n [IdleAction.Pause]: IdlePolicy.Pause,\n [IdleAction.Stop]: IdlePolicy.Stop,\n [IdleAction.Delete]: IdlePolicy.Delete,\n};\n\nexport interface LifecycleFieldsIface {\n // Null: no idle timeout. This adapter has none by default (the platform's is five minutes).\n idleTimeoutS: number | null;\n // Undefined: the platform's default (pause).\n idlePolicy: IdlePolicy | undefined;\n // Undefined: the platform's default (wakes).\n autoResume: boolean | undefined;\n // Null: none, for organizations entitled to run without one. Undefined: a max lifetime\n // replaces it.\n wallClockLimitS: number | null | undefined;\n // Undefined: none to set (create) or keep; null removes one.\n maxLifetime: MaxLifetimeBodyIface | null;\n}\n\nexport const lifecycleFields = (\n lifecycle: LifecycleIface,\n defaultWallClockS: number = DEFAULT_WALL_CLOCK_S,\n): LifecycleFieldsIface => {\n const { idle, max } = lifecycle;\n return {\n idleTimeoutS: idle?.afterS ?? null,\n idlePolicy: idle == null ? undefined : IDLE_POLICIES[idle.action],\n autoResume: lifecycle.autoResume,\n wallClockLimitS: max === undefined ? defaultWallClockS : max === null ? null : undefined,\n maxLifetime:\n max == null\n ? null\n : { after_s: max.afterS, action: max.action, from: MaxLifetimeFrom.Create },\n };\n};\n\nconst IDLE_ACTIONS: Record<IdlePolicy, IdleAction> = {\n [IdlePolicy.Pause]: IdleAction.Pause,\n [IdlePolicy.Stop]: IdleAction.Stop,\n [IdlePolicy.Delete]: IdleAction.Delete,\n};\n\nconst MAX_ACTIONS: Partial<Record<IdlePolicy, MaxAction>> = {\n [IdlePolicy.Stop]: MaxAction.Stop,\n [IdlePolicy.Delete]: MaxAction.Delete,\n};\n\nexport interface LifecycleStateIface {\n // As set; null when the platform's default applies or the timer is off.\n idleTimeoutS: number | null;\n idleTimeoutDisabled: boolean;\n idlePolicy: IdlePolicy;\n autoResume: boolean;\n // Its own lifetime (a create's time to live reads as a delete from create).\n maxLifetime: { afterS: number; action: 'stop' | 'delete' } | null;\n wallClockLimitS: number | null;\n // Set while running unless the wall-clock limit is off or replaced.\n wallClockDeadlineAt: Date | null;\n running: boolean;\n}\n\n// The lifecycle in force, as far as the API shows it. A field at the platform's default is left\n// out (the API does not say what the default is), and so is a wall-clock limit that pauses,\n// which has no router equivalent. A lifetime counted from create is reported as a maximum like\n// one counted from the latest start. The wall-clock limit is reported as kept, capped at the\n// plan's maximum.\nexport const lifecycleOf = (state: LifecycleStateIface): LifecycleIface => {\n const idle = state.idleTimeoutDisabled\n ? null\n : state.idleTimeoutS == null\n ? undefined\n : { afterS: state.idleTimeoutS, action: IDLE_ACTIONS[state.idlePolicy] };\n const wallClockAction = MAX_ACTIONS[state.idlePolicy];\n const max =\n state.maxLifetime != null\n ? {\n afterS: state.maxLifetime.afterS,\n action: state.maxLifetime.action === 'stop' ? MaxAction.Stop : MaxAction.Delete,\n }\n : state.wallClockLimitS != null\n ? wallClockAction == null\n ? undefined\n : { afterS: state.wallClockLimitS, action: wallClockAction }\n : state.running && state.wallClockDeadlineAt == null\n ? null\n : undefined;\n return {\n ...(idle === undefined ? {} : { idle }),\n ...(max === undefined ? {} : { max }),\n autoResume: state.autoResume,\n };\n};\n"],
5
+ "mappings": "AAAA,SAAS,YAAY,iBAAsC;AAC3D,SAAS,YAAY,uBAAkD;AAUhE,MAAM,uBAAuB,MAAM,KAAK,KAAK;AAEpD,MAAM,gBAAgD;AAAA,EACpD,CAAC,WAAW,KAAK,GAAG,WAAW;AAAA,EAC/B,CAAC,WAAW,IAAI,GAAG,WAAW;AAAA,EAC9B,CAAC,WAAW,MAAM,GAAG,WAAW;AAClC;AAgBO,MAAM,kBAAkB,CAC7B,WACA,oBAA4B,yBACH;AACzB,QAAM,EAAE,MAAM,IAAI,IAAI;AACtB,SAAO;AAAA,IACL,cAAc,MAAM,UAAU;AAAA,IAC9B,YAAY,QAAQ,OAAO,SAAY,cAAc,KAAK,MAAM;AAAA,IAChE,YAAY,UAAU;AAAA,IACtB,iBAAiB,QAAQ,SAAY,oBAAoB,QAAQ,OAAO,OAAO;AAAA,IAC/E,aACE,OAAO,OACH,OACA,EAAE,SAAS,IAAI,QAAQ,QAAQ,IAAI,QAAQ,MAAM,gBAAgB,OAAO;AAAA,EAChF;AACF;AAEA,MAAM,eAA+C;AAAA,EACnD,CAAC,WAAW,KAAK,GAAG,WAAW;AAAA,EAC/B,CAAC,WAAW,IAAI,GAAG,WAAW;AAAA,EAC9B,CAAC,WAAW,MAAM,GAAG,WAAW;AAClC;AAEA,MAAM,cAAsD;AAAA,EAC1D,CAAC,WAAW,IAAI,GAAG,UAAU;AAAA,EAC7B,CAAC,WAAW,MAAM,GAAG,UAAU;AACjC;AAqBO,MAAM,cAAc,CAAC,UAA+C;AACzE,QAAM,OAAO,MAAM,sBACf,OACA,MAAM,gBAAgB,OACpB,SACA,EAAE,QAAQ,MAAM,cAAc,QAAQ,aAAa,MAAM,UAAU,EAAE;AAC3E,QAAM,kBAAkB,YAAY,MAAM,UAAU;AACpD,QAAM,MACJ,MAAM,eAAe,OACjB;AAAA,IACE,QAAQ,MAAM,YAAY;AAAA,IAC1B,QAAQ,MAAM,YAAY,WAAW,SAAS,UAAU,OAAO,UAAU;AAAA,EAC3E,IACA,MAAM,mBAAmB,OACvB,mBAAmB,OACjB,SACA,EAAE,QAAQ,MAAM,iBAAiB,QAAQ,gBAAgB,IAC3D,MAAM,WAAW,MAAM,uBAAuB,OAC5C,OACA;AACV,SAAO;AAAA,IACL,GAAI,SAAS,SAAY,CAAC,IAAI,EAAE,KAAK;AAAA,IACrC,GAAI,QAAQ,SAAY,CAAC,IAAI,EAAE,IAAI;AAAA,IACnC,YAAY,MAAM;AAAA,EACpB;AACF;",
6
6
  "names": []
7
7
  }
@@ -3,7 +3,33 @@ import {
3
3
  } from "@finalai/sandbox-router/provider";
4
4
  const DEFAULT_HEALTH = { intervalS: 5, timeoutS: 5, startGraceS: 60 };
5
5
  const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
6
- const healthTarget = (check) => "http" in check ? check.http : "port" in check ? { port: check.port, path: "/" } : null;
6
+ const LOOPBACK_HOSTS = /* @__PURE__ */ new Set(["127.0.0.1", "localhost", "[::1]"]);
7
+ const urlTarget = (value) => {
8
+ let url;
9
+ try {
10
+ url = new URL(value);
11
+ } catch {
12
+ return { ok: false, reason: "a service health check URL must be absolute" };
13
+ }
14
+ if (url.protocol !== "http:" || !LOOPBACK_HOSTS.has(url.hostname))
15
+ return {
16
+ ok: false,
17
+ reason: "a service health check URL must be http to the sandbox itself (127.0.0.1)"
18
+ };
19
+ if (url.username !== "" || url.password !== "")
20
+ return { ok: false, reason: "a service health check URL cannot carry credentials" };
21
+ return {
22
+ ok: true,
23
+ port: url.port === "" ? 80 : Number(url.port),
24
+ path: `${url.pathname}${url.search}`
25
+ };
26
+ };
27
+ const healthTarget = (check) => {
28
+ if ("http" in check) return { ok: true, ...check.http };
29
+ if ("port" in check) return { ok: true, port: check.port, path: "/" };
30
+ if ("url" in check) return urlTarget(check.url);
31
+ return { ok: false, reason: "service health checks are HTTP, URL or port checks" };
32
+ };
7
33
  const serviceWorkload = (service) => {
8
34
  const env = Object.entries(service.env);
9
35
  if (env.some(([name]) => !ENV_NAME.test(name)))
@@ -13,8 +39,7 @@ const serviceWorkload = (service) => {
13
39
  const prefix = env.length === 0 ? [] : ["/usr/bin/env", ...env.map(([name, value]) => `${name}=${value}`)];
14
40
  const argv = service.command == null ? null : typeof service.command === "string" ? [...prefix, "/bin/sh", "-c", service.command] : [...prefix, ...service.command];
15
41
  const check = service.healthCheck == null ? null : healthTarget(service.healthCheck);
16
- if (service.healthCheck != null && check == null)
17
- return { ok: false, reason: "service health checks are HTTP or port checks" };
42
+ if (check?.ok === false) return { ok: false, reason: check.reason };
18
43
  const health = { ...DEFAULT_HEALTH, ...service.health };
19
44
  return {
20
45
  ok: true,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/service.ts"],
4
- "sourcesContent": ["import {\n RestartPolicy,\n type ReadyCheckIface,\n type ServiceSpecIface,\n} from '@finalai/sandbox-router/provider';\nimport type { WorkloadBodyIface } from \"./vm/resources.mjs\";\n\n// A service in the platform's terms: its supervised workload, restarted when it exits and\n// health-checked over HTTP.\n\nconst DEFAULT_HEALTH = { intervalS: 5, timeoutS: 5, startGraceS: 60 };\n\nconst ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\n// The platform checks health with an HTTP GET; a port check asks for `/`.\nconst healthTarget = (check: ReadyCheckIface): { port: number; path: string } | null =>\n 'http' in check ? check.http : 'port' in check ? { port: check.port, path: '/' } : null;\n\nexport type ServicePlanIface =\n | { ok: true; workload: WorkloadBodyIface }\n | { ok: false; reason: string };\n\nexport const serviceWorkload = (service: ServiceSpecIface): ServicePlanIface => {\n const env = Object.entries(service.env);\n if (env.some(([name]) => !ENV_NAME.test(name)))\n return { ok: false, reason: 'service variables must be shell names' };\n if (service.command == null && env.length > 0)\n return { ok: false, reason: 'service variables need a command (the image command takes none)' };\n // The workload takes no environment of its own: `env` sets it for the command.\n const prefix =\n env.length === 0 ? [] : ['/usr/bin/env', ...env.map(([name, value]) => `${name}=${value}`)];\n const argv =\n service.command == null\n ? null\n : typeof service.command === 'string'\n ? [...prefix, '/bin/sh', '-c', service.command]\n : [...prefix, ...service.command];\n const check = service.healthCheck == null ? null : healthTarget(service.healthCheck);\n if (service.healthCheck != null && check == null)\n return { ok: false, reason: 'service health checks are HTTP or port checks' };\n const health = { ...DEFAULT_HEALTH, ...service.health };\n return {\n ok: true,\n workload: {\n entrypoint: argv?.slice(0, 1),\n cmd: argv?.slice(1),\n workdir: service.cwd,\n user: service.user,\n // The platform restarts a workload whenever it exits, failed or not.\n restart: service.restart !== RestartPolicy.Never,\n termination_grace_s: service.graceS,\n health_check:\n check == null\n ? undefined\n : {\n port: check.port,\n path: check.path,\n interval_s: Math.max(1, Math.round(health.intervalS)),\n timeout_s: Math.min(60, Math.max(1, Math.round(health.timeoutS))),\n grace_s: Math.round(health.startGraceS),\n },\n },\n };\n};\n\n// A running sandbox's workload can be replaced but not removed: a stopped service is one that\n// exits at once and is not restarted.\nexport const STOPPED_WORKLOAD: WorkloadBodyIface = {\n entrypoint: ['/bin/sh', '-c', 'exit 0'],\n restart: false,\n};\n\nexport const isStoppedWorkload = (workload: WorkloadBodyIface): boolean =>\n workload.restart === false &&\n (workload.cmd ?? []).length === 0 &&\n JSON.stringify(workload.entrypoint) === JSON.stringify(STOPPED_WORKLOAD.entrypoint);\n"],
5
- "mappings": "AAAA;AAAA,EACE;AAAA,OAGK;AAMP,MAAM,iBAAiB,EAAE,WAAW,GAAG,UAAU,GAAG,aAAa,GAAG;AAEpE,MAAM,WAAW;AAGjB,MAAM,eAAe,CAAC,UACpB,UAAU,QAAQ,MAAM,OAAO,UAAU,QAAQ,EAAE,MAAM,MAAM,MAAM,MAAM,IAAI,IAAI;AAM9E,MAAM,kBAAkB,CAAC,YAAgD;AAC9E,QAAM,MAAM,OAAO,QAAQ,QAAQ,GAAG;AACtC,MAAI,IAAI,KAAK,CAAC,CAAC,IAAI,MAAM,CAAC,SAAS,KAAK,IAAI,CAAC;AAC3C,WAAO,EAAE,IAAI,OAAO,QAAQ,wCAAwC;AACtE,MAAI,QAAQ,WAAW,QAAQ,IAAI,SAAS;AAC1C,WAAO,EAAE,IAAI,OAAO,QAAQ,kEAAkE;AAEhG,QAAM,SACJ,IAAI,WAAW,IAAI,CAAC,IAAI,CAAC,gBAAgB,GAAG,IAAI,IAAI,CAAC,CAAC,MAAM,KAAK,MAAM,GAAG,IAAI,IAAI,KAAK,EAAE,CAAC;AAC5F,QAAM,OACJ,QAAQ,WAAW,OACf,OACA,OAAO,QAAQ,YAAY,WACzB,CAAC,GAAG,QAAQ,WAAW,MAAM,QAAQ,OAAO,IAC5C,CAAC,GAAG,QAAQ,GAAG,QAAQ,OAAO;AACtC,QAAM,QAAQ,QAAQ,eAAe,OAAO,OAAO,aAAa,QAAQ,WAAW;AACnF,MAAI,QAAQ,eAAe,QAAQ,SAAS;AAC1C,WAAO,EAAE,IAAI,OAAO,QAAQ,gDAAgD;AAC9E,QAAM,SAAS,EAAE,GAAG,gBAAgB,GAAG,QAAQ,OAAO;AACtD,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,UAAU;AAAA,MACR,YAAY,MAAM,MAAM,GAAG,CAAC;AAAA,MAC5B,KAAK,MAAM,MAAM,CAAC;AAAA,MAClB,SAAS,QAAQ;AAAA,MACjB,MAAM,QAAQ;AAAA;AAAA,MAEd,SAAS,QAAQ,YAAY,cAAc;AAAA,MAC3C,qBAAqB,QAAQ;AAAA,MAC7B,cACE,SAAS,OACL,SACA;AAAA,QACE,MAAM,MAAM;AAAA,QACZ,MAAM,MAAM;AAAA,QACZ,YAAY,KAAK,IAAI,GAAG,KAAK,MAAM,OAAO,SAAS,CAAC;AAAA,QACpD,WAAW,KAAK,IAAI,IAAI,KAAK,IAAI,GAAG,KAAK,MAAM,OAAO,QAAQ,CAAC,CAAC;AAAA,QAChE,SAAS,KAAK,MAAM,OAAO,WAAW;AAAA,MACxC;AAAA,IACR;AAAA,EACF;AACF;AAIO,MAAM,mBAAsC;AAAA,EACjD,YAAY,CAAC,WAAW,MAAM,QAAQ;AAAA,EACtC,SAAS;AACX;AAEO,MAAM,oBAAoB,CAAC,aAChC,SAAS,YAAY,UACpB,SAAS,OAAO,CAAC,GAAG,WAAW,KAChC,KAAK,UAAU,SAAS,UAAU,MAAM,KAAK,UAAU,iBAAiB,UAAU;",
4
+ "sourcesContent": ["import {\n RestartPolicy,\n type ReadyCheckIface,\n type ServiceSpecIface,\n} from '@finalai/sandbox-router/provider';\nimport type { WorkloadBodyIface } from \"./vm/resources.mjs\";\n\n// A service in the platform's terms: its supervised workload, restarted when it exits and\n// health-checked over HTTP.\n\nconst DEFAULT_HEALTH = { intervalS: 5, timeoutS: 5, startGraceS: 60 };\n\nconst ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\n// The VM itself, as a health-check URL names it.\nconst LOOPBACK_HOSTS = new Set(['127.0.0.1', 'localhost', '[::1]']);\n\ntype HealthTargetType = { ok: true; port: number; path: string } | { ok: false; reason: string };\n\n// A `url` check is the same HTTP GET when it is plain http to the VM itself: its port (80 when\n// it names none), path and query. Anything else (https, another host, credentials) the platform\n// cannot run.\nconst urlTarget = (value: string): HealthTargetType => {\n let url: URL;\n try {\n url = new URL(value);\n } catch {\n return { ok: false, reason: 'a service health check URL must be absolute' };\n }\n if (url.protocol !== 'http:' || !LOOPBACK_HOSTS.has(url.hostname))\n return {\n ok: false,\n reason: 'a service health check URL must be http to the sandbox itself (127.0.0.1)',\n };\n // The platform's probe sends no credentials: a check that needs them would always fail.\n if (url.username !== '' || url.password !== '')\n return { ok: false, reason: 'a service health check URL cannot carry credentials' };\n return {\n ok: true,\n port: url.port === '' ? 80 : Number(url.port),\n path: `${url.pathname}${url.search}`,\n };\n};\n\n// The platform checks health with an HTTP GET; a port check asks for `/`.\nconst healthTarget = (check: ReadyCheckIface): HealthTargetType => {\n if ('http' in check) return { ok: true, ...check.http };\n if ('port' in check) return { ok: true, port: check.port, path: '/' };\n if ('url' in check) return urlTarget(check.url);\n return { ok: false, reason: 'service health checks are HTTP, URL or port checks' };\n};\n\nexport type ServicePlanIface =\n | { ok: true; workload: WorkloadBodyIface }\n | { ok: false; reason: string };\n\nexport const serviceWorkload = (service: ServiceSpecIface): ServicePlanIface => {\n const env = Object.entries(service.env);\n if (env.some(([name]) => !ENV_NAME.test(name)))\n return { ok: false, reason: 'service variables must be shell names' };\n if (service.command == null && env.length > 0)\n return { ok: false, reason: 'service variables need a command (the image command takes none)' };\n // The workload takes no environment of its own: `env` sets it for the command.\n const prefix =\n env.length === 0 ? [] : ['/usr/bin/env', ...env.map(([name, value]) => `${name}=${value}`)];\n const argv =\n service.command == null\n ? null\n : typeof service.command === 'string'\n ? [...prefix, '/bin/sh', '-c', service.command]\n : [...prefix, ...service.command];\n const check = service.healthCheck == null ? null : healthTarget(service.healthCheck);\n if (check?.ok === false) return { ok: false, reason: check.reason };\n const health = { ...DEFAULT_HEALTH, ...service.health };\n return {\n ok: true,\n workload: {\n entrypoint: argv?.slice(0, 1),\n cmd: argv?.slice(1),\n workdir: service.cwd,\n user: service.user,\n // The platform restarts a workload whenever it exits, failed or not.\n restart: service.restart !== RestartPolicy.Never,\n termination_grace_s: service.graceS,\n health_check:\n check == null\n ? undefined\n : {\n port: check.port,\n path: check.path,\n interval_s: Math.max(1, Math.round(health.intervalS)),\n timeout_s: Math.min(60, Math.max(1, Math.round(health.timeoutS))),\n grace_s: Math.round(health.startGraceS),\n },\n },\n };\n};\n\n// A running sandbox's workload can be replaced but not removed: a stopped service is one that\n// exits at once and is not restarted.\nexport const STOPPED_WORKLOAD: WorkloadBodyIface = {\n entrypoint: ['/bin/sh', '-c', 'exit 0'],\n restart: false,\n};\n\nexport const isStoppedWorkload = (workload: WorkloadBodyIface): boolean =>\n workload.restart === false &&\n (workload.cmd ?? []).length === 0 &&\n JSON.stringify(workload.entrypoint) === JSON.stringify(STOPPED_WORKLOAD.entrypoint);\n"],
5
+ "mappings": "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",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@finalai/sandbox-router-final",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "private": false,
5
5
  "description": "Final VMs provider for the Sandbox Router.",
6
6
  "license": "Apache-2.0",