@phpsandbox/sdk 0.0.34 → 0.0.35

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.
@@ -3267,6 +3267,12 @@ var PHPSandbox = (() => {
3267
3267
  };
3268
3268
 
3269
3269
  // src/socket/index.ts
3270
+ var ConnectionTimeoutError = class extends Error {
3271
+ constructor(message = "WebSocket connection timeout") {
3272
+ super(message);
3273
+ this.name = "ConnectionTimeoutError";
3274
+ }
3275
+ };
3270
3276
  var ConnectionFailedError = class extends Error {
3271
3277
  constructor(message, originalError) {
3272
3278
  super(message);
@@ -3325,6 +3331,7 @@ var PHPSandbox = (() => {
3325
3331
  this.validateConfiguration(options);
3326
3332
  this.url = new URL(url);
3327
3333
  this.PING_INTERVAL = options.pingInterval ?? 3e4;
3334
+ this.CONNECT_WAIT_TIMEOUT = options.connectWaitTimeout ?? 3e4;
3328
3335
  this.connectionStats.connectionStartTime = Date.now();
3329
3336
  const startClosed = options.startClosed !== false;
3330
3337
  this.rws = new reconnecting_websocket_mjs_default(this.url.toString(), [], {
@@ -3354,6 +3361,9 @@ var PHPSandbox = (() => {
3354
3361
  if (options.connectionTimeout !== void 0 && (options.connectionTimeout < 100 || options.connectionTimeout > 3e4)) {
3355
3362
  throw new InvalidConfigurationError("connectionTimeout must be between 100ms and 30000ms");
3356
3363
  }
3364
+ if (options.connectWaitTimeout !== void 0 && (options.connectWaitTimeout < 1e3 || options.connectWaitTimeout > 3e5)) {
3365
+ throw new InvalidConfigurationError("connectWaitTimeout must be between 1000ms and 300000ms");
3366
+ }
3357
3367
  if (options.maxRetries !== void 0 && (options.maxRetries < 0 || options.maxRetries > 100)) {
3358
3368
  throw new InvalidConfigurationError("maxRetries must be between 0 and 100");
3359
3369
  }
@@ -4088,34 +4098,50 @@ var PHPSandbox = (() => {
4088
4098
  __privateMethod(this, _Transport_instances, startPeriodicPing_fn).call(this);
4089
4099
  return;
4090
4100
  }
4091
- if (this.rws.readyState === 3) {
4092
- this.rws.reconnect();
4093
- }
4094
4101
  let timeoutId;
4095
- const openHandler = () => {
4102
+ let errorCount = 0;
4103
+ const cleanup = () => {
4096
4104
  this.rws.removeEventListener("open", openHandler);
4097
4105
  this.rws.removeEventListener("error", errorHandler);
4106
+ this.rws.removeEventListener("close", closeHandler);
4098
4107
  clearTimeout(timeoutId);
4108
+ };
4109
+ const openHandler = () => {
4110
+ cleanup();
4099
4111
  this.connectPromise = null;
4100
4112
  resolve();
4101
4113
  __privateMethod(this, _Transport_instances, startPeriodicPing_fn).call(this);
4102
4114
  };
4103
4115
  const errorHandler = (error) => {
4104
- this.rws.removeEventListener("open", openHandler);
4105
- this.rws.removeEventListener("error", errorHandler);
4106
- clearTimeout(timeoutId);
4107
- this.connectPromise = null;
4108
- const detail = describeWebSocketEvent(error);
4109
- reject(new ConnectionFailedError(`WebSocket connection failed: ${detail}`, error));
4116
+ errorCount += 1;
4117
+ this.log("warn", "WebSocket emitted an error while connecting; waiting for reconnect/open", {
4118
+ errorCount,
4119
+ error: describeWebSocketEvent(error)
4120
+ });
4121
+ };
4122
+ const closeHandler = (event) => {
4123
+ const detail = describeWebSocketEvent(event);
4124
+ if ("code" in event && (event.code === 1008 || event.code === 4e3)) {
4125
+ cleanup();
4126
+ this.connectPromise = null;
4127
+ reject(new ConnectionFailedError(`WebSocket connection failed: ${detail}`, event));
4128
+ return;
4129
+ }
4130
+ this.log("warn", "WebSocket closed before opening; waiting for reconnect/open", {
4131
+ error: detail
4132
+ });
4110
4133
  };
4111
- timeoutId = setTimeout(() => {
4112
- this.rws.removeEventListener("open", openHandler);
4113
- this.rws.removeEventListener("error", errorHandler);
4114
- this.connectPromise = null;
4115
- reject(new Error("WebSocket connection timeout"));
4116
- }, 1e4);
4117
4134
  this.rws.addEventListener("open", openHandler);
4118
4135
  this.rws.addEventListener("error", errorHandler);
4136
+ this.rws.addEventListener("close", closeHandler);
4137
+ if (this.rws.readyState === 3) {
4138
+ this.rws.reconnect();
4139
+ }
4140
+ timeoutId = setTimeout(() => {
4141
+ cleanup();
4142
+ this.connectPromise = null;
4143
+ reject(new ConnectionTimeoutError(`WebSocket connection timeout after ${this.CONNECT_WAIT_TIMEOUT}ms`));
4144
+ }, this.CONNECT_WAIT_TIMEOUT);
4119
4145
  });
4120
4146
  return this.connectPromise;
4121
4147
  };