ppio-sandbox 2.1.2 → 2.1.4-a2

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.
@@ -5,7 +5,7 @@ import {
5
5
  TimeoutError,
6
6
  __spreadProps,
7
7
  __spreadValues
8
- } from "./chunk-PQGBLWKQ.mjs";
8
+ } from "./chunk-DKWK5NRD.mjs";
9
9
 
10
10
  // src/desktop/utils.ts
11
11
  import { randomBytes } from "crypto";
@@ -530,4 +530,4 @@ var VNCServer = class {
530
530
  export {
531
531
  Sandbox2 as Sandbox
532
532
  };
533
- //# sourceMappingURL=chunk-3OWF5GCM.mjs.map
533
+ //# sourceMappingURL=chunk-2B6TN3O4.mjs.map
@@ -60,7 +60,7 @@ import createClient from "openapi-fetch";
60
60
  import platform2 from "platform";
61
61
 
62
62
  // package.json
63
- var version = "2.1.2";
63
+ var version = "2.1.4-a2";
64
64
 
65
65
  // src/core/utils.ts
66
66
  import platform from "platform";
@@ -2998,11 +2998,14 @@ function bindingFromJson(data) {
2998
2998
 
2999
2999
  // src/core/sandbox/sandboxApi.ts
3000
3000
  async function createRawApiError(response) {
3001
- var _a3;
3001
+ var _a3, _b, _c;
3002
3002
  let message = response.statusText;
3003
3003
  try {
3004
3004
  const body = await response.json();
3005
3005
  message = (_a3 = body == null ? void 0 : body.message) != null ? _a3 : JSON.stringify(body);
3006
+ if ((body == null ? void 0 : body.item) || (body == null ? void 0 : body.reason)) {
3007
+ message = `${(_b = body.item) != null ? _b : "request"}: ${(_c = body.reason) != null ? _c : "error"}: ${message}`;
3008
+ }
3006
3009
  } catch (e) {
3007
3010
  try {
3008
3011
  message = await response.text();
@@ -3024,6 +3027,51 @@ async function createRawApiError(response) {
3024
3027
  function getRawApiHeaders(config, headers) {
3025
3028
  return __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({}, defaultHeaders), config.apiKey ? { "X-API-KEY": config.apiKey } : {}), config.accessToken ? { Authorization: `Bearer ${config.accessToken}` } : {}), config.headers), headers != null ? headers : {});
3026
3029
  }
3030
+ var MIB_PER_GIB = 1024;
3031
+ function gibToMib(value, name) {
3032
+ if (!Number.isFinite(value) || value <= 0) {
3033
+ throw new SandboxError(`resource target ${name} must be a positive number`);
3034
+ }
3035
+ const mib = value * MIB_PER_GIB;
3036
+ if (!Number.isSafeInteger(mib)) {
3037
+ throw new SandboxError(`resource target ${name} must be an exact number of MiB`);
3038
+ }
3039
+ return mib;
3040
+ }
3041
+ function resourceTargetsToApi(resources) {
3042
+ const unknown = Object.keys(resources != null ? resources : {}).filter(
3043
+ (key) => !["cpu", "memoryGiB", "diskGiB"].includes(key)
3044
+ );
3045
+ if (unknown.length) throw new SandboxError(`unsupported resource field: ${unknown.join(", ")}`);
3046
+ const body = {};
3047
+ const { cpu, memoryGiB, diskGiB } = resources != null ? resources : {};
3048
+ if (cpu !== void 0) {
3049
+ if (!Number.isSafeInteger(cpu) || cpu <= 0) {
3050
+ throw new SandboxError("resource target cpu must be a positive whole number");
3051
+ }
3052
+ body.vcpu = cpu;
3053
+ }
3054
+ if (memoryGiB !== void 0) body.memory_mb = gibToMib(memoryGiB, "memoryGiB");
3055
+ if (diskGiB !== void 0) body.disk_size_mb = gibToMib(diskGiB, "diskGiB");
3056
+ if (!Object.keys(body).length) throw new SandboxError("at least one resource target is required");
3057
+ return body;
3058
+ }
3059
+ function resizeResultFromApi(data) {
3060
+ var _a3, _b, _c;
3061
+ const result = {};
3062
+ for (const key of ["cpu", "memory", "disk"]) {
3063
+ const item = data == null ? void 0 : data[key];
3064
+ if (!item) continue;
3065
+ const divisor = key === "cpu" ? 1 : 1024;
3066
+ result[key] = __spreadProps(__spreadValues({}, item), {
3067
+ requested: ((_a3 = item.requested) != null ? _a3 : 0) / divisor,
3068
+ previous: ((_b = item.previous) != null ? _b : 0) / divisor,
3069
+ current: ((_c = item.current) != null ? _c : 0) / divisor,
3070
+ unit: key === "cpu" ? "cores" : "GiB"
3071
+ });
3072
+ }
3073
+ return result;
3074
+ }
3027
3075
  function getLifecycle(opts) {
3028
3076
  if (opts == null ? void 0 : opts.lifecycle) {
3029
3077
  return opts.lifecycle;
@@ -3221,28 +3269,24 @@ var SandboxApi = class {
3221
3269
  templateType: "snapshot_template"
3222
3270
  };
3223
3271
  }
3224
- static async hotplugMemory(sandboxId, requestedSizeMib, opts) {
3272
+ static async resize(sandboxId, resources, opts) {
3273
+ const body = resourceTargetsToApi(resources);
3225
3274
  const config = new ConnectionConfig(opts);
3226
- raiseIfLegacy(config, "Sandbox.hotplugMemory", SandboxError);
3275
+ raiseIfLegacy(config, "Sandbox.resize", SandboxError);
3227
3276
  const response = await fetch(
3228
- `${config.apiUrl}/sandboxes/${encodeURIComponent(sandboxId)}/hotplug-memory`,
3277
+ `${config.apiUrl}/sandboxes/${encodeURIComponent(sandboxId)}/resize`,
3229
3278
  {
3230
3279
  method: "POST",
3231
- headers: getRawApiHeaders(config, {
3232
- "Content-Type": "application/json"
3233
- }),
3234
- body: JSON.stringify({
3235
- requested_size_mib: requestedSizeMib
3236
- }),
3280
+ headers: getRawApiHeaders(config, { "Content-Type": "application/json" }),
3281
+ body: JSON.stringify(body),
3237
3282
  signal: config.getSignal(opts == null ? void 0 : opts.requestTimeoutMs)
3238
3283
  }
3239
3284
  );
3240
3285
  if (response.status === 404) {
3241
3286
  throw new SandboxNotFoundError(`Sandbox ${sandboxId} not found`);
3242
3287
  }
3243
- if (!response.ok) {
3244
- throw await createRawApiError(response);
3245
- }
3288
+ if (!response.ok) throw await createRawApiError(response);
3289
+ return resizeResultFromApi(await response.json());
3246
3290
  }
3247
3291
  static async reset(sandboxId, opts) {
3248
3292
  var _a3;
@@ -3340,7 +3384,7 @@ var SandboxApi = class {
3340
3384
  throw await createRawApiError(response);
3341
3385
  }
3342
3386
  const data = await response.json();
3343
- return {
3387
+ return __spreadProps(__spreadValues({
3344
3388
  sandboxId: data.sandboxID,
3345
3389
  templateId: data.templateID,
3346
3390
  name: data.name,
@@ -3349,10 +3393,11 @@ var SandboxApi = class {
3349
3393
  endAt: new Date(data.endAt),
3350
3394
  state: data.state,
3351
3395
  cpuCount: data.cpuCount,
3352
- memoryMB: data.memoryMB,
3396
+ memoryMB: data.memoryMB
3397
+ }, data.diskSizeMB !== void 0 ? { diskSizeMB: data.diskSizeMB } : {}), {
3353
3398
  envdVersion: data.envdVersion,
3354
3399
  volumeMounts: (_b = data.volumeMounts) != null ? _b : []
3355
- };
3400
+ });
3356
3401
  }
3357
3402
  static async setNetwork(sandboxId, opts) {
3358
3403
  const config = new ConnectionConfig(opts);
@@ -3466,6 +3511,7 @@ var SandboxApi = class {
3466
3511
  state: res.data.state,
3467
3512
  cpuCount: res.data.cpuCount,
3468
3513
  memoryMB: res.data.memoryMB,
3514
+ diskSizeMB: res.data.diskSizeMB,
3469
3515
  network: res.data.network ? {
3470
3516
  allowOut: res.data.network.allowOut,
3471
3517
  denyOut: res.data.network.denyOut,
@@ -3618,7 +3664,7 @@ var SandboxApi = class {
3618
3664
  return true;
3619
3665
  }
3620
3666
  static async createSandbox(template, timeoutMs, opts) {
3621
- var _a3, _b, _c, _d, _e, _f, _g;
3667
+ var _a3, _b, _c, _d, _e, _f, _g, _h;
3622
3668
  const config = new ConnectionConfig(opts);
3623
3669
  const longRunning = ((_a3 = opts == null ? void 0 : opts.metadata) == null ? void 0 : _a3.long_running) === "true";
3624
3670
  const metadata = longRunning ? __spreadValues({}, opts == null ? void 0 : opts.metadata) : opts == null ? void 0 : opts.metadata;
@@ -3644,6 +3690,9 @@ var SandboxApi = class {
3644
3690
  if (opts == null ? void 0 : opts.secretEnvs) {
3645
3691
  raiseIfLegacy(config, "Sandbox.create secretEnvs", SandboxError);
3646
3692
  }
3693
+ if (opts == null ? void 0 : opts.resources) {
3694
+ raiseIfLegacy(config, "Sandbox.create resources", SandboxError);
3695
+ }
3647
3696
  const body2 = __spreadValues(__spreadValues({
3648
3697
  templateID: template,
3649
3698
  timeout,
@@ -3705,6 +3754,30 @@ var SandboxApi = class {
3705
3754
  ;
3706
3755
  body.secrets = validatedSecretEnvs;
3707
3756
  }
3757
+ if (opts == null ? void 0 : opts.resources) {
3758
+ const targets = resourceTargetsToApi(opts.resources);
3759
+ const response = await fetch(`${config.apiUrl}/sandboxes/custom`, {
3760
+ method: "POST",
3761
+ headers: getRawApiHeaders(config, { "Content-Type": "application/json" }),
3762
+ body: JSON.stringify(__spreadProps(__spreadValues({}, body), { resources: targets })),
3763
+ signal: config.getSignal((_h = opts == null ? void 0 : opts.requestTimeoutMs) != null ? _h : 36e4)
3764
+ });
3765
+ if (!response.ok) throw await createRawApiError(response);
3766
+ const data = await response.json();
3767
+ if (compareVersions4(data.envdVersion, "0.1.0") < 0) {
3768
+ await this.kill(data.sandboxID, opts);
3769
+ throw new TemplateError(
3770
+ "You need to update the template to use the new SDK. You can do this by running `ppio template build` in the directory with the template."
3771
+ );
3772
+ }
3773
+ return {
3774
+ sandboxId: data.sandboxID,
3775
+ sandboxDomain: data.domain || void 0,
3776
+ envdVersion: data.envdVersion,
3777
+ envdAccessToken: data.envdAccessToken,
3778
+ trafficAccessToken: data.trafficAccessToken || void 0
3779
+ };
3780
+ }
3708
3781
  const res = await client.api.POST("/sandboxes", {
3709
3782
  body,
3710
3783
  signal: config.getSignal(opts == null ? void 0 : opts.requestTimeoutMs)
@@ -3926,7 +3999,7 @@ var SandboxPaginator = class extends BasePaginator {
3926
3999
  const data = await response.json();
3927
4000
  return (data != null ? data : []).map((sandbox) => {
3928
4001
  var _a4, _b2;
3929
- return __spreadProps(__spreadValues({
4002
+ return __spreadProps(__spreadValues(__spreadProps(__spreadValues({
3930
4003
  sandboxId: sandbox.clientID != null ? `${sandbox.sandboxID}-${sandbox.clientID}` : sandbox.sandboxID,
3931
4004
  templateId: sandbox.templateID
3932
4005
  }, sandbox.alias && { name: sandbox.alias }), {
@@ -3935,7 +4008,10 @@ var SandboxPaginator = class extends BasePaginator {
3935
4008
  endAt: new Date(sandbox.endAt),
3936
4009
  state: sandbox.state,
3937
4010
  cpuCount: sandbox.cpuCount,
3938
- memoryMB: sandbox.memoryMB,
4011
+ memoryMB: sandbox.memoryMB
4012
+ }), sandbox.diskSizeMB !== void 0 && {
4013
+ diskSizeMB: sandbox.diskSizeMB
4014
+ }), {
3939
4015
  envdVersion: sandbox.envdVersion,
3940
4016
  volumeMounts: (_b2 = sandbox.volumeMounts) != null ? _b2 : []
3941
4017
  });
@@ -3961,7 +4037,7 @@ var SandboxPaginator = class extends BasePaginator {
3961
4037
  return ((_e = res.data) != null ? _e : []).map(
3962
4038
  (sandbox) => {
3963
4039
  var _a4, _b2;
3964
- return __spreadProps(__spreadValues({
4040
+ return __spreadProps(__spreadValues(__spreadProps(__spreadValues({
3965
4041
  sandboxId: this.config.isLegacyDomain && sandbox.clientID != null ? `${sandbox.sandboxID}-${sandbox.clientID}` : sandbox.sandboxID,
3966
4042
  templateId: sandbox.templateID
3967
4043
  }, sandbox.alias && { name: sandbox.alias }), {
@@ -3970,7 +4046,10 @@ var SandboxPaginator = class extends BasePaginator {
3970
4046
  endAt: new Date(sandbox.endAt),
3971
4047
  state: sandbox.state,
3972
4048
  cpuCount: sandbox.cpuCount,
3973
- memoryMB: sandbox.memoryMB,
4049
+ memoryMB: sandbox.memoryMB
4050
+ }), sandbox.diskSizeMB !== void 0 && {
4051
+ diskSizeMB: sandbox.diskSizeMB
4052
+ }), {
3974
4053
  envdVersion: sandbox.envdVersion,
3975
4054
  volumeMounts: (_b2 = sandbox.volumeMounts) != null ? _b2 : []
3976
4055
  });
@@ -4194,19 +4273,6 @@ var Sandbox = class extends SandboxApi {
4194
4273
  snapshotTemplateId: cloneResult.snapshotTemplateId
4195
4274
  };
4196
4275
  }
4197
- /**
4198
- * Dynamically adjust the hotplugged memory on a running sandbox.
4199
- *
4200
- * The value represents the new *total* plugged size in MiB, not a delta.
4201
- * Use `0` to unplug all hotplugged memory.
4202
- */
4203
- static async hotplugMemory(sandboxId, requestedSizeMib, opts) {
4204
- return await SandboxApi.hotplugMemory(
4205
- sandboxId,
4206
- requestedSizeMib,
4207
- opts
4208
- );
4209
- }
4210
4276
  /**
4211
4277
  * Mount a volume to a sandbox.
4212
4278
  *
@@ -4217,6 +4283,10 @@ var Sandbox = class extends SandboxApi {
4217
4283
  *
4218
4284
  * @returns updated sandbox information.
4219
4285
  */
4286
+ /** Increase running sandbox resources to absolute CPU and GiB targets. */
4287
+ static async resize(sandboxId, resources, opts) {
4288
+ return await SandboxApi.resize(sandboxId, resources, opts);
4289
+ }
4220
4290
  static async mountVolume(sandboxId, name, path3, opts) {
4221
4291
  return await SandboxApi.mountVolume(sandboxId, name, path3, opts);
4222
4292
  }
@@ -4501,21 +4571,9 @@ var Sandbox = class extends SandboxApi {
4501
4571
  }
4502
4572
  await SandboxApi.kill(this.sandboxId, this.resolveApiOpts(opts));
4503
4573
  }
4504
- /**
4505
- * Dynamically adjust the hotplugged memory on a running sandbox.
4506
- *
4507
- * The value represents the new *total* plugged size in MiB, not a delta.
4508
- * Use `0` to unplug all hotplugged memory.
4509
- *
4510
- * @param requestedSizeMib new total plugged memory size in MiB.
4511
- * @param opts connection options.
4512
- */
4513
- async hotplugMemory(requestedSizeMib, opts) {
4514
- await SandboxApi.hotplugMemory(
4515
- this.sandboxId,
4516
- requestedSizeMib,
4517
- this.resolveApiOpts(opts)
4518
- );
4574
+ /** Increase this sandbox's resources to absolute CPU and GiB targets. */
4575
+ async resize(resources, opts) {
4576
+ return await SandboxApi.resize(this.sandboxId, resources, this.resolveApiOpts(opts));
4519
4577
  }
4520
4578
  /**
4521
4579
  * Pause a sandbox by its ID.
@@ -7765,6 +7823,9 @@ var RuntimeSandboxNamespace = class {
7765
7823
  getQuota(opts) {
7766
7824
  return Sandbox.getQuota(mergeConnectionOpts(this.opts, opts));
7767
7825
  }
7826
+ resize(sandboxId, resources, opts) {
7827
+ return Sandbox.resize(sandboxId, resources, mergeConnectionOpts(this.opts, opts));
7828
+ }
7768
7829
  getEvents(opts) {
7769
7830
  return Sandbox.getEvents(mergeConnectionOpts(this.opts, opts));
7770
7831
  }
@@ -7852,6 +7913,9 @@ var SandboxNamespace = class {
7852
7913
  getQuota(opts) {
7853
7914
  return Sandbox.getQuota(mergeConnectionOpts(this.opts, opts));
7854
7915
  }
7916
+ resize(sandboxId, resources, opts) {
7917
+ return Sandbox.resize(sandboxId, resources, mergeConnectionOpts(this.opts, opts));
7918
+ }
7855
7919
  getEvents(opts) {
7856
7920
  return Sandbox.getEvents(mergeConnectionOpts(this.opts, opts));
7857
7921
  }
@@ -8003,12 +8067,12 @@ var PPIO = class {
8003
8067
  this.secret = new SecretNamespace(opts);
8004
8068
  this.codeInterpreter = new RuntimeSandboxNamespace(
8005
8069
  opts,
8006
- () => import("./sandbox-DMY2YDSO.mjs")
8070
+ () => import("./sandbox-3WKRVKUL.mjs")
8007
8071
  );
8008
8072
  this.code_interpreter = this.codeInterpreter;
8009
8073
  this.desktop = new RuntimeSandboxNamespace(
8010
8074
  opts,
8011
- () => import("./sandbox-TS3DDCSN.mjs")
8075
+ () => import("./sandbox-AGCVAX7T.mjs")
8012
8076
  );
8013
8077
  }
8014
8078
  async create(templateOrOpts, opts) {
@@ -8087,4 +8151,4 @@ export {
8087
8151
  ALL_TRAFFIC,
8088
8152
  core_default
8089
8153
  };
8090
- //# sourceMappingURL=chunk-PQGBLWKQ.mjs.map
8154
+ //# sourceMappingURL=chunk-DKWK5NRD.mjs.map