capnweb 0.0.0-32e381c → 0.0.0-3732129

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/README.md CHANGED
@@ -45,7 +45,7 @@ import { RpcTarget, newWebSocketRpcSession } from "capnweb";
45
45
  ```
46
46
 
47
47
  To use `using` declarations, your `tsconfig.json` needs `"target": "esnext"` and matching `lib`s.
48
- See [Installation](packages/docs/src/content/docs/start/installation.md).
48
+ See [Installation](packages/docs/src/content/docs/start/installation.mdx).
49
49
 
50
50
  ## Example
51
51
 
@@ -114,49 +114,49 @@ under [`packages/docs/`](packages/docs/), and every page is readable as Markdown
114
114
 
115
115
  Start here:
116
116
 
117
- | Page | What it covers |
118
- | -------------------------------------------------------------------------- | ---------------------------------------------------- |
119
- | [Introduction](packages/docs/src/content/docs/start/introduction.md) | What Cap'n Web is and why object capabilities matter |
120
- | [Quickstart](packages/docs/src/content/docs/start/quickstart.md) | A working client and server |
121
- | [Pipelining tour](packages/docs/src/content/docs/start/pipelining-tour.md) | The part that makes it fast |
122
- | [How it compares](packages/docs/src/content/docs/guides/comparisons.md) | Against tRPC, JSON-RPC, GraphQL and Cap'n Proto |
117
+ | Page | What it covers |
118
+ | --------------------------------------------------------------------------- | ---------------------------------------------------- |
119
+ | [Introduction](packages/docs/src/content/docs/start/introduction.mdx) | What Cap'n Web is and why object capabilities matter |
120
+ | [Quickstart](packages/docs/src/content/docs/start/quickstart.mdx) | A working client and server |
121
+ | [Pipelining tour](packages/docs/src/content/docs/start/pipelining-tour.mdx) | The part that makes it fast |
122
+ | [How it compares](packages/docs/src/content/docs/guides/comparisons.mdx) | Against tRPC, JSON-RPC, GraphQL and Cap'n Proto |
123
123
 
124
124
  Core concepts:
125
- [What can be passed](packages/docs/src/content/docs/concepts/values.md) ·
126
- [RpcTarget](packages/docs/src/content/docs/concepts/rpc-target.md) ·
127
- [RpcStub](packages/docs/src/content/docs/concepts/stubs.md) ·
128
- [RpcPromise & pipelining](packages/docs/src/content/docs/concepts/promises.md) ·
129
- [The magic `map()`](packages/docs/src/content/docs/concepts/map.md) ·
130
- [Streaming](packages/docs/src/content/docs/concepts/streaming.md) ·
131
- [Disposal](packages/docs/src/content/docs/concepts/disposal.md)
125
+ [What can be passed](packages/docs/src/content/docs/concepts/values.mdx) ·
126
+ [RpcTarget](packages/docs/src/content/docs/concepts/rpc-target.mdx) ·
127
+ [RpcStub](packages/docs/src/content/docs/concepts/stubs.mdx) ·
128
+ [RpcPromise & pipelining](packages/docs/src/content/docs/concepts/promises.mdx) ·
129
+ [The magic `map()`](packages/docs/src/content/docs/concepts/map.mdx) ·
130
+ [Streaming](packages/docs/src/content/docs/concepts/streaming.mdx) ·
131
+ [Disposal](packages/docs/src/content/docs/concepts/disposal.mdx)
132
132
 
133
133
  Transports:
134
- [Overview](packages/docs/src/content/docs/transports/index.md) ·
135
- [HTTP batch](packages/docs/src/content/docs/transports/http-batch.md) ·
136
- [WebSocket](packages/docs/src/content/docs/transports/websocket.md) ·
137
- [MessagePort](packages/docs/src/content/docs/transports/message-port.md) ·
138
- [Custom](packages/docs/src/content/docs/transports/custom.md)
134
+ [Overview](packages/docs/src/content/docs/transports/index.mdx) ·
135
+ [HTTP batch](packages/docs/src/content/docs/transports/http-batch.mdx) ·
136
+ [WebSocket](packages/docs/src/content/docs/transports/websocket.mdx) ·
137
+ [MessagePort](packages/docs/src/content/docs/transports/message-port.mdx) ·
138
+ [Custom](packages/docs/src/content/docs/transports/custom.mdx)
139
139
 
140
140
  Server runtimes:
141
- [Cloudflare Workers](packages/docs/src/content/docs/servers/workers.md) ·
142
- [Node.js](packages/docs/src/content/docs/servers/node.md) ·
143
- [Deno](packages/docs/src/content/docs/servers/deno.md) ·
144
- [Bun](packages/docs/src/content/docs/servers/bun.md) ·
145
- [Hono](packages/docs/src/content/docs/servers/hono.md) ·
146
- [Other](packages/docs/src/content/docs/servers/other.md)
141
+ [Cloudflare Workers](packages/docs/src/content/docs/servers/workers.mdx) ·
142
+ [Node.js](packages/docs/src/content/docs/servers/node.mdx) ·
143
+ [Deno](packages/docs/src/content/docs/servers/deno.mdx) ·
144
+ [Bun](packages/docs/src/content/docs/servers/bun.mdx) ·
145
+ [Hono](packages/docs/src/content/docs/servers/hono.mdx) ·
146
+ [Other](packages/docs/src/content/docs/servers/other.mdx)
147
147
 
148
148
  Guides and reference:
149
- [Security considerations](packages/docs/src/content/docs/guides/security.md) ·
150
- [Sessions & reconnection](packages/docs/src/content/docs/guides/sessions.md) ·
151
- [Runtime validation](packages/docs/src/content/docs/guides/validation.md) ·
152
- [Workers RPC interop](packages/docs/src/content/docs/guides/workers-rpc.md) ·
153
- [Wire protocol](packages/docs/src/content/docs/reference/protocol.md) ·
154
- [API cheat sheet](packages/docs/src/content/docs/reference/api.md)
149
+ [Security considerations](packages/docs/src/content/docs/guides/security.mdx) ·
150
+ [Sessions & reconnection](packages/docs/src/content/docs/guides/sessions.mdx) ·
151
+ [Runtime validation](packages/docs/src/content/docs/guides/validation.mdx) ·
152
+ [Workers RPC interop](packages/docs/src/content/docs/guides/workers-rpc.mdx) ·
153
+ [Wire protocol](packages/docs/src/content/docs/reference/protocol.mdx) ·
154
+ [API cheat sheet](packages/docs/src/content/docs/reference/api.mdx)
155
155
 
156
156
  To run the site locally, with both examples embedded as live in-browser playgrounds:
157
157
 
158
158
  ```sh
