@vastblast/capnweb 0.4.3 → 0.5.0

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.

Potentially problematic release.


This version of @vastblast/capnweb might be problematic. Click here for more details.

package/dist/index.d.ts CHANGED
@@ -1,207 +1,212 @@
1
1
  import { IncomingMessage, ServerResponse, OutgoingHttpHeaders, OutgoingHttpHeader } from 'node:http';
2
2
 
3
- // Copyright (c) 2025 Cloudflare, Inc.
4
- // Licensed under the MIT license found in the LICENSE.txt file or at:
5
- // https://opensource.org/license/mit
6
-
7
- // This file borrows heavily from `types/defines/rpc.d.ts` in workerd.
8
-
9
- // Branded types for identifying `WorkerEntrypoint`/`DurableObject`/`Target`s.
10
- // TypeScript uses *structural* typing meaning anything with the same shape as type `T` is a `T`.
11
- // For the classes exported by `cloudflare:workers` we want *nominal* typing (i.e. we only want to
12
- // accept `WorkerEntrypoint` from `cloudflare:workers`, not any other class with the same shape)
13
- declare const __RPC_STUB_BRAND: '__RPC_STUB_BRAND';
14
- declare const __RPC_TARGET_BRAND: '__RPC_TARGET_BRAND';
15
- interface RpcTargetBranded {
16
- [__RPC_TARGET_BRAND]: never;
17
- }
18
-
19
- type IsAny<T> = 0 extends (1 & T) ? true : false;
20
- type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) : false;
21
-
22
- // Types that can be used through `Stub`s.
23
- // `never[]` preserves compatibility with strongly-typed function signatures without introducing
24
- // `any` into inference.
25
- type Stubable = RpcTargetBranded | ((...args: never[]) => unknown);
26
-
27
- type TypedArray =
28
- | Uint8Array
29
- | Uint8ClampedArray
30
- | Uint16Array
31
- | Uint32Array
32
- | Int8Array
33
- | Int16Array
34
- | Int32Array
35
- | BigUint64Array
36
- | BigInt64Array
37
- | Float32Array
38
- | Float64Array;
39
-
40
- // This represents all the types that can be sent as-is over an RPC boundary.
41
- type BaseType =
42
- | void
43
- | undefined
44
- | null
45
- | boolean
46
- | number
47
- | bigint
48
- | string
49
- | TypedArray
50
- | ArrayBuffer
51
- | DataView
52
- | Date
53
- | Error
54
- | RegExp
55
- | ReadableStream<Uint8Array>
56
- | WritableStream<any> // Chunk type can be any RPC-compatible type
57
- | Request
58
- | Response
59
- | Headers;
60
-
61
- // Base type for all RPC stubs, including common memory management methods.
62
- // `T` is used as a marker type for unwrapping `Stub`s later.
63
- interface StubBase<T = unknown> extends Disposable {
64
- [__RPC_STUB_BRAND]: T;
65
- dup(): this;
66
- onRpcBroken(callback: (error: any) => void): void;
67
- }
68
-
69
- // Types that can be passed over RPC.
70
- // The reason for using a generic type here is to build a serializable subset of structured
71
- // cloneable composite types. This allows types defined with the "interface" keyword to pass the
72
- // serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
73
- type RpcCompatible<T> =
74
- // Structured cloneables
75
- | BaseType
76
- // Structured cloneable composites
77
- | Map<
78
- T extends Map<infer U, unknown> ? RpcCompatible<U> : never,
79
- T extends Map<unknown, infer U> ? RpcCompatible<U> : never
80
- >
81
- | Set<T extends Set<infer U> ? RpcCompatible<U> : never>
82
- | Array<T extends Array<infer U> ? RpcCompatible<U> : never>
83
- | ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never>
84
- | {
85
- [K in keyof T as K extends string | number ? K : never]: RpcCompatible<T[K]>;
86
- }
87
- | Promise<T extends Promise<infer U> ? RpcCompatible<U> : never>
88
- // Special types
89
- | Stub<Stubable>
90
- // Serialized as stubs, see `Stubify`
91
- | Stubable;
92
-
93
- type Stub<T extends RpcCompatible<T>> =
94
- T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
95
-
96
- // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
97
- // prettier-ignore
98
- type Stubify<T> =
99
- T extends Stubable ? Stub<T>
100
- : T extends Promise<infer U> ? Stubify<U>
101
- : T extends StubBase<any> ? T
102
- : T extends Map<infer K, infer V> ? Map<Stubify<K>, Stubify<V>>
103
- : T extends Set<infer V> ? Set<Stubify<V>>
104
- : T extends [] ? []
105
- : T extends [infer Head, ...infer Tail] ? [Stubify<Head>, ...Stubify<Tail>]
106
- : T extends readonly [] ? readonly []
107
- : T extends readonly [infer Head, ...infer Tail] ? readonly [Stubify<Head>, ...Stubify<Tail>]
108
- : T extends Array<infer V> ? Array<Stubify<V>>
109
- : T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V>>
110
- : T extends BaseType ? T
111
- // When using "unknown" instead of "any", interfaces are not stubified.
112
- : T extends { [key: string | number]: any }
113
- ? {
114
- [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]>;
115
- }
116
- : T;
117
-
118
- // Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
119
- // Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
120
- // `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
121
- // prettier-ignore
122
- type UnstubifyInner<T> =
123
- // Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
124
- // is already assignable to the value type (important for callback contextual typing).
125
- T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyInner<V>))
126
- : T extends Promise<infer U> ? UnstubifyInner<U>
127
- : T extends Map<infer K, infer V> ? Map<Unstubify<K>, Unstubify<V>>
128
- : T extends Set<infer V> ? Set<Unstubify<V>>
129
- : T extends [] ? []
130
- : T extends [infer Head, ...infer Tail] ? [Unstubify<Head>, ...Unstubify<Tail>]
131
- : T extends readonly [] ? readonly []
132
- : T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head>, ...Unstubify<Tail>]
133
- : T extends Array<infer V> ? Array<Unstubify<V>>
134
- : T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>>
135
- : T extends BaseType ? T
136
- : T extends { [key: string | number]: unknown }
137
- ? {
138
- [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]>;
139
- }
140
- : T;
141
-
142
- // You can put promises anywhere in the params and they'll be resolved before delivery.
143
- // (This also covers RpcPromise, because it's defined as being a Promise.)
144
- type Unstubify<T> = UnstubifyInner<T> | Promise<UnstubifyInner<T>>;
145
-
146
- type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]> };
147
-
148
- // Utility type for adding `Disposable`s to `object` types only.
149
- // Note `unknown & T` is equivalent to `T`.
150
- type MaybeDisposable<T> = T extends object ? Disposable : unknown;
151
-
152
- // Type for method return or property on an RPC interface.
153
- // - Stubable types are replaced by stubs.
154
- // - RpcCompatible types are passed by value, with stubable types replaced by stubs
155
- // and a top-level `Disposer`.
156
- // Everything else can't be passed over RPC.
157
- // Technically, we use custom thenables here, but they quack like `Promise`s.
158
- // Intersecting with `(Maybe)Provider` allows pipelining.
159
- type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>;
160
-
161
- // prettier-ignore
162
- type Result<R> =
163
- IsAny<R> extends true ? UnknownResult
164
- : IsUnknown<R> extends true ? UnknownResult
165
- : R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R>
166
- : R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R>
167
- : never;
168
-
169
- type HasAnyFunctionParts<P extends readonly unknown[], R> =
170
- IsAny<R> extends true ? true : IsAny<P[number]>;
171
-
172
- // Type for method or property on an RPC interface.
173
- // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
174
- // Unwrapping `Stub`s allows calling with `Stubable` arguments.
175
- // For properties, rewrite types to be `Result`s.
176
- // In each case, unwrap `Promise`s.
177
- type MethodOrProperty<V> = V extends (...args: infer P) => infer R
178
- ? HasAnyFunctionParts<P, R> extends true
179
- ? (...args: unknown[]) => UnknownResult
180
- : (...args: UnstubifyAll<P>) => Result<Awaited<R>>
181
- : Result<Awaited<V>>;
182
-
183
- // Type for the callable part of a `Provider` if `T` is callable.
184
- // This is intersected with methods/properties.
185
- type MaybeCallableProvider<T> = T extends (...args: any[]) => any
186
- ? MethodOrProperty<T>
187
- : unknown;
188
-
189
- // Base type for all other types providing RPC-like interfaces.
190
- // Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
191
- type Provider<T> = MaybeCallableProvider<T> &
192
- (T extends Array<infer U>
193
- ? {
194
- [key: number]: MethodOrProperty<U>;
195
- } & {
196
- map<V>(callback: (elem: Result<U>) => V): Result<Array<V>>;
197
- }
198
- : {
199
- [K in Exclude<
200
- keyof T,
201
- symbol | keyof StubBase<never>
202
- >]: MethodOrProperty<T[K]>;
203
- } & {
204
- map<V>(callback: (value: Result<NonNullable<T>>) => V): Result<Array<V>>;
3
+ // Copyright (c) 2025 Cloudflare, Inc.
4
+ // Licensed under the MIT license found in the LICENSE.txt file or at:
5
+ // https://opensource.org/license/mit
6
+
7
+ // This file borrows heavily from `types/defines/rpc.d.ts` in workerd.
8
+
9
+ // Branded types for identifying `WorkerEntrypoint`/`DurableObject`/`Target`s.
10
+ // TypeScript uses *structural* typing meaning anything with the same shape as type `T` is a `T`.
11
+ // For the classes exported by `cloudflare:workers` we want *nominal* typing (i.e. we only want to
12
+ // accept `WorkerEntrypoint` from `cloudflare:workers`, not any other class with the same shape)
13
+ declare const __RPC_STUB_BRAND: '__RPC_STUB_BRAND';
14
+ declare const __RPC_TARGET_BRAND: '__RPC_TARGET_BRAND';
15
+ interface RpcTargetBranded {
16
+ [__RPC_TARGET_BRAND]: never;
17
+ }
18
+
19
+ type IsAny<T> = 0 extends (1 & T) ? true : false;
20
+ type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) : false;
21
+
22
+ // Types that can be used through `Stub`s.
23
+ // `never[]` preserves compatibility with strongly-typed function signatures without introducing
24
+ // `any` into inference.
25
+ type Stubable = RpcTargetBranded | ((...args: never[]) => unknown);
26
+
27
+ type TypedArray =
28
+ | Uint8Array
29
+ | Uint8ClampedArray
30
+ | Uint16Array
31
+ | Uint32Array
32
+ | Int8Array
33
+ | Int16Array
34
+ | Int32Array
35
+ | BigUint64Array
36
+ | BigInt64Array
37
+ | Float32Array
38
+ | Float64Array;
39
+
40
+ // This represents all the types that can be sent as-is over an RPC boundary
41
+ type BaseType =
42
+ | void
43
+ | undefined
44
+ | null
45
+ | boolean
46
+ | number
47
+ | bigint
48
+ | string
49
+ | TypedArray
50
+ | ArrayBuffer
51
+ | DataView
52
+ | Date
53
+ | Error
54
+ | RegExp
55
+ | ReadableStream<Uint8Array>
56
+ | WritableStream<any> // Chunk type can be any RPC-compatible type
57
+ | Request
58
+ | Response
59
+ | Headers;
60
+
61
+ // Base type for all RPC stubs, including common memory management methods.
62
+ // `T` is used as a marker type for unwrapping `Stub`s later.
63
+ interface StubBase<T = unknown> extends Disposable {
64
+ [__RPC_STUB_BRAND]: T;
65
+ dup(): this;
66
+ onRpcBroken(callback: (error: any) => void): void;
67
+ }
68
+
69
+ // Types that can be passed over RPC.
70
+ // The reason for using a generic type here is to build a serializable subset of structured
71
+ // cloneable composite types. This allows types defined with the "interface" keyword to pass the
72
+ // serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
73
+ type RpcCompatible<T> =
74
+ // Structured cloneables
75
+ | BaseType
76
+ // Structured cloneable composites
77
+ | Map<
78
+ T extends Map<infer U, unknown> ? RpcCompatible<U> : never,
79
+ T extends Map<unknown, infer U> ? RpcCompatible<U> : never
80
+ >
81
+ | Set<T extends Set<infer U> ? RpcCompatible<U> : never>
82
+ | Array<T extends Array<infer U> ? RpcCompatible<U> : never>
83
+ | ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never>
84
+ | {
85
+ [K in keyof T as K extends string | number ? K : never]: RpcCompatible<T[K]>;
86
+ }
87
+ | Promise<T extends Promise<infer U> ? RpcCompatible<U> : never>
88
+ // Special types
89
+ | Stub<Stubable>
90
+ // Serialized as stubs, see `Stubify`
91
+ | Stubable;
92
+
93
+ type Stub<T extends RpcCompatible<T>> =
94
+ T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
95
+
96
+ // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
97
+ // prettier-ignore
98
+ type Stubify<T> =
99
+ T extends Stubable ? Stub<T>
100
+ : T extends Promise<infer U> ? Stubify<U>
101
+ : T extends StubBase<any> ? T
102
+ : T extends Map<infer K, infer V> ? Map<Stubify<K>, Stubify<V>>
103
+ : T extends Set<infer V> ? Set<Stubify<V>>
104
+ : T extends [] ? []
105
+ : T extends [infer Head, ...infer Tail] ? [Stubify<Head>, ...Stubify<Tail>]
106
+ : T extends readonly [] ? readonly []
107
+ : T extends readonly [infer Head, ...infer Tail] ? readonly [Stubify<Head>, ...Stubify<Tail>]
108
+ : T extends Array<infer V> ? Array<Stubify<V>>
109
+ : T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V>>
110
+ : T extends BaseType ? T
111
+ // When using "unknown" instead of "any", interfaces are not stubified.
112
+ : T extends { [key: string | number]: any }
113
+ ? {
114
+ [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]>;
115
+ }
116
+ : T;
117
+
118
+ // Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
119
+ // Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
120
+ // `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
121
+ // prettier-ignore
122
+ type UnstubifyInner<T> =
123
+ // Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
124
+ // is already assignable to the value type (important for callback contextual typing).
125
+ T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyInner<V>))
126
+ : T extends Promise<infer U> ? UnstubifyInner<U>
127
+ : T extends Map<infer K, infer V> ? Map<Unstubify<K>, Unstubify<V>>
128
+ : T extends Set<infer V> ? Set<Unstubify<V>>
129
+ : T extends [] ? []
130
+ : T extends [infer Head, ...infer Tail] ? [Unstubify<Head>, ...Unstubify<Tail>]
131
+ : T extends readonly [] ? readonly []
132
+ : T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head>, ...Unstubify<Tail>]
133
+ : T extends Array<infer V> ? Array<Unstubify<V>>
134
+ : T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>>
135
+ : T extends BaseType ? T
136
+ : T extends { [key: string | number]: unknown }
137
+ ? {
138
+ [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]>;
139
+ }
140
+ : T;
141
+
142
+ // You can put promises anywhere in the params and they'll be resolved before delivery.
143
+ // (This also covers RpcPromise, because it's defined as being a Promise.)
144
+ type Unstubify<T> = UnstubifyInner<T> | Promise<UnstubifyInner<T>>;
145
+
146
+ type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]> };
147
+
148
+ // Utility type for adding `Disposable`s to `object` types only.
149
+ // Note `unknown & T` is equivalent to `T`.
150
+ type MaybeDisposable<T> = T extends object ? Disposable : unknown;
151
+
152
+ // Type for method return or property on an RPC interface.
153
+ // - Stubable types are replaced by stubs.
154
+ // - RpcCompatible types are passed by value, with stubable types replaced by stubs
155
+ // and a top-level `Disposer`.
156
+ // Everything else can't be passed over RPC.
157
+ // Technically, we use custom thenables here, but they quack like `Promise`s.
158
+ // Intersecting with `(Maybe)Provider` allows pipelining.
159
+ type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>;
160
+
161
+ // prettier-ignore
162
+ type Result<R> =
163
+ IsAny<R> extends true ? UnknownResult
164
+ : IsUnknown<R> extends true ? UnknownResult
165
+ : R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R>
166
+ : R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R>
167
+ : never;
168
+
169
+ // Type for method or property on an RPC interface.
170
+ // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
171
+ // Unwrapping `Stub`s allows calling with `Stubable` arguments.
172
+ // For properties, rewrite types to be `Result`s.
173
+ // In each case, unwrap `Promise`s.
174
+ type MethodOrProperty<V> = V extends (...args: infer P) => infer R
175
+ ? (...args: UnstubifyAll<P>) => (IsAny<R> extends true ? UnknownResult : Result<Awaited<R>>)
176
+ : Result<Awaited<V>>;
177
+
178
+ // Type for the callable part of an `Provider` if `T` is callable.
179
+ // This is intersected with methods/properties.
180
+ type MaybeCallableProvider<T> = T extends (...args: any[]) => any
181
+ ? MethodOrProperty<T>
182
+ : unknown;
183
+
184
+ type TupleIndexKeys<T extends ReadonlyArray<unknown>> =
185
+ Extract<keyof T, `${number}`>;
186
+ type MapCallbackValue<T> =
187
+ // `Omit` removes call signatures, so re-intersect callable provider behavior.
188
+ T extends unknown ? Omit<Result<T>, keyof Promise<unknown>> & MaybeCallableProvider<T> : never;
189
+ type ArrayProvider<E> = {
190
+ [K in number]: MethodOrProperty<E>;
191
+ } & {
192
+ map<V>(callback: (elem: MapCallbackValue<E>) => V): Result<Array<V>>;
193
+ };
194
+ type TupleProvider<T extends ReadonlyArray<unknown>> = {
195
+ [K in TupleIndexKeys<T>]: MethodOrProperty<T[K]>;
196
+ } & ArrayProvider<T[number]>;
197
+
198
+ // Base type for all other types providing RPC-like interfaces.
199
+ // Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
200
+ type Provider<T> = MaybeCallableProvider<T> &
201
+ (T extends ReadonlyArray<unknown>
202
+ ? number extends T["length"] ? ArrayProvider<T[number]> : TupleProvider<T>
203
+ : {
204
+ [K in Exclude<
205
+ keyof T,
206
+ symbol | keyof StubBase<never>
207
+ >]: MethodOrProperty<T[K]>;
208
+ } & {
209
+ map<V>(callback: (value: MapCallbackValue<NonNullable<T>>) => V): Result<Array<V>>;
205
210
  });
