computesdk 2.1.1 → 2.1.2

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
@@ -2309,7 +2309,9 @@ var Sandbox = class {
2309
2309
  terminal.setWaitCommandHandler(async (cmdId, timeout) => {
2310
2310
  const params = timeout ? new URLSearchParams({ timeout: timeout.toString() }) : "";
2311
2311
  const endpoint = `/terminals/${terminalId}/commands/${cmdId}/wait${params ? `?${params}` : ""}`;
2312
- return this.request(endpoint);
2312
+ return this.request(endpoint, {
2313
+ headers: timeout ? { "X-Request-Timeout": timeout.toString() } : void 0
2314
+ });
2313
2315
  });
2314
2316
  terminal.setDestroyHandler(async () => {
2315
2317
  await this.request(`/terminals/${terminalId}`, {
@@ -2325,6 +2327,11 @@ var Sandbox = class {
2325
2327
  async request(endpoint, options = {}) {
2326
2328
  const controller = new AbortController();
2327
2329
  const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
2330
+ if (process.env.COMPUTESDK_DEBUG) {
2331
+ console.log(`[ComputeSDK] Request: ${options.method || "GET"} ${this.config.sandboxUrl}${endpoint}`);
2332
+ console.log(`[ComputeSDK] Sandbox ID: ${this.config.sandboxId}`);
2333
+ console.log(`[ComputeSDK] Token: ${this._token || "(no token)"}`);
2334
+ }
2328
2335
  try {
2329
2336
  const headers = {
2330
2337
  ...this.config.headers
@@ -2367,6 +2374,15 @@ API request failed (${response.status}): ${error}`
2367
2374
  );
2368
2375
  }
2369
2376
  }
2377
+ if (response.status === 403 && process.env.COMPUTESDK_DEBUG) {
2378
+ console.error(`[ComputeSDK] 403 Forbidden Debug Info:`);
2379
+ console.error(` Endpoint: ${endpoint}`);
2380
+ console.error(` Method: ${options.method || "GET"}`);
2381
+ console.error(` Sandbox URL: ${this.config.sandboxUrl}`);
2382
+ console.error(` Sandbox ID: ${this.config.sandboxId}`);
2383
+ console.error(` Token: ${this._token || "(no token)"}`);
2384
+ console.error(` Error: ${error}`);
2385
+ }
2370
2386
  throw new Error(`API request failed (${response.status}): ${error}`);
2371
2387
  }
2372
2388
  return data;
@@ -2886,7 +2902,9 @@ API request failed (${response.status}): ${error}`
2886
2902
  async waitForCommand(terminalId, cmdId, timeout) {
2887
2903
  const params = timeout ? new URLSearchParams({ timeout: timeout.toString() }) : "";
2888
2904
  const endpoint = `/terminals/${terminalId}/commands/${cmdId}/wait${params ? `?${params}` : ""}`;
2889
- return this.request(endpoint);
2905
+ return this.request(endpoint, {
2906
+ headers: timeout ? { "X-Request-Timeout": timeout.toString() } : void 0
2907
+ });
2890
2908
  }
2891
2909
  /**
2892
2910
  * Wait for a background command to complete using long-polling
@@ -3337,6 +3355,7 @@ API request failed (${response.status}): ${error}`
3337
3355
  * @param options.env - Environment variables (server uses cmd.Env)
3338
3356
  * @param options.onStdout - Callback for streaming stdout data
3339
3357
  * @param options.onStderr - Callback for streaming stderr data
3358
+ * @param options.timeout - Timeout in seconds (max 300 for long-running commands)
3340
3359
  * @returns Command execution result
3341
3360
  *
3342
3361
  * @example
@@ -3358,11 +3377,21 @@ API request failed (${response.status}): ${error}`
3358
3377
  * onStdout: (data) => console.log(data),
3359
3378
  * onStderr: (data) => console.error(data),
3360
3379
  * })
3380
+ *
3381
+ * // With timeout for long-running commands
3382
+ * await sandbox.runCommand('npm install', { timeout: 120 })
3361
3383
  * ```
3362
3384
  */
3363
3385
  async runCommand(command, options) {
3364
3386
  const hasStreamingCallbacks = options?.onStdout || options?.onStderr;
3365
3387
  if (!hasStreamingCallbacks) {
3388
+ if (options?.timeout && !options?.background) {
3389
+ return this.run.command(command, {
3390
+ ...options,
3391
+ background: true,
3392
+ waitForCompletion: { timeoutSeconds: options.timeout }
3393
+ });
3394
+ }
3366
3395
  return this.run.command(command, options);
3367
3396
  }
3368
3397
  const ws = await this.ensureWebSocket();
@@ -3420,9 +3449,20 @@ API request failed (${response.status}): ${error}`
3420
3449
  durationMs: 0
3421
3450
  };
3422
3451
  }
3423
- return new Promise((resolve) => {
3452
+ const commandPromise = new Promise((resolve) => {
3424
3453
  resolvePromise = resolve;
3425
3454
  });
3455
+ if (options?.timeout) {
3456
+ const timeoutMs = options.timeout * 1e3;
3457
+ const timeoutPromise = new Promise((_, reject) => {
3458
+ setTimeout(() => {
3459
+ cleanup();
3460
+ reject(new Error(`Command timed out after ${options.timeout} seconds`));
3461
+ }, timeoutMs);
3462
+ });
3463
+ return Promise.race([commandPromise, timeoutPromise]);
3464
+ }
3465
+ return commandPromise;
3426
3466
  }
3427
3467
  /**
3428
3468
  * Get server information
@@ -4024,7 +4064,7 @@ Get your API key at: https://computesdk.com/dashboard`
4024
4064
  const providerHeaders = buildProviderHeaders2(config);
4025
4065
  return {
4026
4066
  apiKey: computesdkApiKey,
4027
- gatewayUrl: config.gatewayUrl || GATEWAY_URL,
4067
+ gatewayUrl: config.gatewayUrl || process.env.COMPUTESDK_GATEWAY_URL || GATEWAY_URL,
4028
4068
  provider: config.provider,
4029
4069
  providerHeaders,
4030
4070
  requestTimeoutMs: config.requestTimeoutMs,