159
- cd packages/docs && npm install && npm run dev
159
+ pnpm install && pnpm run dev:docs
160
160
  ```
161
161
 
162
162
  ## Examples
@@ -180,7 +180,7 @@ Runnable examples live in [`examples/`](examples/):
180
180
  Cap'n Web gives you strong authorization tools, but a few things are your responsibility:
181
181
  authenticating in-band rather than with cookies, rate-limiting because pipelining is cheap for
182
182
  attackers, setting transport payload limits, and validating types at runtime. Read
183
- [Security considerations](packages/docs/src/content/docs/guides/security.md) before exposing a
183
+ [Security considerations](packages/docs/src/content/docs/guides/security.mdx) before exposing a
184
184
  service to untrusted peers.
185
185
 
186
186
  To report a vulnerability, see [SECURITY.md](SECURITY.md).
@@ -1,5 +1,4 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
-
3
2
  //#region src/symbols.ts
4
3
  let WORKERS_MODULE_SYMBOL = Symbol("workers-module");
5
4
 
@@ -42,6 +41,7 @@ function typeForRpc(value) {
42
41
  case AsyncFunction.prototype: return "function";
43
42
  case Array.prototype: return "array";
44
43
  case Date.prototype: return "date";
44
+ case RegExp.prototype: return "regexp";
45
45
  case Uint8Array.prototype:
46
46
  case BUFFER_PROTOTYPE:
47
47
  case ArrayBuffer.prototype:
@@ -359,12 +359,13 @@ var RpcPayload = class RpcPayload {
359
359
  return TargetStubHook.create(target, parent);
360
360
  } else if (this.source === "return") {
361
361
  let hook = this.rpcTargets?.get(target);
362
- if (hook) if (dupStubs) return hook.dup();
363
- else {
364
- this.rpcTargets?.delete(target);
365
- return hook;
366
- }
367
- else {
362
+ if (hook) {
363
+ if (dupStubs) return hook.dup();
364
+ else {
365
+ this.rpcTargets?.delete(target);
366
+ return hook;
367
+ }
368
+ } else {
368
369
  hook = TargetStubHook.create(target, parent);
369
370
  if (dupStubs) {
370
371
  if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
@@ -378,12 +379,13 @@ var RpcPayload = class RpcPayload {
378
379
  if (this.source === "params") return streamImpl.createWritableStreamHook(stream);
379
380
  else if (this.source === "return") {
380
381
  let hook = this.rpcTargets?.get(stream);
381
- if (hook) if (dupStubs) return hook.dup();
382
- else {
383
- this.rpcTargets?.delete(stream);
384
- return hook;
385
- }
386
- else {
382
+ if (hook) {
383
+ if (dupStubs) return hook.dup();
384
+ else {
385
+ this.rpcTargets?.delete(stream);
386
+ return hook;
387
+ }
388
+ } else {
387
389
  hook = streamImpl.createWritableStreamHook(stream);
388
390
  if (dupStubs) {
389
391
  if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
@@ -397,12 +399,13 @@ var RpcPayload = class RpcPayload {
397
399
  if (this.source === "params") return streamImpl.createReadableStreamHook(stream);
398
400
  else if (this.source === "return") {
399
401
  let hook = this.rpcTargets?.get(stream);
400
- if (hook) if (dupStubs) return hook.dup();
401
- else {
402
- this.rpcTargets?.delete(stream);
403
- return hook;
404
- }
405
- else {
402
+ if (hook) {
403
+ if (dupStubs) return hook.dup();
404
+ else {
405
+ this.rpcTargets?.delete(stream);
406
+ return hook;
407
+ }
408
+ } else {
406
409
  hook = streamImpl.createReadableStreamHook(stream);
407
410
  if (dupStubs) {
408
411
  if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
@@ -421,6 +424,7 @@ var RpcPayload = class RpcPayload {
421
424
  case "bytes":
422
425
  case "blob":
423
426
  case "url":
427
+ case "regexp":
424
428
  case "error":
425
429
  case "undefined": return value;
426
430
  case "array": {
@@ -619,6 +623,7 @@ var RpcPayload = class RpcPayload {
619
623
  case "blob":
620
624
  case "date":
621
625
  case "url":
626
+ case "regexp":
622
627
  case "error":
623
628
  case "undefined": return;
624
629
  case "array": {
@@ -702,6 +707,7 @@ var RpcPayload = class RpcPayload {
702
707
  case "writable":
703
708
  case "readable":
704
709
  case "url":
710
+ case "regexp":
705
711
  case "headers":
706
712
  case "request":
707
713
  case "response": return;
@@ -772,6 +778,7 @@ function followPath(value, parent, path, owner) {
772
778
  case "date":
773
779
  case "error":
774
780
  case "url":
781
+ case "regexp":
775
782
  case "headers":
776
783
  case "request":
777
784
  case "response":
@@ -817,7 +824,8 @@ var ValueStubHook = class extends StubHook {
817
824
  args.dispose();
818
825
  return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
819
826
  }
820
- return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
827
+ let promise = args.deliverCall(followResult.value, followResult.parent);
828
+ return new PromiseStubHook(promise.then((payload) => {
821
829
  return new PayloadStubHook(payload);
822
830
  }));
823
831
  }
@@ -1015,7 +1023,7 @@ const DEFAULT_MAX_DEPTH = 256;
1015
1023
  const DEFAULT_LIMITS = {
1016
1024
  maxBigIntDigits: 16384,
1017
1025
  maxDepth: 256,
1018
- maxMessageSize: 32 * 1024 * 1024
1026
+ maxMessageSize: 33554432
1019
1027
  };
1020
1028
  const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
1021
1029
  const BYTE_CONTAINER_TYPE_NAMES = [
@@ -1077,9 +1085,7 @@ function swapByteOrder(bytes, elementSize) {
1077
1085
  case 4:
1078
1086
  view.setUint32(offset, view.getUint32(offset, false), true);
1079
1087
  break;
1080
- case 8:
1081
- view.setBigUint64(offset, view.getBigUint64(offset, false), true);
1082
- break;
1088
+ case 8: view.setBigUint64(offset, view.getBigUint64(offset, false), true);
1083
1089
  }
1084
1090
  }
1085
1091
  var NullExporter = class {
@@ -1172,6 +1178,15 @@ var Devaluator = class Devaluator {
1172
1178
  const time = value.getTime();
1173
1179
  return ["date", Number.isNaN(time) ? null : time];
1174
1180
  }
1181
+ case "regexp": {
1182
+ if (this.encodingLevel === "structuredClonable") return value;
1183
+ let re = value;
1184
+ return re.flags ? [
1185
+ "regexp",
1186
+ re.source,
1187
+ re.flags
1188
+ ] : ["regexp", re.source];
1189
+ }
1175
1190
  case "bytes": {
1176
1191
  let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
1177
1192
  let bytes;
@@ -1329,13 +1344,16 @@ var Devaluator = class Devaluator {
1329
1344
  if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
1330
1345
  let { hook, pathIfPromise } = unwrapStubAndPath(value);
1331
1346
  let importId = this.exporter.getImport(hook);
1332
- if (importId !== void 0) if (pathIfPromise) if (pathIfPromise.length > 0) return [
1333
- "pipeline",
1334
- importId,
1335
- pathIfPromise
1336
- ];
1337
- else return ["pipeline", importId];
1338
- else return ["import", importId];
1347
+ if (importId !== void 0) {
1348
+ if (pathIfPromise) {
1349
+ if (pathIfPromise.length > 0) return [
1350
+ "pipeline",
1351
+ importId,
1352
+ pathIfPromise
1353
+ ];
1354
+ else return ["pipeline", importId];
1355
+ } else return ["import", importId];
1356
+ }
1339
1357
  if (pathIfPromise) hook = hook.get(pathIfPromise);
1340
1358
  else hook = hook.dup();
1341
1359
  return this.devaluateHook(pathIfPromise ? "promise" : "export", hook);
@@ -1396,16 +1414,18 @@ var NullImporter = class {
1396
1414
  };
1397
1415
  const NULL_IMPORTER = new NullImporter();
1398
1416
  function fixBrokenRequestBody(request, body) {
1399
- return new RpcPromise$1(new PromiseStubHook(new Response(body).arrayBuffer().then((arrayBuffer) => {
1417
+ let promise = new Response(body).arrayBuffer().then((arrayBuffer) => {
1400
1418
  let bytes = new Uint8Array(arrayBuffer);
1401
1419
  let result = new Request(request, { body: bytes });
1402
1420
  return new PayloadStubHook(RpcPayload.fromAppReturn(result));
1403
- })), []);
1421
+ });
1422
+ return new RpcPromise$1(new PromiseStubHook(promise), []);
1404
1423
  }
1405
1424
  function streamToBlobPromise(stream, type) {
1406
- return new RpcPromise$1(new PromiseStubHook(streamToBlob(stream, type).then((blob) => {
1425
+ let promise = streamToBlob(stream, type).then((blob) => {
1407
1426
  return new PayloadStubHook(RpcPayload.fromAppReturn(blob));
1408
- })), []);
1427
+ });
1428
+ return new RpcPromise$1(new PromiseStubHook(promise), []);
1409
1429
  }
1410
1430
  var Evaluator = class Evaluator {
1411
1431
  importer;
@@ -1438,7 +1458,7 @@ var Evaluator = class Evaluator {
1438
1458
  let maxDepth = this.limits.maxDepth;
1439
1459
  if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
1440
1460
  if (this.encodingLevel === "structuredClonable") {
1441
- if (value instanceof Date || typeof value === "bigint") return value;
1461
+ if (value instanceof Date || value instanceof RegExp || typeof value === "bigint") return value;
1442
1462
  }
1443
1463
  if (value instanceof Array) {
1444
1464
  if (value.length == 1 && value[0] instanceof Array) {
@@ -1458,18 +1478,22 @@ var Evaluator = class Evaluator {
1458
1478
  if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
1459
1479
  if (typeof value[1] == "number") return new Date(value[1]);
1460
1480
  break;
1481
+ case "regexp":
1482
+ if (typeof value[1] === "string" && (value.length === 2 || value.length === 3 && typeof value[2] === "string")) return new RegExp(value[1], value[2]);
1483
+ break;
1461
1484
  case "bytes": {
1462
1485
  let bytes;
1463
1486
  if (value[1] instanceof Uint8Array) bytes = value[1];
1464
- else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
1465
- else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
1466
- else {
1467
- let bs = atob(value[1]);
1468
- let len = bs.length;
1469
- bytes = new Uint8Array(len);
1470
- for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
1471
- }
1472
- else break;
1487
+ else if (typeof value[1] == "string") {
1488
+ if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
1489
+ else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
1490
+ else {
1491
+ let bs = atob(value[1]);
1492
+ let len = bs.length;
1493
+ bytes = new Uint8Array(len);
1494
+ for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
1495
+ }
1496
+ } else break;
1473
1497
  if (value.length === 2) return bytes;
1474
1498
  if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
1475
1499
  if (!isValidByteContainerName(value[2])) {
@@ -1495,7 +1519,6 @@ var Evaluator = class Evaluator {
1495
1519
  case "BigUint64Array": return new BigUint64Array(buffer);
1496
1520
  case "Float32Array": return new Float32Array(buffer);
1497
1521
  case "Float64Array": return new Float64Array(buffer);
1498
- default:
1499
1522
  }
1500
1523
  }
1501
1524
  case "error":
@@ -1601,8 +1624,10 @@ var Evaluator = class Evaluator {
1601
1624
  return new RpcPromise$1(hook, []);
1602
1625
  }
1603
1626
  };
1604
- if (value.length == 2) if (isPromise) return addStub(hook.get([]));
1605
- else return addStub(hook.dup());
1627
+ if (value.length == 2) {
1628
+ if (isPromise) return addStub(hook.get([]));
1629
+ else return addStub(hook.dup());
1630
+ }
1606
1631
  let path = value[2];
1607
1632
  if (!(path instanceof Array)) break;
1608
1633
  if (!path.every((part) => {
@@ -1632,7 +1657,8 @@ var Evaluator = class Evaluator {
1632
1657
  }
1633
1658
  });
1634
1659
  let instructions = value[4];
1635
- let promise = new RpcPromise$1(hook.map(path, captures, instructions), []);
1660
+ let resultHook = hook.map(path, captures, instructions);
1661
+ let promise = new RpcPromise$1(resultHook, []);
1636
1662
  this.promises.push({
1637
1663
  promise,
1638
1664
  parent,
@@ -1642,18 +1668,21 @@ var Evaluator = class Evaluator {
1642
1668
  }
1643
1669
  case "export":
1644
1670
  case "promise":
1645
- if (typeof value[1] == "number") if (value[0] == "promise") {
1646
- let promise = new RpcPromise$1(this.importer.importPromise(value[1]), []);
1647
- this.promises.push({
1648
- parent,
1649
- property,
1650
- promise
1651
- });
1652
- return promise;
1653
- } else {
1654
- let hook = this.importer.importStub(value[1]);
1655
- this.hooks.push(hook);
1656
- return new RpcStub$1(hook);
1671
+ if (typeof value[1] == "number") {
1672
+ if (value[0] == "promise") {
1673
+ let hook = this.importer.importPromise(value[1]);
1674
+ let promise = new RpcPromise$1(hook, []);
1675
+ this.promises.push({
1676
+ parent,
1677
+ property,
1678
+ promise
1679
+ });
1680
+ return promise;
1681
+ } else {
1682
+ let hook = this.importer.importStub(value[1]);
1683
+ this.hooks.push(hook);
1684
+ return new RpcStub$1(hook);
1685
+ }
1657
1686
  }
1658
1687
  break;
1659
1688
  case "writable":
@@ -1664,14 +1693,12 @@ var Evaluator = class Evaluator {
1664
1693
  return stream;
1665
1694
  }
1666
1695
  break;
1667
- case "readable":
1668
- if (typeof value[1] == "number") {
1669
- let stream = this.importer.getPipeReadable(value[1]);
1670
- let hook = streamImpl.createReadableStreamHook(stream);
1671
- this.hooks.push(hook);
1672
- return stream;
1673
- }
1674
- break;
1696
+ case "readable": if (typeof value[1] == "number") {
1697
+ let stream = this.importer.getPipeReadable(value[1]);
1698
+ let hook = streamImpl.createReadableStreamHook(stream);
1699
+ this.hooks.push(hook);
1700
+ return stream;
1701
+ }
1675
1702
  }
1676
1703
  throw new TypeError(`unknown special value: ${JSON.stringify(value)}`);
1677
1704
  } else if (value instanceof Object) {
@@ -1716,6 +1743,7 @@ function estimateEncodedSize(value, seen, depth = 0) {
1716
1743
  if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
1717
1744
  if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
1718
1745
  if (value instanceof Date) return 16;
1746
+ if (value instanceof RegExp) return ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.source) + estimateStringSize(value.flags);
1719
1747
  seen ??= /* @__PURE__ */ new WeakSet();
1720
1748
  if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
1721
1749
  seen.add(value);
@@ -2251,7 +2279,8 @@ var RpcSessionImpl = class {
2251
2279
  if (msg instanceof Array) switch (msg[0]) {
2252
2280
  case "push":
2253
2281
  if (msg.length > 1) {
2254
- let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
2282
+ let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
2283
+ let hook = new PayloadStubHook(payload);
2255
2284
  hook.ignoreUnhandledRejections();
2256
2285
  this.exports.push({
2257
2286
  hook,
@@ -2262,7 +2291,8 @@ var RpcSessionImpl = class {
2262
2291
  break;
2263
2292
  case "stream":
2264
2293
  if (msg.length > 1) {
2265
- let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
2294
+ let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
2295
+ let hook = new PayloadStubHook(payload);
2266
2296
  hook.ignoreUnhandledRejections();
2267
2297
  let exportId = this.exports.length;
2268
2298
  this.exports.push({
@@ -2297,13 +2327,14 @@ var RpcSessionImpl = class {
2297
2327
  let importId = msg[1];
2298
2328
  if (typeof importId == "number" && msg.length > 2) {
2299
2329
  let imp = this.imports[importId];
2300
- if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
2301
- else {
2302
- let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
2303
- payload.dispose();
2304
- imp.resolve(new ErrorStubHook(payload.value));
2305
- }
2306
- else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
2330
+ if (imp) {
2331
+ if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
2332
+ else {
2333
+ let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
2334
+ payload.dispose();
2335
+ imp.resolve(new ErrorStubHook(payload.value));
2336
+ }
2337
+ } else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
2307
2338
  continue;
2308
2339
  }
2309
2340
  break;
@@ -2375,7 +2406,8 @@ var RpcSession$1 = class {
2375
2406
  const MAX_CLOSE_REASON_BYTES = 123;
2376
2407
  function newWebSocketRpcSession$1(webSocket, localMain, options) {
2377
2408
  if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
2378
- return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
2409
+ let transport = new WebSocketTransport(webSocket);
2410
+ return new RpcSession$1(transport, localMain, options).getRemoteMain();
2379
2411
  }
2380
2412
  /**
2381
2413
  * For use in Cloudflare Workers: Construct an HTTP response that starts a WebSocket RPC session
@@ -2412,12 +2444,13 @@ var WebSocketTransport = class {
2412
2444
  });
2413
2445
  }
2414
2446
  webSocket.addEventListener("message", (event) => {
2415
- if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) if (this.#receiveResolver) {
2416
- this.#receiveResolver(event.data);
2417
- this.#receiveResolver = void 0;
2418
- this.#receiveRejecter = void 0;
2419
- } else this.#receiveQueue.push(event.data);
2420
- else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
2447
+ if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) {
2448
+ if (this.#receiveResolver) {
2449
+ this.#receiveResolver(event.data);
2450
+ this.#receiveResolver = void 0;
2451
+ this.#receiveRejecter = void 0;
2452
+ } else this.#receiveQueue.push(event.data);
2453
+ } else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
2421
2454
  });
2422
2455
  webSocket.addEventListener("close", (event) => {
2423
2456
  this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
@@ -2509,7 +2542,8 @@ function newHttpBatchRpcSession$1(urlOrRequest, options) {
2509
2542
  let body = await response.text();
2510
2543
  return body == "" ? [] : body.split("\n");
2511
2544
  };
2512
- return new RpcSession$1(new BatchClientTransport(sendBatch), void 0, options).getRemoteMain();
2545
+ let transport = new BatchClientTransport(sendBatch);
2546
+ return new RpcSession$1(transport, void 0, options).getRemoteMain();
2513
2547
  }
2514
2548
  var BatchServerTransport = class {
2515
2549
  constructor(batch) {
@@ -2552,7 +2586,8 @@ var BatchServerTransport = class {
2552
2586
  async function newHttpBatchRpcResponse(request, localMain, options) {
2553
2587
  if (request.method !== "POST") return new Response("This endpoint only accepts POST requests.", { status: 405 });
2554
2588
  let body = await request.text();
2555
- let transport = new BatchServerTransport(body === "" ? [] : body.split("\n"));
2589
+ let batch = body === "" ? [] : body.split("\n");
2590
+ let transport = new BatchServerTransport(batch);
2556
2591
  let rpc = new RpcSession$1(transport, localMain, options);
2557
2592
  await transport.whenAllReceived();
2558
2593
  await rpc.drain();
@@ -2582,7 +2617,8 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
2582
2617
  });
2583
2618
  request.on("error", reject);
2584
2619
  });
2585
- let transport = new BatchServerTransport(body === "" ? [] : body.split("\n"));
2620
+ let batch = body === "" ? [] : body.split("\n");
2621
+ let transport = new BatchServerTransport(batch);
2586
2622
  let rpc = new RpcSession$1(transport, localMain, options);
2587
2623
  await transport.whenAllReceived();
2588
2624
  await rpc.drain();
@@ -2593,7 +2629,8 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
2593
2629
  //#endregion
2594
2630
  //#region src/messageport.ts
2595
2631
  function newMessagePortRpcSession$1(port, localMain, options) {
2596
- return new RpcSession$1(new MessagePortTransport(port), localMain, options).getRemoteMain();
2632
+ let transport = new MessagePortTransport(port);
2633
+ return new RpcSession$1(transport, localMain, options).getRemoteMain();
2597
2634
  }
2598
2635
  var MessagePortTransport = class {
2599
2636
  encodingLevel = "structuredClonable";
@@ -2843,7 +2880,8 @@ var MapApplicator = class {
2843
2880
  }
2844
2881
  };
2845
2882
  function applyMapToElement(input, parent, owner, captures, instructions) {
2846
- let mapper = new MapApplicator(captures, new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner)));
2883
+ let inputHook = new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner));
2884
+ let mapper = new MapApplicator(captures, inputHook);
2847
2885
  try {
2848
2886
  return mapper.apply(instructions);
2849
2887
  } finally {
@@ -2876,9 +2914,10 @@ mapImpl.applyMap = (input, parent, owner, captures, instructions) => {
2876
2914
  var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2877
2915
  state;
2878
2916
  static create(stream) {
2917
+ let writer = stream.getWriter();
2879
2918
  return new WritableStreamStubHook({
2880
2919
  refcount: 1,
2881
- writer: stream.getWriter(),
2920
+ writer,
2882
2921
  closed: false
2883
2922
  });
2884
2923
  }
@@ -2898,7 +2937,8 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2898
2937
  const method = path[0];
2899
2938
  if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
2900
2939
  if (method === "close" || method === "abort") state.closed = true;
2901
- return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
2940
+ let promise = method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer);
2941
+ return new PromiseStubHook(promise.then((payload) => new PayloadStubHook(payload)));
2902
2942
  } catch (err) {
2903
2943
  args.dispose();
2904
2944
  return new ErrorStubHook(err);
@@ -2912,7 +2952,8 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2912
2952
  return new ErrorStubHook(/* @__PURE__ */ new Error("Cannot access properties on a WritableStream stub"));
2913
2953
  }
2914
2954
  dup() {
2915
- return new WritableStreamStubHook(this.getState(), this);
2955
+ let state = this.getState();
2956
+ return new WritableStreamStubHook(state, this);
2916
2957
  }
2917
2958
  pull() {
2918
2959
  return Promise.reject(/* @__PURE__ */ new Error("Cannot pull a WritableStream stub"));
@@ -2930,9 +2971,9 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2930
2971
  }
2931
2972
  onBroken(callback) {}
2932
2973
  };
2933
- const INITIAL_WINDOW = 256 * 1024;
2934
- const MAX_WINDOW = 1024 * 1024 * 1024;
2935
- const MIN_WINDOW = 64 * 1024;
2974
+ const INITIAL_WINDOW = 262144;
2975
+ const MAX_WINDOW = 1073741824;
2976
+ const MIN_WINDOW = 65536;
2936
2977
  const STARTUP_GROWTH_FACTOR = 2;
2937
2978
  const STEADY_GROWTH_FACTOR = 1.25;
2938
2979
  const DECAY_FACTOR = .9;