capnweb 0.0.0-189fa79 → 0.0.0-25baebf

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.
@@ -1,2 +1,2 @@
1
- export { RpcCompatible, RpcPromise, RpcSession, RpcSessionOptions, RpcStub, RpcTarget, RpcTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize } from './index.cjs';
1
+ export { c as RpcCompatible, e as RpcPromise, f as RpcSession, b as RpcSessionOptions, d as RpcStub, g as RpcTarget, R as RpcTransport, h as deserialize, n as newHttpBatchRpcResponse, i as newHttpBatchRpcSession, j as newMessagePortRpcSession, k as newWebSocketRpcSession, l as newWorkersRpcResponse, m as newWorkersWebSocketRpcResponse, o as nodeHttpBatchRpcResponse, s as serialize } from './index-workers-CiDKhXAE.cjs';
2
2
  import 'node:http';
@@ -1,2 +1,2 @@
1
- export { RpcCompatible, RpcPromise, RpcSession, RpcSessionOptions, RpcStub, RpcTarget, RpcTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize } from './index.js';
1
+ export { c as RpcCompatible, e as RpcPromise, f as RpcSession, b as RpcSessionOptions, d as RpcStub, g as RpcTarget, R as RpcTransport, h as deserialize, n as newHttpBatchRpcResponse, i as newHttpBatchRpcSession, j as newMessagePortRpcSession, k as newWebSocketRpcSession, l as newWorkersRpcResponse, m as newWorkersWebSocketRpcResponse, o as nodeHttpBatchRpcResponse, s as serialize } from './index-workers-CiDKhXAE.js';
2
2
  import 'node:http';
