@cloudflare/sandbox 0.8.0 → 0.8.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.js CHANGED
@@ -737,6 +737,9 @@ var BaseTransport = class {
737
737
  setRetryTimeoutMs(ms) {
738
738
  this.retryTimeoutMs = ms;
739
739
  }
740
+ getRetryTimeoutMs() {
741
+ return this.retryTimeoutMs;
742
+ }
740
743
  /**
741
744
  * Fetch with automatic retry for 503 (container starting)
742
745
  *
@@ -841,6 +844,7 @@ var HttpTransport = class extends BaseTransport {
841
844
  const DEFAULT_REQUEST_TIMEOUT_MS = 12e4;
842
845
  const DEFAULT_STREAM_IDLE_TIMEOUT_MS = 3e5;
843
846
  const DEFAULT_CONNECT_TIMEOUT_MS = 3e4;
847
+ const MIN_TIME_FOR_CONNECT_RETRY_MS = 15e3;
844
848
  /**
845
849
  * WebSocket transport implementation
846
850
  *
@@ -943,6 +947,26 @@ var WebSocketTransport = class extends BaseTransport {
943
947
  if (this.config.stub) await this.connectViaFetch();
944
948
  else await this.connectViaWebSocket();
945
949
  }
950
+ async fetchUpgradeWithRetry(attemptUpgrade) {
951
+ const retryTimeoutMs = this.getRetryTimeoutMs();
952
+ const startTime = Date.now();
953
+ let attempt = 0;
954
+ while (true) {
955
+ const response = await attemptUpgrade();
956
+ if (response.status !== 503) return response;
957
+ const remaining = retryTimeoutMs - (Date.now() - startTime);
958
+ if (remaining <= MIN_TIME_FOR_CONNECT_RETRY_MS) return response;
959
+ const delay = Math.min(3e3 * 2 ** attempt, 3e4);
960
+ this.logger.info("WebSocket container not ready, retrying", {
961
+ status: response.status,
962
+ attempt: attempt + 1,
963
+ delayMs: delay,
964
+ remainingSec: Math.floor(remaining / 1e3)
965
+ });
966
+ await this.sleep(delay);
967
+ attempt++;
968
+ }
969
+ }
946
970
  /**
947
971
  * Connect using fetch-based WebSocket (Cloudflare Workers style)
948
972
  * This is required when running inside a Durable Object.
@@ -952,20 +976,10 @@ var WebSocketTransport = class extends BaseTransport {
952
976
  */
953
977
  async connectViaFetch() {
954
978
  const timeoutMs = this.config.connectTimeoutMs ?? DEFAULT_CONNECT_TIMEOUT_MS;
955
- const controller = new AbortController();
956
- const timeout = setTimeout(() => controller.abort(), timeoutMs);
957
979
  try {
958
980
  const wsPath = new URL(this.config.wsUrl).pathname;
959
981
  const httpUrl = `http://localhost:${this.config.port || 3e3}${wsPath}`;
960
- const request = new Request(httpUrl, {
961
- headers: {
962
- Upgrade: "websocket",
963
- Connection: "Upgrade"
964
- },
965
- signal: controller.signal
966
- });
967
- const response = await this.config.stub.fetch(request);
968
- clearTimeout(timeout);
982
+ const response = await this.fetchUpgradeWithRetry(() => this.fetchUpgradeAttempt(httpUrl, timeoutMs));
969
983
  if (response.status !== 101) throw new Error(`WebSocket upgrade failed: ${response.status} ${response.statusText}`);
970
984
  const ws = response.webSocket;
971
985
  if (!ws) throw new Error("No WebSocket in upgrade response");
@@ -976,12 +990,27 @@ var WebSocketTransport = class extends BaseTransport {
976
990
  this.ws.addEventListener("message", this.boundHandleMessage);
977
991
  this.logger.debug("WebSocket connected via fetch", { url: this.config.wsUrl });
978
992
  } catch (error) {
979
- clearTimeout(timeout);
980
993
  this.state = "error";
981
994
  this.logger.error("WebSocket fetch connection failed", error instanceof Error ? error : new Error(String(error)));
982
995
  throw error;
983
996
  }
984
997
  }
998
+ async fetchUpgradeAttempt(httpUrl, timeoutMs) {
999
+ const controller = new AbortController();
1000
+ const timeout = setTimeout(() => controller.abort(), timeoutMs);
1001
+ try {
1002
+ const request = new Request(httpUrl, {
1003
+ headers: {
1004
+ Upgrade: "websocket",
1005
+ Connection: "Upgrade"
1006
+ },
1007
+ signal: controller.signal
1008
+ });
1009
+ return await this.config.stub.fetch(request);
1010
+ } finally {
1011
+ clearTimeout(timeout);
1012
+ }
1013
+ }
985
1014
  /**
986
1015
  * Connect using standard WebSocket API (browser/Node style)
987
1016
  */
@@ -3562,7 +3591,7 @@ function buildS3fsSource(bucket, prefix) {
3562
3591
  * This file is auto-updated by .github/changeset-version.ts during releases
3563
3592
  * DO NOT EDIT MANUALLY - Changes will be overwritten on the next version bump
3564
3593
  */
3565
- const SDK_VERSION = "0.8.0";
3594
+ const SDK_VERSION = "0.8.1";
3566
3595
 
3567
3596
  //#endregion
3568
3597
  //#region src/sandbox.ts