capnweb 0.0.0-610ab3a → 0.0.0-6664f94
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 +37 -2
- package/dist/index-bun.cjs +227 -33
- package/dist/index-bun.cjs.map +1 -1
- package/dist/index-bun.d.cts +24 -3
- package/dist/index-bun.d.ts +24 -3
- package/dist/index-bun.js +226 -34
- package/dist/index-bun.js.map +1 -1
- package/dist/index-workers.cjs +227 -33
- package/dist/index-workers.cjs.map +1 -1
- package/dist/index-workers.d.cts +21 -3
- package/dist/index-workers.d.ts +21 -3
- package/dist/index-workers.js +226 -34
- package/dist/index-workers.js.map +1 -1
- package/dist/index.cjs +227 -33
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -3
- package/dist/index.d.ts +21 -3
- package/dist/index.js +226 -34
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -199,7 +199,7 @@ The following types can be passed over RPC (in arguments or return values), and
|
|
|
199
199
|
* Arrays
|
|
200
200
|
* `bigint`
|
|
201
201
|
* `Date`
|
|
202
|
-
* `
|
|
202
|
+
* `ArrayBuffer`, `DataView`, and typed arrays
|
|
203
203
|
* `Error` and its well-known subclasses
|
|
204
204
|
* `Blob`
|
|
205
205
|
* `ReadableStream` and `WritableStream`, with automatic flow control.
|
|
@@ -207,7 +207,6 @@ The following types can be passed over RPC (in arguments or return values), and
|
|
|
207
207
|
|
|
208
208
|
The following types are not supported as of this writing, but may be added in the future:
|
|
209
209
|
* `Map` and `Set`
|
|
210
|
-
* `ArrayBuffer` and typed arrays other than `Uint8Array`
|
|
211
210
|
* `RegExp`
|
|
212
211
|
|
|
213
212
|
The following are intentionally NOT supported:
|
|
@@ -379,6 +378,40 @@ Sometimes you need to pass a stub somewhere where it will be disposed, but also
|
|
|
379
378
|
|
|
380
379
|
Hint: You can call `.dup()` on a property of a stub or promise, in order to create a stub backed by that property. This is particularly useful when you know in advance that the property is going to resolve to a stub: calling `.dup()` on it gives you a stub you can start using immediately, that otherwise behaves exactly the same as the eventual stub would if you awaited it.
|
|
381
380
|
|
|
381
|
+
#### Holding on to a callback past the call that delivered it
|
|
382
|
+
|
|
383
|
+
A common bidirectional-calling pattern is for the client to pass a callback to the server, which the server then invokes later (for example from a timer, an event handler, or a subsequent RPC). Because the callback parameter is a stub, and stubs in params are implicitly disposed when the call returns, the server must duplicate the stub with `.dup()` if it wants to invoke the callback after the call completes:
|
|
384
|
+
|
|
385
|
+
```ts
|
|
386
|
+
import { type RpcStub, RpcTarget } from 'capnweb';
|
|
387
|
+
|
|
388
|
+
// A callback the client passes in: a stub wrapping a function.
|
|
389
|
+
type Listener = RpcStub<(msg: string) => void>;
|
|
390
|
+
|
|
391
|
+
class Api extends RpcTarget {
|
|
392
|
+
#listener?: Listener;
|
|
393
|
+
|
|
394
|
+
// Stubs passed as params are disposed when the call returns, so `.dup()`
|
|
395
|
+
// to keep a reference that outlives registerListener().
|
|
396
|
+
registerListener(listener: Listener) {
|
|
397
|
+
this.#listener?.[Symbol.dispose](); // release any previous listener
|
|
398
|
+
this.#listener = listener.dup();
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// A *later* call can invoke the retained callback -- still valid thanks to .dup().
|
|
402
|
+
notify(msg: string) {
|
|
403
|
+
this.#listener?.(msg);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Dispose our duplicate when done so the client-side stub can be freed.
|
|
407
|
+
[Symbol.dispose]() {
|
|
408
|
+
this.#listener?.[Symbol.dispose]();
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
The same rule applies in the other direction: if the server returns a stub to the client and the client wants to keep using it after disposing the result, the client should `.dup()` the stub before the result is disposed.
|
|
414
|
+
|
|
382
415
|
### Listening for disposal
|
|
383
416
|
|
|
384
417
|
An `RpcTarget` may declare a `Symbol.dispose` method. If it does, the RPC system will automatically invoke it when a stub pointing at it (and all its duplicates) has been disposed.
|
|
@@ -405,6 +438,8 @@ If anything happens to the stub that would cause all further method calls and pr
|
|
|
405
438
|
|
|
406
439
|
* Cap'n Web's pipelining can make it easy for a malicious client to enqueue a large amount of work to occur on a server. To mitigate this, we recommend implementing rate limits on expensive operations. If using Cloudflare Workers, you may also consider configuring [per-request CPU limits](https://developers.cloudflare.com/workers/wrangler/configuration/#limits) to be lower than the default 30s. Note that in stateless Workers (i.e. not Durable Objects), the system considers an entire WebSocket session to be one "request" for CPU limits purposes.
|
|
407
440
|
|
|
441
|
+
* Cap'n Web applies receiver-side resource limits before expensive message processing, including a maximum incoming message size before `JSON.parse`. If your app is exposed to untrusted peers, also configure native transport or socket payload limits where available, such as `ws`'s `maxPayload`, Bun's `maxPayloadLength`, or the runtime's built-in WebSocket cap. Cap'n Web's own check runs after `RpcTransport.receive()` has returned a complete message string, so transport-level limits are still the first line of defense against buffering very large frames.
|
|
442
|
+
|
|
408
443
|
* Cap'n Web currently does not provide any runtime type checking. When using TypeScript, keep in mind that types are checked only at compile time. A malicious client can send types you did not expect, and this could cause you application to behave in unexpected ways. For example, MongoDB uses special property names to express queries; placing attacker-provided values directly into queries can result in query injection vulnerabilities (similar to SQL injection). Of course, JSON has always had the same problem, and there exists tooling to solve it. You might consider using a runtime type-checking framework like Zod to check your inputs. In the future, we hope to explore auto-generating type-checking code based on TypeScript types.
|
|
409
444
|
|
|
410
445
|
## Setting up a session
|
package/dist/index-bun.cjs
CHANGED
|
@@ -43,7 +43,19 @@ function typeForRpc(value) {
|
|
|
43
43
|
case Array.prototype: return "array";
|
|
44
44
|
case Date.prototype: return "date";
|
|
45
45
|
case Uint8Array.prototype:
|
|
46
|
-
case BUFFER_PROTOTYPE:
|
|
46
|
+
case BUFFER_PROTOTYPE:
|
|
47
|
+
case ArrayBuffer.prototype:
|
|
48
|
+
case DataView.prototype:
|
|
49
|
+
case Int8Array.prototype:
|
|
50
|
+
case Uint8ClampedArray.prototype:
|
|
51
|
+
case Int16Array.prototype:
|
|
52
|
+
case Uint16Array.prototype:
|
|
53
|
+
case Int32Array.prototype:
|
|
54
|
+
case Uint32Array.prototype:
|
|
55
|
+
case BigInt64Array.prototype:
|
|
56
|
+
case BigUint64Array.prototype:
|
|
57
|
+
case Float32Array.prototype:
|
|
58
|
+
case Float64Array.prototype: return "bytes";
|
|
47
59
|
case WritableStream.prototype: return "writable";
|
|
48
60
|
case ReadableStream.prototype: return "readable";
|
|
49
61
|
case Headers.prototype: return "headers";
|
|
@@ -513,6 +525,30 @@ var RpcPayload = class RpcPayload {
|
|
|
513
525
|
this.dispose();
|
|
514
526
|
}
|
|
515
527
|
}
|
|
528
|
+
async deliverStreamWrite(writer) {
|
|
529
|
+
try {
|
|
530
|
+
let promises = [];
|
|
531
|
+
this.deliverTo(this, "value", promises);
|
|
532
|
+
if (promises.length > 0) await Promise.all(promises);
|
|
533
|
+
let chunk = this.value[0];
|
|
534
|
+
if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
|
|
535
|
+
if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
|
|
536
|
+
value: () => this.dispose(),
|
|
537
|
+
writable: true,
|
|
538
|
+
enumerable: false,
|
|
539
|
+
configurable: true
|
|
540
|
+
});
|
|
541
|
+
await writer.write(chunk);
|
|
542
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
543
|
+
}
|
|
544
|
+
await writer.write(chunk);
|
|
545
|
+
this.dispose();
|
|
546
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
547
|
+
} catch (err) {
|
|
548
|
+
this.dispose();
|
|
549
|
+
throw err;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
516
552
|
async deliverResolve() {
|
|
517
553
|
try {
|
|
518
554
|
let promises = [];
|
|
@@ -931,6 +967,77 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
931
967
|
|
|
932
968
|
//#endregion
|
|
933
969
|
//#region src/serialize.ts
|
|
970
|
+
const DEFAULT_MAX_DEPTH = 256;
|
|
971
|
+
const DEFAULT_LIMITS = {
|
|
972
|
+
maxBigIntDigits: 16384,
|
|
973
|
+
maxDepth: 256,
|
|
974
|
+
maxMessageSize: 32 * 1024 * 1024
|
|
975
|
+
};
|
|
976
|
+
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
977
|
+
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
978
|
+
"ArrayBuffer",
|
|
979
|
+
"DataView",
|
|
980
|
+
"Int8Array",
|
|
981
|
+
"Uint8Array",
|
|
982
|
+
"Uint8ClampedArray",
|
|
983
|
+
"Int16Array",
|
|
984
|
+
"Uint16Array",
|
|
985
|
+
"Int32Array",
|
|
986
|
+
"Uint32Array",
|
|
987
|
+
"BigInt64Array",
|
|
988
|
+
"BigUint64Array",
|
|
989
|
+
"Float32Array",
|
|
990
|
+
"Float64Array"
|
|
991
|
+
];
|
|
992
|
+
function isValidByteContainerName(value) {
|
|
993
|
+
return BYTE_CONTAINER_TYPE_NAMES.includes(value);
|
|
994
|
+
}
|
|
995
|
+
const TYPED_ARRAY_ELEMENT_SIZE = {
|
|
996
|
+
ArrayBuffer: void 0,
|
|
997
|
+
DataView: void 0,
|
|
998
|
+
Int8Array: void 0,
|
|
999
|
+
Uint8Array: void 0,
|
|
1000
|
+
Uint8ClampedArray: void 0,
|
|
1001
|
+
Int16Array: 2,
|
|
1002
|
+
Uint16Array: 2,
|
|
1003
|
+
Int32Array: 4,
|
|
1004
|
+
Uint32Array: 4,
|
|
1005
|
+
BigInt64Array: 8,
|
|
1006
|
+
BigUint64Array: 8,
|
|
1007
|
+
Float32Array: 4,
|
|
1008
|
+
Float64Array: 8
|
|
1009
|
+
};
|
|
1010
|
+
const BYTE_CONTAINER_PROTOTYPES = {
|
|
1011
|
+
ArrayBuffer: ArrayBuffer.prototype,
|
|
1012
|
+
DataView: DataView.prototype,
|
|
1013
|
+
Int8Array: Int8Array.prototype,
|
|
1014
|
+
Uint8ClampedArray: Uint8ClampedArray.prototype,
|
|
1015
|
+
Int16Array: Int16Array.prototype,
|
|
1016
|
+
Uint16Array: Uint16Array.prototype,
|
|
1017
|
+
Int32Array: Int32Array.prototype,
|
|
1018
|
+
Uint32Array: Uint32Array.prototype,
|
|
1019
|
+
BigInt64Array: BigInt64Array.prototype,
|
|
1020
|
+
BigUint64Array: BigUint64Array.prototype,
|
|
1021
|
+
Float32Array: Float32Array.prototype,
|
|
1022
|
+
Float64Array: Float64Array.prototype
|
|
1023
|
+
};
|
|
1024
|
+
const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
|
|
1025
|
+
for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
|
|
1026
|
+
function swapByteOrder(bytes, elementSize) {
|
|
1027
|
+
if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
|
|
1028
|
+
let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1029
|
+
for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
|
|
1030
|
+
case 2:
|
|
1031
|
+
view.setUint16(offset, view.getUint16(offset, false), true);
|
|
1032
|
+
break;
|
|
1033
|
+
case 4:
|
|
1034
|
+
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1035
|
+
break;
|
|
1036
|
+
case 8:
|
|
1037
|
+
view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1038
|
+
break;
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
934
1041
|
var NullExporter = class {
|
|
935
1042
|
exportStub(stub) {
|
|
936
1043
|
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
@@ -951,6 +1058,7 @@ async function streamToBlob(stream, type) {
|
|
|
951
1058
|
return b.type === type ? b : b.slice(0, b.size, type);
|
|
952
1059
|
}
|
|
953
1060
|
const ERROR_TYPES = {
|
|
1061
|
+
__proto__: null,
|
|
954
1062
|
Error,
|
|
955
1063
|
EvalError,
|
|
956
1064
|
RangeError,
|
|
@@ -982,7 +1090,7 @@ var Devaluator = class Devaluator {
|
|
|
982
1090
|
}
|
|
983
1091
|
exports;
|
|
984
1092
|
devaluateImpl(value, parent, depth) {
|
|
985
|
-
if (depth >=
|
|
1093
|
+
if (depth >= 256) throw new Error("Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
|
|
986
1094
|
switch (typeForRpc(value)) {
|
|
987
1095
|
case "unsupported": {
|
|
988
1096
|
let msg;
|
|
@@ -1021,17 +1129,38 @@ var Devaluator = class Devaluator {
|
|
|
1021
1129
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1022
1130
|
}
|
|
1023
1131
|
case "bytes": {
|
|
1024
|
-
let
|
|
1025
|
-
|
|
1026
|
-
if (
|
|
1132
|
+
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1133
|
+
let bytes;
|
|
1134
|
+
if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
|
|
1135
|
+
else if (alternateTypeName === void 0) bytes = value;
|
|
1136
|
+
else {
|
|
1137
|
+
let view = value;
|
|
1138
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1139
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
|
|
1140
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize) {
|
|
1141
|
+
bytes = bytes.slice();
|
|
1142
|
+
swapByteOrder(bytes, elementSize);
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1146
|
+
"bytes",
|
|
1147
|
+
bytes,
|
|
1148
|
+
alternateTypeName
|
|
1149
|
+
];
|
|
1027
1150
|
let b64;
|
|
1028
|
-
if (
|
|
1151
|
+
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1152
|
+
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
1029
1153
|
else {
|
|
1030
1154
|
let binary = "";
|
|
1031
1155
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
1032
1156
|
b64 = btoa(binary);
|
|
1033
1157
|
}
|
|
1034
|
-
|
|
1158
|
+
b64 = b64.replace(/=+$/, "");
|
|
1159
|
+
return alternateTypeName === void 0 ? ["bytes", b64] : [
|
|
1160
|
+
"bytes",
|
|
1161
|
+
b64,
|
|
1162
|
+
alternateTypeName
|
|
1163
|
+
];
|
|
1035
1164
|
}
|
|
1036
1165
|
case "headers": return ["headers", [...value]];
|
|
1037
1166
|
case "request": {
|
|
@@ -1216,6 +1345,9 @@ var NullImporter = class {
|
|
|
1216
1345
|
getPipeReadable(exportId) {
|
|
1217
1346
|
throw new Error("Cannot retrieve pipe readable without an RPC session.");
|
|
1218
1347
|
}
|
|
1348
|
+
getLimits() {
|
|
1349
|
+
return DEFAULT_LIMITS;
|
|
1350
|
+
}
|
|
1219
1351
|
};
|
|
1220
1352
|
const NULL_IMPORTER = new NullImporter();
|
|
1221
1353
|
function fixBrokenRequestBody(request, body) {
|
|
@@ -1233,16 +1365,21 @@ function streamToBlobPromise(stream, type) {
|
|
|
1233
1365
|
var Evaluator = class Evaluator {
|
|
1234
1366
|
importer;
|
|
1235
1367
|
encodingLevel;
|
|
1368
|
+
limits;
|
|
1236
1369
|
constructor(importer, encodingLevel = "string") {
|
|
1237
1370
|
this.importer = importer;
|
|
1238
1371
|
this.encodingLevel = encodingLevel;
|
|
1372
|
+
this.limits = importer.getLimits();
|
|
1239
1373
|
}
|
|
1240
1374
|
hooks = [];
|
|
1241
1375
|
promises = [];
|
|
1242
1376
|
evaluate(value) {
|
|
1377
|
+
return this.evaluateWithDepth(value, 0);
|
|
1378
|
+
}
|
|
1379
|
+
evaluateWithDepth(value, depth) {
|
|
1243
1380
|
let payload = RpcPayload.forEvaluate(this.hooks, this.promises);
|
|
1244
1381
|
try {
|
|
1245
|
-
payload.value = this.evaluateImpl(value, payload, "value");
|
|
1382
|
+
payload.value = this.evaluateImpl(value, payload, "value", depth);
|
|
1246
1383
|
return payload;
|
|
1247
1384
|
} catch (err) {
|
|
1248
1385
|
payload.dispose();
|
|
@@ -1252,35 +1389,70 @@ var Evaluator = class Evaluator {
|
|
|
1252
1389
|
evaluateCopy(value) {
|
|
1253
1390
|
return this.evaluate(structuredClone(value));
|
|
1254
1391
|
}
|
|
1255
|
-
evaluateImpl(value, parent, property) {
|
|
1392
|
+
evaluateImpl(value, parent, property, depth) {
|
|
1393
|
+
let maxDepth = this.limits.maxDepth;
|
|
1394
|
+
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1256
1395
|
if (this.encodingLevel === "structuredClonable") {
|
|
1257
1396
|
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1258
1397
|
}
|
|
1259
1398
|
if (value instanceof Array) {
|
|
1260
1399
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1261
1400
|
let result = value[0];
|
|
1262
|
-
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i);
|
|
1401
|
+
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i, depth + 1);
|
|
1263
1402
|
return result;
|
|
1264
1403
|
} else switch (value[0]) {
|
|
1265
1404
|
case "bigint":
|
|
1266
|
-
if (typeof value[1] == "string")
|
|
1405
|
+
if (typeof value[1] == "string") {
|
|
1406
|
+
let digits = value[1];
|
|
1407
|
+
let maxBigIntDigits = this.limits.maxBigIntDigits;
|
|
1408
|
+
if (digits.length > maxBigIntDigits) throw new TypeError(`Deserialized bigint exceeds maximum length of ${maxBigIntDigits} digits.`);
|
|
1409
|
+
return BigInt(digits);
|
|
1410
|
+
}
|
|
1267
1411
|
break;
|
|
1268
1412
|
case "date":
|
|
1269
1413
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1270
1414
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1271
1415
|
break;
|
|
1272
|
-
case "bytes":
|
|
1273
|
-
|
|
1274
|
-
if (
|
|
1275
|
-
else if (
|
|
1416
|
+
case "bytes": {
|
|
1417
|
+
let bytes;
|
|
1418
|
+
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1419
|
+
else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1420
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1276
1421
|
else {
|
|
1277
1422
|
let bs = atob(value[1]);
|
|
1278
1423
|
let len = bs.length;
|
|
1279
|
-
|
|
1424
|
+
bytes = new Uint8Array(len);
|
|
1280
1425
|
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1281
|
-
return bytes;
|
|
1282
1426
|
}
|
|
1283
|
-
break;
|
|
1427
|
+
else break;
|
|
1428
|
+
if (value.length === 2) return bytes;
|
|
1429
|
+
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1430
|
+
if (!isValidByteContainerName(value[2])) {
|
|
1431
|
+
let marker = value[2].slice(0, 64);
|
|
1432
|
+
throw new TypeError(`Unknown bytes type marker: ${marker}`);
|
|
1433
|
+
}
|
|
1434
|
+
let marker = value[2];
|
|
1435
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
|
|
1436
|
+
if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
|
|
1437
|
+
let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
1438
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
|
|
1439
|
+
switch (marker) {
|
|
1440
|
+
case "ArrayBuffer": return buffer;
|
|
1441
|
+
case "DataView": return new DataView(buffer);
|
|
1442
|
+
case "Int8Array": return new Int8Array(buffer);
|
|
1443
|
+
case "Uint8Array": return new Uint8Array(buffer);
|
|
1444
|
+
case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
|
|
1445
|
+
case "Int16Array": return new Int16Array(buffer);
|
|
1446
|
+
case "Uint16Array": return new Uint16Array(buffer);
|
|
1447
|
+
case "Int32Array": return new Int32Array(buffer);
|
|
1448
|
+
case "Uint32Array": return new Uint32Array(buffer);
|
|
1449
|
+
case "BigInt64Array": return new BigInt64Array(buffer);
|
|
1450
|
+
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1451
|
+
case "Float32Array": return new Float32Array(buffer);
|
|
1452
|
+
case "Float64Array": return new Float64Array(buffer);
|
|
1453
|
+
default:
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1284
1456
|
case "error":
|
|
1285
1457
|
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1286
1458
|
let cls = ERROR_TYPES[value[1]] || Error;
|
|
@@ -1293,7 +1465,11 @@ var Evaluator = class Evaluator {
|
|
|
1293
1465
|
let propsObj = props;
|
|
1294
1466
|
for (let key of Object.keys(propsObj)) {
|
|
1295
1467
|
if (key === "name" || key === "message" || key === "stack") continue;
|
|
1296
|
-
|
|
1468
|
+
if (key in Object.prototype || key === "toJSON") {
|
|
1469
|
+
this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1470
|
+
continue;
|
|
1471
|
+
}
|
|
1472
|
+
anyResult[key] = this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1297
1473
|
}
|
|
1298
1474
|
}
|
|
1299
1475
|
return result;
|
|
@@ -1314,11 +1490,11 @@ var Evaluator = class Evaluator {
|
|
|
1314
1490
|
let init = value[2];
|
|
1315
1491
|
if (typeof init !== "object" || init === null) break;
|
|
1316
1492
|
if (init.body) {
|
|
1317
|
-
init.body = this.evaluateImpl(init.body, init, "body");
|
|
1493
|
+
init.body = this.evaluateImpl(init.body, init, "body", depth + 1);
|
|
1318
1494
|
if (init.body === null || typeof init.body === "string" || init.body instanceof Uint8Array || init.body instanceof ReadableStream) {} else throw new TypeError("Request body must be of type ReadableStream.");
|
|
1319
1495
|
}
|
|
1320
1496
|
if (init.signal) {
|
|
1321
|
-
init.signal = this.evaluateImpl(init.signal, init, "signal");
|
|
1497
|
+
init.signal = this.evaluateImpl(init.signal, init, "signal", depth + 1);
|
|
1322
1498
|
if (!(init.signal instanceof AbortSignal)) throw new TypeError("Request siganl must be of type AbortSignal.");
|
|
1323
1499
|
}
|
|
1324
1500
|
if (init.headers && !(init.headers instanceof Array)) throw new TypeError("Request headers must be serialized as an array of pairs.");
|
|
@@ -1335,7 +1511,7 @@ var Evaluator = class Evaluator {
|
|
|
1335
1511
|
}
|
|
1336
1512
|
case "response": {
|
|
1337
1513
|
if (value.length !== 3) break;
|
|
1338
|
-
let body = this.evaluateImpl(value[1], parent, property);
|
|
1514
|
+
let body = this.evaluateImpl(value[1], parent, property, depth + 1);
|
|
1339
1515
|
if (body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ReadableStream) {} else throw new TypeError("Response body must be of type ReadableStream.");
|
|
1340
1516
|
let init = value[2];
|
|
1341
1517
|
if (typeof init !== "object" || init === null) break;
|
|
@@ -1346,7 +1522,7 @@ var Evaluator = class Evaluator {
|
|
|
1346
1522
|
case "blob": {
|
|
1347
1523
|
if (value.length !== 3 || typeof value[1] !== "string") break;
|
|
1348
1524
|
let contentType = value[1];
|
|
1349
|
-
let content = this.evaluateImpl(value[2], parent, property);
|
|
1525
|
+
let content = this.evaluateImpl(value[2], parent, property, depth + 1);
|
|
1350
1526
|
if (!(content instanceof ReadableStream)) throw new TypeError("Blob content must be serialized as a ReadableStream.");
|
|
1351
1527
|
let promise = streamToBlobPromise(content, contentType);
|
|
1352
1528
|
this.promises.push({
|
|
@@ -1387,7 +1563,7 @@ var Evaluator = class Evaluator {
|
|
|
1387
1563
|
if (value.length == 3) return addStub(hook.get(path));
|
|
1388
1564
|
let args = value[3];
|
|
1389
1565
|
if (!(args instanceof Array)) break;
|
|
1390
|
-
args = new Evaluator(this.importer).
|
|
1566
|
+
args = new Evaluator(this.importer).evaluateWithDepth([args], depth);
|
|
1391
1567
|
return addStub(hook.call(path, args));
|
|
1392
1568
|
}
|
|
1393
1569
|
case "remap": {
|
|
@@ -1453,9 +1629,9 @@ var Evaluator = class Evaluator {
|
|
|
1453
1629
|
} else if (value instanceof Object) {
|
|
1454
1630
|
let result = value;
|
|
1455
1631
|
for (let key in result) if (key in Object.prototype || key === "toJSON") {
|
|
1456
|
-
this.evaluateImpl(result[key], result, key);
|
|
1632
|
+
this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1457
1633
|
delete result[key];
|
|
1458
|
-
} else result[key] = this.evaluateImpl(result[key], result, key);
|
|
1634
|
+
} else result[key] = this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1459
1635
|
return result;
|
|
1460
1636
|
} else return value;
|
|
1461
1637
|
}
|
|
@@ -1678,6 +1854,7 @@ var RpcSessionImpl = class {
|
|
|
1678
1854
|
pullCount = 0;
|
|
1679
1855
|
onBrokenCallbacks = [];
|
|
1680
1856
|
encodingLevel;
|
|
1857
|
+
limits;
|
|
1681
1858
|
constructor(transport, mainHook, options) {
|
|
1682
1859
|
this.transport = transport;
|
|
1683
1860
|
this.options = options;
|
|
@@ -1690,6 +1867,10 @@ var RpcSessionImpl = class {
|
|
|
1690
1867
|
}
|
|
1691
1868
|
}
|
|
1692
1869
|
this.encodingLevel = level;
|
|
1870
|
+
this.limits = {
|
|
1871
|
+
...DEFAULT_LIMITS,
|
|
1872
|
+
...options.limits
|
|
1873
|
+
};
|
|
1693
1874
|
this.exports.push({
|
|
1694
1875
|
hook: mainHook,
|
|
1695
1876
|
refcount: 1
|
|
@@ -1832,6 +2013,9 @@ var RpcSessionImpl = class {
|
|
|
1832
2013
|
entry.pipeReadable = void 0;
|
|
1833
2014
|
return readable;
|
|
1834
2015
|
}
|
|
2016
|
+
getLimits() {
|
|
2017
|
+
return this.limits;
|
|
2018
|
+
}
|
|
1835
2019
|
createPipe(readable, readableHook) {
|
|
1836
2020
|
if (this.abortReason) throw this.abortReason;
|
|
1837
2021
|
this.send(["pipe"]);
|
|
@@ -1991,6 +2175,7 @@ var RpcSessionImpl = class {
|
|
|
1991
2175
|
} finally {
|
|
1992
2176
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1993
2177
|
}
|
|
2178
|
+
if (this.encodingLevel === "string" && raw.length > this.limits.maxMessageSize) throw new TypeError(`Incoming message exceeds maximum size of ${this.limits.maxMessageSize} UTF-16 code units.`);
|
|
1994
2179
|
if (this.abortReason) break;
|
|
1995
2180
|
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
1996
2181
|
if (msg instanceof Array) switch (msg[0]) {
|
|
@@ -2065,7 +2250,7 @@ var RpcSessionImpl = class {
|
|
|
2065
2250
|
case "abort": {
|
|
2066
2251
|
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2067
2252
|
payload.dispose();
|
|
2068
|
-
this.abort(payload, false);
|
|
2253
|
+
this.abort(payload.value, false);
|
|
2069
2254
|
break;
|
|
2070
2255
|
}
|
|
2071
2256
|
}
|
|
@@ -2116,6 +2301,8 @@ var RpcSession$1 = class {
|
|
|
2116
2301
|
|
|
2117
2302
|
//#endregion
|
|
2118
2303
|
//#region src/websocket.ts
|
|
2304
|
+
/** Close-frame reason max UTF-8 bytes: 125 payload − 2-byte status (RFC 6455 §5.5). */
|
|
2305
|
+
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2119
2306
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2120
2307
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2121
2308
|
return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
|
|
@@ -2191,6 +2378,8 @@ var WebSocketTransport = class {
|
|
|
2191
2378
|
let message;
|
|
2192
2379
|
if (reason instanceof Error) message = reason.message;
|
|
2193
2380
|
else message = `${reason}`;
|
|
2381
|
+
let reasonBytes = new TextEncoder().encode(message);
|
|
2382
|
+
if (reasonBytes.length > 123) message = new TextDecoder().decode(reasonBytes.subarray(0, 123), { stream: true });
|
|
2194
2383
|
this.#webSocket.close(3e3, message);
|
|
2195
2384
|
if (!this.#error) this.#error = reason;
|
|
2196
2385
|
}
|
|
@@ -2208,6 +2397,7 @@ var WebSocketTransport = class {
|
|
|
2208
2397
|
|
|
2209
2398
|
//#endregion
|
|
2210
2399
|
//#region src/batch.ts
|
|
2400
|
+
const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
2211
2401
|
var BatchClientTransport = class {
|
|
2212
2402
|
constructor(sendBatch) {
|
|
2213
2403
|
this.#promise = this.#scheduleBatch(sendBatch);
|
|
@@ -2229,7 +2419,7 @@ var BatchClientTransport = class {
|
|
|
2229
2419
|
this.#aborted = reason;
|
|
2230
2420
|
}
|
|
2231
2421
|
async #scheduleBatch(sendBatch) {
|
|
2232
|
-
await
|
|
2422
|
+
await yieldToMacrotask();
|
|
2233
2423
|
if (this.#aborted !== void 0) throw this.#aborted;
|
|
2234
2424
|
let batch = this.#batchToSend;
|
|
2235
2425
|
this.#batchToSend = null;
|
|
@@ -2576,6 +2766,9 @@ var MapApplicator = class {
|
|
|
2576
2766
|
getPipeReadable(exportId) {
|
|
2577
2767
|
throw new Error("A mapper function cannot use pipe readables.");
|
|
2578
2768
|
}
|
|
2769
|
+
getLimits() {
|
|
2770
|
+
return DEFAULT_LIMITS;
|
|
2771
|
+
}
|
|
2579
2772
|
};
|
|
2580
2773
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2581
2774
|
let mapper = new MapApplicator(captures, new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner)));
|
|
@@ -2636,8 +2829,7 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2636
2829
|
throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2637
2830
|
}
|
|
2638
2831
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2639
|
-
|
|
2640
|
-
return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
|
|
2832
|
+
return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
|
|
2641
2833
|
} catch (err) {
|
|
2642
2834
|
return new ErrorStubHook(err);
|
|
2643
2835
|
}
|
|
@@ -2914,12 +3106,12 @@ let newMessagePortRpcSession = newMessagePortRpcSession$1;
|
|
|
2914
3106
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
2915
3107
|
* be safe.
|
|
2916
3108
|
*/
|
|
2917
|
-
async function newWorkersRpcResponse(request, localMain) {
|
|
3109
|
+
async function newWorkersRpcResponse(request, localMain, options) {
|
|
2918
3110
|
if (request.method === "POST") {
|
|
2919
|
-
let response = await newHttpBatchRpcResponse(request, localMain);
|
|
3111
|
+
let response = await newHttpBatchRpcResponse(request, localMain, options);
|
|
2920
3112
|
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
2921
3113
|
return response;
|
|
2922
|
-
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain);
|
|
3114
|
+
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain, options);
|
|
2923
3115
|
else return new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
|
|
2924
3116
|
}
|
|
2925
3117
|
|
|
@@ -3041,6 +3233,8 @@ let newBunWebSocketRpcSession = newBunWebSocketRpcSession$1;
|
|
|
3041
3233
|
|
|
3042
3234
|
//#endregion
|
|
3043
3235
|
exports.BunWebSocketTransport = BunWebSocketTransport;
|
|
3236
|
+
exports.DEFAULT_LIMITS = DEFAULT_LIMITS;
|
|
3237
|
+
exports.DEFAULT_MAX_DEPTH = DEFAULT_MAX_DEPTH;
|
|
3044
3238
|
exports.RpcPromise = RpcPromise;
|
|
3045
3239
|
exports.RpcSession = RpcSession;
|
|
3046
3240
|
exports.RpcStub = RpcStub;
|