@phpsandbox/sdk 0.0.32 → 0.0.33

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.
@@ -684,19 +684,30 @@ var PHPSandbox = (() => {
684
684
  },
685
685
  close: dispose
686
686
  });
687
+ let controller = null;
687
688
  const output = new ReadableStream({
688
- start: (controller) => {
689
+ start: (_controller) => {
690
+ controller = _controller;
689
691
  disposables.add(
690
692
  this.listen(`terminal.output.${id}`, (data) => {
691
- controller.enqueue(data.output);
693
+ controller?.enqueue(data.output);
692
694
  })
693
695
  );
694
696
  },
695
- cancel: dispose
697
+ cancel: () => {
698
+ controller = null;
699
+ dispose();
700
+ }
696
701
  });
697
702
  const exit = new Promise((resolve) => {
698
703
  disposables.add(
699
704
  this.listen(`terminal.close.${id}`, (data) => {
705
+ if (controller) {
706
+ try {
707
+ controller.close();
708
+ } catch {
709
+ }
710
+ }
700
711
  dispose();
701
712
  resolve(data.exitCode);
702
713
  })
@@ -3227,7 +3238,7 @@ var PHPSandbox = (() => {
3227
3238
  this.connectionStats.connectionStartTime = Date.now();
3228
3239
  const startClosed = options.startClosed !== false;
3229
3240
  this.rws = new reconnecting_websocket_mjs_default(this.url.toString(), [], {
3230
- WebSocket: globalThis.WebSocket ?? browser_default,
3241
+ WebSocket: options.webSocket ?? globalThis.WebSocket ?? browser_default,
3231
3242
  connectionTimeout: options.connectionTimeout,
3232
3243
  maxReconnectionDelay: 2e3,
3233
3244
  minReconnectionDelay: 200,
@@ -3282,7 +3293,19 @@ var PHPSandbox = (() => {
3282
3293
  async registerWatchers() {
3283
3294
  const onMessage = (ev) => {
3284
3295
  if (!(ev.data instanceof Blob)) {
3285
- throw new Error("Unexpected message type: " + typeof ev.data);
3296
+ const error = new Error("Unexpected message type: " + typeof ev.data);
3297
+ this.connectionStats.totalErrors++;
3298
+ this.log("error", "Unexpected WebSocket message type", {
3299
+ error: error.message,
3300
+ messageType: typeof ev.data
3301
+ });
3302
+ this.eventEmitter.emit("transport.error", {
3303
+ type: "message_type_error",
3304
+ error,
3305
+ rawMessage: ev.data,
3306
+ timestamp: Date.now()
3307
+ });
3308
+ return;
3286
3309
  }
3287
3310
  ev.data.arrayBuffer().then((buffer) => {
3288
3311
  if (buffer.byteLength === 0) {
@@ -3290,7 +3313,19 @@ var PHPSandbox = (() => {
3290
3313
  return;
3291
3314
  }
3292
3315
  try {
3293
- this.handleRawMessage(decode(buffer));
3316
+ void this.handleRawMessage(decode(buffer)).catch((error) => {
3317
+ this.connectionStats.totalErrors++;
3318
+ this.log("error", "Failed to process WebSocket frame", {
3319
+ error: error instanceof Error ? error.message : String(error),
3320
+ byteLength: buffer.byteLength
3321
+ });
3322
+ this.eventEmitter.emit("transport.error", {
3323
+ type: "message_handle_error",
3324
+ error,
3325
+ rawMessage: buffer,
3326
+ timestamp: Date.now()
3327
+ });
3328
+ });
3294
3329
  } catch (error) {
3295
3330
  this.connectionStats.totalErrors++;
3296
3331
  this.log("error", "Failed to decode WebSocket frame", {
@@ -3314,6 +3349,10 @@ var PHPSandbox = (() => {
3314
3349
  });
3315
3350
  }
3316
3351
  async handleRawMessage(ev) {
3352
+ if (this.closed) {
3353
+ this.log("debug", "Ignoring message received after transport was closed");
3354
+ return;
3355
+ }
3317
3356
  if (typeof ev !== "object" || ev === null) {
3318
3357
  this.log("debug", "Received invalid message format", { ev });
3319
3358
  return;
@@ -4975,7 +5014,8 @@ var PHPSandbox = (() => {
4975
5014
  this.emitter = EventManager.createInstance();
4976
5015
  this.socket = new Transport(data.okraUrl, this.emitter, {
4977
5016
  debug: client.options.debug,
4978
- startClosed: client.options.startClosed
5017
+ startClosed: client.options.startClosed,
5018
+ webSocket: client.options.webSocket
4979
5019
  });
4980
5020
  this.watchConnection();
4981
5021
  __privateSet(this, _initPromise, __privateMethod(this, _NotebookInstance_instances, init_fn).call(this));
@@ -5143,6 +5183,8 @@ var PHPSandbox = (() => {
5143
5183
  reject(error);
5144
5184
  });
5145
5185
  }));
5186
+ void __privateGet(this, _initPromise).catch(() => {
5187
+ });
5146
5188
  return __privateGet(this, _initPromise);
5147
5189
  };
5148
5190
  var NotebookSecrets = class {