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.js
CHANGED
|
@@ -961,12 +961,14 @@ const ERROR_TYPES = {
|
|
|
961
961
|
var Devaluator = class Devaluator {
|
|
962
962
|
exporter;
|
|
963
963
|
source;
|
|
964
|
-
|
|
964
|
+
encodingLevel;
|
|
965
|
+
constructor(exporter, source, encodingLevel) {
|
|
965
966
|
this.exporter = exporter;
|
|
966
967
|
this.source = source;
|
|
968
|
+
this.encodingLevel = encodingLevel;
|
|
967
969
|
}
|
|
968
|
-
static devaluate(value, parent, exporter = NULL_EXPORTER, source) {
|
|
969
|
-
let devaluator = new Devaluator(exporter, source);
|
|
970
|
+
static devaluate(value, parent, exporter = NULL_EXPORTER, source, encodingLevel = "string") {
|
|
971
|
+
let devaluator = new Devaluator(exporter, source, encodingLevel);
|
|
970
972
|
try {
|
|
971
973
|
return devaluator.devaluateImpl(value, parent, 0);
|
|
972
974
|
} catch (err) {
|
|
@@ -989,10 +991,12 @@ var Devaluator = class Devaluator {
|
|
|
989
991
|
}
|
|
990
992
|
throw new TypeError(msg);
|
|
991
993
|
}
|
|
992
|
-
case "primitive": if (typeof value === "number" && !isFinite(value))
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
994
|
+
case "primitive": if (typeof value === "number" && !isFinite(value)) {
|
|
995
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
996
|
+
if (value === Infinity) return ["inf"];
|
|
997
|
+
else if (value === -Infinity) return ["-inf"];
|
|
998
|
+
else return ["nan"];
|
|
999
|
+
} else return value;
|
|
996
1000
|
case "object": {
|
|
997
1001
|
let object = value;
|
|
998
1002
|
let result = {};
|
|
@@ -1006,13 +1010,17 @@ var Devaluator = class Devaluator {
|
|
|
1006
1010
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1007
1011
|
return [result];
|
|
1008
1012
|
}
|
|
1009
|
-
case "bigint":
|
|
1013
|
+
case "bigint":
|
|
1014
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1015
|
+
return ["bigint", value.toString()];
|
|
1010
1016
|
case "date": {
|
|
1017
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1011
1018
|
const time = value.getTime();
|
|
1012
1019
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1013
1020
|
}
|
|
1014
1021
|
case "bytes": {
|
|
1015
1022
|
let bytes = value;
|
|
1023
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return ["bytes", bytes];
|
|
1016
1024
|
if (bytes.toBase64) return ["bytes", bytes.toBase64({ omitPadding: true })];
|
|
1017
1025
|
let b64;
|
|
1018
1026
|
if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
@@ -1137,7 +1145,9 @@ var Devaluator = class Devaluator {
|
|
|
1137
1145
|
} else if (rewritten && rewritten.stack) result.push(rewritten.stack);
|
|
1138
1146
|
return result;
|
|
1139
1147
|
}
|
|
1140
|
-
case "undefined":
|
|
1148
|
+
case "undefined":
|
|
1149
|
+
if (this.encodingLevel === "structuredClonable") return;
|
|
1150
|
+
return ["undefined"];
|
|
1141
1151
|
case "stub":
|
|
1142
1152
|
case "rpc-promise": {
|
|
1143
1153
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
@@ -1220,8 +1230,10 @@ function streamToBlobPromise(stream, type) {
|
|
|
1220
1230
|
}
|
|
1221
1231
|
var Evaluator = class Evaluator {
|
|
1222
1232
|
importer;
|
|
1223
|
-
|
|
1233
|
+
encodingLevel;
|
|
1234
|
+
constructor(importer, encodingLevel = "string") {
|
|
1224
1235
|
this.importer = importer;
|
|
1236
|
+
this.encodingLevel = encodingLevel;
|
|
1225
1237
|
}
|
|
1226
1238
|
hooks = [];
|
|
1227
1239
|
promises = [];
|
|
@@ -1239,6 +1251,9 @@ var Evaluator = class Evaluator {
|
|
|
1239
1251
|
return this.evaluate(structuredClone(value));
|
|
1240
1252
|
}
|
|
1241
1253
|
evaluateImpl(value, parent, property) {
|
|
1254
|
+
if (this.encodingLevel === "structuredClonable") {
|
|
1255
|
+
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1256
|
+
}
|
|
1242
1257
|
if (value instanceof Array) {
|
|
1243
1258
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1244
1259
|
let result = value[0];
|
|
@@ -1253,6 +1268,7 @@ var Evaluator = class Evaluator {
|
|
|
1253
1268
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1254
1269
|
break;
|
|
1255
1270
|
case "bytes":
|
|
1271
|
+
if (value[1] instanceof Uint8Array) return value[1];
|
|
1256
1272
|
if (typeof value[1] == "string") if (typeof Buffer !== "undefined") return Buffer.from(value[1], "base64");
|
|
1257
1273
|
else if (Uint8Array.fromBase64) return Uint8Array.fromBase64(value[1]);
|
|
1258
1274
|
else {
|
|
@@ -1453,6 +1469,47 @@ function deserialize(value) {
|
|
|
1453
1469
|
|
|
1454
1470
|
//#endregion
|
|
1455
1471
|
//#region src/rpc.ts
|
|
1472
|
+
const ESTIMATED_OBJECT_OVERHEAD = 16;
|
|
1473
|
+
const ESTIMATED_ENTRY_OVERHEAD = 8;
|
|
1474
|
+
const ESTIMATED_BINARY_OVERHEAD = 16;
|
|
1475
|
+
const MAX_ESTIMATE_DEPTH = 64;
|
|
1476
|
+
function estimateStringSize(value) {
|
|
1477
|
+
return 2 + value.length * 3;
|
|
1478
|
+
}
|
|
1479
|
+
function estimateEncodedSize(value, seen, depth = 0) {
|
|
1480
|
+
if (depth >= MAX_ESTIMATE_DEPTH) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1481
|
+
switch (typeof value) {
|
|
1482
|
+
case "string": return estimateStringSize(value);
|
|
1483
|
+
case "number": return 16;
|
|
1484
|
+
case "bigint": return 16;
|
|
1485
|
+
case "boolean": return 8;
|
|
1486
|
+
case "undefined": return 16;
|
|
1487
|
+
case "object": {
|
|
1488
|
+
if (value === null) return 8;
|
|
1489
|
+
if (ArrayBuffer.isView(value)) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1490
|
+
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1491
|
+
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1492
|
+
if (value instanceof Date) return 16;
|
|
1493
|
+
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1494
|
+
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1495
|
+
seen.add(value);
|
|
1496
|
+
if (value instanceof Array) {
|
|
1497
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1498
|
+
for (let item of value) size += ESTIMATED_ENTRY_OVERHEAD + estimateEncodedSize(item, seen, depth + 1);
|
|
1499
|
+
return size;
|
|
1500
|
+
}
|
|
1501
|
+
if (value instanceof Error) {
|
|
1502
|
+
let size = ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.name) + estimateStringSize(value.message) + estimateStringSize(value.stack ?? "");
|
|
1503
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1504
|
+
return size;
|
|
1505
|
+
}
|
|
1506
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1507
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1508
|
+
return size;
|
|
1509
|
+
}
|
|
1510
|
+
default: return 16;
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1456
1513
|
var ImportTableEntry = class {
|
|
1457
1514
|
session;
|
|
1458
1515
|
importId;
|
|
@@ -1618,9 +1675,19 @@ var RpcSessionImpl = class {
|
|
|
1618
1675
|
onBatchDone;
|
|
1619
1676
|
pullCount = 0;
|
|
1620
1677
|
onBrokenCallbacks = [];
|
|
1678
|
+
encodingLevel;
|
|
1621
1679
|
constructor(transport, mainHook, options) {
|
|
1622
1680
|
this.transport = transport;
|
|
1623
1681
|
this.options = options;
|
|
1682
|
+
let level = "string";
|
|
1683
|
+
if ("encodingLevel" in transport) {
|
|
1684
|
+
let raw = transport.encodingLevel;
|
|
1685
|
+
if (raw !== void 0) {
|
|
1686
|
+
if (raw !== "string" && raw !== "jsonCompatible" && raw !== "jsonCompatibleWithBytes" && raw !== "structuredClonable") throw new TypeError(`Unknown transport encodingLevel: ${String(raw)}`);
|
|
1687
|
+
level = raw;
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
this.encodingLevel = level;
|
|
1624
1691
|
this.exports.push({
|
|
1625
1692
|
hook: mainHook,
|
|
1626
1693
|
refcount: 1
|
|
@@ -1701,7 +1768,7 @@ var RpcSessionImpl = class {
|
|
|
1701
1768
|
let autoRelease = exp.autoRelease;
|
|
1702
1769
|
++this.pullCount;
|
|
1703
1770
|
exp.pull = resolve().then((payload) => {
|
|
1704
|
-
let value = Devaluator.devaluate(payload.value, void 0, this, payload);
|
|
1771
|
+
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
1705
1772
|
this.send([
|
|
1706
1773
|
"resolve",
|
|
1707
1774
|
exportId,
|
|
@@ -1712,7 +1779,7 @@ var RpcSessionImpl = class {
|
|
|
1712
1779
|
this.send([
|
|
1713
1780
|
"reject",
|
|
1714
1781
|
exportId,
|
|
1715
|
-
Devaluator.devaluate(error, void 0, this)
|
|
1782
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1716
1783
|
]);
|
|
1717
1784
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1718
1785
|
}).catch((error) => {
|
|
@@ -1720,7 +1787,7 @@ var RpcSessionImpl = class {
|
|
|
1720
1787
|
this.send([
|
|
1721
1788
|
"reject",
|
|
1722
1789
|
exportId,
|
|
1723
|
-
Devaluator.devaluate(error, void 0, this)
|
|
1790
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1724
1791
|
]);
|
|
1725
1792
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1726
1793
|
} catch (error2) {
|
|
@@ -1776,17 +1843,33 @@ var RpcSessionImpl = class {
|
|
|
1776
1843
|
}
|
|
1777
1844
|
send(msg) {
|
|
1778
1845
|
if (this.abortReason !== void 0) return 0;
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
msgText = JSON.stringify(msg);
|
|
1782
|
-
} catch (err) {
|
|
1846
|
+
if (this.encodingLevel === "string") {
|
|
1847
|
+
let msgText;
|
|
1783
1848
|
try {
|
|
1784
|
-
|
|
1785
|
-
} catch (
|
|
1786
|
-
|
|
1849
|
+
msgText = JSON.stringify(msg);
|
|
1850
|
+
} catch (err) {
|
|
1851
|
+
try {
|
|
1852
|
+
this.abort(err);
|
|
1853
|
+
} catch (err2) {}
|
|
1854
|
+
throw err;
|
|
1855
|
+
}
|
|
1856
|
+
try {
|
|
1857
|
+
let sent = this.transport.send(msgText);
|
|
1858
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => this.abort(err, false));
|
|
1859
|
+
} catch (err) {
|
|
1860
|
+
queueMicrotask(() => this.abort(err, false));
|
|
1861
|
+
}
|
|
1862
|
+
return msgText.length;
|
|
1863
|
+
} else try {
|
|
1864
|
+
let size = this.transport.send(msg);
|
|
1865
|
+
if (typeof size === "number") return size;
|
|
1866
|
+
let thenable = size;
|
|
1867
|
+
if (thenable && typeof thenable.then === "function") Promise.resolve(thenable).catch((err) => this.abort(err, false));
|
|
1868
|
+
return;
|
|
1869
|
+
} catch (err) {
|
|
1870
|
+
queueMicrotask(() => this.abort(err, false));
|
|
1871
|
+
return;
|
|
1787
1872
|
}
|
|
1788
|
-
this.transport.send(msgText).catch((err) => this.abort(err, false));
|
|
1789
|
-
return msgText.length;
|
|
1790
1873
|
}
|
|
1791
1874
|
sendCall(id, path, args) {
|
|
1792
1875
|
if (this.abortReason) throw this.abortReason;
|
|
@@ -1796,7 +1879,7 @@ var RpcSessionImpl = class {
|
|
|
1796
1879
|
path
|
|
1797
1880
|
];
|
|
1798
1881
|
if (args) {
|
|
1799
|
-
let devalue = Devaluator.devaluate(args.value, void 0, this, args);
|
|
1882
|
+
let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
1800
1883
|
value.push(devalue[0]);
|
|
1801
1884
|
}
|
|
1802
1885
|
this.send(["push", value]);
|
|
@@ -1811,9 +1894,11 @@ var RpcSessionImpl = class {
|
|
|
1811
1894
|
id,
|
|
1812
1895
|
path
|
|
1813
1896
|
];
|
|
1814
|
-
let devalue = Devaluator.devaluate(args.value, void 0, this, args);
|
|
1897
|
+
let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
1815
1898
|
value.push(devalue[0]);
|
|
1816
|
-
let
|
|
1899
|
+
let msg = ["stream", value];
|
|
1900
|
+
let size = this.send(msg);
|
|
1901
|
+
if (size === void 0) size = estimateEncodedSize(msg);
|
|
1817
1902
|
let importId = this.imports.length;
|
|
1818
1903
|
let entry = new ImportTableEntry(this, importId, true);
|
|
1819
1904
|
entry.remoteRefcount = 0;
|
|
@@ -1869,7 +1954,14 @@ var RpcSessionImpl = class {
|
|
|
1869
1954
|
this.cancelReadLoop?.(error);
|
|
1870
1955
|
this.cancelReadLoop = void 0;
|
|
1871
1956
|
if (trySendAbortMessage) try {
|
|
1872
|
-
|
|
1957
|
+
let abortMsg = ["abort", Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)];
|
|
1958
|
+
if (this.encodingLevel === "string") {
|
|
1959
|
+
let sent = this.transport.send(JSON.stringify(abortMsg));
|
|
1960
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => {});
|
|
1961
|
+
} else {
|
|
1962
|
+
let result = this.transport.send(abortMsg);
|
|
1963
|
+
if (result && typeof result.then === "function") Promise.resolve(result).catch((err) => {});
|
|
1964
|
+
}
|
|
1873
1965
|
} catch (err) {}
|
|
1874
1966
|
if (error === void 0) error = "undefined";
|
|
1875
1967
|
this.abortReason = error;
|
|
@@ -1891,18 +1983,18 @@ var RpcSessionImpl = class {
|
|
|
1891
1983
|
while (!this.abortReason) {
|
|
1892
1984
|
let readCanceled = Promise.withResolvers();
|
|
1893
1985
|
this.cancelReadLoop = readCanceled.reject;
|
|
1894
|
-
let
|
|
1986
|
+
let raw;
|
|
1895
1987
|
try {
|
|
1896
|
-
|
|
1988
|
+
raw = await Promise.race([this.transport.receive(), readCanceled.promise]);
|
|
1897
1989
|
} finally {
|
|
1898
1990
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1899
1991
|
}
|
|
1900
|
-
let msg = JSON.parse(msgText);
|
|
1901
1992
|
if (this.abortReason) break;
|
|
1993
|
+
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
1902
1994
|
if (msg instanceof Array) switch (msg[0]) {
|
|
1903
1995
|
case "push":
|
|
1904
1996
|
if (msg.length > 1) {
|
|
1905
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
1997
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1906
1998
|
hook.ignoreUnhandledRejections();
|
|
1907
1999
|
this.exports.push({
|
|
1908
2000
|
hook,
|
|
@@ -1913,7 +2005,7 @@ var RpcSessionImpl = class {
|
|
|
1913
2005
|
break;
|
|
1914
2006
|
case "stream":
|
|
1915
2007
|
if (msg.length > 1) {
|
|
1916
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2008
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1917
2009
|
hook.ignoreUnhandledRejections();
|
|
1918
2010
|
let exportId = this.exports.length;
|
|
1919
2011
|
this.exports.push({
|
|
@@ -1948,13 +2040,13 @@ var RpcSessionImpl = class {
|
|
|
1948
2040
|
let importId = msg[1];
|
|
1949
2041
|
if (typeof importId == "number" && msg.length > 2) {
|
|
1950
2042
|
let imp = this.imports[importId];
|
|
1951
|
-
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this).evaluate(msg[2])));
|
|
2043
|
+
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
1952
2044
|
else {
|
|
1953
|
-
let payload = new Evaluator(this).evaluate(msg[2]);
|
|
2045
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
1954
2046
|
payload.dispose();
|
|
1955
2047
|
imp.resolve(new ErrorStubHook(payload.value));
|
|
1956
2048
|
}
|
|
1957
|
-
else if (msg[0] == "resolve") new Evaluator(this).evaluate(msg[2]).dispose();
|
|
2049
|
+
else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
1958
2050
|
continue;
|
|
1959
2051
|
}
|
|
1960
2052
|
break;
|
|
@@ -1969,7 +2061,7 @@ var RpcSessionImpl = class {
|
|
|
1969
2061
|
break;
|
|
1970
2062
|
}
|
|
1971
2063
|
case "abort": {
|
|
1972
|
-
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
2064
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
1973
2065
|
payload.dispose();
|
|
1974
2066
|
this.abort(payload, false);
|
|
1975
2067
|
break;
|
|
@@ -2041,9 +2133,14 @@ function newWorkersWebSocketRpcResponse(request, localMain, options) {
|
|
|
2041
2133
|
webSocket: pair[1]
|
|
2042
2134
|
});
|
|
2043
2135
|
}
|
|
2136
|
+
/**
|
|
2137
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
2138
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
2139
|
+
*/
|
|
2044
2140
|
var WebSocketTransport = class {
|
|
2045
2141
|
constructor(webSocket) {
|
|
2046
2142
|
this.#webSocket = webSocket;
|
|
2143
|
+
webSocket.binaryType = "arraybuffer";
|
|
2047
2144
|
if (webSocket.readyState === WebSocket.CONNECTING) {
|
|
2048
2145
|
this.#sendQueue = [];
|
|
2049
2146
|
webSocket.addEventListener("open", (event) => {
|
|
@@ -2056,12 +2153,12 @@ var WebSocketTransport = class {
|
|
|
2056
2153
|
});
|
|
2057
2154
|
}
|
|
2058
2155
|
webSocket.addEventListener("message", (event) => {
|
|
2059
|
-
if (this.#error) {} else if (typeof event.data === "string") if (this.#receiveResolver) {
|
|
2156
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) if (this.#receiveResolver) {
|
|
2060
2157
|
this.#receiveResolver(event.data);
|
|
2061
2158
|
this.#receiveResolver = void 0;
|
|
2062
2159
|
this.#receiveRejecter = void 0;
|
|
2063
2160
|
} else this.#receiveQueue.push(event.data);
|
|
2064
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received
|
|
2161
|
+
else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2065
2162
|
});
|
|
2066
2163
|
webSocket.addEventListener("close", (event) => {
|
|
2067
2164
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2076,13 +2173,13 @@ var WebSocketTransport = class {
|
|
|
2076
2173
|
#receiveRejecter;
|
|
2077
2174
|
#receiveQueue = [];
|
|
2078
2175
|
#error;
|
|
2079
|
-
|
|
2176
|
+
send(message) {
|
|
2080
2177
|
if (this.#sendQueue === void 0) this.#webSocket.send(message);
|
|
2081
2178
|
else this.#sendQueue.push(message);
|
|
2082
2179
|
}
|
|
2083
|
-
|
|
2084
|
-
if (this.#receiveQueue.length > 0) return this.#receiveQueue.shift();
|
|
2085
|
-
else if (this.#error)
|
|
2180
|
+
receive() {
|
|
2181
|
+
if (this.#receiveQueue.length > 0) return Promise.resolve(this.#receiveQueue.shift());
|
|
2182
|
+
else if (this.#error) return Promise.reject(this.#error);
|
|
2086
2183
|
else return new Promise((resolve, reject) => {
|
|
2087
2184
|
this.#receiveResolver = resolve;
|
|
2088
2185
|
this.#receiveRejecter = reject;
|
|
@@ -2117,7 +2214,7 @@ var BatchClientTransport = class {
|
|
|
2117
2214
|
#aborted;
|
|
2118
2215
|
#batchToSend = [];
|
|
2119
2216
|
#batchToReceive = null;
|
|
2120
|
-
|
|
2217
|
+
send(message) {
|
|
2121
2218
|
if (this.#batchToSend !== null) this.#batchToSend.push(message);
|
|
2122
2219
|
}
|
|
2123
2220
|
async receive() {
|
|
@@ -2159,7 +2256,7 @@ var BatchServerTransport = class {
|
|
|
2159
2256
|
#batchToSend = [];
|
|
2160
2257
|
#batchToReceive;
|
|
2161
2258
|
#allReceived = Promise.withResolvers();
|
|
2162
|
-
|
|
2259
|
+
send(message) {
|
|
2163
2260
|
this.#batchToSend.push(message);
|
|
2164
2261
|
}
|
|
2165
2262
|
async receive() {
|
|
@@ -2208,7 +2305,11 @@ async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
|
2208
2305
|
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
2209
2306
|
*/
|
|
2210
2307
|
async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
2211
|
-
if (request.method !== "POST")
|
|
2308
|
+
if (request.method !== "POST") {
|
|
2309
|
+
response.writeHead(405, "This endpoint only accepts POST requests.", options?.headers);
|
|
2310
|
+
response.end();
|
|
2311
|
+
return;
|
|
2312
|
+
}
|
|
2212
2313
|
let body = await new Promise((resolve, reject) => {
|
|
2213
2314
|
let chunks = [];
|
|
2214
2315
|
request.on("data", (chunk) => {
|
|
@@ -2233,17 +2334,17 @@ function newMessagePortRpcSession$1(port, localMain, options) {
|
|
|
2233
2334
|
return new RpcSession$1(new MessagePortTransport(port), localMain, options).getRemoteMain();
|
|
2234
2335
|
}
|
|
2235
2336
|
var MessagePortTransport = class {
|
|
2337
|
+
encodingLevel = "structuredClonable";
|
|
2236
2338
|
constructor(port) {
|
|
2237
2339
|
this.#port = port;
|
|
2238
2340
|
port.start();
|
|
2239
2341
|
port.addEventListener("message", (event) => {
|
|
2240
2342
|
if (this.#error) {} else if (event.data === null) this.#receivedError(/* @__PURE__ */ new Error("Peer closed MessagePort connection."));
|
|
2241
|
-
else if (
|
|
2343
|
+
else if (this.#receiveResolver) {
|
|
2242
2344
|
this.#receiveResolver(event.data);
|
|
2243
2345
|
this.#receiveResolver = void 0;
|
|
2244
2346
|
this.#receiveRejecter = void 0;
|
|
2245
2347
|
} else this.#receiveQueue.push(event.data);
|
|
2246
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received non-string message from MessagePort."));
|
|
2247
2348
|
});
|
|
2248
2349
|
port.addEventListener("messageerror", (event) => {
|
|
2249
2350
|
this.#receivedError(/* @__PURE__ */ new Error("MessagePort message error."));
|
|
@@ -2254,7 +2355,7 @@ var MessagePortTransport = class {
|
|
|
2254
2355
|
#receiveRejecter;
|
|
2255
2356
|
#receiveQueue = [];
|
|
2256
2357
|
#error;
|
|
2257
|
-
|
|
2358
|
+
send(message) {
|
|
2258
2359
|
if (this.#error) throw this.#error;
|
|
2259
2360
|
this.#port.postMessage(message);
|
|
2260
2361
|
}
|
|
@@ -2821,5 +2922,5 @@ async function newWorkersRpcResponse(request, localMain) {
|
|
|
2821
2922
|
}
|
|
2822
2923
|
|
|
2823
2924
|
//#endregion
|
|
2824
|
-
export { RpcPromise, RpcSession, RpcStub, RpcTarget, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
2925
|
+
export { RpcPromise, RpcSession, RpcStub, RpcTarget, WebSocketTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
2825
2926
|
//# sourceMappingURL=index.js.map
|