capnweb 0.0.0-409821 → 0.0.0-47df697
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 +7 -0
- package/dist/index-bun.cjs +149 -47
- package/dist/index-bun.cjs.map +1 -1
- package/dist/index-bun.d.cts +82 -6
- package/dist/index-bun.d.ts +82 -6
- package/dist/index-bun.js +149 -48
- package/dist/index-bun.js.map +1 -1
- package/dist/index-workers.cjs +149 -47
- package/dist/index-workers.cjs.map +1 -1
- package/dist/index-workers.d.cts +82 -6
- package/dist/index-workers.d.ts +82 -6
- package/dist/index-workers.js +149 -48
- package/dist/index-workers.js.map +1 -1
- package/dist/index.cjs +149 -47
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +82 -6
- package/dist/index.d.ts +82 -6
- package/dist/index.js +149 -48
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/dist/index-workers.js
CHANGED
|
@@ -967,12 +967,14 @@ const ERROR_TYPES = {
|
|
|
967
967
|
var Devaluator = class Devaluator {
|
|
968
968
|
exporter;
|
|
969
969
|
source;
|
|
970
|
-
|
|
970
|
+
encodingLevel;
|
|
971
|
+
constructor(exporter, source, encodingLevel) {
|
|
971
972
|
this.exporter = exporter;
|
|
972
973
|
this.source = source;
|
|
974
|
+
this.encodingLevel = encodingLevel;
|
|
973
975
|
}
|
|
974
|
-
static devaluate(value, parent, exporter = NULL_EXPORTER, source) {
|
|
975
|
-
let devaluator = new Devaluator(exporter, source);
|
|
976
|
+
static devaluate(value, parent, exporter = NULL_EXPORTER, source, encodingLevel = "string") {
|
|
977
|
+
let devaluator = new Devaluator(exporter, source, encodingLevel);
|
|
976
978
|
try {
|
|
977
979
|
return devaluator.devaluateImpl(value, parent, 0);
|
|
978
980
|
} catch (err) {
|
|
@@ -995,10 +997,12 @@ var Devaluator = class Devaluator {
|
|
|
995
997
|
}
|
|
996
998
|
throw new TypeError(msg);
|
|
997
999
|
}
|
|
998
|
-
case "primitive": if (typeof value === "number" && !isFinite(value))
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1000
|
+
case "primitive": if (typeof value === "number" && !isFinite(value)) {
|
|
1001
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1002
|
+
if (value === Infinity) return ["inf"];
|
|
1003
|
+
else if (value === -Infinity) return ["-inf"];
|
|
1004
|
+
else return ["nan"];
|
|
1005
|
+
} else return value;
|
|
1002
1006
|
case "object": {
|
|
1003
1007
|
let object = value;
|
|
1004
1008
|
let result = {};
|
|
@@ -1012,13 +1016,17 @@ var Devaluator = class Devaluator {
|
|
|
1012
1016
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1013
1017
|
return [result];
|
|
1014
1018
|
}
|
|
1015
|
-
case "bigint":
|
|
1019
|
+
case "bigint":
|
|
1020
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1021
|
+
return ["bigint", value.toString()];
|
|
1016
1022
|
case "date": {
|
|
1023
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1017
1024
|
const time = value.getTime();
|
|
1018
1025
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1019
1026
|
}
|
|
1020
1027
|
case "bytes": {
|
|
1021
1028
|
let bytes = value;
|
|
1029
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return ["bytes", bytes];
|
|
1022
1030
|
if (bytes.toBase64) return ["bytes", bytes.toBase64({ omitPadding: true })];
|
|
1023
1031
|
let b64;
|
|
1024
1032
|
if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
@@ -1143,7 +1151,9 @@ var Devaluator = class Devaluator {
|
|
|
1143
1151
|
} else if (rewritten && rewritten.stack) result.push(rewritten.stack);
|
|
1144
1152
|
return result;
|
|
1145
1153
|
}
|
|
1146
|
-
case "undefined":
|
|
1154
|
+
case "undefined":
|
|
1155
|
+
if (this.encodingLevel === "structuredClonable") return;
|
|
1156
|
+
return ["undefined"];
|
|
1147
1157
|
case "stub":
|
|
1148
1158
|
case "rpc-promise": {
|
|
1149
1159
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
@@ -1226,8 +1236,10 @@ function streamToBlobPromise(stream, type) {
|
|
|
1226
1236
|
}
|
|
1227
1237
|
var Evaluator = class Evaluator {
|
|
1228
1238
|
importer;
|
|
1229
|
-
|
|
1239
|
+
encodingLevel;
|
|
1240
|
+
constructor(importer, encodingLevel = "string") {
|
|
1230
1241
|
this.importer = importer;
|
|
1242
|
+
this.encodingLevel = encodingLevel;
|
|
1231
1243
|
}
|
|
1232
1244
|
hooks = [];
|
|
1233
1245
|
promises = [];
|
|
@@ -1245,6 +1257,9 @@ var Evaluator = class Evaluator {
|
|
|
1245
1257
|
return this.evaluate(structuredClone(value));
|
|
1246
1258
|
}
|
|
1247
1259
|
evaluateImpl(value, parent, property) {
|
|
1260
|
+
if (this.encodingLevel === "structuredClonable") {
|
|
1261
|
+
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1262
|
+
}
|
|
1248
1263
|
if (value instanceof Array) {
|
|
1249
1264
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1250
1265
|
let result = value[0];
|
|
@@ -1259,6 +1274,7 @@ var Evaluator = class Evaluator {
|
|
|
1259
1274
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1260
1275
|
break;
|
|
1261
1276
|
case "bytes":
|
|
1277
|
+
if (value[1] instanceof Uint8Array) return value[1];
|
|
1262
1278
|
if (typeof value[1] == "string") if (typeof Buffer !== "undefined") return Buffer.from(value[1], "base64");
|
|
1263
1279
|
else if (Uint8Array.fromBase64) return Uint8Array.fromBase64(value[1]);
|
|
1264
1280
|
else {
|
|
@@ -1459,6 +1475,47 @@ function deserialize(value) {
|
|
|
1459
1475
|
|
|
1460
1476
|
//#endregion
|
|
1461
1477
|
//#region src/rpc.ts
|
|
1478
|
+
const ESTIMATED_OBJECT_OVERHEAD = 16;
|
|
1479
|
+
const ESTIMATED_ENTRY_OVERHEAD = 8;
|
|
1480
|
+
const ESTIMATED_BINARY_OVERHEAD = 16;
|
|
1481
|
+
const MAX_ESTIMATE_DEPTH = 64;
|
|
1482
|
+
function estimateStringSize(value) {
|
|
1483
|
+
return 2 + value.length * 3;
|
|
1484
|
+
}
|
|
1485
|
+
function estimateEncodedSize(value, seen, depth = 0) {
|
|
1486
|
+
if (depth >= MAX_ESTIMATE_DEPTH) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1487
|
+
switch (typeof value) {
|
|
1488
|
+
case "string": return estimateStringSize(value);
|
|
1489
|
+
case "number": return 16;
|
|
1490
|
+
case "bigint": return 16;
|
|
1491
|
+
case "boolean": return 8;
|
|
1492
|
+
case "undefined": return 16;
|
|
1493
|
+
case "object": {
|
|
1494
|
+
if (value === null) return 8;
|
|
1495
|
+
if (ArrayBuffer.isView(value)) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1496
|
+
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1497
|
+
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1498
|
+
if (value instanceof Date) return 16;
|
|
1499
|
+
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1500
|
+
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1501
|
+
seen.add(value);
|
|
1502
|
+
if (value instanceof Array) {
|
|
1503
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1504
|
+
for (let item of value) size += ESTIMATED_ENTRY_OVERHEAD + estimateEncodedSize(item, seen, depth + 1);
|
|
1505
|
+
return size;
|
|
1506
|
+
}
|
|
1507
|
+
if (value instanceof Error) {
|
|
1508
|
+
let size = ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.name) + estimateStringSize(value.message) + estimateStringSize(value.stack ?? "");
|
|
1509
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1510
|
+
return size;
|
|
1511
|
+
}
|
|
1512
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1513
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1514
|
+
return size;
|
|
1515
|
+
}
|
|
1516
|
+
default: return 16;
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1462
1519
|
var ImportTableEntry = class {
|
|
1463
1520
|
session;
|
|
1464
1521
|
importId;
|
|
@@ -1624,9 +1681,19 @@ var RpcSessionImpl = class {
|
|
|
1624
1681
|
onBatchDone;
|
|
1625
1682
|
pullCount = 0;
|
|
1626
1683
|
onBrokenCallbacks = [];
|
|
1684
|
+
encodingLevel;
|
|
1627
1685
|
constructor(transport, mainHook, options) {
|
|
1628
1686
|
this.transport = transport;
|
|
1629
1687
|
this.options = options;
|
|
1688
|
+
let level = "string";
|
|
1689
|
+
if ("encodingLevel" in transport) {
|
|
1690
|
+
let raw = transport.encodingLevel;
|
|
1691
|
+
if (raw !== void 0) {
|
|
1692
|
+
if (raw !== "string" && raw !== "jsonCompatible" && raw !== "jsonCompatibleWithBytes" && raw !== "structuredClonable") throw new TypeError(`Unknown transport encodingLevel: ${String(raw)}`);
|
|
1693
|
+
level = raw;
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
this.encodingLevel = level;
|
|
1630
1697
|
this.exports.push({
|
|
1631
1698
|
hook: mainHook,
|
|
1632
1699
|
refcount: 1
|
|
@@ -1707,7 +1774,7 @@ var RpcSessionImpl = class {
|
|
|
1707
1774
|
let autoRelease = exp.autoRelease;
|
|
1708
1775
|
++this.pullCount;
|
|
1709
1776
|
exp.pull = resolve().then((payload) => {
|
|
1710
|
-
let value = Devaluator.devaluate(payload.value, void 0, this, payload);
|
|
1777
|
+
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
1711
1778
|
this.send([
|
|
1712
1779
|
"resolve",
|
|
1713
1780
|
exportId,
|
|
@@ -1718,7 +1785,7 @@ var RpcSessionImpl = class {
|
|
|
1718
1785
|
this.send([
|
|
1719
1786
|
"reject",
|
|
1720
1787
|
exportId,
|
|
1721
|
-
Devaluator.devaluate(error, void 0, this)
|
|
1788
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1722
1789
|
]);
|
|
1723
1790
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1724
1791
|
}).catch((error) => {
|
|
@@ -1726,7 +1793,7 @@ var RpcSessionImpl = class {
|
|
|
1726
1793
|
this.send([
|
|
1727
1794
|
"reject",
|
|
1728
1795
|
exportId,
|
|
1729
|
-
Devaluator.devaluate(error, void 0, this)
|
|
1796
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1730
1797
|
]);
|
|
1731
1798
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1732
1799
|
} catch (error2) {
|
|
@@ -1782,17 +1849,33 @@ var RpcSessionImpl = class {
|
|
|
1782
1849
|
}
|
|
1783
1850
|
send(msg) {
|
|
1784
1851
|
if (this.abortReason !== void 0) return 0;
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
msgText = JSON.stringify(msg);
|
|
1788
|
-
} catch (err) {
|
|
1852
|
+
if (this.encodingLevel === "string") {
|
|
1853
|
+
let msgText;
|
|
1789
1854
|
try {
|
|
1790
|
-
|
|
1791
|
-
} catch (
|
|
1792
|
-
|
|
1855
|
+
msgText = JSON.stringify(msg);
|
|
1856
|
+
} catch (err) {
|
|
1857
|
+
try {
|
|
1858
|
+
this.abort(err);
|
|
1859
|
+
} catch (err2) {}
|
|
1860
|
+
throw err;
|
|
1861
|
+
}
|
|
1862
|
+
try {
|
|
1863
|
+
let sent = this.transport.send(msgText);
|
|
1864
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => this.abort(err, false));
|
|
1865
|
+
} catch (err) {
|
|
1866
|
+
queueMicrotask(() => this.abort(err, false));
|
|
1867
|
+
}
|
|
1868
|
+
return msgText.length;
|
|
1869
|
+
} else try {
|
|
1870
|
+
let size = this.transport.send(msg);
|
|
1871
|
+
if (typeof size === "number") return size;
|
|
1872
|
+
let thenable = size;
|
|
1873
|
+
if (thenable && typeof thenable.then === "function") Promise.resolve(thenable).catch((err) => this.abort(err, false));
|
|
1874
|
+
return;
|
|
1875
|
+
} catch (err) {
|
|
1876
|
+
queueMicrotask(() => this.abort(err, false));
|
|
1877
|
+
return;
|
|
1793
1878
|
}
|
|
1794
|
-
this.transport.send(msgText).catch((err) => this.abort(err, false));
|
|
1795
|
-
return msgText.length;
|
|
1796
1879
|
}
|
|
1797
1880
|
sendCall(id, path, args) {
|
|
1798
1881
|
if (this.abortReason) throw this.abortReason;
|
|
@@ -1802,7 +1885,7 @@ var RpcSessionImpl = class {
|
|
|
1802
1885
|
path
|
|
1803
1886
|
];
|
|
1804
1887
|
if (args) {
|
|
1805
|
-
let devalue = Devaluator.devaluate(args.value, void 0, this, args);
|
|
1888
|
+
let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
1806
1889
|
value.push(devalue[0]);
|
|
1807
1890
|
}
|
|
1808
1891
|
this.send(["push", value]);
|
|
@@ -1817,9 +1900,11 @@ var RpcSessionImpl = class {
|
|
|
1817
1900
|
id,
|
|
1818
1901
|
path
|
|
1819
1902
|
];
|
|
1820
|
-
let devalue = Devaluator.devaluate(args.value, void 0, this, args);
|
|
1903
|
+
let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
1821
1904
|
value.push(devalue[0]);
|
|
1822
|
-
let
|
|
1905
|
+
let msg = ["stream", value];
|
|
1906
|
+
let size = this.send(msg);
|
|
1907
|
+
if (size === void 0) size = estimateEncodedSize(msg);
|
|
1823
1908
|
let importId = this.imports.length;
|
|
1824
1909
|
let entry = new ImportTableEntry(this, importId, true);
|
|
1825
1910
|
entry.remoteRefcount = 0;
|
|
@@ -1875,7 +1960,14 @@ var RpcSessionImpl = class {
|
|
|
1875
1960
|
this.cancelReadLoop?.(error);
|
|
1876
1961
|
this.cancelReadLoop = void 0;
|
|
1877
1962
|
if (trySendAbortMessage) try {
|
|
1878
|
-
|
|
1963
|
+
let abortMsg = ["abort", Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)];
|
|
1964
|
+
if (this.encodingLevel === "string") {
|
|
1965
|
+
let sent = this.transport.send(JSON.stringify(abortMsg));
|
|
1966
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => {});
|
|
1967
|
+
} else {
|
|
1968
|
+
let result = this.transport.send(abortMsg);
|
|
1969
|
+
if (result && typeof result.then === "function") Promise.resolve(result).catch((err) => {});
|
|
1970
|
+
}
|
|
1879
1971
|
} catch (err) {}
|
|
1880
1972
|
if (error === void 0) error = "undefined";
|
|
1881
1973
|
this.abortReason = error;
|
|
@@ -1897,18 +1989,18 @@ var RpcSessionImpl = class {
|
|
|
1897
1989
|
while (!this.abortReason) {
|
|
1898
1990
|
let readCanceled = Promise.withResolvers();
|
|
1899
1991
|
this.cancelReadLoop = readCanceled.reject;
|
|
1900
|
-
let
|
|
1992
|
+
let raw;
|
|
1901
1993
|
try {
|
|
1902
|
-
|
|
1994
|
+
raw = await Promise.race([this.transport.receive(), readCanceled.promise]);
|
|
1903
1995
|
} finally {
|
|
1904
1996
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1905
1997
|
}
|
|
1906
|
-
let msg = JSON.parse(msgText);
|
|
1907
1998
|
if (this.abortReason) break;
|
|
1999
|
+
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
1908
2000
|
if (msg instanceof Array) switch (msg[0]) {
|
|
1909
2001
|
case "push":
|
|
1910
2002
|
if (msg.length > 1) {
|
|
1911
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2003
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1912
2004
|
hook.ignoreUnhandledRejections();
|
|
1913
2005
|
this.exports.push({
|
|
1914
2006
|
hook,
|
|
@@ -1919,7 +2011,7 @@ var RpcSessionImpl = class {
|
|
|
1919
2011
|
break;
|
|
1920
2012
|
case "stream":
|
|
1921
2013
|
if (msg.length > 1) {
|
|
1922
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2014
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1923
2015
|
hook.ignoreUnhandledRejections();
|
|
1924
2016
|
let exportId = this.exports.length;
|
|
1925
2017
|
this.exports.push({
|
|
@@ -1954,13 +2046,13 @@ var RpcSessionImpl = class {
|
|
|
1954
2046
|
let importId = msg[1];
|
|
1955
2047
|
if (typeof importId == "number" && msg.length > 2) {
|
|
1956
2048
|
let imp = this.imports[importId];
|
|
1957
|
-
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this).evaluate(msg[2])));
|
|
2049
|
+
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
1958
2050
|
else {
|
|
1959
|
-
let payload = new Evaluator(this).evaluate(msg[2]);
|
|
2051
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
1960
2052
|
payload.dispose();
|
|
1961
2053
|
imp.resolve(new ErrorStubHook(payload.value));
|
|
1962
2054
|
}
|
|
1963
|
-
else if (msg[0] == "resolve") new Evaluator(this).evaluate(msg[2]).dispose();
|
|
2055
|
+
else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
1964
2056
|
continue;
|
|
1965
2057
|
}
|
|
1966
2058
|
break;
|
|
@@ -1975,7 +2067,7 @@ var RpcSessionImpl = class {
|
|
|
1975
2067
|
break;
|
|
1976
2068
|
}
|
|
1977
2069
|
case "abort": {
|
|
1978
|
-
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
2070
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
1979
2071
|
payload.dispose();
|
|
1980
2072
|
this.abort(payload, false);
|
|
1981
2073
|
break;
|
|
@@ -2047,9 +2139,14 @@ function newWorkersWebSocketRpcResponse(request, localMain, options) {
|
|
|
2047
2139
|
webSocket: pair[1]
|
|
2048
2140
|
});
|
|
2049
2141
|
}
|
|
2142
|
+
/**
|
|
2143
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
2144
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
2145
|
+
*/
|
|
2050
2146
|
var WebSocketTransport = class {
|
|
2051
2147
|
constructor(webSocket) {
|
|
2052
2148
|
this.#webSocket = webSocket;
|
|
2149
|
+
webSocket.binaryType = "arraybuffer";
|
|
2053
2150
|
if (webSocket.readyState === WebSocket.CONNECTING) {
|
|
2054
2151
|
this.#sendQueue = [];
|
|
2055
2152
|
webSocket.addEventListener("open", (event) => {
|
|
@@ -2062,12 +2159,12 @@ var WebSocketTransport = class {
|
|
|
2062
2159
|
});
|
|
2063
2160
|
}
|
|
2064
2161
|
webSocket.addEventListener("message", (event) => {
|
|
2065
|
-
if (this.#error) {} else if (typeof event.data === "string") if (this.#receiveResolver) {
|
|
2162
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) if (this.#receiveResolver) {
|
|
2066
2163
|
this.#receiveResolver(event.data);
|
|
2067
2164
|
this.#receiveResolver = void 0;
|
|
2068
2165
|
this.#receiveRejecter = void 0;
|
|
2069
2166
|
} else this.#receiveQueue.push(event.data);
|
|
2070
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received
|
|
2167
|
+
else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2071
2168
|
});
|
|
2072
2169
|
webSocket.addEventListener("close", (event) => {
|
|
2073
2170
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2082,13 +2179,13 @@ var WebSocketTransport = class {
|
|
|
2082
2179
|
#receiveRejecter;
|
|
2083
2180
|
#receiveQueue = [];
|
|
2084
2181
|
#error;
|
|
2085
|
-
|
|
2182
|
+
send(message) {
|
|
2086
2183
|
if (this.#sendQueue === void 0) this.#webSocket.send(message);
|
|
2087
2184
|
else this.#sendQueue.push(message);
|
|
2088
2185
|
}
|
|
2089
|
-
|
|
2090
|
-
if (this.#receiveQueue.length > 0) return this.#receiveQueue.shift();
|
|
2091
|
-
else if (this.#error)
|
|
2186
|
+
receive() {
|
|
2187
|
+
if (this.#receiveQueue.length > 0) return Promise.resolve(this.#receiveQueue.shift());
|
|
2188
|
+
else if (this.#error) return Promise.reject(this.#error);
|
|
2092
2189
|
else return new Promise((resolve, reject) => {
|
|
2093
2190
|
this.#receiveResolver = resolve;
|
|
2094
2191
|
this.#receiveRejecter = reject;
|
|
@@ -2123,7 +2220,7 @@ var BatchClientTransport = class {
|
|
|
2123
2220
|
#aborted;
|
|
2124
2221
|
#batchToSend = [];
|
|
2125
2222
|
#batchToReceive = null;
|
|
2126
|
-
|
|
2223
|
+
send(message) {
|
|
2127
2224
|
if (this.#batchToSend !== null) this.#batchToSend.push(message);
|
|
2128
2225
|
}
|
|
2129
2226
|
async receive() {
|
|
@@ -2165,7 +2262,7 @@ var BatchServerTransport = class {
|
|
|
2165
2262
|
#batchToSend = [];
|
|
2166
2263
|
#batchToReceive;
|
|
2167
2264
|
#allReceived = Promise.withResolvers();
|
|
2168
|
-
|
|
2265
|
+
send(message) {
|
|
2169
2266
|
this.#batchToSend.push(message);
|
|
2170
2267
|
}
|
|
2171
2268
|
async receive() {
|
|
@@ -2214,7 +2311,11 @@ async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
|
2214
2311
|
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
2215
2312
|
*/
|
|
2216
2313
|
async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
2217
|
-
if (request.method !== "POST")
|
|
2314
|
+
if (request.method !== "POST") {
|
|
2315
|
+
response.writeHead(405, "This endpoint only accepts POST requests.", options?.headers);
|
|
2316
|
+
response.end();
|
|
2317
|
+
return;
|
|
2318
|
+
}
|
|
2218
2319
|
let body = await new Promise((resolve, reject) => {
|
|
2219
2320
|
let chunks = [];
|
|
2220
2321
|
request.on("data", (chunk) => {
|
|
@@ -2239,17 +2340,17 @@ function newMessagePortRpcSession$1(port, localMain, options) {
|
|
|
2239
2340
|
return new RpcSession$1(new MessagePortTransport(port), localMain, options).getRemoteMain();
|
|
2240
2341
|
}
|
|
2241
2342
|
var MessagePortTransport = class {
|
|
2343
|
+
encodingLevel = "structuredClonable";
|
|
2242
2344
|
constructor(port) {
|
|
2243
2345
|
this.#port = port;
|
|
2244
2346
|
port.start();
|
|
2245
2347
|
port.addEventListener("message", (event) => {
|
|
2246
2348
|
if (this.#error) {} else if (event.data === null) this.#receivedError(/* @__PURE__ */ new Error("Peer closed MessagePort connection."));
|
|
2247
|
-
else if (
|
|
2349
|
+
else if (this.#receiveResolver) {
|
|
2248
2350
|
this.#receiveResolver(event.data);
|
|
2249
2351
|
this.#receiveResolver = void 0;
|
|
2250
2352
|
this.#receiveRejecter = void 0;
|
|
2251
2353
|
} else this.#receiveQueue.push(event.data);
|
|
2252
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received non-string message from MessagePort."));
|
|
2253
2354
|
});
|
|
2254
2355
|
port.addEventListener("messageerror", (event) => {
|
|
2255
2356
|
this.#receivedError(/* @__PURE__ */ new Error("MessagePort message error."));
|
|
@@ -2260,7 +2361,7 @@ var MessagePortTransport = class {
|
|
|
2260
2361
|
#receiveRejecter;
|
|
2261
2362
|
#receiveQueue = [];
|
|
2262
2363
|
#error;
|
|
2263
|
-
|
|
2364
|
+
send(message) {
|
|
2264
2365
|
if (this.#error) throw this.#error;
|
|
2265
2366
|
this.#port.postMessage(message);
|
|
2266
2367
|
}
|
|
@@ -2827,5 +2928,5 @@ async function newWorkersRpcResponse(request, localMain) {
|
|
|
2827
2928
|
}
|
|
2828
2929
|
|
|
2829
2930
|
//#endregion
|
|
2830
|
-
export { RpcPromise, RpcSession, RpcStub, RpcTarget, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
2931
|
+
export { RpcPromise, RpcSession, RpcStub, RpcTarget, WebSocketTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
2831
2932
|
//# sourceMappingURL=index-workers.js.map
|