computesdk 1.11.0 → 1.11.1

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/index.mjs CHANGED
@@ -3567,6 +3567,34 @@ async function gatewayFetch(url, config, options = {}) {
3567
3567
  throw error;
3568
3568
  }
3569
3569
  }
3570
+ async function waitForSandboxStatus(config, endpoint, body, options = {}) {
3571
+ const maxWaitMs = options.maxWaitMs ?? 6e4;
3572
+ const initialDelayMs = 500;
3573
+ const maxDelayMs = 2e3;
3574
+ const backoffFactor = 1.5;
3575
+ const startTime = Date.now();
3576
+ let currentDelay = initialDelayMs;
3577
+ while (Date.now() - startTime < maxWaitMs) {
3578
+ const result = await gatewayFetch(endpoint, config, {
3579
+ method: "POST",
3580
+ body: JSON.stringify(body)
3581
+ });
3582
+ if (!result.success || !result.data) {
3583
+ return result;
3584
+ }
3585
+ if (result.data.status !== "creating") {
3586
+ return result;
3587
+ }
3588
+ if (process.env.COMPUTESDK_DEBUG) {
3589
+ console.log(`[Compute] Sandbox still creating, waiting ${currentDelay}ms...`);
3590
+ }
3591
+ await new Promise((resolve) => setTimeout(resolve, currentDelay));
3592
+ currentDelay = Math.min(currentDelay * backoffFactor, maxDelayMs);
3593
+ }
3594
+ throw new Error(
3595
+ `Sandbox is still being created after ${maxWaitMs}ms. This may indicate the sandbox failed to start. Check your provider dashboard.`
3596
+ );
3597
+ }
3570
3598
  var ComputeManager = class {
3571
3599
  constructor() {
3572
3600
  this.config = null;
@@ -3654,14 +3682,15 @@ var ComputeManager = class {
3654
3682
  findOrCreate: async (options) => {
3655
3683
  const config = this.getGatewayConfig();
3656
3684
  const { name, namespace, ...restOptions } = options;
3657
- const result = await gatewayFetch(`${config.gatewayUrl}/v1/sandboxes/find-or-create`, config, {
3658
- method: "POST",
3659
- body: JSON.stringify({
3685
+ const result = await waitForSandboxStatus(
3686
+ config,
3687
+ `${config.gatewayUrl}/v1/sandboxes/find-or-create`,
3688
+ {
3660
3689
  namespace: namespace || "default",
3661
3690
  name,
3662
3691
  ...restOptions
3663
- })
3664
- });
3692
+ }
3693
+ );
3665
3694
  if (!result.success || !result.data) {
3666
3695
  throw new Error(`Gateway returned invalid response`);
3667
3696
  }
@@ -3691,13 +3720,14 @@ var ComputeManager = class {
3691
3720
  */
3692
3721
  find: async (options) => {
3693
3722
  const config = this.getGatewayConfig();
3694
- const result = await gatewayFetch(`${config.gatewayUrl}/v1/sandboxes/find`, config, {
3695
- method: "POST",
3696
- body: JSON.stringify({
3723
+ const result = await waitForSandboxStatus(
3724
+ config,
3725
+ `${config.gatewayUrl}/v1/sandboxes/find`,
3726
+ {
3697
3727
  namespace: options.namespace || "default",
3698
3728
  name: options.name
3699
- })
3700
- });
3729
+ }
3730
+ );
3701
3731
  if (!result.success || !result.data) {
3702
3732
  return null;
3703
3733
  }