capnweb 0.6.1 → 0.8.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.
package/dist/index.d.ts CHANGED
@@ -1,444 +1,2 @@
1
- import { IncomingMessage, ServerResponse, OutgoingHttpHeaders, OutgoingHttpHeader } from 'node:http';
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
- // Distinguishes mapper placeholders from regular values so param unwrapping can accept them.
16
- declare const __RPC_MAP_VALUE_BRAND: unique symbol;
17
- interface RpcTargetBranded {
18
- [__RPC_TARGET_BRAND]: never;
19
- }
20
-
21
- // Types that can be used through `Stub`s
22
- // `never[]` preserves compatibility with strongly-typed function signatures without introducing
23
- // `any` into inference.
24
- type Stubable = RpcTargetBranded | ((...args: never[]) => unknown);
25
-
26
- type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) : false;
27
-
28
- // Types that can be passed over RPC
29
- // The reason for using a generic type here is to build a serializable subset of structured
30
- // cloneable composite types. This allows types defined with the "interface" keyword to pass the
31
- // serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
32
- type RpcCompatible<T> =
33
- // Allow `unknown` as a leaf so records/interfaces with `unknown` fields remain compatible.
34
- | (IsUnknown<T> extends true ? unknown : never)
35
- // Structured cloneables
36
- | BaseType
37
- // Structured cloneable composites
38
- | Map<
39
- T extends Map<infer U, unknown> ? RpcCompatible<U> : never,
40
- T extends Map<unknown, infer U> ? RpcCompatible<U> : never
41
- >
42
- | Set<T extends Set<infer U> ? RpcCompatible<U> : never>
43
- | Array<T extends Array<infer U> ? RpcCompatible<U> : never>
44
- | ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never>
45
- | {
46
- [K in keyof T as K extends string | number ? K : never]: RpcCompatible<T[K]>;
47
- }
48
- | Promise<T extends Promise<infer U> ? RpcCompatible<U> : never>
49
- // Special types
50
- | Stub<Stubable>
51
- // Serialized as stubs, see `Stubify`
52
- | Stubable;
53
-
54
- // Base type for all RPC stubs, including common memory management methods.
55
- // `T` is used as a marker type for unwrapping `Stub`s later.
56
- interface StubBase<T = unknown> extends Disposable {
57
- [__RPC_STUB_BRAND]: T;
58
- dup(): this;
59
- onRpcBroken(callback: (error: any) => void): void;
60
- }
61
- type Stub<T extends RpcCompatible<T>> =
62
- T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
63
-
64
- type TypedArray =
65
- | Uint8Array
66
- | Uint8ClampedArray
67
- | Uint16Array
68
- | Uint32Array
69
- | Int8Array
70
- | Int16Array
71
- | Int32Array
72
- | BigUint64Array
73
- | BigInt64Array
74
- | Float32Array
75
- | Float64Array;
76
-
77
- // This represents all the types that can be sent as-is over an RPC boundary
78
- type BaseType =
79
- | void
80
- | undefined
81
- | null
82
- | boolean
83
- | number
84
- | bigint
85
- | string
86
- | TypedArray
87
- | ArrayBuffer
88
- | DataView
89
- | Date
90
- | Error
91
- | RegExp
92
- | ReadableStream<Uint8Array>
93
- | WritableStream<any> // Chunk type can be any RPC-compatible type
94
- | Request
95
- | Response
96
- | Headers;
97
- // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
98
- // prettier-ignore
99
- type Stubify<T> =
100
- T extends Stubable ? Stub<T>
101
- : T extends Promise<infer U> ? Stubify<U>
102
- : T extends StubBase<any> ? T
103
- : T extends Map<infer K, infer V> ? Map<Stubify<K>, Stubify<V>>
104
- : T extends Set<infer V> ? Set<Stubify<V>>
105
- : T extends [] ? []
106
- : T extends [infer Head, ...infer Tail] ? [Stubify<Head>, ...Stubify<Tail>]
107
- : T extends readonly [] ? readonly []
108
- : T extends readonly [infer Head, ...infer Tail] ? readonly [Stubify<Head>, ...Stubify<Tail>]
109
- : T extends Array<infer V> ? Array<Stubify<V>>
110
- : T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V>>
111
- : T extends BaseType ? T
112
- // When using "unknown" instead of "any", interfaces are not stubified.
113
- : T extends { [key: string | number]: any } ? { [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]> }
114
- : T;
115
-
116
- // Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
117
- // Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
118
- // `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
119
- // prettier-ignore
120
- type UnstubifyInner<T> =
121
- // Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
122
- // is already assignable to the value type (important for callback contextual typing).
123
- T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyInner<V>))
124
- : T extends Promise<infer U> ? UnstubifyInner<U>
125
- : T extends Map<infer K, infer V> ? Map<Unstubify<K>, Unstubify<V>>
126
- : T extends Set<infer V> ? Set<Unstubify<V>>
127
- : T extends [] ? []
128
- : T extends [infer Head, ...infer Tail] ? [Unstubify<Head>, ...Unstubify<Tail>]
129
- : T extends readonly [] ? readonly []
130
- : T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head>, ...Unstubify<Tail>]
131
- : T extends Array<infer V> ? Array<Unstubify<V>>
132
- : T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>>
133
- : T extends BaseType ? T
134
- : T extends { [key: string | number]: unknown } ? { [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]> }
135
- : T;
136
-
137
- // You can put promises anywhere in the params and they'll be resolved before delivery.
138
- // (This also covers RpcPromise, because it's defined as being a Promise.)
139
- // Map placeholders are also allowed so primitive map callback inputs can be forwarded directly
140
- // into RPC params.
141
- //
142
- // Keep raw non-stub members so generic assignability still works when UnstubifyInner<T> is deferred.
143
- // Remove stub members from mixed unions so callback params don’t get both stub and unstubbed signatures.
144
- // Marker carried by map() callback inputs. This lets primitive placeholders flow through params.
145
- type Unstubify<T> =
146
- | NonStubMembers<T>
147
- | UnstubifyInner<T>
148
- | Promise<UnstubifyInner<T>>
149
- | MapValuePlaceholder<UnstubifyInner<T>>;
150
-
151
- type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]> };
152
-
153
- interface MapValuePlaceholder<T> {
154
- [__RPC_MAP_VALUE_BRAND]: T;
155
- }
156
-
157
- type NonStubMembers<T> = Exclude<T, StubBase<any>>;
158
-
159
- // Utility type for adding `Disposable`s to `object` types only.
160
- // Note `unknown & T` is equivalent to `T`.
161
- type MaybeDisposable<T> = T extends object ? Disposable : unknown;
162
-
163
- // Type for method return or property on an RPC interface.
164
- // - Stubable types are replaced by stubs.
165
- // - RpcCompatible types are passed by value, with stubable types replaced by stubs
166
- // and a top-level `Disposer`.
167
- // Everything else can't be passed over RPC.
168
- // Technically, we use custom thenables here, but they quack like `Promise`s.
169
- // Intersecting with `(Maybe)Provider` allows pipelining.
170
- // prettier-ignore
171
- type Result<R> =
172
- IsAny<R> extends true ? UnknownResult
173
- : IsUnknown<R> extends true ? UnknownResult
174
- : R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R>
175
- : R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R>
176
- : never;
177
-
178
- type IsAny<T> = 0 extends (1 & T) ? true : false;
179
- type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>;
180
-
181
- // Type for method or property on an RPC interface.
182
- // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
183
- // Unwrapping `Stub`s allows calling with `Stubable` arguments.
184
- // For properties, rewrite types to be `Result`s.
185
- // In each case, unwrap `Promise`s.
186
- type MethodOrProperty<V> = V extends (...args: infer P) => infer R
187
- ? (...args: UnstubifyAll<P>) => IsAny<R> extends true ? UnknownResult : Result<Awaited<R>>
188
- : Result<Awaited<V>>;
189
-
190
- // Type for the callable part of an `Provider` if `T` is callable.
191
- // This is intersected with methods/properties.
192
- type MaybeCallableProvider<T> = T extends (...args: any[]) => any
193
- ? MethodOrProperty<T>
194
- : unknown;
195
-
196
- type TupleIndexKeys<T extends ReadonlyArray<unknown>> = Extract<keyof T, `${number}`>;
197
- type MapCallbackValue<T> =
198
- // `Omit` removes call signatures, so re-intersect callable provider behavior.
199
- T extends unknown
200
- ? Omit<Result<T>, keyof Promise<unknown>> &
201
- MaybeCallableProvider<T> &
202
- MapValuePlaceholder<T>
203
- : never;
204
- type InvalidNativePromiseInMapResult<T, Seen = never> =
205
- T extends unknown ? InvalidNativePromiseInMapResultImpl<T, Seen> : never;
206
- type InvalidNativePromiseInMapResultImpl<T, Seen> =
207
- [T] extends [Seen] ? never
208
- // RpcPromise is modeled as Promise & StubBase, so allow promise-like stub values.
209
- : T extends StubBase<any> ? never
210
- // Native thenables cannot be represented in map recordings, even when typed as PromiseLike.
211
- : T extends PromiseLike<unknown> ? T
212
- : T extends Map<infer K, infer V>
213
- ? InvalidNativePromiseInMapResult<K, Seen | T> |
214
- InvalidNativePromiseInMapResult<V, Seen | T>
215
- : T extends Set<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T>
216
- : T extends readonly [] ? never
217
- : T extends readonly [infer Head, ...infer Tail]
218
- ? InvalidNativePromiseInMapResult<Head, Seen | T> |
219
- InvalidNativePromiseInMapResult<Tail[number], Seen | T>
220
- : T extends ReadonlyArray<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T>
221
- : T extends { [key: string | number]: unknown }
222
- ? InvalidNativePromiseInMapResult<
223
- T[Extract<keyof T, string | number>],
224
- Seen | T
225
- >
226
- : never;
227
- type MapCallbackReturn<T> =
228
- InvalidNativePromiseInMapResult<T> extends never ? T : never;
229
- type ArrayProvider<E> = {
230
- [K in number]: MethodOrProperty<E>;
231
- } & {
232
- map<V>(callback: (elem: MapCallbackValue<E>) => MapCallbackReturn<V>): Result<Array<V>>;
233
- };
234
- type TupleProvider<T extends ReadonlyArray<unknown>> = {
235
- [K in TupleIndexKeys<T>]: MethodOrProperty<T[K]>;
236
- } & ArrayProvider<T[number]>;
237
-
238
- // Base type for all other types providing RPC-like interfaces.
239
- // Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
240
- type Provider<T> = MaybeCallableProvider<T> &
241
- (T extends ReadonlyArray<unknown>
242
- ? number extends T["length"] ? ArrayProvider<T[number]> : TupleProvider<T>
243
- : {
244
- [K in Exclude<
245
- keyof T,
246
- symbol | keyof StubBase<never>
247
- >]: MethodOrProperty<T[K]>;
248
- } & {
249
- map<V>(
250
- callback: (value: MapCallbackValue<NonNullable<T>>) => MapCallbackReturn<V>
251
- ): Result<Array<V>>;
252
- });
253
-
254
- /**
255
- * Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
256
- * RPC stubs, but it will support basic data types.
257
- */
258
- declare function serialize(value: unknown): string;
259
- /**
260
- * Deserialize a value serialized using serialize().
261
- */
262
- declare function deserialize(value: string): unknown;
263
-
264
- /**
265
- * Interface for an RPC transport, which is a simple bidirectional message stream. Implement this
266
- * interface if the built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
267
- */
268
- interface RpcTransport {
269
- /**
270
- * Sends a message to the other end.
271
- */
272
- send(message: string): Promise<void>;
273
- /**
274
- * Receives a message sent by the other end.
275
- *
276
- * If and when the transport becomes disconnected, this will reject. The thrown error will be
277
- * propagated to all outstanding calls and future calls on any stubs associated with the session.
278
- * If there are no outstanding calls (and none are made in the future), then the error does not
279
- * propagate anywhere -- this is considered a "clean" shutdown.
280
- */
281
- receive(): Promise<string>;
282
- /**
283
- * Indicates that the RPC system has suffered an error that prevents the session from continuing.
284
- * The transport should ideally try to send any queued messages if it can, and then close the
285
- * connection. (It's not strictly necessary to deliver queued messages, but the last message sent
286
- * before abort() is called is often an "abort" message, which communicates the error to the
287
- * peer, so if that is dropped, the peer may have less information about what happened.)
288
- */
289
- abort?(reason: any): void;
290
- }
291
- /**
292
- * Options to customize behavior of an RPC session. All functions which start a session should
293
- * optionally accept this.
294
- */
295
- type RpcSessionOptions = {
296
- /**
297
- * If provided, this function will be called whenever an `Error` object is serialized (for any
298
- * reason, not just because it was thrown). This can be used to log errors, and also to redact
299
- * them.
300
- *
301
- * If `onSendError` returns an Error object, than object will be substituted in place of the
302
- * original. If it has a stack property, the stack will be sent to the client.
303
- *
304
- * If `onSendError` doesn't return anything (or is not provided at all), the default behavior is
305
- * to serialize the error with the stack omitted.
306
- */
307
- onSendError?: (error: Error) => Error | void;
308
- };
309
-
310
- /**
311
- * For use in Cloudflare Workers: Construct an HTTP response that starts a WebSocket RPC session
312
- * with the given `localMain`.
313
- */
314
- declare function newWorkersWebSocketRpcResponse(request: Request, localMain?: any, options?: RpcSessionOptions): Response;
315
-
316
- /**
317
- * Implements the server end of an HTTP batch session, using standard Fetch API types to represent
318
- * HTTP requests and responses.
319
- *
320
- * @param request The request received from the client initiating the session.
321
- * @param localMain The main stub or RpcTarget which the server wishes to expose to the client.
322
- * @param options Optional RPC session options.
323
- * @returns The HTTP response to return to the client. Note that the returned object has mutable
324
- * headers, so you can modify them using e.g. `response.headers.set("Foo", "bar")`.
325
- */
326
- declare function newHttpBatchRpcResponse(request: Request, localMain: any, options?: RpcSessionOptions): Promise<Response>;
327
- /**
328
- * Implements the server end of an HTTP batch session using traditional Node.js HTTP APIs.
329
- *
330
- * @param request The request received from the client initiating the session.
331
- * @param response The response object, to which the response should be written.
332
- * @param localMain The main stub or RpcTarget which the server wishes to expose to the client.
333
- * @param options Optional RPC session options. You can also pass headers to set on the response.
334
- */
335
- declare function nodeHttpBatchRpcResponse(request: IncomingMessage, response: ServerResponse, localMain: any, options?: RpcSessionOptions & {
336
- headers?: OutgoingHttpHeaders | OutgoingHttpHeader[];
337
- }): Promise<void>;
338
-
339
- /**
340
- * Represents a reference to a remote object, on which methods may be remotely invoked via RPC.
341
- *
342
- * `RpcStub` can represent any interface (when using TypeScript, you pass the specific interface
343
- * type as `T`, but this isn't known at runtime). The way this works is, `RpcStub` is actually a
344
- * `Proxy`. It makes itself appear as if every possible method / property name is defined. You can
345
- * invoke any method name, and the invocation will be sent to the server. If it turns out that no
346
- * such method exists on the remote object, an exception is thrown back. But the client does not
347
- * actually know, until that point, what methods exist.
348
- */
349
- type RpcStub<T extends RpcCompatible<T>> = Stub<T>;
350
- declare const RpcStub: {
351
- new <T extends RpcCompatible<T>>(value: T): RpcStub<T>;
352
- };
353
- /**
354
- * Represents the result of an RPC call.
355
- *
356
- * Also used to represent properties. That is, `stub.foo` evaluates to an `RpcPromise` for the
357
- * value of `foo`.
358
- *
359
- * This isn't actually a JavaScript `Promise`. It does, however, have `then()`, `catch()`, and
360
- * `finally()` methods, like `Promise` does, and because it has a `then()` method, JavaScript will
361
- * allow you to treat it like a promise, e.g. you can `await` it.
362
- *
363
- * An `RpcPromise` is also a proxy, just like `RpcStub`, where calling methods or awaiting
364
- * properties will make a pipelined network request.
365
- *
366
- * Note that and `RpcPromise` is "lazy": the actual final result is not requested from the server
367
- * until you actually `await` the promise (or call `then()`, etc. on it). This is an optimization:
368
- * if you only intend to use the promise for pipelining and you never await it, then there's no
369
- * need to transmit the resolution!
370
- */
371
- type RpcPromise<T extends RpcCompatible<T>> = Stub<T> & Promise<Stubify<T>>;
372
- declare const RpcPromise: {};
373
- /**
374
- * Use to construct an `RpcSession` on top of a custom `RpcTransport`.
375
- *
376
- * Most people won't use this. You only need it if you've implemented your own `RpcTransport`.
377
- */
378
- interface RpcSession<T extends RpcCompatible<T> = undefined> {
379
- getRemoteMain(): RpcStub<T>;
380
- getStats(): {
381
- imports: number;
382
- exports: number;
383
- };
384
- drain(): Promise<void>;
385
- }
386
- declare const RpcSession: {
387
- new <T extends RpcCompatible<T> = undefined>(transport: RpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
388
- };
389
- /**
390
- * Classes which are intended to be passed by reference and called over RPC must extend
391
- * `RpcTarget`. A class which does not extend `RpcTarget` (and which doesn't have built-in support
392
- * from the RPC system) cannot be passed in an RPC message at all; an exception will be thrown.
393
- *
394
- * Note that on Cloudflare Workers, this `RpcTarget` is an alias for the one exported from the
395
- * "cloudflare:workers" module, so they can be used interchangably.
396
- */
397
- interface RpcTarget extends RpcTargetBranded {
398
- }
399
- declare const RpcTarget: {
400
- new (): RpcTarget;
401
- };
402
- /**
403
- * Empty interface used as default type parameter for sessions where the other side doesn't
404
- * necessarily export a main interface.
405
- */
406
- interface Empty {
407
- }
408
- /**
409
- * Start a WebSocket session given either an already-open WebSocket or a URL.
410
- *
411
- * @param webSocket Either the `wss://` URL to connect to, or an already-open WebSocket object to
412
- * use.
413
- * @param localMain The main RPC interface to expose to the peer. Returns a stub for the main
414
- * interface exposed from the peer.
415
- */
416
- declare let newWebSocketRpcSession: <T extends RpcCompatible<T> = Empty>(webSocket: WebSocket | string, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
417
- /**
418
- * Initiate an HTTP batch session from the client side.
419
- *
420
- * The parameters to this method have exactly the same signature as `fetch()`, but the return
421
- * value is an RpcStub. You can customize anything about the request except for the method
422
- * (it will always be set to POST) and the body (which the RPC system will fill in).
423
- */
424
- declare let newHttpBatchRpcSession: <T extends RpcCompatible<T>>(urlOrRequest: string | Request, options?: RpcSessionOptions) => RpcStub<T>;
425
- /**
426
- * Initiate an RPC session over a MessagePort, which is particularly useful for communicating
427
- * between an iframe and its parent frame in a browser context. Each side should call this function
428
- * on its own end of the MessageChannel.
429
- */
430
- declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty>(port: MessagePort, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
431
- /**
432
- * Implements unified handling of HTTP-batch and WebSocket responses for the Cloudflare Workers
433
- * Runtime.
434
- *
435
- * SECURITY WARNING: This function accepts cross-origin requests. If you do not want this, you
436
- * should validate the `Origin` header before calling this, or use `newHttpBatchRpcSession()` and
437
- * `newWebSocketRpcSession()` directly with appropriate security measures for each type of request.
438
- * But if your API uses in-band authorization (i.e. it has an RPC method that takes the user's
439
- * credentials as parameters and returns the authorized API), then cross-origin requests should
440
- * be safe.
441
- */
442
- declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
443
-
444
- export { type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
1
+ export { c as RpcCompatible, e as RpcPromise, f as RpcSession, b as RpcSessionOptions, d as RpcStub, g as RpcTarget, R as RpcTransport, h as deserialize, n as newHttpBatchRpcResponse, i as newHttpBatchRpcSession, j as newMessagePortRpcSession, k as newWebSocketRpcSession, l as newWorkersRpcResponse, m as newWorkersWebSocketRpcResponse, o as nodeHttpBatchRpcResponse, s as serialize } from './index-workers-CiDKhXAE.js';
2
+ import 'node:http';
package/dist/index.js CHANGED
@@ -68,6 +68,8 @@ function typeForRpc(value) {
68
68
  return "request";
69
69
  case Response.prototype:
70
70
  return "response";
71
+ case Blob.prototype:
72
+ return "blob";
71
73
  // TODO: All other structured clone types.
72
74
  case RpcStub.prototype:
73
75
  return "stub";
@@ -538,6 +540,7 @@ var RpcPayload = class _RpcPayload {
538
540
  case "bigint":
539
541
  case "date":
540
542
  case "bytes":
543
+ case "blob":
541
544
  case "error":
542
545
  case "undefined":
543
546
  return value;
@@ -774,6 +777,7 @@ var RpcPayload = class _RpcPayload {
774
777
  case "primitive":
775
778
  case "bigint":
776
779
  case "bytes":
780
+ case "blob":
777
781
  case "date":
778
782
  case "error":
779
783
  case "undefined":
@@ -876,6 +880,7 @@ var RpcPayload = class _RpcPayload {
876
880
  case "primitive":
877
881
  case "bigint":
878
882
  case "bytes":
883
+ case "blob":
879
884
  case "date":
880
885
  case "error":
881
886
  case "undefined":
@@ -967,6 +972,7 @@ function followPath(value, parent, path, owner) {
967
972
  case "primitive":
968
973
  case "bigint":
969
974
  case "bytes":
975
+ case "blob":
970
976
  case "date":
971
977
  case "error":
972
978
  case "headers":
@@ -1293,6 +1299,10 @@ var NullExporter = class {
1293
1299
  }
1294
1300
  };
1295
1301
  var NULL_EXPORTER = new NullExporter();
1302
+ async function streamToBlob(stream, type) {
1303
+ let b = await new Response(stream).blob();
1304
+ return b.type === type ? b : b.slice(0, b.size, type);
1305
+ }
1296
1306
  var ERROR_TYPES = {
1297
1307
  Error,
1298
1308
  EvalError,
@@ -1380,8 +1390,10 @@ var Devaluator = class _Devaluator {
1380
1390
  }
1381
1391
  case "bigint":
1382
1392
  return ["bigint", value.toString()];
1383
- case "date":
1384
- return ["date", value.getTime()];
1393
+ case "date": {
1394
+ const time = value.getTime();
1395
+ return ["date", Number.isNaN(time) ? null : time];
1396
+ }
1385
1397
  case "bytes": {
1386
1398
  let bytes = value;
1387
1399
  if (bytes.toBase64) {
@@ -1467,14 +1479,52 @@ var Devaluator = class _Devaluator {
1467
1479
  }
1468
1480
  return ["response", body, init];
1469
1481
  }
1482
+ case "blob": {
1483
+ let blob = value;
1484
+ let readable = blob.stream();
1485
+ let hook = streamImpl.createReadableStreamHook(readable);
1486
+ let importId = this.exporter.createPipe(readable, hook);
1487
+ return ["blob", blob.type, ["readable", importId]];
1488
+ }
1470
1489
  case "error": {
1471
1490
  let e = value;
1472
1491
  let rewritten = this.exporter.onSendError(e);
1473
1492
  if (rewritten) {
1474
1493
  e = rewritten;
1475
1494
  }
1495
+ let anyE = e;
1496
+ let props;
1497
+ let captureProp = (key, val) => {
1498
+ let exportsBefore = this.exports?.length ?? 0;
1499
+ try {
1500
+ let encoded = this.devaluateImpl(val, e, depth + 1);
1501
+ if (!props) props = {};
1502
+ props[key] = encoded;
1503
+ } catch (err) {
1504
+ if (this.exports && this.exports.length > exportsBefore) {
1505
+ let tail = this.exports.splice(exportsBefore);
1506
+ try {
1507
+ this.exporter.unexport(tail);
1508
+ } catch (err2) {
1509
+ }
1510
+ }
1511
+ }
1512
+ };
1513
+ for (let key of Object.keys(e)) {
1514
+ if (key === "name" || key === "message" || key === "stack") continue;
1515
+ captureProp(key, anyE[key]);
1516
+ }
1517
+ if ("cause" in e) {
1518
+ captureProp("cause", anyE.cause);
1519
+ }
1520
+ if (e instanceof AggregateError) {
1521
+ captureProp("errors", e.errors);
1522
+ }
1476
1523
  let result = ["error", e.name, e.message];
1477
- if (rewritten && rewritten.stack) {
1524
+ if (props) {
1525
+ result.push(rewritten && rewritten.stack ? rewritten.stack : null);
1526
+ result.push(props);
1527
+ } else if (rewritten && rewritten.stack) {
1478
1528
  result.push(rewritten.stack);
1479
1529
  }
1480
1530
  return result;
@@ -1574,6 +1624,12 @@ function fixBrokenRequestBody(request, body) {
1574
1624
  });
1575
1625
  return new RpcPromise(new PromiseStubHook(promise), []);
1576
1626
  }
1627
+ function streamToBlobPromise(stream, type) {
1628
+ let promise = streamToBlob(stream, type).then((blob) => {
1629
+ return new PayloadStubHook(RpcPayload.fromAppReturn(blob));
1630
+ });
1631
+ return new RpcPromise(new PromiseStubHook(promise), []);
1632
+ }
1577
1633
  var Evaluator = class _Evaluator {
1578
1634
  constructor(importer) {
1579
1635
  this.importer = importer;
@@ -1609,6 +1665,9 @@ var Evaluator = class _Evaluator {
1609
1665
  }
1610
1666
  break;
1611
1667
  case "date":
1668
+ if (value[1] === null) {
1669
+ return /* @__PURE__ */ new Date(NaN);
1670
+ }
1612
1671
  if (typeof value[1] == "number") {
1613
1672
  return new Date(value[1]);
1614
1673
  }
@@ -1634,10 +1693,22 @@ var Evaluator = class _Evaluator {
1634
1693
  case "error":
1635
1694
  if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
1636
1695
  let cls = ERROR_TYPES[value[1]] || Error;
1637
- let result = new cls(value[2]);
1696
+ let result = cls === AggregateError ? new cls([], value[2]) : new cls(value[2]);
1638
1697
  if (typeof value[3] === "string") {
1639
1698
  result.stack = value[3];
1640
1699
  }
1700
+ if (value.length >= 5) {
1701
+ let props = value[4];
1702
+ if (!props || typeof props !== "object" || Array.isArray(props)) {
1703
+ break;
1704
+ }
1705
+ let anyResult = result;
1706
+ let propsObj = props;
1707
+ for (let key of Object.keys(propsObj)) {
1708
+ if (key === "name" || key === "message" || key === "stack") continue;
1709
+ anyResult[key] = this.evaluateImpl(propsObj[key], result, key);
1710
+ }
1711
+ }
1641
1712
  return result;
1642
1713
  }
1643
1714
  break;
@@ -1702,6 +1773,17 @@ var Evaluator = class _Evaluator {
1702
1773
  }
1703
1774
  return new Response(body, init);
1704
1775
  }
1776
+ case "blob": {
1777
+ if (value.length !== 3 || typeof value[1] !== "string") break;
1778
+ let contentType = value[1];
1779
+ let content = this.evaluateImpl(value[2], parent, property);
1780
+ if (!(content instanceof ReadableStream)) {
1781
+ throw new TypeError("Blob content must be serialized as a ReadableStream.");
1782
+ }
1783
+ let promise = streamToBlobPromise(content, contentType);
1784
+ this.promises.push({ promise, parent, property });
1785
+ return promise;
1786
+ }
1705
1787
  case "import":
1706
1788
  case "pipeline": {
1707
1789
  if (value.length < 2 || value.length > 4) {
@@ -2042,12 +2124,7 @@ var RpcSessionImpl = class {
2042
2124
  this.options = options;
2043
2125
  this.exports.push({ hook: mainHook, refcount: 1 });
2044
2126
  this.imports.push(new ImportTableEntry(this, 0, false));
2045
- let rejectFunc;
2046
- let abortPromise = new Promise((resolve, reject) => {
2047
- rejectFunc = reject;
2048
- });
2049
- this.cancelReadLoop = rejectFunc;
2050
- this.readLoop(abortPromise).catch((err) => this.abort(err));
2127
+ this.readLoop().catch((err) => this.abort(err));
2051
2128
  }
2052
2129
  exports = [];
2053
2130
  reverseExports = /* @__PURE__ */ new Map();
@@ -2331,7 +2408,8 @@ var RpcSessionImpl = class {
2331
2408
  }
2332
2409
  abort(error, trySendAbortMessage = true) {
2333
2410
  if (this.abortReason !== void 0) return;
2334
- this.cancelReadLoop(error);
2411
+ this.cancelReadLoop?.(error);
2412
+ this.cancelReadLoop = void 0;
2335
2413
  if (trySendAbortMessage) {
2336
2414
  try {
2337
2415
  this.transport.send(JSON.stringify(["abort", Devaluator.devaluate(error, void 0, this)])).catch((err) => {
@@ -2367,9 +2445,19 @@ var RpcSessionImpl = class {
2367
2445
  this.exports[i].hook.dispose();
2368
2446
  }
2369
2447
  }
2370
- async readLoop(abortPromise) {
2448
+ async readLoop() {
2371
2449
  while (!this.abortReason) {
2372
- let msg = JSON.parse(await Promise.race([this.transport.receive(), abortPromise]));
2450
+ let readCanceled = Promise.withResolvers();
2451
+ this.cancelReadLoop = readCanceled.reject;
2452
+ let msgText;
2453
+ try {
2454
+ msgText = await Promise.race([this.transport.receive(), readCanceled.promise]);
2455
+ } finally {
2456
+ if (this.cancelReadLoop === readCanceled.reject) {
2457
+ this.cancelReadLoop = void 0;
2458
+ }
2459
+ }
2460
+ let msg = JSON.parse(msgText);
2373
2461
  if (this.abortReason) break;
2374
2462
  if (msg instanceof Array) {
2375
2463
  switch (msg[0]) {