206
211
 
207
212
  /**
package/dist/index.js CHANGED
@@ -60,6 +60,12 @@ function typeForRpc(value) {
60
60
  return "writable";
61
61
  case ReadableStream.prototype:
62
62
  return "readable";
63
+ case Headers.prototype:
64
+ return "headers";
65
+ case Request.prototype:
66
+ return "request";
67
+ case Response.prototype:
68
+ return "response";
63
69
  // TODO: All other structured clone types.
64
70
  case RpcStub.prototype:
65
71
  return "stub";
@@ -613,6 +619,22 @@ var RpcPayload = class _RpcPayload {
613
619
  this.hooks.push(hook);
614
620
  return stream;
615
621
  }
622
+ case "headers":
623
+ return new Headers(value);
624
+ case "request": {
625
+ let req = value;
626
+ if (req.body) {
627
+ this.deepCopy(req.body, req, "body", req, dupStubs, owner);
628
+ }
629
+ return new Request(req);
630
+ }
631
+ case "response": {
632
+ let resp = value;
633
+ if (resp.body) {
634
+ this.deepCopy(resp.body, resp, "body", resp, dupStubs, owner);
635
+ }
636
+ return new Response(resp.body, resp);
637
+ }
616
638
  default:
617
639
  throw new Error("unreachable");
618
640
  }
@@ -792,6 +814,18 @@ var RpcPayload = class _RpcPayload {
792
814
  }
793
815
  case "rpc-thenable":
794
816
  return;
817
+ case "headers":
818
+ return;
819
+ case "request": {
820
+ let req = value;
821
+ if (req.body) this.disposeImpl(req.body, req);
822
+ return;
823
+ }
824
+ case "response": {
825
+ let resp = value;
826
+ if (resp.body) this.disposeImpl(resp.body, resp);
827
+ return;
828
+ }
795
829
  case "writable": {
796
830
  let stream = value;
797
831
  let hook = this.rpcTargets?.get(stream);
@@ -847,6 +881,9 @@ var RpcPayload = class _RpcPayload {
847
881
  case "rpc-target":
848
882
  case "writable":
849
883
  case "readable":
884
+ case "headers":
885
+ case "request":
886
+ case "response":
850
887
  return;
851
888
  case "array": {
852
889
  let array = value;
@@ -930,6 +967,9 @@ function followPath(value, parent, path, owner) {
930
967
  case "bytes":
931
968
  case "date":
932
969
  case "error":
970
+ case "headers":
971
+ case "request":
972
+ case "response":
933
973
  value = void 0;
934
974
  break;
935
975
  case "undefined":
@@ -1351,6 +1391,73 @@ var Devaluator = class _Devaluator {
1351
1391
  ];
1352
1392
  }
1353
1393
  }
1394
+ case "headers":
1395
+ return ["headers", [...value]];
1396
+ case "request": {
1397
+ let req = value;
1398
+ let init = {};
1399
+ if (req.method !== "GET") init.method = req.method;
1400
+ let headers = [...req.headers];
1401
+ if (headers.length > 0) {
1402
+ init.headers = headers;
1403
+ }
1404
+ if (req.body) {
1405
+ init.body = this.devaluateImpl(req.body, req, depth + 1);
1406
+ init.duplex = req.duplex || "half";
1407
+ } else if (req.body === void 0 && !["GET", "HEAD", "OPTIONS", "TRACE", "DELETE"].includes(req.method)) {
1408
+ let bodyPromise = req.arrayBuffer();
1409
+ let readable = new ReadableStream({
1410
+ async start(controller) {
1411
+ try {
1412
+ controller.enqueue(new Uint8Array(await bodyPromise));
1413
+ controller.close();
1414
+ } catch (err) {
1415
+ controller.error(err);
1416
+ }
1417
+ }
1418
+ });
1419
+ let hook = streamImpl.createReadableStreamHook(readable);
1420
+ let importId = this.exporter.createPipe(readable, hook);
1421
+ init.body = ["readable", importId];
1422
+ init.duplex = req.duplex || "half";
1423
+ }
1424
+ if (req.cache && req.cache !== "default") init.cache = req.cache;
1425
+ if (req.redirect !== "follow") init.redirect = req.redirect;
1426
+ if (req.integrity) init.integrity = req.integrity;
1427
+ if (req.mode && req.mode !== "cors") init.mode = req.mode;
1428
+ if (req.credentials && req.credentials !== "same-origin") {
1429
+ init.credentials = req.credentials;
1430
+ }
1431
+ if (req.referrer && req.referrer !== "about:client") init.referrer = req.referrer;
1432
+ if (req.referrerPolicy) init.referrerPolicy = req.referrerPolicy;
1433
+ if (req.keepalive) init.keepalive = req.keepalive;
1434
+ let cfReq = req;
1435
+ if (cfReq.cf) init.cf = cfReq.cf;
1436
+ if (cfReq.encodeResponseBody && cfReq.encodeResponseBody !== "automatic") {
1437
+ init.encodeResponseBody = cfReq.encodeResponseBody;
1438
+ }
1439
+ return ["request", req.url, init];
1440
+ }
1441
+ case "response": {
1442
+ let resp = value;
1443
+ let body = this.devaluateImpl(resp.body, resp, depth + 1);
1444
+ let init = {};
1445
+ if (resp.status !== 200) init.status = resp.status;
1446
+ if (resp.statusText) init.statusText = resp.statusText;
1447
+ let headers = [...resp.headers];
1448
+ if (headers.length > 0) {
1449
+ init.headers = headers;
1450
+ }
1451
+ let cfResp = resp;
1452
+ if (cfResp.cf) init.cf = cfResp.cf;
1453
+ if (cfResp.encodeBody && cfResp.encodeBody !== "automatic") {
1454
+ init.encodeBody = cfResp.encodeBody;
1455
+ }
1456
+ if (cfResp.webSocket) {
1457
+ throw new TypeError("Can't serialize a Response containing a webSocket.");
1458
+ }
1459
+ return ["response", body, init];
1460
+ }
1354
1461
  case "error": {
1355
1462
  let e = value;
1356
1463
  let rewritten = this.exporter.onSendError(e);
@@ -1450,6 +1557,14 @@ var NullImporter = class {
1450
1557
  }
1451
1558
  };
1452
1559
  var NULL_IMPORTER = new NullImporter();
1560
+ function fixBrokenRequestBody(request, body) {
1561
+ let promise = new Response(body).arrayBuffer().then((arrayBuffer) => {
1562
+ let bytes = new Uint8Array(arrayBuffer);
1563
+ let result = new Request(request, { body: bytes });
1564
+ return new PayloadStubHook(RpcPayload.fromAppReturn(result));
1565
+ });
1566
+ return new RpcPromise(new PromiseStubHook(promise), []);
1567
+ }
1453
1568
  var Evaluator = class _Evaluator {
1454
1569
  constructor(importer) {
1455
1570
  this.importer = importer;
@@ -1527,6 +1642,56 @@ var Evaluator = class _Evaluator {
1527
1642
  return -Infinity;
1528
1643
  case "nan":
1529
1644
  return NaN;
1645
+ case "headers":
1646
+ if (value.length === 2 && value[1] instanceof Array) {
1647
+ return new Headers(value[1]);
1648
+ }
1649
+ break;
1650
+ case "request": {
1651
+ if (value.length !== 3 || typeof value[1] !== "string") break;
1652
+ let url = value[1];
1653
+ let init = value[2];
1654
+ if (typeof init !== "object" || init === null) break;
1655
+ if (init.body) {
1656
+ init.body = this.evaluateImpl(init.body, init, "body");
1657
+ if (init.body === null || typeof init.body === "string" || init.body instanceof Uint8Array || init.body instanceof ReadableStream) ; else {
1658
+ throw new TypeError("Request body must be of type ReadableStream.");
1659
+ }
1660
+ }
1661
+ if (init.signal) {
1662
+ init.signal = this.evaluateImpl(init.signal, init, "signal");
1663
+ if (!(init.signal instanceof AbortSignal)) {
1664
+ throw new TypeError("Request siganl must be of type AbortSignal.");
1665
+ }
1666
+ }
1667
+ if (init.headers && !(init.headers instanceof Array)) {
1668
+ throw new TypeError("Request headers must be serialized as an array of pairs.");
1669
+ }
1670
+ let result = new Request(url, init);
1671
+ if (init.body instanceof ReadableStream && result.body === void 0) {
1672
+ let promise = fixBrokenRequestBody(result, init.body);
1673
+ this.promises.push({ promise, parent, property });
1674
+ return promise;
1675
+ } else {
1676
+ return result;
1677
+ }
1678
+ }
1679
+ case "response": {
1680
+ if (value.length !== 3) break;
1681
+ let body = this.evaluateImpl(value[1], parent, property);
1682
+ if (body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ReadableStream) ; else {
1683
+ throw new TypeError("Response body must be of type ReadableStream.");
1684
+ }
1685
+ let init = value[2];
1686
+ if (typeof init !== "object" || init === null) break;
1687
+ if (init.webSocket) {
1688
+ throw new TypeError("Can't deserialize a Response containing a webSocket.");
1689
+ }
1690
+ if (init.headers && !(init.headers instanceof Array)) {
1691
+ throw new TypeError("Request headers must be serialized as an array of pairs.");
1692
+ }
1693
+ return new Response(body, init);
1694
+ }
1530
1695
  case "import":
1531
1696
  case "pipeline": {
1532
1697
  if (value.length < 2 || value.length > 4) {