sandboxedjs 0.1.44 → 0.1.46

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.
@@ -27141,8 +27141,17 @@ var VirtualIncomingMessage = class extends import_stream_browserify2.default.Rea
27141
27141
  httpVersionMajor = 1;
27142
27142
  httpVersionMinor = 1;
27143
27143
  complete = true;
27144
+ /*
27145
+ * Mutable, and `connection` a getter over it, because an upgraded request
27146
+ * carries a real socket rather than the stub. `emit("upgrade", req, socket)`
27147
+ * hands the same object to both places, and a library that reaches it as
27148
+ * `req.socket` — rather than through the argument — has to find the one it
27149
+ * can actually write to.
27150
+ */
27144
27151
  socket = socketStub();
27145
- connection = this.socket;
27152
+ get connection() {
27153
+ return this.socket;
27154
+ }
27146
27155
  constructor(init) {
27147
27156
  super();
27148
27157
  this.method = (init.method ?? "GET").toUpperCase();
@@ -27248,6 +27257,80 @@ var VirtualServerResponse = class extends import_stream_browserify2.default.Writ
27248
27257
  return this;
27249
27258
  }
27250
27259
  };
27260
+ var VirtualSocket = class extends import_stream_browserify2.default.Duplex {
27261
+ remoteAddress = "127.0.0.1";
27262
+ remotePort = 0;
27263
+ localAddress = "127.0.0.1";
27264
+ localPort = 0;
27265
+ encrypted = false;
27266
+ bufferSize = 0;
27267
+ peer;
27268
+ /** Set once the peer is told, so `end()` then `destroy()` reports once. */
27269
+ hungUp = false;
27270
+ constructor(peer) {
27271
+ super({ allowHalfOpen: false });
27272
+ this.peer = peer;
27273
+ }
27274
+ _read() {
27275
+ }
27276
+ _write(chunk, encoding, callback) {
27277
+ const bytes2 = Buffer2.isBuffer(chunk) ? chunk : Buffer2.from(chunk, encoding);
27278
+ try {
27279
+ this.peer?.data(new Uint8Array(bytes2));
27280
+ callback();
27281
+ } catch (error) {
27282
+ callback(error);
27283
+ }
27284
+ }
27285
+ _final(callback) {
27286
+ this.hangUp();
27287
+ callback();
27288
+ }
27289
+ _destroy(error, callback) {
27290
+ this.hangUp();
27291
+ callback(error);
27292
+ }
27293
+ hangUp() {
27294
+ if (this.hungUp) return;
27295
+ this.hungUp = true;
27296
+ try {
27297
+ this.peer?.close();
27298
+ } catch {
27299
+ }
27300
+ this.peer = null;
27301
+ }
27302
+ /** Bytes arriving from the far end. */
27303
+ deliver(bytes2) {
27304
+ if (this.destroyed || this.hungUp) return;
27305
+ this.push(Buffer2.from(bytes2));
27306
+ }
27307
+ /** The far end went away; let the server's stream end cleanly. */
27308
+ peerClosed() {
27309
+ if (this.destroyed) return;
27310
+ this.push(null);
27311
+ }
27312
+ /* The parts of `net.Socket` a WebSocket library reaches for. None of them
27313
+ * mean anything without a kernel, but every one of them is called. */
27314
+ setNoDelay() {
27315
+ return this;
27316
+ }
27317
+ setKeepAlive() {
27318
+ return this;
27319
+ }
27320
+ setTimeout(_milliseconds, callback) {
27321
+ if (callback) this.once("timeout", callback);
27322
+ return this;
27323
+ }
27324
+ destroySoon() {
27325
+ this.end();
27326
+ }
27327
+ ref() {
27328
+ return this;
27329
+ }
27330
+ unref() {
27331
+ return this;
27332
+ }
27333
+ };
27251
27334
  var VirtualHttpServer = class extends import_events.default {
27252
27335
  constructor(router, owner, listener) {
27253
27336
  super();
@@ -27347,6 +27430,33 @@ var VirtualHttpRouter = class {
27347
27430
  return { statusCode: 500, statusMessage: "Internal Server Error", headers: {}, body: error instanceof Error ? error.message : String(error) };
27348
27431
  }
27349
27432
  }
27433
+ /**
27434
+ * Open a connection that leaves HTTP behind — a WebSocket, in practice.
27435
+ *
27436
+ * The counterpart to {@link request}, and the thing whose absence made a dev
27437
+ * server look broken. Every Node WebSocket library is built the same way: it
27438
+ * hands `http.Server` an `upgrade` listener and waits. Nothing here ever
27439
+ * emitted one, so `ws` sat holding a server that could not receive a single
27440
+ * connection, and Vite lost the channel it uses to tell a page to reload —
27441
+ * which is the only way it can recover after re-optimizing dependencies.
27442
+ *
27443
+ * Null means there is nothing to connect to: no server on the port, or a
27444
+ * server that never asked for upgrades. Both are refusals the caller should
27445
+ * report rather than wait out.
27446
+ */
27447
+ connect(port, init, peer) {
27448
+ const item = this.servers.get(port);
27449
+ if (!item) return null;
27450
+ if (item.server.listenerCount("upgrade") === 0) return null;
27451
+ const request = new VirtualIncomingMessage(init);
27452
+ const socket = new VirtualSocket(peer);
27453
+ request.socket = socket;
27454
+ item.server.emit("upgrade", request, socket, Buffer2.alloc(0));
27455
+ return {
27456
+ send: (bytes2) => socket.deliver(bytes2),
27457
+ close: () => socket.peerClosed()
27458
+ };
27459
+ }
27350
27460
  };
27351
27461
  var VirtualClientResponse = class extends import_stream_browserify2.default.Readable {
27352
27462
  constructor(statusCode, statusMessage, headers, body) {
@@ -30766,6 +30876,13 @@ async function run(port, start) {
30766
30876
  finish(137);
30767
30877
  }
30768
30878
  if (message?.type === "http-request") void answer(port, router, message);
30879
+ if (message?.type === "ws-open") openSocket(port, router, message);
30880
+ if (message?.type === "ws-data") sockets.get(Number(message.id))?.send(new Uint8Array(message.data));
30881
+ if (message?.type === "ws-close") {
30882
+ const id = Number(message.id);
30883
+ sockets.get(id)?.close();
30884
+ sockets.delete(id);
30885
+ }
30769
30886
  });
30770
30887
  engine = new CommonJsEngine(volume, {
30771
30888
  cwd: start.cwd,
@@ -30795,6 +30912,21 @@ async function settle(router, owner, core, killed) {
30795
30912
  await new Promise((resolve2) => setTimeout(resolve2, busy ? 5 : 0));
30796
30913
  }
30797
30914
  }
30915
+ var sockets = /* @__PURE__ */ new Map();
30916
+ function openSocket(port, router, message) {
30917
+ const opened = router.connect(message.port, message.init, {
30918
+ data: (bytes2) => port.postMessage({ type: "ws-data", id: message.id, data: bytes2 }),
30919
+ close: () => {
30920
+ sockets.delete(message.id);
30921
+ port.postMessage({ type: "ws-close", id: message.id });
30922
+ }
30923
+ });
30924
+ if (!opened) {
30925
+ port.postMessage({ type: "ws-close", id: message.id });
30926
+ return;
30927
+ }
30928
+ sockets.set(message.id, opened);
30929
+ }
30798
30930
  async function answer(port, router, message) {
30799
30931
  try {
30800
30932
  const response = await router.request(message.port, message.init);