@sproutboat/runtime 0.33.0 → 0.34.0

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.
@@ -4298,6 +4298,10 @@ void (*sb_host_note_hook)(const char* text, size_t length) = 0;
4298
4298
  // WebSockets (from 2026-10-30): set only by a host that carries client sockets (sproutd's
4299
4299
  // native-host-bridge.c, at init); null everywhere else. See __sbWsOp for the ops.
4300
4300
  long long (*sb_ws_hook)(int op, unsigned int id, const char* data, size_t length, char* out, size_t cap) = 0;
4301
+ // Outbound WebSockets (from 2026-11-01): set by the embedded transport (a constructor there);
4302
+ // null with the broker transport. Ids with the top bit set belong to it. See __sbWsOp.
4303
+ long long (*sb_wsc_hook)(int op, unsigned int id, const char* data, size_t length, size_t n, char* out, size_t cap) = 0;
4304
+ int (*sb_wsc_wakeable_hook)(void) = 0;
4301
4305
 
4302
4306
  // --- #133: SHA-2 + HMAC ---------------------------------------------------
4303
4307
  // Standalone links BearSSL, but a deployed sprout does not, and this inline C
@@ -6684,6 +6688,7 @@ globalThis.__sbInstallFetch = function () {
6684
6688
  if (globalThis.__sbCompat >= "2026-10-23") return Promise.reject(error);
6685
6689
  throw error;
6686
6690
  }
6691
+ if (__sbWsOutboundOn() && __sbWsIsUpgrade(parts)) return __sbWsFetchUpgrade(parts);
6687
6692
  const send = (body) => {
6688
6693
  const msg = { url: parts.url, method: parts.method, headers: parts.headers, ...body };
6689
6694
  return __sbFetchFollowing("fetch", msg, "sproutboat fetch", parts.redirect, parts.signal);
@@ -7037,13 +7042,17 @@ function __sbGetDOInstance(cls, idText, idObj) {
7037
7042
  },
7038
7043
  };
7039
7044
  // The hibernation API, from 2026-10-30 (where WebSocketPair exists).
7040
- const holder = { inst: null, auto: null };
7045
+ const holder = { inst: null, auto: null, cls, id, restored: [] };
7041
7046
  if (__sbWsEnabled()) __sbDOWebSocketState(state, holder);
7042
7047
  inst = new Ctor(state, globalThis.env);
7043
7048
  holder.inst = inst;
7044
7049
  inst.__sbTasks = __sbTasks;
7045
7050
  inst.__sbGate = gate;
7046
7051
  __sbDOInstances[cacheKey] = inst;
7052
+ // Sockets restored from hibernation start delivering now that the object exists.
7053
+ const restored = holder.restored;
7054
+ holder.restored = [];
7055
+ for (let i = 0; i < restored.length; i++) __sbWsPump(restored[i]);
7047
7056
  }
7048
7057
  return inst;
7049
7058
  }