@@ -71,6 +71,8 @@ function typeForRpc(value) {
71
71
  return "request";
72
72
  case Response.prototype:
73
73
  return "response";
74
+ case Blob.prototype:
75
+ return "blob";
74
76
  // TODO: All other structured clone types.
75
77
  case RpcStub.prototype:
76
78
  return "stub";
@@ -541,6 +543,7 @@ var RpcPayload = class _RpcPayload {
541
543
  case "bigint":
542
544
  case "date":
543
545
  case "bytes":
546
+ case "blob":
544
547
  case "error":
545
548
  case "undefined":
546
549
  return value;
@@ -777,6 +780,7 @@ var RpcPayload = class _RpcPayload {
777
780
  case "primitive":
778
781
  case "bigint":
779
782
  case "bytes":
783
+ case "blob":
780
784
  case "date":
781
785
  case "error":
782
786
  case "undefined":
@@ -879,6 +883,7 @@ var RpcPayload = class _RpcPayload {
879
883
  case "primitive":
880
884
  case "bigint":
881
885
  case "bytes":
886
+ case "blob":
882
887
  case "date":
883
888
  case "error":
884
889
  case "undefined":
@@ -970,6 +975,7 @@ function followPath(value, parent, path, owner) {
970
975
  case "primitive":
971
976
  case "bigint":
972
977
  case "bytes":
978
+ case "blob":
973
979
  case "date":
974
980
  case "error":
975
981
  case "headers":
@@ -1296,6 +1302,10 @@ var NullExporter = class {
1296
1302
  }
1297
1303
  };
1298
1304
  var NULL_EXPORTER = new NullExporter();
1305
+ async function streamToBlob(stream, type) {
1306
+ let b = await new Response(stream).blob();
1307
+ return b.type === type ? b : b.slice(0, b.size, type);
1308
+ }
1299
1309
  var ERROR_TYPES = {
1300
1310
  Error,
1301
1311
  EvalError,
@@ -1383,8 +1393,10 @@ var Devaluator = class _Devaluator {
1383
1393
  }
1384
1394
  case "bigint":
1385
1395
  return ["bigint", value.toString()];
1386
- case "date":
1387
- return ["date", value.getTime()];
1396
+ case "date": {
1397
+ const time = value.getTime();
1398
+ return ["date", Number.isNaN(time) ? null : time];
1399
+ }
1388
1400
  case "bytes": {
1389
1401
  let bytes = value;
1390
1402
  if (bytes.toBase64) {
@@ -1470,6 +1482,13 @@ var Devaluator = class _Devaluator {
1470
1482
  }
1471
1483
  return ["response", body, init];
1472
1484
  }
1485
+ case "blob": {
1486
+ let blob = value;
1487
+ let readable = blob.stream();
1488
+ let hook = streamImpl.createReadableStreamHook(readable);
1489
+ let importId = this.exporter.createPipe(readable, hook);
1490
+ return ["blob", blob.type, ["readable", importId]];
1491
+ }
1473
1492
  case "error": {
1474
1493
  let e = value;
1475
1494
  let rewritten = this.exporter.onSendError(e);
@@ -1577,6 +1596,12 @@ function fixBrokenRequestBody(request, body) {
1577
1596
  });
1578
1597
  return new RpcPromise(new PromiseStubHook(promise), []);
1579
1598
  }
1599
+ function streamToBlobPromise(stream, type) {
1600
+ let promise = streamToBlob(stream, type).then((blob) => {
1601
+ return new PayloadStubHook(RpcPayload.fromAppReturn(blob));
1602
+ });
1603
+ return new RpcPromise(new PromiseStubHook(promise), []);
1604
+ }
1580
1605
  var Evaluator = class _Evaluator {
1581
1606
  constructor(importer) {
1582
1607
  this.importer = importer;
@@ -1612,6 +1637,9 @@ var Evaluator = class _Evaluator {
1612
1637
  }
1613
1638
  break;
1614
1639
  case "date":
1640
+ if (value[1] === null) {
1641
+ return /* @__PURE__ */ new Date(NaN);
1642
+ }
1615
1643
  if (typeof value[1] == "number") {
1616
1644
  return new Date(value[1]);
1617
1645
  }
@@ -1705,6 +1733,17 @@ var Evaluator = class _Evaluator {
1705
1733
  }
1706
1734
  return new Response(body, init);
1707
1735
  }
1736
+ case "blob": {
1737
+ if (value.length !== 3 || typeof value[1] !== "string") break;
1738
+ let contentType = value[1];
1739
+ let content = this.evaluateImpl(value[2], parent, property);
1740
+ if (!(content instanceof ReadableStream)) {
1741
+ throw new TypeError("Blob content must be serialized as a ReadableStream.");
1742
+ }
1743
+ let promise = streamToBlobPromise(content, contentType);
1744
+ this.promises.push({ promise, parent, property });
1745
+ return promise;
1746
+ }
1708
1747
  case "import":
1709
1748
  case "pipeline": {
1710
1749
  if (value.length < 2 || value.length > 4) {
@@ -2045,12 +2084,7 @@ var RpcSessionImpl = class {
2045
2084
  this.options = options;
2046
2085
  this.exports.push({ hook: mainHook, refcount: 1 });
2047
2086
  this.imports.push(new ImportTableEntry(this, 0, false));
2048
- let rejectFunc;
2049
- let abortPromise = new Promise((resolve, reject) => {
2050
- rejectFunc = reject;
2051
- });
2052
- this.cancelReadLoop = rejectFunc;
2053
- this.readLoop(abortPromise).catch((err) => this.abort(err));
2087
+ this.readLoop().catch((err) => this.abort(err));
2054
2088
  }
2055
2089
  exports = [];
2056
2090
  reverseExports = /* @__PURE__ */ new Map();
@@ -2334,7 +2368,8 @@ var RpcSessionImpl = class {
2334
2368
  }
2335
2369
  abort(error, trySendAbortMessage = true) {
2336
2370
  if (this.abortReason !== void 0) return;
2337
- this.cancelReadLoop(error);
2371
+ this.cancelReadLoop?.(error);
2372
+ this.cancelReadLoop = void 0;
2338
2373
  if (trySendAbortMessage) {
2339
2374
  try {
2340
2375
  this.transport.send(JSON.stringify(["abort", Devaluator.devaluate(error, void 0, this)])).catch((err) => {
@@ -2370,9 +2405,19 @@ var RpcSessionImpl = class {
2370
2405
  this.exports[i].hook.dispose();
2371
2406
  }
2372
2407
  }
2373
- async readLoop(abortPromise) {
2408
+ async readLoop() {
2374
2409
  while (!this.abortReason) {
2375
- let msg = JSON.parse(await Promise.race([this.transport.receive(), abortPromise]));
2410
+ let readCanceled = Promise.withResolvers();
2411
+ this.cancelReadLoop = readCanceled.reject;
2412
+ let msgText;
2413
+ try {
2414
+ msgText = await Promise.race([this.transport.receive(), readCanceled.promise]);
2415
+ } finally {
2416
+ if (this.cancelReadLoop === readCanceled.reject) {
2417
+ this.cancelReadLoop = void 0;
2418
+ }
2419
+ }
2420
+ let msg = JSON.parse(msgText);
2376
2421
  if (this.abortReason) break;
2377
2422
  if (msg instanceof Array) {
2378
2423
  switch (msg[0]) {