capnweb 0.0.0-92baaed → 0.0.0-9751a4e
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 +58 -2
- package/dist/index-bun.cjs +274 -55
- package/dist/index-bun.cjs.map +1 -1
- package/dist/index-bun.d.cts +58 -12
- package/dist/index-bun.d.ts +58 -12
- package/dist/index-bun.js +274 -55
- package/dist/index-bun.js.map +1 -1
- package/dist/index-workers.cjs +274 -55
- package/dist/index-workers.cjs.map +1 -1
- package/dist/index-workers.d.cts +54 -11
- package/dist/index-workers.d.ts +54 -11
- package/dist/index-workers.js +274 -55
- package/dist/index-workers.js.map +1 -1
- package/dist/index.cjs +274 -55
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +54 -11
- package/dist/index.d.ts +54 -11
- package/dist/index.js +274 -55
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -199,15 +199,15 @@ 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.
|
|
206
|
+
* `URL`
|
|
206
207
|
* `Headers`, `Request`, and `Response` from the Fetch API.
|
|
207
208
|
|
|
208
209
|
The following types are not supported as of this writing, but may be added in the future:
|
|
209
210
|
* `Map` and `Set`
|
|
210
|
-
* `ArrayBuffer` and typed arrays other than `Uint8Array`
|
|
211
211
|
* `RegExp`
|
|
212
212
|
|
|
213
213
|
The following are intentionally NOT supported:
|
|
@@ -267,6 +267,28 @@ let profile = await api.getUserProfile(user.id);
|
|
|
267
267
|
|
|
268
268
|
Whenever an `RpcPromise` is passed in the parameters to an RPC, or returned as part of the result, the promise will be replaced with its resolution before delivery to the receiving application. So, you can use an `RpcPromise<T>` anywhere where a `T` is required!
|
|
269
269
|
|
|
270
|
+
#### Constructing `RpcPromise` from a `Promise`
|
|
271
|
+
|
|
272
|
+
You can construct an `RpcPromise<T>` directly from a regular `Promise<T>`, allowing you to perform promise pipelining on a regular local promise. Pipelined calls will wait until the inner promise resolves, then will be delivered, in-order, to the resolution. This is useful when you plan to obtain some stub in the future, but you want to allow code to start queuing calls on it immediately.
|
|
273
|
+
|
|
274
|
+
Wrapping a `Promise<T>` in this way is semantically identical to creating a local-loopback RPC and then invoking it. That is:
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
// this...
|
|
278
|
+
let rpcPromise = new RpcPromise(myPromise);
|
|
279
|
+
|
|
280
|
+
// is semantically the same as this...
|
|
281
|
+
let rpcFunc = new RpcStub(() => myPromise);
|
|
282
|
+
let rpcPromise = rpcFunc();
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
In other words, this means:
|
|
286
|
+
* The result of the promise must be serializable.
|
|
287
|
+
* If the promise resolution contains `RpcTarget`s or `Function`s, the `RpcPromise`'s resolution will replace them with stubs.
|
|
288
|
+
* Ownership of any stubs in the Promise result is transferred away. If you want to keep your own copies, you need to `dup()` them.
|
|
289
|
+
* If the promise rejects, the rejection propagates to all pipelined calls.
|
|
290
|
+
* etc.
|
|
291
|
+
|
|
270
292
|
### The magic `map()` method
|
|
271
293
|
|
|
272
294
|
Every RPC promise has a special method `.map()` which can be used to remotely transform a value, without pulling it back locally. Here's an example:
|
|
@@ -379,6 +401,40 @@ Sometimes you need to pass a stub somewhere where it will be disposed, but also
|
|
|
379
401
|
|
|
380
402
|
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
403
|
|
|
404
|
+
#### Holding on to a callback past the call that delivered it
|
|
405
|
+
|
|
406
|
+
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:
|
|
407
|
+
|
|
408
|
+
```ts
|
|
409
|
+
import { type RpcStub, RpcTarget } from 'capnweb';
|
|
410
|
+
|
|
411
|
+
// A callback the client passes in: a stub wrapping a function.
|
|
412
|
+
type Listener = RpcStub<(msg: string) => void>;
|
|
413
|
+
|
|
414
|
+
class Api extends RpcTarget {
|
|
415
|
+
#listener?: Listener;
|
|
416
|
+
|
|
417
|
+
// Stubs passed as params are disposed when the call returns, so `.dup()`
|
|
418
|
+
// to keep a reference that outlives registerListener().
|
|
419
|
+
registerListener(listener: Listener) {
|
|
420
|
+
this.#listener?.[Symbol.dispose](); // release any previous listener
|
|
421
|
+
this.#listener = listener.dup();
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// A *later* call can invoke the retained callback -- still valid thanks to .dup().
|
|
425
|
+
notify(msg: string) {
|
|
426
|
+
this.#listener?.(msg);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Dispose our duplicate when done so the client-side stub can be freed.
|
|
430
|
+
[Symbol.dispose]() {
|
|
431
|
+
this.#listener?.[Symbol.dispose]();
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
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.
|
|
437
|
+
|
|
382
438
|
### Listening for disposal
|
|
383
439
|
|
|
384
440
|
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.
|
package/dist/index-bun.cjs
CHANGED
|
@@ -43,9 +43,22 @@ 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";
|
|
61
|
+
case URL.prototype: return "url";
|
|
49
62
|
case Headers.prototype: return "headers";
|
|
50
63
|
case Request.prototype: return "request";
|
|
51
64
|
case Response.prototype: return "response";
|
|
@@ -79,7 +92,14 @@ let streamImpl = {
|
|
|
79
92
|
};
|
|
80
93
|
var StubHook = class {
|
|
81
94
|
stream(path, args) {
|
|
82
|
-
let
|
|
95
|
+
let hook = this.call(path, args);
|
|
96
|
+
let pulled;
|
|
97
|
+
try {
|
|
98
|
+
pulled = hook.pull();
|
|
99
|
+
} catch (err) {
|
|
100
|
+
hook.dispose();
|
|
101
|
+
throw err;
|
|
102
|
+
}
|
|
83
103
|
let promise;
|
|
84
104
|
if (pulled instanceof Promise) promise = pulled.then((p) => {
|
|
85
105
|
p.dispose();
|
|
@@ -98,9 +118,11 @@ var ErrorStubHook = class extends StubHook {
|
|
|
98
118
|
this.error = error;
|
|
99
119
|
}
|
|
100
120
|
call(path, args) {
|
|
121
|
+
args.dispose();
|
|
101
122
|
return this;
|
|
102
123
|
}
|
|
103
124
|
map(path, captures, instructions) {
|
|
125
|
+
for (let cap of captures) cap.dispose();
|
|
104
126
|
return this;
|
|
105
127
|
}
|
|
106
128
|
get(path) {
|
|
@@ -224,7 +246,25 @@ var RpcStub$1 = class RpcStub$1 extends RpcTarget$1 {
|
|
|
224
246
|
};
|
|
225
247
|
var RpcPromise$1 = class extends RpcStub$1 {
|
|
226
248
|
constructor(hook, pathIfPromise) {
|
|
227
|
-
super(hook, pathIfPromise);
|
|
249
|
+
if (hook instanceof StubHook) super(hook, pathIfPromise ?? []);
|
|
250
|
+
else {
|
|
251
|
+
if (pathIfPromise !== void 0) throw new TypeError("RpcPromise constructor expected one argument, received two.");
|
|
252
|
+
let kind = typeForRpc(hook);
|
|
253
|
+
if (kind === "rpc-promise") {
|
|
254
|
+
let raw = unwrapStubAndPath(hook);
|
|
255
|
+
if (raw.pathIfPromise.length > 0) super(raw.hook, raw.pathIfPromise);
|
|
256
|
+
else {
|
|
257
|
+
let adopted = raw.hook;
|
|
258
|
+
raw.hook = DISPOSED_HOOK;
|
|
259
|
+
super(adopted, []);
|
|
260
|
+
}
|
|
261
|
+
} else if (kind === "rpc-thenable") super(TargetStubHook.create(hook, void 0), []);
|
|
262
|
+
else {
|
|
263
|
+
let promiseHook = new PromiseStubHook(Promise.resolve(hook).then((value) => new PayloadStubHook(RpcPayload.fromAppReturn(value))));
|
|
264
|
+
promiseHook.ignoreUnhandledRejections();
|
|
265
|
+
super(promiseHook, []);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
228
268
|
}
|
|
229
269
|
then(onfulfilled, onrejected) {
|
|
230
270
|
return pullPromise(this).then(...arguments);
|
|
@@ -380,6 +420,7 @@ var RpcPayload = class RpcPayload {
|
|
|
380
420
|
case "date":
|
|
381
421
|
case "bytes":
|
|
382
422
|
case "blob":
|
|
423
|
+
case "url":
|
|
383
424
|
case "error":
|
|
384
425
|
case "undefined": return value;
|
|
385
426
|
case "array": {
|
|
@@ -513,6 +554,30 @@ var RpcPayload = class RpcPayload {
|
|
|
513
554
|
this.dispose();
|
|
514
555
|
}
|
|
515
556
|
}
|
|
557
|
+
async deliverStreamWrite(writer) {
|
|
558
|
+
try {
|
|
559
|
+
let promises = [];
|
|
560
|
+
this.deliverTo(this, "value", promises);
|
|
561
|
+
if (promises.length > 0) await Promise.all(promises);
|
|
562
|
+
let chunk = this.value[0];
|
|
563
|
+
if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
|
|
564
|
+
if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
|
|
565
|
+
value: () => this.dispose(),
|
|
566
|
+
writable: true,
|
|
567
|
+
enumerable: false,
|
|
568
|
+
configurable: true
|
|
569
|
+
});
|
|
570
|
+
await writer.write(chunk);
|
|
571
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
572
|
+
}
|
|
573
|
+
await writer.write(chunk);
|
|
574
|
+
this.dispose();
|
|
575
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
576
|
+
} catch (err) {
|
|
577
|
+
this.dispose();
|
|
578
|
+
throw err;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
516
581
|
async deliverResolve() {
|
|
517
582
|
try {
|
|
518
583
|
let promises = [];
|
|
@@ -553,6 +618,7 @@ var RpcPayload = class RpcPayload {
|
|
|
553
618
|
case "bytes":
|
|
554
619
|
case "blob":
|
|
555
620
|
case "date":
|
|
621
|
+
case "url":
|
|
556
622
|
case "error":
|
|
557
623
|
case "undefined": return;
|
|
558
624
|
case "array": {
|
|
@@ -635,6 +701,7 @@ var RpcPayload = class RpcPayload {
|
|
|
635
701
|
case "rpc-target":
|
|
636
702
|
case "writable":
|
|
637
703
|
case "readable":
|
|
704
|
+
case "url":
|
|
638
705
|
case "headers":
|
|
639
706
|
case "request":
|
|
640
707
|
case "response": return;
|
|
@@ -704,6 +771,7 @@ function followPath(value, parent, path, owner) {
|
|
|
704
771
|
case "blob":
|
|
705
772
|
case "date":
|
|
706
773
|
case "error":
|
|
774
|
+
case "url":
|
|
707
775
|
case "headers":
|
|
708
776
|
case "request":
|
|
709
777
|
case "response":
|
|
@@ -736,17 +804,22 @@ function followPath(value, parent, path, owner) {
|
|
|
736
804
|
}
|
|
737
805
|
var ValueStubHook = class extends StubHook {
|
|
738
806
|
call(path, args) {
|
|
807
|
+
let followResult;
|
|
739
808
|
try {
|
|
740
809
|
let { value, owner } = this.getValue();
|
|
741
|
-
|
|
742
|
-
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
743
|
-
if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
|
|
744
|
-
return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
|
|
745
|
-
return new PayloadStubHook(payload);
|
|
746
|
-
}));
|
|
810
|
+
followResult = followPath(value, void 0, path, owner);
|
|
747
811
|
} catch (err) {
|
|
812
|
+
args.dispose();
|
|
748
813
|
return new ErrorStubHook(err);
|
|
749
814
|
}
|
|
815
|
+
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
816
|
+
if (typeof followResult.value != "function") {
|
|
817
|
+
args.dispose();
|
|
818
|
+
return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
|
|
819
|
+
}
|
|
820
|
+
return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
|
|
821
|
+
return new PayloadStubHook(payload);
|
|
822
|
+
}));
|
|
750
823
|
}
|
|
751
824
|
map(path, captures, instructions) {
|
|
752
825
|
try {
|
|
@@ -767,7 +840,10 @@ var ValueStubHook = class extends StubHook {
|
|
|
767
840
|
get(path) {
|
|
768
841
|
try {
|
|
769
842
|
let { value, owner } = this.getValue();
|
|
770
|
-
if (path.length === 0 && owner === null)
|
|
843
|
+
if (path.length === 0 && owner === null) {
|
|
844
|
+
if (value instanceof Object && "then" in value) return this.dup();
|
|
845
|
+
throw new Error("Can't dup an RpcTarget stub as a promise.");
|
|
846
|
+
}
|
|
771
847
|
let followResult = followPath(value, void 0, path, owner);
|
|
772
848
|
if (followResult.hook) return followResult.hook.get(followResult.remainingPath);
|
|
773
849
|
return new PayloadStubHook(RpcPayload.deepCopyFrom(followResult.value, followResult.parent, followResult.owner));
|
|
@@ -870,7 +946,10 @@ var TargetStubHook = class TargetStubHook extends ValueStubHook {
|
|
|
870
946
|
this.target = void 0;
|
|
871
947
|
}
|
|
872
948
|
}
|
|
873
|
-
onBroken(callback) {
|
|
949
|
+
onBroken(callback) {
|
|
950
|
+
let target = this.target;
|
|
951
|
+
if (target && "then" in target) Promise.resolve(target).then(() => {}, callback);
|
|
952
|
+
}
|
|
874
953
|
};
|
|
875
954
|
var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
876
955
|
promise;
|
|
@@ -884,12 +963,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
884
963
|
}
|
|
885
964
|
call(path, args) {
|
|
886
965
|
args.ensureDeepCopied();
|
|
887
|
-
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args))
|
|
966
|
+
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
|
|
967
|
+
args.dispose();
|
|
968
|
+
throw err;
|
|
969
|
+
}));
|
|
888
970
|
}
|
|
889
971
|
stream(path, args) {
|
|
890
972
|
args.ensureDeepCopied();
|
|
891
|
-
return { promise: this.promise.then((hook) => {
|
|
892
|
-
|
|
973
|
+
return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
|
|
974
|
+
args.dispose();
|
|
975
|
+
throw err;
|
|
893
976
|
}) };
|
|
894
977
|
}
|
|
895
978
|
map(path, captures, instructions) {
|
|
@@ -916,10 +999,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
916
999
|
}, (err) => {});
|
|
917
1000
|
}
|
|
918
1001
|
dispose() {
|
|
919
|
-
|
|
920
|
-
else this.promise.then((hook) => {
|
|
921
|
-
hook.dispose();
|
|
922
|
-
}, (err) => {});
|
|
1002
|
+
this.promise.then((hook) => hook.dispose(), () => {});
|
|
923
1003
|
}
|
|
924
1004
|
onBroken(callback) {
|
|
925
1005
|
if (this.resolution) this.resolution.onBroken(callback);
|
|
@@ -937,6 +1017,71 @@ const DEFAULT_LIMITS = {
|
|
|
937
1017
|
maxDepth: 256,
|
|
938
1018
|
maxMessageSize: 32 * 1024 * 1024
|
|
939
1019
|
};
|
|
1020
|
+
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
1021
|
+
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
1022
|
+
"ArrayBuffer",
|
|
1023
|
+
"DataView",
|
|
1024
|
+
"Int8Array",
|
|
1025
|
+
"Uint8Array",
|
|
1026
|
+
"Uint8ClampedArray",
|
|
1027
|
+
"Int16Array",
|
|
1028
|
+
"Uint16Array",
|
|
1029
|
+
"Int32Array",
|
|
1030
|
+
"Uint32Array",
|
|
1031
|
+
"BigInt64Array",
|
|
1032
|
+
"BigUint64Array",
|
|
1033
|
+
"Float32Array",
|
|
1034
|
+
"Float64Array"
|
|
1035
|
+
];
|
|
1036
|
+
function isValidByteContainerName(value) {
|
|
1037
|
+
return BYTE_CONTAINER_TYPE_NAMES.includes(value);
|
|
1038
|
+
}
|
|
1039
|
+
const TYPED_ARRAY_ELEMENT_SIZE = {
|
|
1040
|
+
ArrayBuffer: void 0,
|
|
1041
|
+
DataView: void 0,
|
|
1042
|
+
Int8Array: void 0,
|
|
1043
|
+
Uint8Array: void 0,
|
|
1044
|
+
Uint8ClampedArray: void 0,
|
|
1045
|
+
Int16Array: 2,
|
|
1046
|
+
Uint16Array: 2,
|
|
1047
|
+
Int32Array: 4,
|
|
1048
|
+
Uint32Array: 4,
|
|
1049
|
+
BigInt64Array: 8,
|
|
1050
|
+
BigUint64Array: 8,
|
|
1051
|
+
Float32Array: 4,
|
|
1052
|
+
Float64Array: 8
|
|
1053
|
+
};
|
|
1054
|
+
const BYTE_CONTAINER_PROTOTYPES = {
|
|
1055
|
+
ArrayBuffer: ArrayBuffer.prototype,
|
|
1056
|
+
DataView: DataView.prototype,
|
|
1057
|
+
Int8Array: Int8Array.prototype,
|
|
1058
|
+
Uint8ClampedArray: Uint8ClampedArray.prototype,
|
|
1059
|
+
Int16Array: Int16Array.prototype,
|
|
1060
|
+
Uint16Array: Uint16Array.prototype,
|
|
1061
|
+
Int32Array: Int32Array.prototype,
|
|
1062
|
+
Uint32Array: Uint32Array.prototype,
|
|
1063
|
+
BigInt64Array: BigInt64Array.prototype,
|
|
1064
|
+
BigUint64Array: BigUint64Array.prototype,
|
|
1065
|
+
Float32Array: Float32Array.prototype,
|
|
1066
|
+
Float64Array: Float64Array.prototype
|
|
1067
|
+
};
|
|
1068
|
+
const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
|
|
1069
|
+
for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
|
|
1070
|
+
function swapByteOrder(bytes, elementSize) {
|
|
1071
|
+
if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
|
|
1072
|
+
let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1073
|
+
for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
|
|
1074
|
+
case 2:
|
|
1075
|
+
view.setUint16(offset, view.getUint16(offset, false), true);
|
|
1076
|
+
break;
|
|
1077
|
+
case 4:
|
|
1078
|
+
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1079
|
+
break;
|
|
1080
|
+
case 8:
|
|
1081
|
+
view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1082
|
+
break;
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
940
1085
|
var NullExporter = class {
|
|
941
1086
|
exportStub(stub) {
|
|
942
1087
|
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
@@ -956,7 +1101,8 @@ async function streamToBlob(stream, type) {
|
|
|
956
1101
|
let b = await new Response(stream).blob();
|
|
957
1102
|
return b.type === type ? b : b.slice(0, b.size, type);
|
|
958
1103
|
}
|
|
959
|
-
const ERROR_TYPES =
|
|
1104
|
+
const ERROR_TYPES = {
|
|
1105
|
+
__proto__: null,
|
|
960
1106
|
Error,
|
|
961
1107
|
EvalError,
|
|
962
1108
|
RangeError,
|
|
@@ -965,7 +1111,7 @@ const ERROR_TYPES = Object.assign(Object.create(null), {
|
|
|
965
1111
|
TypeError,
|
|
966
1112
|
URIError,
|
|
967
1113
|
AggregateError
|
|
968
|
-
}
|
|
1114
|
+
};
|
|
969
1115
|
var Devaluator = class Devaluator {
|
|
970
1116
|
exporter;
|
|
971
1117
|
source;
|
|
@@ -1027,18 +1173,40 @@ var Devaluator = class Devaluator {
|
|
|
1027
1173
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1028
1174
|
}
|
|
1029
1175
|
case "bytes": {
|
|
1030
|
-
let
|
|
1031
|
-
|
|
1032
|
-
if (
|
|
1176
|
+
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1177
|
+
let bytes;
|
|
1178
|
+
if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
|
|
1179
|
+
else if (alternateTypeName === void 0) bytes = value;
|
|
1180
|
+
else {
|
|
1181
|
+
let view = value;
|
|
1182
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1183
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
|
|
1184
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize) {
|
|
1185
|
+
bytes = bytes.slice();
|
|
1186
|
+
swapByteOrder(bytes, elementSize);
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1190
|
+
"bytes",
|
|
1191
|
+
bytes,
|
|
1192
|
+
alternateTypeName
|
|
1193
|
+
];
|
|
1033
1194
|
let b64;
|
|
1034
|
-
if (
|
|
1195
|
+
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1196
|
+
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
1035
1197
|
else {
|
|
1036
1198
|
let binary = "";
|
|
1037
1199
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
1038
1200
|
b64 = btoa(binary);
|
|
1039
1201
|
}
|
|
1040
|
-
|
|
1202
|
+
b64 = b64.replace(/=+$/, "");
|
|
1203
|
+
return alternateTypeName === void 0 ? ["bytes", b64] : [
|
|
1204
|
+
"bytes",
|
|
1205
|
+
b64,
|
|
1206
|
+
alternateTypeName
|
|
1207
|
+
];
|
|
1041
1208
|
}
|
|
1209
|
+
case "url": return ["url", value.href];
|
|
1042
1210
|
case "headers": return ["headers", [...value]];
|
|
1043
1211
|
case "request": {
|
|
1044
1212
|
let req = value;
|
|
@@ -1290,18 +1458,46 @@ var Evaluator = class Evaluator {
|
|
|
1290
1458
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1291
1459
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1292
1460
|
break;
|
|
1293
|
-
case "bytes":
|
|
1294
|
-
|
|
1295
|
-
if (
|
|
1296
|
-
else if (
|
|
1461
|
+
case "bytes": {
|
|
1462
|
+
let bytes;
|
|
1463
|
+
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1464
|
+
else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1465
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1297
1466
|
else {
|
|
1298
1467
|
let bs = atob(value[1]);
|
|
1299
1468
|
let len = bs.length;
|
|
1300
|
-
|
|
1469
|
+
bytes = new Uint8Array(len);
|
|
1301
1470
|
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1302
|
-
return bytes;
|
|
1303
1471
|
}
|
|
1304
|
-
break;
|
|
1472
|
+
else break;
|
|
1473
|
+
if (value.length === 2) return bytes;
|
|
1474
|
+
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1475
|
+
if (!isValidByteContainerName(value[2])) {
|
|
1476
|
+
let marker = value[2].slice(0, 64);
|
|
1477
|
+
throw new TypeError(`Unknown bytes type marker: ${marker}`);
|
|
1478
|
+
}
|
|
1479
|
+
let marker = value[2];
|
|
1480
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
|
|
1481
|
+
if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
|
|
1482
|
+
let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
1483
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
|
|
1484
|
+
switch (marker) {
|
|
1485
|
+
case "ArrayBuffer": return buffer;
|
|
1486
|
+
case "DataView": return new DataView(buffer);
|
|
1487
|
+
case "Int8Array": return new Int8Array(buffer);
|
|
1488
|
+
case "Uint8Array": return new Uint8Array(buffer);
|
|
1489
|
+
case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
|
|
1490
|
+
case "Int16Array": return new Int16Array(buffer);
|
|
1491
|
+
case "Uint16Array": return new Uint16Array(buffer);
|
|
1492
|
+
case "Int32Array": return new Int32Array(buffer);
|
|
1493
|
+
case "Uint32Array": return new Uint32Array(buffer);
|
|
1494
|
+
case "BigInt64Array": return new BigInt64Array(buffer);
|
|
1495
|
+
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1496
|
+
case "Float32Array": return new Float32Array(buffer);
|
|
1497
|
+
case "Float64Array": return new Float64Array(buffer);
|
|
1498
|
+
default:
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1305
1501
|
case "error":
|
|
1306
1502
|
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1307
1503
|
let cls = ERROR_TYPES[value[1]] || Error;
|
|
@@ -1330,6 +1526,9 @@ var Evaluator = class Evaluator {
|
|
|
1330
1526
|
case "inf": return Infinity;
|
|
1331
1527
|
case "-inf": return -Infinity;
|
|
1332
1528
|
case "nan": return NaN;
|
|
1529
|
+
case "url":
|
|
1530
|
+
if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
|
|
1531
|
+
break;
|
|
1333
1532
|
case "headers":
|
|
1334
1533
|
if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
|
|
1335
1534
|
break;
|
|
@@ -1551,10 +1750,6 @@ var ImportTableEntry = class {
|
|
|
1551
1750
|
resolution;
|
|
1552
1751
|
onBrokenRegistrations;
|
|
1553
1752
|
resolve(resolution) {
|
|
1554
|
-
if (this.resolution) {
|
|
1555
|
-
resolution.dispose();
|
|
1556
|
-
return;
|
|
1557
|
-
}
|
|
1558
1753
|
if (this.localRefcount == 0) {
|
|
1559
1754
|
resolution.dispose();
|
|
1560
1755
|
return;
|
|
@@ -1633,24 +1828,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
|
|
|
1633
1828
|
if (this.entry) return this.entry;
|
|
1634
1829
|
else throw new Error("This RpcImportHook was already disposed.");
|
|
1635
1830
|
}
|
|
1831
|
+
getEntryTakingOwnership(disposeOwned) {
|
|
1832
|
+
try {
|
|
1833
|
+
return this.getEntry();
|
|
1834
|
+
} catch (err) {
|
|
1835
|
+
disposeOwned();
|
|
1836
|
+
throw err;
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1636
1839
|
call(path, args) {
|
|
1637
|
-
let entry = this.
|
|
1840
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1638
1841
|
if (entry.resolution) return entry.resolution.call(path, args);
|
|
1639
1842
|
else return entry.session.sendCall(entry.importId, path, args);
|
|
1640
1843
|
}
|
|
1641
1844
|
stream(path, args) {
|
|
1642
|
-
let entry = this.
|
|
1845
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1643
1846
|
if (entry.resolution) return entry.resolution.stream(path, args);
|
|
1644
1847
|
else return entry.session.sendStream(entry.importId, path, args);
|
|
1645
1848
|
}
|
|
1646
1849
|
map(path, captures, instructions) {
|
|
1647
|
-
let entry
|
|
1648
|
-
try {
|
|
1649
|
-
entry = this.getEntry();
|
|
1650
|
-
} catch (err) {
|
|
1850
|
+
let entry = this.getEntryTakingOwnership(() => {
|
|
1651
1851
|
for (let cap of captures) cap.dispose();
|
|
1652
|
-
|
|
1653
|
-
}
|
|
1852
|
+
});
|
|
1654
1853
|
if (entry.resolution) return entry.resolution.map(path, captures, instructions);
|
|
1655
1854
|
else return entry.session.sendMap(entry.importId, path, captures, instructions);
|
|
1656
1855
|
}
|
|
@@ -1911,14 +2110,23 @@ var RpcSessionImpl = class {
|
|
|
1911
2110
|
}
|
|
1912
2111
|
}
|
|
1913
2112
|
sendCall(id, path, args) {
|
|
1914
|
-
if (this.abortReason)
|
|
2113
|
+
if (this.abortReason) {
|
|
2114
|
+
args?.dispose();
|
|
2115
|
+
throw this.abortReason;
|
|
2116
|
+
}
|
|
1915
2117
|
let value = [
|
|
1916
2118
|
"pipeline",
|
|
1917
2119
|
id,
|
|
1918
2120
|
path
|
|
1919
2121
|
];
|
|
1920
2122
|
if (args) {
|
|
1921
|
-
let devalue
|
|
2123
|
+
let devalue;
|
|
2124
|
+
try {
|
|
2125
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2126
|
+
} catch (err) {
|
|
2127
|
+
args.dispose();
|
|
2128
|
+
throw err;
|
|
2129
|
+
}
|
|
1922
2130
|
value.push(devalue[0]);
|
|
1923
2131
|
}
|
|
1924
2132
|
this.send(["push", value]);
|
|
@@ -1927,13 +2135,22 @@ var RpcSessionImpl = class {
|
|
|
1927
2135
|
return new RpcImportHook(true, entry);
|
|
1928
2136
|
}
|
|
1929
2137
|
sendStream(id, path, args) {
|
|
1930
|
-
if (this.abortReason)
|
|
2138
|
+
if (this.abortReason) {
|
|
2139
|
+
args.dispose();
|
|
2140
|
+
throw this.abortReason;
|
|
2141
|
+
}
|
|
1931
2142
|
let value = [
|
|
1932
2143
|
"pipeline",
|
|
1933
2144
|
id,
|
|
1934
2145
|
path
|
|
1935
2146
|
];
|
|
1936
|
-
let devalue
|
|
2147
|
+
let devalue;
|
|
2148
|
+
try {
|
|
2149
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2150
|
+
} catch (err) {
|
|
2151
|
+
args.dispose();
|
|
2152
|
+
throw err;
|
|
2153
|
+
}
|
|
1937
2154
|
value.push(devalue[0]);
|
|
1938
2155
|
let msg = ["stream", value];
|
|
1939
2156
|
let size = this.send(msg);
|
|
@@ -2154,6 +2371,8 @@ var RpcSession$1 = class {
|
|
|
2154
2371
|
|
|
2155
2372
|
//#endregion
|
|
2156
2373
|
//#region src/websocket.ts
|
|
2374
|
+
/** Close-frame reason max UTF-8 bytes: 125 payload - 2-byte status (RFC 6455 section 5.5). */
|
|
2375
|
+
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2157
2376
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2158
2377
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2159
2378
|
return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
|
|
@@ -2248,6 +2467,7 @@ var WebSocketTransport = class {
|
|
|
2248
2467
|
|
|
2249
2468
|
//#endregion
|
|
2250
2469
|
//#region src/batch.ts
|
|
2470
|
+
const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
2251
2471
|
var BatchClientTransport = class {
|
|
2252
2472
|
constructor(sendBatch) {
|
|
2253
2473
|
this.#promise = this.#scheduleBatch(sendBatch);
|
|
@@ -2269,7 +2489,7 @@ var BatchClientTransport = class {
|
|
|
2269
2489
|
this.#aborted = reason;
|
|
2270
2490
|
}
|
|
2271
2491
|
async #scheduleBatch(sendBatch) {
|
|
2272
|
-
await
|
|
2492
|
+
await yieldToMacrotask();
|
|
2273
2493
|
if (this.#aborted !== void 0) throw this.#aborted;
|
|
2274
2494
|
let batch = this.#batchToSend;
|
|
2275
2495
|
this.#batchToSend = null;
|
|
@@ -2561,9 +2781,11 @@ var MapVariableHook = class extends StubHook {
|
|
|
2561
2781
|
else throwMapperBuilderUseError();
|
|
2562
2782
|
}
|
|
2563
2783
|
call(path, args) {
|
|
2784
|
+
args.dispose();
|
|
2564
2785
|
throwMapperBuilderUseError();
|
|
2565
2786
|
}
|
|
2566
2787
|
map(path, captures, instructions) {
|
|
2788
|
+
for (let cap of captures) cap.dispose();
|
|
2567
2789
|
throwMapperBuilderUseError();
|
|
2568
2790
|
}
|
|
2569
2791
|
pull() {
|
|
@@ -2674,14 +2896,11 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2674
2896
|
let state = this.getState();
|
|
2675
2897
|
if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
|
|
2676
2898
|
const method = path[0];
|
|
2677
|
-
if (method !== "write" && method !== "close" && method !== "abort") {
|
|
2678
|
-
args.dispose();
|
|
2679
|
-
throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2680
|
-
}
|
|
2899
|
+
if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2681
2900
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2682
|
-
|
|
2683
|
-
return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
|
|
2901
|
+
return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
|
|
2684
2902
|
} catch (err) {
|
|
2903
|
+
args.dispose();
|
|
2685
2904
|
return new ErrorStubHook(err);
|
|
2686
2905
|
}
|
|
2687
2906
|
}
|