capnweb 0.11.1 → 0.13.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.
@@ -20,7 +20,7 @@ var __copyProps = (to, from, except, desc) => {
20
20
  }
21
21
  return to;
22
22
  };
23
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
24
24
  value: mod,
25
25
  enumerable: true
26
26
  }) : target, mod));
@@ -75,6 +75,9 @@ function typeForRpc(value) {
75
75
  case AsyncFunction.prototype: return "function";
76
76
  case Array.prototype: return "array";
77
77
  case Date.prototype: return "date";
78
+ case RegExp.prototype: return "regexp";
79
+ case Set.prototype: return "set";
80
+ case Map.prototype: return "map";
78
81
  case Uint8Array.prototype:
79
82
  case BUFFER_PROTOTYPE:
80
83
  case ArrayBuffer.prototype:
@@ -125,7 +128,14 @@ let streamImpl = {
125
128
  };
126
129
  var StubHook = class {
127
130
  stream(path, args) {
128
- let pulled = this.call(path, args).pull();
131
+ let hook = this.call(path, args);
132
+ let pulled;
133
+ try {
134
+ pulled = hook.pull();
135
+ } catch (err) {
136
+ hook.dispose();
137
+ throw err;
138
+ }
129
139
  let promise;
130
140
  if (pulled instanceof Promise) promise = pulled.then((p) => {
131
141
  p.dispose();
@@ -144,9 +154,11 @@ var ErrorStubHook = class extends StubHook {
144
154
  this.error = error;
145
155
  }
146
156
  call(path, args) {
157
+ args.dispose();
147
158
  return this;
148
159
  }
149
160
  map(path, captures, instructions) {
161
+ for (let cap of captures) cap.dispose();
150
162
  return this;
151
163
  }
152
164
  get(path) {
@@ -270,7 +282,25 @@ var RpcStub$1 = class RpcStub$1 extends RpcTarget$1 {
270
282
  };
271
283
  var RpcPromise$1 = class extends RpcStub$1 {
272
284
  constructor(hook, pathIfPromise) {
273
- super(hook, pathIfPromise);
285
+ if (hook instanceof StubHook) super(hook, pathIfPromise ?? []);
286
+ else {
287
+ if (pathIfPromise !== void 0) throw new TypeError("RpcPromise constructor expected one argument, received two.");
288
+ let kind = typeForRpc(hook);
289
+ if (kind === "rpc-promise") {
290
+ let raw = unwrapStubAndPath(hook);
291
+ if (raw.pathIfPromise.length > 0) super(raw.hook, raw.pathIfPromise);
292
+ else {
293
+ let adopted = raw.hook;
294
+ raw.hook = DISPOSED_HOOK;
295
+ super(adopted, []);
296
+ }
297
+ } else if (kind === "rpc-thenable") super(TargetStubHook.create(hook, void 0), []);
298
+ else {
299
+ let promiseHook = new PromiseStubHook(Promise.resolve(hook).then((value) => new PayloadStubHook(RpcPayload.fromAppReturn(value))));
300
+ promiseHook.ignoreUnhandledRejections();
301
+ super(promiseHook, []);
302
+ }
303
+ }
274
304
  }
275
305
  then(onfulfilled, onrejected) {
276
306
  return pullPromise(this).then(...arguments);
@@ -311,6 +341,9 @@ async function pullPromise(promise) {
311
341
  if (pathIfPromise.length > 0) hook = hook.get(pathIfPromise);
312
342
  return (await hook.pull()).deliverResolve();
313
343
  }
344
+ function mapKeyToPropertyName(key) {
345
+ return typeof key === "string" || typeof key === "number" ? key : { mapKey: key };
346
+ }
314
347
  var RpcPayload = class RpcPayload {
315
348
  value;
316
349
  source;
@@ -365,12 +398,13 @@ var RpcPayload = class RpcPayload {
365
398
  return TargetStubHook.create(target, parent);
366
399
  } else if (this.source === "return") {
367
400
  let hook = this.rpcTargets?.get(target);
368
- if (hook) if (dupStubs) return hook.dup();
369
- else {
370
- this.rpcTargets?.delete(target);
371
- return hook;
372
- }
373
- else {
401
+ if (hook) {
402
+ if (dupStubs) return hook.dup();
403
+ else {
404
+ this.rpcTargets?.delete(target);
405
+ return hook;
406
+ }
407
+ } else {
374
408
  hook = TargetStubHook.create(target, parent);
375
409
  if (dupStubs) {
376
410
  if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
@@ -384,12 +418,13 @@ var RpcPayload = class RpcPayload {
384
418
  if (this.source === "params") return streamImpl.createWritableStreamHook(stream);
385
419
  else if (this.source === "return") {
386
420
  let hook = this.rpcTargets?.get(stream);
387
- if (hook) if (dupStubs) return hook.dup();
388
- else {
389
- this.rpcTargets?.delete(stream);
390
- return hook;
391
- }
392
- else {
421
+ if (hook) {
422
+ if (dupStubs) return hook.dup();
423
+ else {
424
+ this.rpcTargets?.delete(stream);
425
+ return hook;
426
+ }
427
+ } else {
393
428
  hook = streamImpl.createWritableStreamHook(stream);
394
429
  if (dupStubs) {
395
430
  if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
@@ -403,12 +438,13 @@ var RpcPayload = class RpcPayload {
403
438
  if (this.source === "params") return streamImpl.createReadableStreamHook(stream);
404
439
  else if (this.source === "return") {
405
440
  let hook = this.rpcTargets?.get(stream);
406
- if (hook) if (dupStubs) return hook.dup();
407
- else {
408
- this.rpcTargets?.delete(stream);
409
- return hook;
410
- }
411
- else {
441
+ if (hook) {
442
+ if (dupStubs) return hook.dup();
443
+ else {
444
+ this.rpcTargets?.delete(stream);
445
+ return hook;
446
+ }
447
+ } else {
412
448
  hook = streamImpl.createReadableStreamHook(stream);
413
449
  if (dupStubs) {
414
450
  if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
@@ -416,7 +452,7 @@ var RpcPayload = class RpcPayload {
416
452
  return hook.dup();
417
453
  } else return hook;
418
454
  }
419
- } else throw new Error("owned payload shouldn't contain raw ReadableStreams");
455
+ } else return streamImpl.createReadableStreamHook(stream);
420
456
  }
421
457
  deepCopy(value, oldParent, property, parent, dupStubs, owner) {
422
458
  switch (typeForRpc(value)) {
@@ -427,6 +463,7 @@ var RpcPayload = class RpcPayload {
427
463
  case "bytes":
428
464
  case "blob":
429
465
  case "url":
466
+ case "regexp":
430
467
  case "error":
431
468
  case "undefined": return value;
432
469
  case "array": {
@@ -436,6 +473,35 @@ var RpcPayload = class RpcPayload {
436
473
  for (let i = 0; i < len; i++) result[i] = this.deepCopy(array[i], array, i, result, dupStubs, owner);
437
474
  return result;
438
475
  }
476
+ case "set": {
477
+ let set = value;
478
+ let elements = [...set];
479
+ for (let val of elements) {
480
+ let kind = typeForRpc(val);
481
+ if (kind === "rpc-promise" || kind === "rpc-thenable") throw new TypeError("Cannot pass a promise as an element of a Set. Await the value before adding it to the Set.");
482
+ }
483
+ let result = /* @__PURE__ */ new Set();
484
+ let index = 0;
485
+ for (let val of elements) {
486
+ let copy = this.deepCopy(val, set, index++, result, dupStubs, owner);
487
+ result.add(copy);
488
+ }
489
+ return result;
490
+ }
491
+ case "map": {
492
+ let map = value;
493
+ let entries = [...map];
494
+ for (let [key] of entries) {
495
+ let kind = typeForRpc(key);
496
+ if (kind === "rpc-promise" || kind === "rpc-thenable") throw new TypeError("Cannot pass a promise as a key of a Map. Await the value before using it as a key.");
497
+ }
498
+ let result = /* @__PURE__ */ new Map();
499
+ for (let [key, val] of entries) {
500
+ let keyCopy = this.deepCopy(key, map, -1, result, dupStubs, owner);
501
+ result.set(keyCopy, this.deepCopy(val, map, mapKeyToPropertyName(keyCopy), result, dupStubs, owner));
502
+ }
503
+ return result;
504
+ }
439
505
  case "object": {
440
506
  let result = {};
441
507
  let object = value;
@@ -493,8 +559,10 @@ var RpcPayload = class RpcPayload {
493
559
  case "readable": {
494
560
  let stream = value;
495
561
  let hook;
496
- if (owner) hook = owner.getHookForReadableStream(stream, oldParent, dupStubs);
497
- else hook = streamImpl.createReadableStreamHook(stream);
562
+ if (owner) {
563
+ if (owner.source === "owned") throw new Error("Can't copy a ReadableStream.");
564
+ hook = owner.getHookForReadableStream(stream, oldParent, dupStubs);
565
+ } else hook = streamImpl.createReadableStreamHook(stream);
498
566
  this.hooks.push(hook);
499
567
  return stream;
500
568
  }
@@ -520,6 +588,10 @@ var RpcPayload = class RpcPayload {
520
588
  try {
521
589
  this.value = this.deepCopy(this.value, void 0, "value", this, dupStubs, this);
522
590
  } catch (err) {
591
+ if (dupStubs) {
592
+ this.hooks.forEach((hook) => hook.dispose());
593
+ this.promises.forEach((promise) => promise.promise[Symbol.dispose]());
594
+ }
523
595
  this.hooks = void 0;
524
596
  this.promises = void 0;
525
597
  throw err;
@@ -533,7 +605,8 @@ var RpcPayload = class RpcPayload {
533
605
  this.ensureDeepCopied();
534
606
  if (this.value instanceof RpcPromise$1) RpcPayload.deliverRpcPromiseTo(this.value, parent, property, promises);
535
607
  else {
536
- parent[property] = this.value;
608
+ if (parent instanceof Map) parent.set(typeof property === "object" ? property.mapKey : property, this.value);
609
+ else parent[property] = this.value;
537
610
  for (let record of this.promises) RpcPayload.deliverRpcPromiseTo(record.promise, record.parent, record.property, promises);
538
611
  }
539
612
  }
@@ -625,6 +698,7 @@ var RpcPayload = class RpcPayload {
625
698
  case "blob":
626
699
  case "date":
627
700
  case "url":
701
+ case "regexp":
628
702
  case "error":
629
703
  case "undefined": return;
630
704
  case "array": {
@@ -633,6 +707,19 @@ var RpcPayload = class RpcPayload {
633
707
  for (let i = 0; i < len; i++) this.disposeImpl(array[i], array);
634
708
  return;
635
709
  }
710
+ case "set": {
711
+ let set = value;
712
+ for (let element of set) this.disposeImpl(element, set);
713
+ return;
714
+ }
715
+ case "map": {
716
+ let map = value;
717
+ for (let [key, val] of map) {
718
+ this.disposeImpl(key, map);
719
+ this.disposeImpl(val, map);
720
+ }
721
+ return;
722
+ }
636
723
  case "object": {
637
724
  let object = value;
638
725
  for (let i in object) this.disposeImpl(object[i], object);
@@ -708,6 +795,7 @@ var RpcPayload = class RpcPayload {
708
795
  case "writable":
709
796
  case "readable":
710
797
  case "url":
798
+ case "regexp":
711
799
  case "headers":
712
800
  case "request":
713
801
  case "response": return;
@@ -717,6 +805,19 @@ var RpcPayload = class RpcPayload {
717
805
  for (let i = 0; i < len; i++) this.ignoreUnhandledRejectionsImpl(array[i]);
718
806
  return;
719
807
  }
808
+ case "set": {
809
+ let set = value;
810
+ for (let element of set) this.ignoreUnhandledRejectionsImpl(element);
811
+ return;
812
+ }
813
+ case "map": {
814
+ let map = value;
815
+ for (let [key, val] of map) {
816
+ this.ignoreUnhandledRejectionsImpl(key);
817
+ this.ignoreUnhandledRejectionsImpl(val);
818
+ }
819
+ return;
820
+ }
720
821
  case "object": {
721
822
  let object = value;
722
823
  for (let i in object) this.ignoreUnhandledRejectionsImpl(object[i]);
@@ -776,8 +877,11 @@ function followPath(value, parent, path, owner) {
776
877
  case "bytes":
777
878
  case "blob":
778
879
  case "date":
880
+ case "set":
881
+ case "map":
779
882
  case "error":
780
883
  case "url":
884
+ case "regexp":
781
885
  case "headers":
782
886
  case "request":
783
887
  case "response":
@@ -810,17 +914,23 @@ function followPath(value, parent, path, owner) {
810
914
  }
811
915
  var ValueStubHook = class extends StubHook {
812
916
  call(path, args) {
917
+ let followResult;
813
918
  try {
814
919
  let { value, owner } = this.getValue();
815
- let followResult = followPath(value, void 0, path, owner);
816
- if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
817
- if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
818
- return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
819
- return new PayloadStubHook(payload);
820
- }));
920
+ followResult = followPath(value, void 0, path, owner);
821
921
  } catch (err) {
922
+ args.dispose();
822
923
  return new ErrorStubHook(err);
823
924
  }
925
+ if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
926
+ if (typeof followResult.value != "function") {
927
+ args.dispose();
928
+ return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
929
+ }
930
+ let promise = args.deliverCall(followResult.value, followResult.parent);
931
+ return new PromiseStubHook(promise.then((payload) => {
932
+ return new PayloadStubHook(payload);
933
+ }));
824
934
  }
825
935
  map(path, captures, instructions) {
826
936
  try {
@@ -841,7 +951,10 @@ var ValueStubHook = class extends StubHook {
841
951
  get(path) {
842
952
  try {
843
953
  let { value, owner } = this.getValue();
844
- if (path.length === 0 && owner === null) throw new Error("Can't dup an RpcTarget stub as a promise.");
954
+ if (path.length === 0 && owner === null) {
955
+ if (value instanceof Object && "then" in value) return this.dup();
956
+ throw new Error("Can't dup an RpcTarget stub as a promise.");
957
+ }
845
958
  let followResult = followPath(value, void 0, path, owner);
846
959
  if (followResult.hook) return followResult.hook.get(followResult.remainingPath);
847
960
  return new PayloadStubHook(RpcPayload.deepCopyFrom(followResult.value, followResult.parent, followResult.owner));
@@ -944,7 +1057,10 @@ var TargetStubHook = class TargetStubHook extends ValueStubHook {
944
1057
  this.target = void 0;
945
1058
  }
946
1059
  }
947
- onBroken(callback) {}
1060
+ onBroken(callback) {
1061
+ let target = this.target;
1062
+ if (target && "then" in target) Promise.resolve(target).then(() => {}, callback);
1063
+ }
948
1064
  };
949
1065
  var PromiseStubHook = class PromiseStubHook extends StubHook {
950
1066
  promise;
@@ -958,12 +1074,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
958
1074
  }
959
1075
  call(path, args) {
960
1076
  args.ensureDeepCopied();
961
- return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args)));
1077
+ return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
1078
+ args.dispose();
1079
+ throw err;
1080
+ }));
962
1081
  }
963
1082
  stream(path, args) {
964
1083
  args.ensureDeepCopied();
965
- return { promise: this.promise.then((hook) => {
966
- return hook.stream(path, args).promise;
1084
+ return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
1085
+ args.dispose();
1086
+ throw err;
967
1087
  }) };
968
1088
  }
969
1089
  map(path, captures, instructions) {
@@ -990,10 +1110,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
990
1110
  }, (err) => {});
991
1111
  }
992
1112
  dispose() {
993
- if (this.resolution) this.resolution.dispose();
994
- else this.promise.then((hook) => {
995
- hook.dispose();
996
- }, (err) => {});
1113
+ this.promise.then((hook) => hook.dispose(), () => {});
997
1114
  }
998
1115
  onBroken(callback) {
999
1116
  if (this.resolution) this.resolution.onBroken(callback);
@@ -1009,7 +1126,7 @@ const DEFAULT_MAX_DEPTH = 256;
1009
1126
  const DEFAULT_LIMITS = {
1010
1127
  maxBigIntDigits: 16384,
1011
1128
  maxDepth: 256,
1012
- maxMessageSize: 32 * 1024 * 1024
1129
+ maxMessageSize: 33554432
1013
1130
  };
1014
1131
  const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
1015
1132
  const BYTE_CONTAINER_TYPE_NAMES = [
@@ -1071,9 +1188,7 @@ function swapByteOrder(bytes, elementSize) {
1071
1188
  case 4:
1072
1189
  view.setUint32(offset, view.getUint32(offset, false), true);
1073
1190
  break;
1074
- case 8:
1075
- view.setBigUint64(offset, view.getBigUint64(offset, false), true);
1076
- break;
1191
+ case 8: view.setBigUint64(offset, view.getBigUint64(offset, false), true);
1077
1192
  }
1078
1193
  }
1079
1194
  var NullExporter = class {
@@ -1158,6 +1273,30 @@ var Devaluator = class Devaluator {
1158
1273
  for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
1159
1274
  return [result];
1160
1275
  }
1276
+ case "set": {
1277
+ let set = value;
1278
+ let setElements = [...set];
1279
+ let elements = [];
1280
+ for (let element of setElements) switch (typeForRpc(element)) {
1281
+ case "blob": throw new TypeError("Cannot serialize a Blob as an element of a Set.");
1282
+ case "rpc-promise":
1283
+ case "rpc-thenable": throw new TypeError("Cannot serialize a promise as an element of a Set. Await the value before adding it to the Set.");
1284
+ }
1285
+ for (let element of setElements) elements.push(this.devaluateImpl(element, set, depth + 1));
1286
+ return ["set", elements];
1287
+ }
1288
+ case "map": {
1289
+ let map = value;
1290
+ let mapEntries = [...map];
1291
+ let entries = [];
1292
+ for (let [key] of mapEntries) switch (typeForRpc(key)) {
1293
+ case "blob": throw new TypeError("Cannot serialize a Blob as a key of a Map.");
1294
+ case "rpc-promise":
1295
+ case "rpc-thenable": throw new TypeError("Cannot serialize a promise as a key of a Map. Await the value before using it as a key.");
1296
+ }
1297
+ for (let [key, val] of mapEntries) entries.push([this.devaluateImpl(key, map, depth + 1), this.devaluateImpl(val, map, depth + 1)]);
1298
+ return ["map", entries];
1299
+ }
1161
1300
  case "bigint":
1162
1301
  if (this.encodingLevel === "structuredClonable") return value;
1163
1302
  return ["bigint", value.toString()];
@@ -1166,6 +1305,15 @@ var Devaluator = class Devaluator {
1166
1305
  const time = value.getTime();
1167
1306
  return ["date", Number.isNaN(time) ? null : time];
1168
1307
  }
1308
+ case "regexp": {
1309
+ if (this.encodingLevel === "structuredClonable") return value;
1310
+ let re = value;
1311
+ return re.flags ? [
1312
+ "regexp",
1313
+ re.source,
1314
+ re.flags
1315
+ ] : ["regexp", re.source];
1316
+ }
1169
1317
  case "bytes": {
1170
1318
  let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
1171
1319
  let bytes;
@@ -1180,11 +1328,14 @@ var Devaluator = class Devaluator {
1180
1328
  swapByteOrder(bytes, elementSize);
1181
1329
  }
1182
1330
  }
1183
- if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
1184
- "bytes",
1185
- bytes,
1186
- alternateTypeName
1187
- ];
1331
+ if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") {
1332
+ if (bytes.byteLength !== bytes.buffer.byteLength) bytes = new Uint8Array(bytes);
1333
+ return alternateTypeName === void 0 ? ["bytes", bytes] : [
1334
+ "bytes",
1335
+ bytes,
1336
+ alternateTypeName
1337
+ ];
1338
+ }
1188
1339
  let b64;
1189
1340
  if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
1190
1341
  else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
@@ -1323,13 +1474,16 @@ var Devaluator = class Devaluator {
1323
1474
  if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
1324
1475
  let { hook, pathIfPromise } = unwrapStubAndPath(value);
1325
1476
  let importId = this.exporter.getImport(hook);
1326
- if (importId !== void 0) if (pathIfPromise) if (pathIfPromise.length > 0) return [
1327
- "pipeline",
1328
- importId,
1329
- pathIfPromise
1330
- ];
1331
- else return ["pipeline", importId];
1332
- else return ["import", importId];
1477
+ if (importId !== void 0) {
1478
+ if (pathIfPromise) {
1479
+ if (pathIfPromise.length > 0) return [
1480
+ "pipeline",
1481
+ importId,
1482
+ pathIfPromise
1483
+ ];
1484
+ else return ["pipeline", importId];
1485
+ } else return ["import", importId];
1486
+ }
1333
1487
  if (pathIfPromise) hook = hook.get(pathIfPromise);
1334
1488
  else hook = hook.dup();
1335
1489
  return this.devaluateHook(pathIfPromise ? "promise" : "export", hook);
@@ -1390,16 +1544,18 @@ var NullImporter = class {
1390
1544
  };
1391
1545
  const NULL_IMPORTER = new NullImporter();
1392
1546
  function fixBrokenRequestBody(request, body) {
1393
- return new RpcPromise$1(new PromiseStubHook(new Response(body).arrayBuffer().then((arrayBuffer) => {
1547
+ let promise = new Response(body).arrayBuffer().then((arrayBuffer) => {
1394
1548
  let bytes = new Uint8Array(arrayBuffer);
1395
1549
  let result = new Request(request, { body: bytes });
1396
1550
  return new PayloadStubHook(RpcPayload.fromAppReturn(result));
1397
- })), []);
1551
+ });
1552
+ return new RpcPromise$1(new PromiseStubHook(promise), []);
1398
1553
  }
1399
1554
  function streamToBlobPromise(stream, type) {
1400
- return new RpcPromise$1(new PromiseStubHook(streamToBlob(stream, type).then((blob) => {
1555
+ let promise = streamToBlob(stream, type).then((blob) => {
1401
1556
  return new PayloadStubHook(RpcPayload.fromAppReturn(blob));
1402
- })), []);
1557
+ });
1558
+ return new RpcPromise$1(new PromiseStubHook(promise), []);
1403
1559
  }
1404
1560
  var Evaluator = class Evaluator {
1405
1561
  importer;
@@ -1432,7 +1588,7 @@ var Evaluator = class Evaluator {
1432
1588
  let maxDepth = this.limits.maxDepth;
1433
1589
  if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
1434
1590
  if (this.encodingLevel === "structuredClonable") {
1435
- if (value instanceof Date || typeof value === "bigint") return value;
1591
+ if (value instanceof Date || value instanceof RegExp || typeof value === "bigint") return value;
1436
1592
  }
1437
1593
  if (value instanceof Array) {
1438
1594
  if (value.length == 1 && value[0] instanceof Array) {
@@ -1452,18 +1608,48 @@ var Evaluator = class Evaluator {
1452
1608
  if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
1453
1609
  if (typeof value[1] == "number") return new Date(value[1]);
1454
1610
  break;
1611
+ case "regexp":
1612
+ if (typeof value[1] === "string" && (value.length === 2 || value.length === 3 && typeof value[2] === "string")) return new RegExp(value[1], value[2]);
1613
+ break;
1614
+ case "set":
1615
+ if (value.length === 2 && value[1] instanceof Array) {
1616
+ let elements = value[1];
1617
+ let set = /* @__PURE__ */ new Set();
1618
+ for (let i = 0; i < elements.length; i++) {
1619
+ let copy = this.evaluateImpl(elements[i], set, i, depth + 1);
1620
+ if (copy instanceof RpcPromise$1) throw new TypeError("Cannot deserialize a promise as an element of a Set.");
1621
+ set.add(copy);
1622
+ }
1623
+ return set;
1624
+ }
1625
+ break;
1626
+ case "map":
1627
+ if (value.length === 2 && value[1] instanceof Array) {
1628
+ let entries = value[1];
1629
+ let map = /* @__PURE__ */ new Map();
1630
+ for (let i = 0; i < entries.length; i++) {
1631
+ let entry = entries[i];
1632
+ if (!(entry instanceof Array) || entry.length !== 2) throw new TypeError("Map entries must be serialized as key/value pairs.");
1633
+ let key = this.evaluateImpl(entry[0], map, i, depth + 1);
1634
+ if (key instanceof RpcPromise$1) throw new TypeError("Cannot deserialize a promise as a key of a Map.");
1635
+ map.set(key, this.evaluateImpl(entry[1], map, mapKeyToPropertyName(key), depth + 1));
1636
+ }
1637
+ return map;
1638
+ }
1639
+ break;
1455
1640
  case "bytes": {
1456
1641
  let bytes;
1457
1642
  if (value[1] instanceof Uint8Array) bytes = value[1];
1458
- else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
1459
- else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
1460
- else {
1461
- let bs = atob(value[1]);
1462
- let len = bs.length;
1463
- bytes = new Uint8Array(len);
1464
- for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
1465
- }
1466
- else break;
1643
+ else if (typeof value[1] == "string") {
1644
+ if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
1645
+ else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
1646
+ else {
1647
+ let bs = atob(value[1]);
1648
+ let len = bs.length;
1649
+ bytes = new Uint8Array(len);
1650
+ for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
1651
+ }
1652
+ } else break;
1467
1653
  if (value.length === 2) return bytes;
1468
1654
  if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
1469
1655
  if (!isValidByteContainerName(value[2])) {
@@ -1489,7 +1675,6 @@ var Evaluator = class Evaluator {
1489
1675
  case "BigUint64Array": return new BigUint64Array(buffer);
1490
1676
  case "Float32Array": return new Float32Array(buffer);
1491
1677
  case "Float64Array": return new Float64Array(buffer);
1492
- default:
1493
1678
  }
1494
1679
  }
1495
1680
  case "error":
@@ -1592,11 +1777,13 @@ var Evaluator = class Evaluator {
1592
1777
  return promise;
1593
1778
  } else {
1594
1779
  this.hooks.push(hook);
1595
- return new RpcPromise$1(hook, []);
1780
+ return new RpcStub$1(hook);
1596
1781
  }
1597
1782
  };
1598
- if (value.length == 2) if (isPromise) return addStub(hook.get([]));
1599
- else return addStub(hook.dup());
1783
+ if (value.length == 2) {
1784
+ if (isPromise) return addStub(hook.get([]));
1785
+ else return addStub(hook.dup());
1786
+ }
1600
1787
  let path = value[2];
1601
1788
  if (!(path instanceof Array)) break;
1602
1789
  if (!path.every((part) => {
@@ -1626,7 +1813,8 @@ var Evaluator = class Evaluator {
1626
1813
  }
1627
1814
  });
1628
1815
  let instructions = value[4];
1629
- let promise = new RpcPromise$1(hook.map(path, captures, instructions), []);
1816
+ let resultHook = hook.map(path, captures, instructions);
1817
+ let promise = new RpcPromise$1(resultHook, []);
1630
1818
  this.promises.push({
1631
1819
  promise,
1632
1820
  parent,
@@ -1636,18 +1824,21 @@ var Evaluator = class Evaluator {
1636
1824
  }
1637
1825
  case "export":
1638
1826
  case "promise":
1639
- if (typeof value[1] == "number") if (value[0] == "promise") {
1640
- let promise = new RpcPromise$1(this.importer.importPromise(value[1]), []);
1641
- this.promises.push({
1642
- parent,
1643
- property,
1644
- promise
1645
- });
1646
- return promise;
1647
- } else {
1648
- let hook = this.importer.importStub(value[1]);
1649
- this.hooks.push(hook);
1650
- return new RpcStub$1(hook);
1827
+ if (typeof value[1] == "number") {
1828
+ if (value[0] == "promise") {
1829
+ let hook = this.importer.importPromise(value[1]);
1830
+ let promise = new RpcPromise$1(hook, []);
1831
+ this.promises.push({
1832
+ parent,
1833
+ property,
1834
+ promise
1835
+ });
1836
+ return promise;
1837
+ } else {
1838
+ let hook = this.importer.importStub(value[1]);
1839
+ this.hooks.push(hook);
1840
+ return new RpcStub$1(hook);
1841
+ }
1651
1842
  }
1652
1843
  break;
1653
1844
  case "writable":
@@ -1658,14 +1849,12 @@ var Evaluator = class Evaluator {
1658
1849
  return stream;
1659
1850
  }
1660
1851
  break;
1661
- case "readable":
1662
- if (typeof value[1] == "number") {
1663
- let stream = this.importer.getPipeReadable(value[1]);
1664
- let hook = streamImpl.createReadableStreamHook(stream);
1665
- this.hooks.push(hook);
1666
- return stream;
1667
- }
1668
- break;
1852
+ case "readable": if (typeof value[1] == "number") {
1853
+ let stream = this.importer.getPipeReadable(value[1]);
1854
+ let hook = streamImpl.createReadableStreamHook(stream);
1855
+ this.hooks.push(hook);
1856
+ return stream;
1857
+ }
1669
1858
  }
1670
1859
  throw new TypeError(`unknown special value: ${JSON.stringify(value)}`);
1671
1860
  } else if (value instanceof Object) {
@@ -1710,6 +1899,7 @@ function estimateEncodedSize(value, seen, depth = 0) {
1710
1899
  if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
1711
1900
  if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
1712
1901
  if (value instanceof Date) return 16;
1902
+ if (value instanceof RegExp) return ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.source) + estimateStringSize(value.flags);
1713
1903
  seen ??= /* @__PURE__ */ new WeakSet();
1714
1904
  if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
1715
1905
  seen.add(value);
@@ -1733,10 +1923,11 @@ function estimateEncodedSize(value, seen, depth = 0) {
1733
1923
  var ImportTableEntry = class {
1734
1924
  session;
1735
1925
  importId;
1926
+ pulling;
1736
1927
  constructor(session, importId, pulling) {
1737
1928
  this.session = session;
1738
1929
  this.importId = importId;
1739
- if (pulling) this.activePull = Promise.withResolvers();
1930
+ this.pulling = pulling;
1740
1931
  }
1741
1932
  localRefcount = 0;
1742
1933
  remoteRefcount = 1;
@@ -1767,7 +1958,7 @@ var ImportTableEntry = class {
1767
1958
  }
1768
1959
  async awaitResolution() {
1769
1960
  if (!this.activePull) {
1770
- this.session.sendPull(this.importId);
1961
+ if (!this.pulling) this.session.sendPull(this.importId);
1771
1962
  this.activePull = Promise.withResolvers();
1772
1963
  }
1773
1964
  await this.activePull.promise;
@@ -1822,24 +2013,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
1822
2013
  if (this.entry) return this.entry;
1823
2014
  else throw new Error("This RpcImportHook was already disposed.");
1824
2015
  }
2016
+ getEntryTakingOwnership(disposeOwned) {
2017
+ try {
2018
+ return this.getEntry();
2019
+ } catch (err) {
2020
+ disposeOwned();
2021
+ throw err;
2022
+ }
2023
+ }
1825
2024
  call(path, args) {
1826
- let entry = this.getEntry();
2025
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1827
2026
  if (entry.resolution) return entry.resolution.call(path, args);
1828
2027
  else return entry.session.sendCall(entry.importId, path, args);
1829
2028
  }
1830
2029
  stream(path, args) {
1831
- let entry = this.getEntry();
2030
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1832
2031
  if (entry.resolution) return entry.resolution.stream(path, args);
1833
2032
  else return entry.session.sendStream(entry.importId, path, args);
1834
2033
  }
1835
2034
  map(path, captures, instructions) {
1836
- let entry;
1837
- try {
1838
- entry = this.getEntry();
1839
- } catch (err) {
2035
+ let entry = this.getEntryTakingOwnership(() => {
1840
2036
  for (let cap of captures) cap.dispose();
1841
- throw err;
1842
- }
2037
+ });
1843
2038
  if (entry.resolution) return entry.resolution.map(path, captures, instructions);
1844
2039
  else return entry.session.sendMap(entry.importId, path, captures, instructions);
1845
2040
  }
@@ -1993,6 +2188,7 @@ var RpcSessionImpl = class {
1993
2188
  let autoRelease = exp.autoRelease;
1994
2189
  ++this.pullCount;
1995
2190
  exp.pull = resolve().then((payload) => {
2191
+ if (this.exports[exportId] !== exp) return;
1996
2192
  let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
1997
2193
  this.send([
1998
2194
  "resolve",
@@ -2001,6 +2197,7 @@ var RpcSessionImpl = class {
2001
2197
  ]);
2002
2198
  if (autoRelease) this.releaseExport(exportId, 1);
2003
2199
  }, (error) => {
2200
+ if (this.exports[exportId] !== exp) return;
2004
2201
  this.send([
2005
2202
  "reject",
2006
2203
  exportId,
@@ -2026,7 +2223,7 @@ var RpcSessionImpl = class {
2026
2223
  }
2027
2224
  }
2028
2225
  getImport(hook) {
2029
- if (hook instanceof RpcImportHook && hook.entry && hook.entry.session === this) return hook.entry.importId;
2226
+ if (hook instanceof RpcImportHook && hook.entry && hook.entry.session === this && !hook.entry.resolution) return hook.entry.importId;
2030
2227
  else return;
2031
2228
  }
2032
2229
  importStub(idx) {
@@ -2100,32 +2297,52 @@ var RpcSessionImpl = class {
2100
2297
  }
2101
2298
  }
2102
2299
  sendCall(id, path, args) {
2103
- if (this.abortReason) throw this.abortReason;
2300
+ if (this.abortReason) {
2301
+ args?.dispose();
2302
+ throw this.abortReason;
2303
+ }
2104
2304
  let value = [
2105
2305
  "pipeline",
2106
2306
  id,
2107
2307
  path
2108
2308
  ];
2109
2309
  if (args) {
2110
- let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2310
+ let devalue;
2311
+ try {
2312
+ devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2313
+ } catch (err) {
2314
+ args.dispose();
2315
+ throw err;
2316
+ }
2111
2317
  value.push(devalue[0]);
2112
2318
  }
2113
2319
  this.send(["push", value]);
2320
+ args?.dispose();
2114
2321
  let entry = new ImportTableEntry(this, this.imports.length, false);
2115
2322
  this.imports.push(entry);
2116
2323
  return new RpcImportHook(true, entry);
2117
2324
  }
2118
2325
  sendStream(id, path, args) {
2119
- if (this.abortReason) throw this.abortReason;
2326
+ if (this.abortReason) {
2327
+ args.dispose();
2328
+ throw this.abortReason;
2329
+ }
2120
2330
  let value = [
2121
2331
  "pipeline",
2122
2332
  id,
2123
2333
  path
2124
2334
  ];
2125
- let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2335
+ let devalue;
2336
+ try {
2337
+ devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2338
+ } catch (err) {
2339
+ args.dispose();
2340
+ throw err;
2341
+ }
2126
2342
  value.push(devalue[0]);
2127
2343
  let msg = ["stream", value];
2128
2344
  let size = this.send(msg);
2345
+ args.dispose();
2129
2346
  if (size === void 0) size = estimateEncodedSize(msg);
2130
2347
  let importId = this.imports.length;
2131
2348
  let entry = new ImportTableEntry(this, importId, true);
@@ -2223,7 +2440,8 @@ var RpcSessionImpl = class {
2223
2440
  if (msg instanceof Array) switch (msg[0]) {
2224
2441
  case "push":
2225
2442
  if (msg.length > 1) {
2226
- let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
2443
+ let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
2444
+ let hook = new PayloadStubHook(payload);
2227
2445
  hook.ignoreUnhandledRejections();
2228
2446
  this.exports.push({
2229
2447
  hook,
@@ -2234,7 +2452,8 @@ var RpcSessionImpl = class {
2234
2452
  break;
2235
2453
  case "stream":
2236
2454
  if (msg.length > 1) {
2237
- let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
2455
+ let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
2456
+ let hook = new PayloadStubHook(payload);
2238
2457
  hook.ignoreUnhandledRejections();
2239
2458
  let exportId = this.exports.length;
2240
2459
  this.exports.push({
@@ -2269,13 +2488,14 @@ var RpcSessionImpl = class {
2269
2488
  let importId = msg[1];
2270
2489
  if (typeof importId == "number" && msg.length > 2) {
2271
2490
  let imp = this.imports[importId];
2272
- if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
2273
- else {
2274
- let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
2275
- payload.dispose();
2276
- imp.resolve(new ErrorStubHook(payload.value));
2277
- }
2278
- else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
2491
+ if (imp) {
2492
+ if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
2493
+ else {
2494
+ let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
2495
+ payload.dispose();
2496
+ imp.resolve(new ErrorStubHook(payload.value));
2497
+ }
2498
+ } else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
2279
2499
  continue;
2280
2500
  }
2281
2501
  break;
@@ -2347,7 +2567,8 @@ var RpcSession$1 = class {
2347
2567
  const MAX_CLOSE_REASON_BYTES = 123;
2348
2568
  function newWebSocketRpcSession$1(webSocket, localMain, options) {
2349
2569
  if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
2350
- return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
2570
+ let transport = new WebSocketTransport(webSocket);
2571
+ return new RpcSession$1(transport, localMain, options).getRemoteMain();
2351
2572
  }
2352
2573
  /**
2353
2574
  * For use in Cloudflare Workers: Construct an HTTP response that starts a WebSocket RPC session
@@ -2384,12 +2605,13 @@ var WebSocketTransport = class {
2384
2605
  });
2385
2606
  }
2386
2607
  webSocket.addEventListener("message", (event) => {
2387
- if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) if (this.#receiveResolver) {
2388
- this.#receiveResolver(event.data);
2389
- this.#receiveResolver = void 0;
2390
- this.#receiveRejecter = void 0;
2391
- } else this.#receiveQueue.push(event.data);
2392
- else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
2608
+ if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) {
2609
+ if (this.#receiveResolver) {
2610
+ this.#receiveResolver(event.data);
2611
+ this.#receiveResolver = void 0;
2612
+ this.#receiveRejecter = void 0;
2613
+ } else this.#receiveQueue.push(event.data);
2614
+ } else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
2393
2615
  });
2394
2616
  webSocket.addEventListener("close", (event) => {
2395
2617
  this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
@@ -2481,7 +2703,8 @@ function newHttpBatchRpcSession$1(urlOrRequest, options) {
2481
2703
  let body = await response.text();
2482
2704
  return body == "" ? [] : body.split("\n");
2483
2705
  };
2484
- return new RpcSession$1(new BatchClientTransport(sendBatch), void 0, options).getRemoteMain();
2706
+ let transport = new BatchClientTransport(sendBatch);
2707
+ return new RpcSession$1(transport, void 0, options).getRemoteMain();
2485
2708
  }
2486
2709
  var BatchServerTransport = class {
2487
2710
  constructor(batch) {
@@ -2524,7 +2747,8 @@ var BatchServerTransport = class {
2524
2747
  async function newHttpBatchRpcResponse(request, localMain, options) {
2525
2748
  if (request.method !== "POST") return new Response("This endpoint only accepts POST requests.", { status: 405 });
2526
2749
  let body = await request.text();
2527
- let transport = new BatchServerTransport(body === "" ? [] : body.split("\n"));
2750
+ let batch = body === "" ? [] : body.split("\n");
2751
+ let transport = new BatchServerTransport(batch);
2528
2752
  let rpc = new RpcSession$1(transport, localMain, options);
2529
2753
  await transport.whenAllReceived();
2530
2754
  await rpc.drain();
@@ -2554,7 +2778,8 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
2554
2778
  });
2555
2779
  request.on("error", reject);
2556
2780
  });
2557
- let transport = new BatchServerTransport(body === "" ? [] : body.split("\n"));
2781
+ let batch = body === "" ? [] : body.split("\n");
2782
+ let transport = new BatchServerTransport(batch);
2558
2783
  let rpc = new RpcSession$1(transport, localMain, options);
2559
2784
  await transport.whenAllReceived();
2560
2785
  await rpc.drain();
@@ -2565,7 +2790,8 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
2565
2790
  //#endregion
2566
2791
  //#region src/messageport.ts
2567
2792
  function newMessagePortRpcSession$1(port, localMain, options) {
2568
- return new RpcSession$1(new MessagePortTransport(port), localMain, options).getRemoteMain();
2793
+ let transport = new MessagePortTransport(port);
2794
+ return new RpcSession$1(transport, localMain, options).getRemoteMain();
2569
2795
  }
2570
2796
  var MessagePortTransport = class {
2571
2797
  encodingLevel = "structuredClonable";
@@ -2753,9 +2979,11 @@ var MapVariableHook = class extends StubHook {
2753
2979
  else throwMapperBuilderUseError();
2754
2980
  }
2755
2981
  call(path, args) {
2982
+ args.dispose();
2756
2983
  throwMapperBuilderUseError();
2757
2984
  }
2758
2985
  map(path, captures, instructions) {
2986
+ for (let cap of captures) cap.dispose();
2759
2987
  throwMapperBuilderUseError();
2760
2988
  }
2761
2989
  pull() {
@@ -2813,7 +3041,8 @@ var MapApplicator = class {
2813
3041
  }
2814
3042
  };
2815
3043
  function applyMapToElement(input, parent, owner, captures, instructions) {
2816
- let mapper = new MapApplicator(captures, new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner)));
3044
+ let inputHook = new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner));
3045
+ let mapper = new MapApplicator(captures, inputHook);
2817
3046
  try {
2818
3047
  return mapper.apply(instructions);
2819
3048
  } finally {
@@ -2846,9 +3075,10 @@ mapImpl.applyMap = (input, parent, owner, captures, instructions) => {
2846
3075
  var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2847
3076
  state;
2848
3077
  static create(stream) {
3078
+ let writer = stream.getWriter();
2849
3079
  return new WritableStreamStubHook({
2850
3080
  refcount: 1,
2851
- writer: stream.getWriter(),
3081
+ writer,
2852
3082
  closed: false
2853
3083
  });
2854
3084
  }
@@ -2866,13 +3096,12 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2866
3096
  let state = this.getState();
2867
3097
  if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
2868
3098
  const method = path[0];
2869
- if (method !== "write" && method !== "close" && method !== "abort") {
2870
- args.dispose();
2871
- throw new Error(`Unknown WritableStream method: ${method}`);
2872
- }
3099
+ if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
2873
3100
  if (method === "close" || method === "abort") state.closed = true;
2874
- return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
3101
+ let promise = method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer);
3102
+ return new PromiseStubHook(promise.then((payload) => new PayloadStubHook(payload)));
2875
3103
  } catch (err) {
3104
+ args.dispose();
2876
3105
  return new ErrorStubHook(err);
2877
3106
  }
2878
3107
  }
@@ -2884,7 +3113,8 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2884
3113
  return new ErrorStubHook(/* @__PURE__ */ new Error("Cannot access properties on a WritableStream stub"));
2885
3114
  }
2886
3115
  dup() {
2887
- return new WritableStreamStubHook(this.getState(), this);
3116
+ let state = this.getState();
3117
+ return new WritableStreamStubHook(state, this);
2888
3118
  }
2889
3119
  pull() {
2890
3120
  return Promise.reject(/* @__PURE__ */ new Error("Cannot pull a WritableStream stub"));
@@ -2902,9 +3132,9 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2902
3132
  }
2903
3133
  onBroken(callback) {}
2904
3134
  };
2905
- const INITIAL_WINDOW = 256 * 1024;
2906
- const MAX_WINDOW = 1024 * 1024 * 1024;
2907
- const MIN_WINDOW = 64 * 1024;
3135
+ const INITIAL_WINDOW = 262144;
3136
+ const MAX_WINDOW = 1073741824;
3137
+ const MIN_WINDOW = 65536;
2908
3138
  const STARTUP_GROWTH_FACTOR = 2;
2909
3139
  const STEADY_GROWTH_FACTOR = 1.25;
2910
3140
  const DECAY_FACTOR = .9;