@@ -8034,7 +8043,8 @@ function __sbStreamResponse(response) {
8034
8043
  // client sends arrives as if that end had sent it.
8035
8044
  //
8036
8045
  // __sbWsOp(op, id, data, n), on the host socket `id`:
8037
- // "o" open one (returns its id, "0" without a host), "t" send text data, "b" send the
8046
+ // "o" open one for the app at compatibility date data (returns its id, "0" without a host or
8047
+ // when the host does not carry sockets at that date), "t" send text data, "b" send the
8038
8048
  // bytes in data, "c" close with code n and reason data, "p" poll the next event ("" for
8039
8049
  // none, else "KIND LENGTH CODE": 0 text, 1 binary, 2 close, 3 connection lost), "i" take
8040
8050
  // that event's payload into the Uint8Array data, "k" acknowledge n bytes dispatched,
@@ -8051,7 +8061,36 @@ function __sbWsOp(op, id, data, n) {
8051
8061
  const unsigned int __id = id.type == 1 && id.val > 0 ? (unsigned int)id.val : 0;
8052
8062
  const size_t __n = n.type == 1 && n.val > 0 ? (size_t)n.val : 0;
8053
8063
  char __r[64]; __r[0] = 0;
8054
- if (sb_ws_hook) {
8064
+ if (__op == 'u' || __op == 'W' || (__id & 0x80000000u)) {
8065
+ long long __w = -1;
8066
+ if (!sb_wsc_hook) snprintf(__r, sizeof(__r), "0");
8067
+ else if (__op == 'W') snprintf(__r, sizeof(__r), "%d", sb_wsc_wakeable_hook && sb_wsc_wakeable_hook() ? 1 : 0);
8068
+ else if (__op == 'p') {
8069
+ sb_wsc_hook('p', __id, 0, 0, 0, __r, sizeof(__r));
8070
+ } else if (__op == 'i') {
8071
+ char* __at = 0; size_t __cap = 0;
8072
+ if (data.type == 81) {
8073
+ const u32 __ta = (u32)data.val;
8074
+ const int32_t __len = *(int32_t*)(MEM + __ta);
8075
+ if (__len > 0) { __at = (char*)(MEM + *(u32*)(MEM + __ta + 4) + 4); __cap = (size_t)__len; }
8076
+ }
8077
+ __w = sb_wsc_hook('i', __id, 0, 0, 0, __at, __cap);
8078
+ snprintf(__r, sizeof(__r), "%lld", __w);
8079
+ } else if (__op == 'b') {
8080
+ const char* __d = 0; size_t __dl = 0;
8081
+ if (porf_native_fetch_read_raw_bytes(data, &__d, &__dl) == 0) __w = sb_wsc_hook('b', __id, __d, __dl, 0, 0, 0);
8082
+ snprintf(__r, sizeof(__r), "%lld", __w);
8083
+ } else if (__op == 't' || __op == 'c' || __op == 'u') {
8084
+ const char* __d; size_t __dl; char* __do = 0;
8085
+ porf_native_fetch_read_value(data, &__d, &__dl, &__do);
8086
+ __w = sb_wsc_hook(__op, __id, __d, __dl, __n, 0, 0);
8087
+ if (__do) free(__do);
8088
+ snprintf(__r, sizeof(__r), "%lld", __w);
8089
+ } else {
8090
+ __w = sb_wsc_hook(__op, __id, 0, 0, __n, 0, 0);
8091
+ snprintf(__r, sizeof(__r), "%lld", __w);
8092
+ }
8093
+ } else if (sb_ws_hook) {
8055
8094
  long long __w = -1;
8056
8095
  if (__op == 'p') {
8057
8096
  sb_ws_hook('p', __id, 0, 0, __r, sizeof(__r));
@@ -8068,7 +8107,7 @@ function __sbWsOp(op, id, data, n) {
8068
8107
  const char* __d = 0; size_t __dl = 0;
8069
8108
  if (porf_native_fetch_read_raw_bytes(data, &__d, &__dl) == 0) __w = sb_ws_hook('b', __id, __d, __dl, 0, 0);
8070
8109
  snprintf(__r, sizeof(__r), "%lld", __w);
8071
- } else if (__op == 't' || __op == 'c') {
8110
+ } else if (__op == 't' || __op == 'c' || __op == 'o' || __op == 'a') {
8072
8111
  const char* __d; size_t __dl; char* __do = 0;
8073
8112
  porf_native_fetch_read_value(data, &__d, &__dl, &__do);
8074
8113
  __w = sb_ws_hook(__op, __id, __d, __dl, 0, __n);
@@ -8094,9 +8133,12 @@ const __sbWsKey = {};
8094
8133
  // addEventListener listeners, as workerd does. Binary messages arrive as a Blob unless
8095
8134
  // binaryType is "arraybuffer" (workerd's standard binary type).
8096
8135
  class __SbWebSocket {
8097
- constructor(key) {
8098
- // Outbound WebSockets (new WebSocket(url)) are not supported; the CLI refuses them too.
8099
- if (key !== __sbWsKey) throw new TypeError("WebSocket: only WebSocketPair can make a WebSocket here");
8136
+ constructor(key, protocols) {
8137
+ // new WebSocket(url) connects out from compatibility date 2026-11-01; before it only a
8138
+ // WebSocketPair makes a WebSocket.
8139
+ if (key !== __sbWsKey && !__sbWsOutboundOn()) throw new TypeError("WebSocket: only WebSocketPair can make a WebSocket here");
8140
+ this._outbound = false;
8141
+ this._pending = null;
8100
8142
  this._peer = null;
8101
8143
  this._accepted = false;
8102
8144
  this._inbox = [];
@@ -8114,6 +8156,7 @@ class __SbWebSocket {
8114
8156
  this.url = null;
8115
8157
  this.protocol = "";
8116
8158
  this.extensions = "";
8159
+ if (key !== __sbWsKey) __sbWsConnectOutbound(this, key, protocols);
8117
8160
  }
8118
8161
  get readyState() {
8119
8162
  return this._state;
@@ -8122,13 +8165,14 @@ class __SbWebSocket {
8122
8165
  return 0;
8123
8166
  }
8124
8167
  accept() {
8125
- if (this._host !== 0) throw new TypeError("Can't accept() a WebSocket that was already used in a response.");
8168
+ if (this._host !== 0 && !this._outbound) throw new TypeError("Can't accept() a WebSocket that was already used in a response.");
8126
8169
  if (this._hib !== null) throw new TypeError("Can't accept() a WebSocket that was accepted with acceptWebSocket().");
8127
8170
  if (this._accepted) return;
8128
8171
  this._accepted = true;
8129
8172
  __sbWsSchedule(this);
8130
8173
  }
8131
8174
  send(message) {
8175
+ if (this._outbound) return __sbWsOutSend(this, message);
8132
8176
  if (!this._accepted && this._hib === null)
8133
8177
  throw new TypeError("You must call one of accept() or state.acceptWebSocket() on this WebSocket before sending messages.");
8134
8178
  if (this._closeSent) throw new TypeError("Can't call WebSocket send() after close().");
@@ -8149,6 +8193,7 @@ class __SbWebSocket {
8149
8193
  const text = reason === undefined ? "" : String(reason);
8150
8194
  if (__sbToBytes(text).length > 123)
8151
8195
  throw __sbCryptoError("SyntaxError", "WebSocket close reason must not be longer than 123 bytes when UTF-8 encoded.");
8196
+ if (this._outbound) return __sbWsOutClose(this, code, text);
8152
8197
  if (this._closeSent) return;
8153
8198
  this._closeSent = true;
8154
8199
  this._state = this._closeSeen ? __SB_WS_CLOSED : __SB_WS_CLOSING;
@@ -8176,6 +8221,7 @@ class __SbWebSocket {
8176
8221
  }
8177
8222
  serializeAttachment(value) {
8178
8223
  this._attachment = value === undefined ? undefined : structuredClone(value);
8224
+ if (this._hib !== null && this._hib.hostId > 0) __sbWsHibUpdate(this);
8179
8225
  }
8180
8226
  deserializeAttachment() {
8181
8227
  return this._attachment === undefined ? null : structuredClone(this._attachment);
@@ -8276,6 +8322,7 @@ function __sbWsDispatch(ws, event) {
8276
8322
  if (ws._peer !== null) __sbWsDeliver(ws._peer, { kind: 2, code: event.code, reason: event.reason, clean: event.clean });
8277
8323
  }
8278
8324
  ws._state = __SB_WS_CLOSED;
8325
+ if (ws._hib !== null && ws._hib.hostId > 0) __sbWsHibForget(ws);
8279
8326
  if (ws._hib !== null) return __sbWsHibernated(ws, "webSocketClose", [ws, event.code, event.reason, event.clean]);
8280
8327
  return __sbWsFire(ws, { type: "close", code: event.code, reason: event.reason, wasClean: event.clean, target: ws });
8281
8328
  }
@@ -8317,13 +8364,14 @@ function __sbDOWebSocketState(state, holder) {
8317
8364
  for (let i = sockets.length - 1; i >= 0; i--) if (sockets[i]._state === __SB_WS_CLOSED) sockets.splice(i, 1);
8318
8365
  return sockets;
8319
8366
  };
8367
+ __sbWsHibRestore(holder, sockets);
8320
8368
  state.acceptWebSocket = function (ws, tags) {
8321
8369
  if (!(ws instanceof __SbWebSocket)) throw new TypeError("acceptWebSocket needs a WebSocket");
8322
8370
  if (ws._accepted || ws._hib !== null || ws._host !== 0) throw new TypeError("This WebSocket was already accepted.");
8323
8371
  const list = [];
8324
8372
  if (tags != null) for (const t of tags) list.push(String(t));
8325
8373
  if (list.length > 10) throw new TypeError("A WebSocket can have at most 10 tags.");
8326
- ws._hib = { holder, tags: list };
8374
+ ws._hib = { holder, tags: list, hostId: 0 };
8327
8375
  live().push(ws);
8328
8376
  __sbWsSchedule(ws);
8329
8377
  };
@@ -8341,6 +8389,7 @@ function __sbDOWebSocketState(state, holder) {
8341
8389
  if (pair != null && !(pair instanceof __SbWebSocketRequestResponsePair))
8342
8390
  throw new TypeError("setWebSocketAutoResponse needs a WebSocketRequestResponsePair");
8343
8391
  holder.auto = pair == null ? null : pair;
8392
+ if (__sbWsHibOn()) __sbWsHibAuto(holder, live());
8344
8393
  };
8345
8394
  state.getWebSocketAutoResponse = function () {
8346
8395
  return holder.auto;
@@ -8364,13 +8413,17 @@ function __sbHasWebSocket(r) {
8364
8413
  // The client end of a 101 response becomes a host socket: marked x-sb-websocket for the
8365
8414
  // host (which upgrades the connection), with what the app already sent going out after it.
8366
8415
  function __sbWsHostResponse(r) {
8416
+ if (r._webSocket instanceof __SbWebSocket && r._webSocket._outbound && !r._webSocket._accepted) __sbWsProxy(r, r._webSocket);
8367
8417
  const ws = r._webSocket;
8368
8418
  if (!(ws instanceof __SbWebSocket) || ws._host !== 0 || ws._accepted || ws._hib !== null) {
8369
8419
  return __sbErrorResponse(new TypeError("A 101 response needs the unaccepted client end of a WebSocketPair."), false);
8370
8420
  }
8371
- const id = Number(__sbWsOp("o", 0, "", 0));
8421
+ // The compatibility date goes along: a standalone or platform server carries client sockets
8422
+ // from 2026-11-01 only (sproutd, whose bridge ignores it, from 2026-10-30).
8423
+ const id = Number(__sbWsOp("o", 0, globalThis.__sbCompat, 0));
8372
8424
  if (!(id > 0)) return new Response("WebSockets are not supported by this host yet", { status: 501 });
8373
8425
  ws._host = id;
8426
+ if (ws._peer !== null && ws._peer._hib !== null && __sbWsHibOn()) __sbWsHibRegister(ws._peer, id);
8374
8427
  r.headers._immutable = false;
8375
8428
  r.headers.set("x-sb-websocket", String(id));
8376
8429
  const held = ws._inbox;
@@ -8388,7 +8441,8 @@ function __sbWsPump(ws) {
8388
8441
  const head = __sbWsOp("p", id, "", 0);
8389
8442
  if (head === "") {
8390
8443
  const timer = setTimeout(() => __sbWsPump(ws), 2147483647);
8391
- __sbWsOp("w", id, "", timer);
8444
+ // From 2026-11-01 a parked socket is not background work (z): its app may be reaped.
8445
+ __sbWsOp(__sbWsHibOn() ? "z" : "w", id, "", timer);
8392
8446
  return;
8393
8447
  }
8394
8448
  const parts = head.split(" ");
@@ -8418,6 +8472,432 @@ function __sbWsPump(ws) {
8418
8472
  }
8419
8473
  }
8420
8474
 
8475
+ // ---------------------------------------------------------------------------
8476
+ // Outbound WebSockets, from compatibility date 2026-11-01: new WebSocket(url, protocols) and
8477
+ // fetch(url, { headers: { Upgrade: "websocket" } }), whose Response carries the connected
8478
+ // socket as response.webSocket (call accept() on it). The transport's C code owns the
8479
+ // connection (sb_wsc_hook, the embedded transport); this end is a host socket with the ops
8480
+ // described at __sbWsOp, ids with the top bit set. Returning that Response from a handler
8481
+ // relays the socket to the client through a pair.
8482
+ function __sbWsOutboundOn() {
8483
+ return globalThis.__sbCompat >= "2026-11-01";
8484
+ }
8485
+ let __sbWsWakeable = -1;
8486
+ function __sbWsCanWake() {
8487
+ if (__sbWsWakeable < 0) __sbWsWakeable = __sbWsOp("W", 0, "", 0) === "1" ? 1 : 0;
8488
+ return __sbWsWakeable === 1;
8489
+ }
8490
+
8491
+ // ws:, wss:, http: and https: URLs, as workerd takes them.
8492
+ function __sbWsTarget(input, what) {
8493
+ let u;
8494
+ try {
8495
+ u = new URL(String(input));
8496
+ } catch {
8497
+ throw new SyntaxError("Failed to construct '" + what + "': The URL '" + String(input) + "' is invalid.");
8498
+ }
8499
+ const scheme = u.protocol;
8500
+ if (scheme !== "ws:" && scheme !== "wss:" && scheme !== "http:" && scheme !== "https:")
8501
+ throw new SyntaxError("Failed to construct '" + what + "': The URL's scheme must be either 'http', 'https', 'ws', or 'wss'. '" + scheme.slice(0, scheme.length - 1) + "' is not allowed.");
8502
+ if (u.hash !== "" && u.hash !== "#") throw new SyntaxError("Failed to construct '" + what + "': The URL contains a fragment identifier ('" + u.hash.slice(1) + "'). Fragment identifiers are not allowed in WebSocket URLs.");
8503
+ const tls = scheme === "wss:" || scheme === "https:";
8504
+ const port = u.port === "" ? (tls ? 443 : 80) : Number(u.port);
8505
+ const path = (u.pathname === "" ? "/" : u.pathname) + u.search;
8506
+ const hostPart = u.hostname + (u.port === "" ? "" : ":" + u.port);
8507
+ return { tls, host: u.hostname, port, path, url: (tls ? "wss://" : "ws://") + hostPart + path };
8508
+ }
8509
+
8510
+ // Opens the connection: the host socket's id, or 0 (outbound sockets not available here, or
8511
+ // too many open).
8512
+ function __sbWsOpen(target, protocols, headers) {
8513
+ let lines = "";
8514
+ if (headers != null) {
8515
+ for (let i = 0; i < headers.length; i++) {
8516
+ const name = String(headers[i][0]).toLowerCase();
8517
+ const value = String(headers[i][1]);
8518
+ if (name === "host" || name === "upgrade" || name === "connection" || name === "content-length" || name === "transfer-encoding") continue;
8519
+ if (name.indexOf("sec-websocket-") === 0) continue;
8520
+ if (name.indexOf("\r") >= 0 || name.indexOf("\n") >= 0 || value.indexOf("\r") >= 0 || value.indexOf("\n") >= 0) continue;
8521
+ lines += name + ": " + value + "\r\n";
8522
+ }
8523
+ }
8524
+ const spec = target.host + "\n" + target.port + "\n" + (target.tls ? "1" : "0") + "\n" + target.path + "\n" + protocols.join(",") + "\n" + lines;
8525
+ return Number(__sbWsOp("u", 0, spec, 0));
8526
+ }
8527
+
8528
+ function __sbWsProtocols(protocols) {
8529
+ if (protocols === undefined) return [];
8530
+ const list = __sbIsStr(protocols) ? [protocols] : Array.from(protocols).map(String);
8531
+ for (let i = 0; i < list.length; i++) {
8532
+ if (list[i] === "" || list[i].indexOf(" ") >= 0 || list[i].indexOf(",") >= 0) throw new SyntaxError("Failed to construct 'WebSocket': The subprotocol '" + list[i] + "' is invalid.");
8533
+ if (list.indexOf(list[i]) !== i) throw new SyntaxError("Failed to construct 'WebSocket': The subprotocol '" + list[i] + "' is duplicated.");
8534
+ }
8535
+ return list;
8536
+ }
8537
+
8538
+ // new WebSocket(url, protocols): CONNECTING until the handshake, then open, as in a browser.
8539
+ function __sbWsConnectOutbound(ws, input, protocols) {
8540
+ const target = __sbWsTarget(input, "WebSocket");
8541
+ const list = __sbWsProtocols(protocols);
8542
+ ws._outbound = true;
8543
+ ws._accepted = true;
8544
+ ws._state = 0;
8545
+ ws.url = target.url;
8546
+ const id = __sbWsOpen(target, list, null);
8547
+ if (!(id > 0)) {
8548
+ ws._state = __SB_WS_CLOSED;
8549
+ setTimeout(() => {
8550
+ __sbWsFire(ws, { type: "error", target: ws });
8551
+ __sbWsFire(ws, { type: "close", code: 1006, reason: "", wasClean: false, target: ws });
8552
+ }, 0);
8553
+ return;
8554
+ }
8555
+ ws._host = id;
8556
+ __sbWsPumpOut(ws);
8557
+ }
8558
+
8559
+ // Takes what the connection received and applies it to ws itself, then waits (a parked timer
8560
+ // the transport fires, or a short poll where nothing can wake the loop) for more.
8561
+ function __sbWsPumpOut(ws) {
8562
+ const id = ws._host;
8563
+ if (id === 0) return;
8564
+ for (;;) {
8565
+ const head = __sbWsOp("p", id, "", 0);
8566
+ if (head === "") {
8567
+ if (__sbWsCanWake()) {
8568
+ const timer = setTimeout(() => __sbWsPumpOut(ws), 2147483647);
8569
+ __sbWsOp("w", id, "", timer);
8570
+ } else setTimeout(() => __sbWsPumpOut(ws), 15);
8571
+ return;
8572
+ }
8573
+ const parts = head.split(" ");
8574
+ const kind = Number(parts[0]);
8575
+ const length = Number(parts[1]);
8576
+ const bytes = new Uint8Array(length);
8577
+ __sbWsOp("i", id, bytes, 0);
8578
+ __sbWsOutEvent(ws, kind, Number(parts[2]), bytes, length);
8579
+ if (ws._host === 0) return;
8580
+ }
8581
+ }
8582
+
8583
+ function __sbWsText(bytes, length) {
8584
+ if (length === 0) return "";
8585
+ let text = __sbFromUtf8Native(bytes);
8586
+ if (text === "") text = new TextDecoder().decode(bytes);
8587
+ return text;
8588
+ }
8589
+
8590
+ // One event from the connection: 0 text, 1 binary, 2 close, 3 lost or never connected,
8591
+ // 4 opened, 5 the server answered the upgrade with something else (its raw head and body).
8592
+ function __sbWsOutEvent(ws, kind, code, bytes, length) {
8593
+ const id = ws._host;
8594
+ const pending = ws._pending;
8595
+ if (kind === 4) {
8596
+ ws._state = __SB_WS_OPEN;
8597
+ ws.protocol = __sbWsText(bytes, length);
8598
+ if (pending) {
8599
+ ws._pending = null;
8600
+ const headers = new Headers();
8601
+ headers.set("upgrade", "websocket");
8602
+ headers.set("connection", "Upgrade");
8603
+ if (ws.protocol !== "") headers.set("sec-websocket-protocol", ws.protocol);
8604
+ pending.resolve(new Response(null, { status: 101, webSocket: ws, headers }));
8605
+ } else __sbWsFire(ws, { type: "open", target: ws });
8606
+ return;
8607
+ }
8608
+ if (kind === 0 || kind === 1) {
8609
+ ws._inbox.push(kind === 1 ? { kind: 1, data: bytes.buffer } : { kind: 0, data: __sbWsText(bytes, length) });
8610
+ if (!ws._flushing) __sbWsFlush(ws);
8611
+ __sbWsOp("k", id, "", length);
8612
+ return;
8613
+ }
8614
+ ws._host = 0;
8615
+ __sbWsOp("r", id, "", 0);
8616
+ if (pending && (kind === 3 || kind === 5)) {
8617
+ ws._pending = null;
8618
+ ws._state = __SB_WS_CLOSED;
8619
+ if (kind === 5) pending.resolve(__sbWsRefusal(code, bytes, length));
8620
+ else pending.reject(new TypeError(__sbWsText(bytes, length) || "WebSocket connection failed"));
8621
+ return;
8622
+ }
8623
+ if (kind === 3) {
8624
+ if (ws._state === 0) __sbWsFire(ws, { type: "error", target: ws });
8625
+ ws._inbox.push({ kind: 2, code: 1006, reason: "", clean: false });
8626
+ } else {
8627
+ ws._inbox.push({ kind: 2, code, reason: __sbWsText(bytes, length), clean: true });
8628
+ }
8629
+ ws._closeSent = true;
8630
+ if (!ws._flushing) __sbWsFlush(ws);
8631
+ if (ws._state !== __SB_WS_CLOSED) ws._state = __SB_WS_CLOSED;
8632
+ }
8633
+
8634
+ // An upgrade the server refused: its answer is the Response.
8635
+ function __sbWsRefusal(status, bytes, length) {
8636
+ let at = 0;
8637
+ while (at + 3 < length && !(bytes[at] === 13 && bytes[at + 1] === 10 && bytes[at + 2] === 13 && bytes[at + 3] === 10)) at++;
8638
+ const head = __sbWsText(bytes.subarray(0, at), at);
8639
+ const headers = new Headers();
8640
+ const lines = head.split("\r\n");
8641
+ for (let i = 1; i < lines.length; i++) {
8642
+ const colon = lines[i].indexOf(":");
8643
+ if (colon > 0) headers.append(lines[i].slice(0, colon).trim(), lines[i].slice(colon + 1).trim());
8644
+ }
8645
+ headers.delete("content-length");
8646
+ headers.delete("transfer-encoding");
8647
+ headers.delete("connection");
8648
+ const body = at + 4 < length ? bytes.slice(at + 4, length) : null;
8649
+ return new Response(status === 204 || status === 304 ? null : body, { status, headers });
8650
+ }
8651
+
8652
+ function __sbWsOutSend(ws, message) {
8653
+ if (ws._state === 0) throw __sbCryptoError("InvalidStateError", "WebSocket is still in CONNECTING state.");
8654
+ if (!ws._accepted) throw new TypeError("You must call accept() on this WebSocket before sending messages.");
8655
+ if (ws._closeSent) throw new TypeError("Can't call WebSocket send() after close().");
8656
+ if (ws._state === __SB_WS_CLOSED || ws._host === 0) return;
8657
+ if (__sbIsStr(message)) __sbWsOp("t", ws._host, message, 0);
8658
+ else if (message instanceof ArrayBuffer) __sbWsOp("b", ws._host, __sbToBytes(new Uint8Array(message)), 0);
8659
+ else if (ArrayBuffer.isView(message)) __sbWsOp("b", ws._host, __sbToBytes(new Uint8Array(message.buffer, message.byteOffset, message.byteLength)), 0);
8660
+ else __sbWsOp("t", ws._host, String(message), 0);
8661
+ }
8662
+
8663
+ function __sbWsOutClose(ws, code, text) {
8664
+ if (ws._closeSent) return;
8665
+ ws._closeSent = true;
8666
+ const id = ws._host;
8667
+ if (id === 0) return;
8668
+ if (ws._state === 0) {
8669
+ // Still connecting: the attempt fails.
8670
+ ws._host = 0;
8671
+ ws._state = __SB_WS_CLOSED;
8672
+ __sbWsOp("r", id, "", 0);
8673
+ const pending = ws._pending;
8674
+ ws._pending = null;
8675
+ if (pending) pending.reject(new TypeError("WebSocket connection was closed before it opened"));
8676
+ else setTimeout(() => {
8677
+ __sbWsFire(ws, { type: "error", target: ws });
8678
+ __sbWsFire(ws, { type: "close", code: 1006, reason: "", wasClean: false, target: ws });
8679
+ }, 0);
8680
+ return;
8681
+ }
8682
+ ws._state = __SB_WS_CLOSING;
8683
+ __sbWsOp("c", id, text, code === undefined ? 1000 : code);
8684
+ // A peer that never answers the close is dropped after a while.
8685
+ setTimeout(() => {
8686
+ if (ws._host !== id) return;
8687
+ ws._host = 0;
8688
+ __sbWsOp("r", id, "", 0);
8689
+ ws._inbox.push({ kind: 2, code: 1006, reason: "", clean: false });
8690
+ if (!ws._flushing) __sbWsFlush(ws);
8691
+ ws._state = __SB_WS_CLOSED;
8692
+ }, 10000);
8693
+ }
8694
+
8695
+ // fetch(url, { headers: { Upgrade: "websocket" } }).
8696
+ function __sbWsIsUpgrade(parts) {
8697
+ if (parts.method !== "GET") return false;
8698
+ for (let i = 0; i < parts.headers.length; i++)
8699
+ if (String(parts.headers[i][0]).toLowerCase() === "upgrade" && String(parts.headers[i][1]).toLowerCase() === "websocket") return true;
8700
+ return false;
8701
+ }
8702
+ function __sbWsFetchUpgrade(parts) {
8703
+ return new Promise((resolve, reject) => {
8704
+ let target;
8705
+ try {
8706
+ target = __sbWsTarget(parts.url, "fetch");
8707
+ } catch (error) {
8708
+ reject(new TypeError(error.message));
8709
+ return;
8710
+ }
8711
+ const protocols = [];
8712
+ for (let i = 0; i < parts.headers.length; i++) {
8713
+ if (String(parts.headers[i][0]).toLowerCase() !== "sec-websocket-protocol") continue;
8714
+ const items = String(parts.headers[i][1]).split(",");
8715
+ for (let j = 0; j < items.length; j++) if (items[j].trim() !== "") protocols.push(items[j].trim());
8716
+ }
8717
+ const ws = new __SbWebSocket(__sbWsKey);
8718
+ ws._outbound = true;
8719
+ ws._state = 0;
8720
+ ws.url = target.url;
8721
+ ws._pending = { resolve, reject };
8722
+ const id = __sbWsOpen(target, protocols, parts.headers);
8723
+ if (!(id > 0)) {
8724
+ reject(new TypeError("Outbound WebSockets are not available here (too many are open, or this transport does not carry them)."));
8725
+ return;
8726
+ }
8727
+ ws._host = id;
8728
+ __sbWsPumpOut(ws);
8729
+ });
8730
+ }
8731
+
8732
+ // A handler returned a fetch() upgrade's Response: the client's socket and the upstream's are
8733
+ // joined through a pair, message for message and close for close.
8734
+ function __sbWsProxy(r, upstream) {
8735
+ const pair = new __SbWebSocketPair();
8736
+ const client = pair[0];
8737
+ const server = pair[1];
8738
+ server.accept();
8739
+ upstream.accept();
8740
+ server.binaryType = "arraybuffer";
8741
+ upstream.binaryType = "arraybuffer";
8742
+ server.addEventListener("message", (e) => {
8743
+ if (upstream._state === __SB_WS_OPEN) upstream.send(e.data);
8744
+ });
8745
+ upstream.addEventListener("message", (e) => {
8746
+ if (server._state === __SB_WS_OPEN) server.send(e.data);
8747
+ });
8748
+ server.addEventListener("close", (e) => {
8749
+ if (upstream._state === __SB_WS_OPEN) upstream.close(e.code === 1005 || e.code === 1006 ? 1000 : e.code, e.reason);
8750
+ });
8751
+ upstream.addEventListener("close", (e) => {
8752
+ if (server._state === __SB_WS_OPEN) server.close(e.code === 1005 || e.code === 1006 ? 1000 : e.code, e.reason);
8753
+ });
8754
+ r._webSocket = client;
8755
+ return r;
8756
+ }
8757
+
8758
+ // ---------------------------------------------------------------------------
8759
+ // Hibernation, from compatibility date 2026-11-01 on a host that keeps client sockets open while
8760
+ // the worker is gone (sproutd; ops "H" "h" "a" "l" "R" "N" of sb_ws_hook). A Durable Object's
8761
+ // hibernatable socket (ctx.acceptWebSocket) whose other end is a host socket is recorded in
8762
+ // storage scoped to the object: its host id, tags, attachment, and the object's auto-response.
8763
+ // The host answers the auto-response itself and may stop the worker while the socket is idle.
8764
+ // When a message or close arrives for a socket the worker no longer knows, the host starts a
8765
+ // worker, tells it which sockets are still open ("l"), and wakes the restore loop ("R", "N"):
8766
+ // the object is constructed again (ctx.getWebSockets() lists its sockets from the start, with
8767
+ // their tags and attachments) and the event is dispatched as webSocketMessage/webSocketClose.
8768
+ let __sbWsHibHost = -1;
8769
+ function __sbWsHibOn() {
8770
+ if (globalThis.__sbCompat < "2026-11-01" || !__sbDOGated()) return false;
8771
+ if (__sbWsHibHost < 0 && __sbWsOp("H", 0, "", 0) === "1") __sbWsHibHost = 1;
8772
+ return __sbWsHibHost === 1;
8773
+ }
8774
+ function __sbWsHibScope(holder) {
8775
+ return "do:" + holder.cls + ":" + holder.id;
8776
+ }
8777
+ function __sbWsHibLoad(holder) {
8778
+ const r = __sbRpc("do.storage.get", { cls: "__sbws", id: __sbWsHibScope(holder), key: "rec" });
8779
+ if (r.found) {
8780
+ try {
8781
+ const rec = JSON.parse(r.value);
8782
+ if (rec && rec.s) return rec;
8783
+ } catch {
8784
+ // A record this runtime cannot read is dropped.
8785
+ }
8786
+ }
8787
+ return { s: {}, auto: null };
8788
+ }
8789
+ function __sbWsHibSave(holder, rec) {
8790
+ __sbRpc("do.storage.put", { cls: "__sbws", id: __sbWsHibScope(holder), key: "rec", value: JSON.stringify(rec) });
8791
+ }
8792
+ function __sbWsHibIndex(id, holder) {
8793
+ if (holder === null) __sbRpc("do.storage.delete", { cls: "__sbws", id: "index", key: "s" + id });
8794
+ else __sbRpc("do.storage.put", { cls: "__sbws", id: "index", key: "s" + id, value: JSON.stringify([holder.cls, holder.id]) });
8795
+ }
8796
+ function __sbWsHibEntry(server) {
8797
+ return { tags: server._hib.tags.slice(), att: server._attachment === undefined ? null : __sbDOEncode(server._attachment) };
8798
+ }
8799
+
8800
+ // The 101 response that carries a hibernatable socket to the host went out: record it.
8801
+ function __sbWsHibRegister(server, id) {
8802
+ const holder = server._hib.holder;
8803
+ server._hib.hostId = id;
8804
+ __sbWsOp("h", id, "", 0);
8805
+ const rec = __sbWsHibLoad(holder);
8806
+ rec.s[String(id)] = __sbWsHibEntry(server);
8807
+ if (holder.auto !== null) {
8808
+ rec.auto = [holder.auto.request, holder.auto.response];
8809
+ __sbWsOp("a", id, holder.auto.request + String.fromCharCode(0) + holder.auto.response, 0);
8810
+ }
8811
+ __sbWsHibSave(holder, rec);
8812
+ __sbWsHibIndex(id, holder);
8813
+ }
8814
+ function __sbWsHibUpdate(server) {
8815
+ const holder = server._hib.holder;
8816
+ const rec = __sbWsHibLoad(holder);
8817
+ rec.s[String(server._hib.hostId)] = __sbWsHibEntry(server);
8818
+ __sbWsHibSave(holder, rec);
8819
+ }
8820
+ function __sbWsHibForget(server) {
8821
+ const id = server._hib.hostId;
8822
+ server._hib.hostId = 0;
8823
+ const holder = server._hib.holder;
8824
+ const rec = __sbWsHibLoad(holder);
8825
+ delete rec.s[String(id)];
8826
+ __sbWsHibSave(holder, rec);
8827
+ __sbWsHibIndex(id, null);
8828
+ }
8829
+ // setWebSocketAutoResponse: the host answers it for every socket of the object.
8830
+ function __sbWsHibAuto(holder, sockets) {
8831
+ const rec = __sbWsHibLoad(holder);
8832
+ rec.auto = holder.auto === null ? null : [holder.auto.request, holder.auto.response];
8833
+ __sbWsHibSave(holder, rec);
8834
+ const text = holder.auto === null ? "" : holder.auto.request + String.fromCharCode(0) + holder.auto.response;
8835
+ for (let i = 0; i < sockets.length; i++) if (sockets[i]._hib.hostId > 0) __sbWsOp("a", sockets[i]._hib.hostId, text, 0);
8836
+ }
8837
+
8838
+ // A new instance of an object: its recorded sockets that the host still holds come back as
8839
+ // hibernatable sockets, before the constructor runs. Their pumps start once the object exists
8840
+ // (holder.restored), so the first events reach webSocketMessage.
8841
+ function __sbWsHibRestore(holder, sockets) {
8842
+ holder.restored = [];
8843
+ if (!__sbWsHibOn()) return;
8844
+ const rec = __sbWsHibLoad(holder);
8845
+ let dirty = false;
8846
+ for (const key in rec.s) {
8847
+ const id = Number(key);
8848
+ if (__sbWsOp("l", id, "", 0) !== "1") {
8849
+ delete rec.s[key];
8850
+ __sbWsHibIndex(id, null);
8851
+ dirty = true;
8852
+ continue;
8853
+ }
8854
+ const pair = new __SbWebSocketPair();
8855
+ const client = pair[0];
8856
+ const server = pair[1];
8857
+ client._host = id;
8858
+ server._hib = { holder, tags: rec.s[key].tags.slice(), hostId: id };
8859
+ server._attachment = rec.s[key].att == null ? undefined : __sbDODecode(rec.s[key].att);
8860
+ sockets.push(server);
8861
+ holder.restored.push(client);
8862
+ }
8863
+ if (rec.auto) holder.auto = new __SbWebSocketRequestResponsePair(rec.auto[0], rec.auto[1]);
8864
+ if (dirty) __sbWsHibSave(holder, rec);
8865
+ }
8866
+
8867
+ // Woken by the host when an event arrives for a socket this worker has not restored.
8868
+ function __sbWsRestoreLoop() {
8869
+ for (;;) {
8870
+ const id = Number(__sbWsOp("N", 0, "", 0));
8871
+ if (!(id > 0)) break;
8872
+ try {
8873
+ __sbWsRestoreSocket(id);
8874
+ } catch (error) {
8875
+ __sbLogUncaught("websocket", error);
8876
+ }
8877
+ }
8878
+ const timer = setTimeout(__sbWsRestoreLoop, 2147483647);
8879
+ __sbWsOp("R", 0, "", timer);
8880
+ }
8881
+ function __sbWsRestoreSocket(id) {
8882
+ const r = __sbRpc("do.storage.get", { cls: "__sbws", id: "index", key: "s" + id });
8883
+ let target = null;
8884
+ if (r.found) {
8885
+ try {
8886
+ target = JSON.parse(r.value);
8887
+ } catch {
8888
+ target = null;
8889
+ }
8890
+ }
8891
+ if (target === null) {
8892
+ // Nothing remembers this socket (another deployment's): close it.
8893
+ __sbWsOp("c", id, "", 1011);
8894
+ __sbWsOp("r", id, "", 0);
8895
+ return;
8896
+ }
8897
+ // Constructing the object restores its sockets (__sbWsHibRestore) and starts their pumps.
8898
+ __sbGetDOInstance(target[0], target[1]);
8899
+ }
8900
+
8421
8901
  function __sbInstallWebSockets() {
8422
8902
  if (globalThis.__sbCompat < "2026-10-30") return;
8423
8903
  globalThis.WebSocketPair = __SbWebSocketPair;
@@ -8800,6 +9280,12 @@ globalThis.__sbEntry = function (handlers, request) {
8800
9280
 
8801
9281
  // B22: a host that delivers this worker's triggers (sproutd) asks it to deliver the queue
8802
9282
  // messages and alarms that are due; the embedded transport answers.
9283
+ // Hibernated WebSockets (2026-11-01): the host woke this worker for one; park the restore timer.
9284
+ if (trigger === "websocket") {
9285
+ if (__sbWsHibOn()) __sbWsRestoreLoop();
9286
+ return new Response(null, { status: 204 });
9287
+ }
9288
+
8803
9289
  if (trigger === "due") return __sbRunLocalDue();
8804
9290
 
8805
9291
  return new Response("unknown trigger", { status: 400 });