capnweb 0.2.0 → 0.3.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.cts CHANGED
@@ -23,21 +23,21 @@ type Stubable = RpcTargetBranded | ((...args: any[]) => any);
23
23
  // The reason for using a generic type here is to build a serializable subset of structured
24
24
  // cloneable composite types. This allows types defined with the "interface" keyword to pass the
25
25
  // serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
26
- type Serializable<T> =
26
+ type RpcCompatible<T> =
27
27
  // Structured cloneables
28
28
  | BaseType
29
29
  // Structured cloneable composites
30
30
  | Map<
31
- T extends Map<infer U, unknown> ? Serializable<U> : never,
32
- T extends Map<unknown, infer U> ? Serializable<U> : never
31
+ T extends Map<infer U, unknown> ? RpcCompatible<U> : never,
32
+ T extends Map<unknown, infer U> ? RpcCompatible<U> : never
33
33
  >
34
- | Set<T extends Set<infer U> ? Serializable<U> : never>
35
- | Array<T extends Array<infer U> ? Serializable<U> : never>
36
- | ReadonlyArray<T extends ReadonlyArray<infer U> ? Serializable<U> : never>
34
+ | Set<T extends Set<infer U> ? RpcCompatible<U> : never>
35
+ | Array<T extends Array<infer U> ? RpcCompatible<U> : never>
36
+ | ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never>
37
37
  | {
38
- [K in keyof T]: K extends number | string ? Serializable<T[K]> : never;
38
+ [K in keyof T]: K extends number | string ? RpcCompatible<T[K]> : never;
39
39
  }
40
- | Promise<T extends Promise<infer U> ? Serializable<U> : never>
40
+ | Promise<T extends Promise<infer U> ? RpcCompatible<U> : never>
41
41
  // Special types
42
42
  | Stub<Stubable>
43
43
  // Serialized as stubs, see `Stubify`
@@ -45,12 +45,12 @@ type Serializable<T> =
45
45
 
46
46
  // Base type for all RPC stubs, including common memory management methods.
47
47
  // `T` is used as a marker type for unwrapping `Stub`s later.
48
- interface StubBase<T extends Serializable<T>> extends Disposable {
48
+ interface StubBase<T extends RpcCompatible<T>> extends Disposable {
49
49
  [__RPC_STUB_BRAND]: T;
50
50
  dup(): this;
51
51
  onRpcBroken(callback: (error: any) => void): void;
52
52
  }
53
- type Stub<T extends Serializable<T>> =
53
+ type Stub<T extends RpcCompatible<T>> =
54
54
  T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
55
55
 
56
56
  type TypedArray =
@@ -94,6 +94,10 @@ type Stubify<T> =
94
94
  : T extends StubBase<any> ? T
95
95
  : T extends Map<infer K, infer V> ? Map<Stubify<K>, Stubify<V>>
96
96
  : T extends Set<infer V> ? Set<Stubify<V>>
97
+ : T extends [] ? []
98
+ : T extends [infer Head, ...infer Tail] ? [Stubify<Head>, ...Stubify<Tail>]
99
+ : T extends readonly [] ? readonly []
100
+ : T extends readonly [infer Head, ...infer Tail] ? readonly [Stubify<Head>, ...Stubify<Tail>]
97
101
  : T extends Array<infer V> ? Array<Stubify<V>>
98
102
  : T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V>>
99
103
  : T extends BaseType ? T
@@ -109,6 +113,10 @@ type UnstubifyInner<T> =
109
113
  T extends StubBase<infer V> ? (T | V) // can provide either stub or local RpcTarget
110
114
  : T extends Map<infer K, infer V> ? Map<Unstubify<K>, Unstubify<V>>
111
115
  : T extends Set<infer V> ? Set<Unstubify<V>>
116
+ : T extends [] ? []
117
+ : T extends [infer Head, ...infer Tail] ? [Unstubify<Head>, ...Unstubify<Tail>]
118
+ : T extends readonly [] ? readonly []
119
+ : T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head>, ...Unstubify<Tail>]
112
120
  : T extends Array<infer V> ? Array<Unstubify<V>>
113
121
  : T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>>
114
122
  : T extends BaseType ? T
@@ -127,7 +135,7 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown;
127
135
 
128
136
  // Type for method return or property on an RPC interface.
129
137
  // - Stubable types are replaced by stubs.
130
- // - Serializable types are passed by value, with stubable types replaced by stubs
138
+ // - RpcCompatible types are passed by value, with stubable types replaced by stubs
131
139
  // and a top-level `Disposer`.
132
140
  // Everything else can't be passed over RPC.
133
141
  // Technically, we use custom thenables here, but they quack like `Promise`s.
@@ -135,7 +143,7 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown;
135
143
  // prettier-ignore
136
144
  type Result<R> =
137
145
  R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R>
138
- : R extends Serializable<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R>
146
+ : R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R>
139
147
  : never;
140
148
 
141
149
  // Type for method or property on an RPC interface.
@@ -266,9 +274,9 @@ declare function nodeHttpBatchRpcResponse(request: IncomingMessage, response: Se
266
274
  * such method exists on the remote object, an exception is thrown back. But the client does not
267
275
  * actually know, until that point, what methods exist.
268
276
  */
269
- type RpcStub<T extends Serializable<T>> = Stub<T>;
277
+ type RpcStub<T extends RpcCompatible<T>> = Stub<T>;
270
278
  declare const RpcStub: {
271
- new <T extends Serializable<T>>(value: T): RpcStub<T>;
279
+ new <T extends RpcCompatible<T>>(value: T): RpcStub<T>;
272
280
  };
273
281
  /**
274
282
  * Represents the result of an RPC call.
@@ -288,14 +296,14 @@ declare const RpcStub: {
288
296
  * if you only intend to use the promise for pipelining and you never await it, then there's no
289
297
  * need to transmit the resolution!
290
298
  */
291
- type RpcPromise<T extends Serializable<T>> = Stub<T> & Promise<Stubify<T>>;
299
+ type RpcPromise<T extends RpcCompatible<T>> = Stub<T> & Promise<Stubify<T>>;
292
300
  declare const RpcPromise: {};
293
301
  /**
294
302
  * Use to construct an `RpcSession` on top of a custom `RpcTransport`.
295
303
  *
296
304
  * Most people won't use this. You only need it if you've implemented your own `RpcTransport`.
297
305
  */
298
- interface RpcSession<T extends Serializable<T> = undefined> {
306
+ interface RpcSession<T extends RpcCompatible<T> = undefined> {
299
307
  getRemoteMain(): RpcStub<T>;
300
308
  getStats(): {
301
309
  imports: number;
@@ -304,7 +312,7 @@ interface RpcSession<T extends Serializable<T> = undefined> {
304
312
  drain(): Promise<void>;
305
313
  }
306
314
  declare const RpcSession: {
307
- new <T extends Serializable<T> = undefined>(transport: RpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
315
+ new <T extends RpcCompatible<T> = undefined>(transport: RpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
308
316
  };
309
317
  /**
310
318
  * Classes which are intended to be passed by reference and called over RPC must extend
@@ -333,7 +341,7 @@ interface Empty {
333
341
  * @param localMain The main RPC interface to expose to the peer. Returns a stub for the main
334
342
  * interface exposed from the peer.
335
343
  */
336
- declare let newWebSocketRpcSession: <T extends Serializable<T> = Empty>(webSocket: WebSocket | string, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
344
+ declare let newWebSocketRpcSession: <T extends RpcCompatible<T> = Empty>(webSocket: WebSocket | string, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
337
345
  /**
338
346
  * Initiate an HTTP batch session from the client side.
339
347
  *
@@ -341,13 +349,13 @@ declare let newWebSocketRpcSession: <T extends Serializable<T> = Empty>(webSocke
341
349
  * value is an RpcStub. You can customize anything about the request except for the method
342
350
  * (it will always be set to POST) and the body (which the RPC system will fill in).
343
351
  */
344
- declare let newHttpBatchRpcSession: <T extends Serializable<T>>(urlOrRequest: string | Request, options?: RpcSessionOptions) => RpcStub<T>;
352
+ declare let newHttpBatchRpcSession: <T extends RpcCompatible<T>>(urlOrRequest: string | Request, options?: RpcSessionOptions) => RpcStub<T>;
345
353
  /**
346
354
  * Initiate an RPC session over a MessagePort, which is particularly useful for communicating
347
355
  * between an iframe and its parent frame in a browser context. Each side should call this function
348
356
  * on its own end of the MessageChannel.
349
357
  */
350
- declare let newMessagePortRpcSession: <T extends Serializable<T> = Empty>(port: MessagePort, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
358
+ declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty>(port: MessagePort, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
351
359
  /**
352
360
  * Implements unified handling of HTTP-batch and WebSocket responses for the Cloudflare Workers
353
361
  * Runtime.
@@ -361,4 +369,4 @@ declare let newMessagePortRpcSession: <T extends Serializable<T> = Empty>(port:
361
369
  */
362
370
  declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
363
371
 
364
- export { RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
372
+ export { type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
package/dist/index.d.ts CHANGED
@@ -23,21 +23,21 @@ type Stubable = RpcTargetBranded | ((...args: any[]) => any);
23
23
  // The reason for using a generic type here is to build a serializable subset of structured
24
24
  // cloneable composite types. This allows types defined with the "interface" keyword to pass the
25
25
  // serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
26
- type Serializable<T> =
26
+ type RpcCompatible<T> =
27
27
  // Structured cloneables
28
28
  | BaseType
29
29
  // Structured cloneable composites
30
30
  | Map<
31
- T extends Map<infer U, unknown> ? Serializable<U> : never,
32
- T extends Map<unknown, infer U> ? Serializable<U> : never
31
+ T extends Map<infer U, unknown> ? RpcCompatible<U> : never,
32
+ T extends Map<unknown, infer U> ? RpcCompatible<U> : never
33
33
  >
34
- | Set<T extends Set<infer U> ? Serializable<U> : never>
35
- | Array<T extends Array<infer U> ? Serializable<U> : never>
36
- | ReadonlyArray<T extends ReadonlyArray<infer U> ? Serializable<U> : never>
34
+ | Set<T extends Set<infer U> ? RpcCompatible<U> : never>
35
+ | Array<T extends Array<infer U> ? RpcCompatible<U> : never>
36
+ | ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never>
37
37
  | {
38
- [K in keyof T]: K extends number | string ? Serializable<T[K]> : never;
38
+ [K in keyof T]: K extends number | string ? RpcCompatible<T[K]> : never;
39
39
  }
40
- | Promise<T extends Promise<infer U> ? Serializable<U> : never>
40
+ | Promise<T extends Promise<infer U> ? RpcCompatible<U> : never>
41
41
  // Special types
42
42
  | Stub<Stubable>
43
43
  // Serialized as stubs, see `Stubify`
@@ -45,12 +45,12 @@ type Serializable<T> =
45
45
 
46
46
  // Base type for all RPC stubs, including common memory management methods.
47
47
  // `T` is used as a marker type for unwrapping `Stub`s later.
48
- interface StubBase<T extends Serializable<T>> extends Disposable {
48
+ interface StubBase<T extends RpcCompatible<T>> extends Disposable {
49
49
  [__RPC_STUB_BRAND]: T;
50
50
  dup(): this;
51
51
  onRpcBroken(callback: (error: any) => void): void;
52
52
  }
53
- type Stub<T extends Serializable<T>> =
53
+ type Stub<T extends RpcCompatible<T>> =
54
54
  T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
55
55
 
56
56
  type TypedArray =
@@ -94,6 +94,10 @@ type Stubify<T> =
94
94
  : T extends StubBase<any> ? T
95
95
  : T extends Map<infer K, infer V> ? Map<Stubify<K>, Stubify<V>>
96
96
  : T extends Set<infer V> ? Set<Stubify<V>>
97
+ : T extends [] ? []
98
+ : T extends [infer Head, ...infer Tail] ? [Stubify<Head>, ...Stubify<Tail>]
99
+ : T extends readonly [] ? readonly []
100
+ : T extends readonly [infer Head, ...infer Tail] ? readonly [Stubify<Head>, ...Stubify<Tail>]
97
101
  : T extends Array<infer V> ? Array<Stubify<V>>
98
102
  : T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V>>
99
103
  : T extends BaseType ? T
@@ -109,6 +113,10 @@ type UnstubifyInner<T> =
109
113
  T extends StubBase<infer V> ? (T | V) // can provide either stub or local RpcTarget
110
114
  : T extends Map<infer K, infer V> ? Map<Unstubify<K>, Unstubify<V>>
111
115
  : T extends Set<infer V> ? Set<Unstubify<V>>
116
+ : T extends [] ? []
117
+ : T extends [infer Head, ...infer Tail] ? [Unstubify<Head>, ...Unstubify<Tail>]
118
+ : T extends readonly [] ? readonly []
119
+ : T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head>, ...Unstubify<Tail>]
112
120
  : T extends Array<infer V> ? Array<Unstubify<V>>
113
121
  : T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>>
114
122
  : T extends BaseType ? T
@@ -127,7 +135,7 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown;
127
135
 
128
136
  // Type for method return or property on an RPC interface.
129
137
  // - Stubable types are replaced by stubs.
130
- // - Serializable types are passed by value, with stubable types replaced by stubs
138
+ // - RpcCompatible types are passed by value, with stubable types replaced by stubs
131
139
  // and a top-level `Disposer`.
132
140
  // Everything else can't be passed over RPC.
133
141
  // Technically, we use custom thenables here, but they quack like `Promise`s.
@@ -135,7 +143,7 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown;
135
143
  // prettier-ignore
136
144
  type Result<R> =
137
145
  R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R>
138
- : R extends Serializable<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R>
146
+ : R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R>
139
147
  : never;
140
148
 
141
149
  // Type for method or property on an RPC interface.
@@ -266,9 +274,9 @@ declare function nodeHttpBatchRpcResponse(request: IncomingMessage, response: Se
266
274
  * such method exists on the remote object, an exception is thrown back. But the client does not
267
275
  * actually know, until that point, what methods exist.
268
276
  */
269
- type RpcStub<T extends Serializable<T>> = Stub<T>;
277
+ type RpcStub<T extends RpcCompatible<T>> = Stub<T>;
270
278
  declare const RpcStub: {
271
- new <T extends Serializable<T>>(value: T): RpcStub<T>;
279
+ new <T extends RpcCompatible<T>>(value: T): RpcStub<T>;
272
280
  };
273
281
  /**
274
282
  * Represents the result of an RPC call.
@@ -288,14 +296,14 @@ declare const RpcStub: {
288
296
  * if you only intend to use the promise for pipelining and you never await it, then there's no
289
297
  * need to transmit the resolution!
290
298
  */
291
- type RpcPromise<T extends Serializable<T>> = Stub<T> & Promise<Stubify<T>>;
299
+ type RpcPromise<T extends RpcCompatible<T>> = Stub<T> & Promise<Stubify<T>>;
292
300
  declare const RpcPromise: {};
293
301
  /**
294
302
  * Use to construct an `RpcSession` on top of a custom `RpcTransport`.
295
303
  *
296
304
  * Most people won't use this. You only need it if you've implemented your own `RpcTransport`.
297
305
  */
298
- interface RpcSession<T extends Serializable<T> = undefined> {
306
+ interface RpcSession<T extends RpcCompatible<T> = undefined> {
299
307
  getRemoteMain(): RpcStub<T>;
300
308
  getStats(): {
301
309
  imports: number;
@@ -304,7 +312,7 @@ interface RpcSession<T extends Serializable<T> = undefined> {
304
312
  drain(): Promise<void>;
305
313
  }
306
314
  declare const RpcSession: {
307
- new <T extends Serializable<T> = undefined>(transport: RpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
315
+ new <T extends RpcCompatible<T> = undefined>(transport: RpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
308
316
  };
309
317
  /**
310
318
  * Classes which are intended to be passed by reference and called over RPC must extend
@@ -333,7 +341,7 @@ interface Empty {
333
341
  * @param localMain The main RPC interface to expose to the peer. Returns a stub for the main
334
342
  * interface exposed from the peer.
335
343
  */
336
- declare let newWebSocketRpcSession: <T extends Serializable<T> = Empty>(webSocket: WebSocket | string, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
344
+ declare let newWebSocketRpcSession: <T extends RpcCompatible<T> = Empty>(webSocket: WebSocket | string, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
337
345
  /**
338
346
  * Initiate an HTTP batch session from the client side.
339
347
  *
@@ -341,13 +349,13 @@ declare let newWebSocketRpcSession: <T extends Serializable<T> = Empty>(webSocke
341
349
  * value is an RpcStub. You can customize anything about the request except for the method
342
350
  * (it will always be set to POST) and the body (which the RPC system will fill in).
343
351
  */
344
- declare let newHttpBatchRpcSession: <T extends Serializable<T>>(urlOrRequest: string | Request, options?: RpcSessionOptions) => RpcStub<T>;
352
+ declare let newHttpBatchRpcSession: <T extends RpcCompatible<T>>(urlOrRequest: string | Request, options?: RpcSessionOptions) => RpcStub<T>;
345
353
  /**
346
354
  * Initiate an RPC session over a MessagePort, which is particularly useful for communicating
347
355
  * between an iframe and its parent frame in a browser context. Each side should call this function
348
356
  * on its own end of the MessageChannel.
349
357
  */
350
- declare let newMessagePortRpcSession: <T extends Serializable<T> = Empty>(port: MessagePort, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
358
+ declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty>(port: MessagePort, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
351
359
  /**
352
360
  * Implements unified handling of HTTP-batch and WebSocket responses for the Cloudflare Workers
353
361
  * Runtime.
@@ -361,4 +369,4 @@ declare let newMessagePortRpcSession: <T extends Serializable<T> = Empty>(port:
361
369
  */
362
370
  declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
363
371
 
364
- export { RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
372
+ export { type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
package/dist/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  // src/symbols.ts
2
- var WORKERS_MODULE_SYMBOL = Symbol("workers-module");
2
+ var WORKERS_MODULE_SYMBOL = /* @__PURE__ */ Symbol("workers-module");
3
3
 
4
4
  // src/core.ts
5
5
  if (!Symbol.dispose) {
6
- Symbol.dispose = Symbol.for("dispose");
6
+ Symbol.dispose = /* @__PURE__ */ Symbol.for("dispose");
7
7
  }
8
8
  if (!Symbol.asyncDispose) {
9
- Symbol.asyncDispose = Symbol.for("asyncDispose");
9
+ Symbol.asyncDispose = /* @__PURE__ */ Symbol.for("asyncDispose");
10
10
  }
11
11
  if (!Promise.withResolvers) {
12
12
  Promise.withResolvers = function() {
@@ -22,6 +22,8 @@ if (!Promise.withResolvers) {
22
22
  var workersModule = globalThis[WORKERS_MODULE_SYMBOL];
23
23
  var RpcTarget = workersModule ? workersModule.RpcTarget : class {
24
24
  };
25
+ var AsyncFunction = (async function() {
26
+ }).constructor;
25
27
  function typeForRpc(value) {
26
28
  switch (typeof value) {
27
29
  case "boolean":
@@ -46,6 +48,7 @@ function typeForRpc(value) {
46
48
  case Object.prototype:
47
49
  return "object";
48
50
  case Function.prototype:
51
+ case AsyncFunction.prototype:
49
52
  return "function";
50
53
  case Array.prototype:
51
54
  return "array";
@@ -129,7 +132,7 @@ function withCallInterceptor(interceptor, callback) {
129
132
  doCall = oldValue;
130
133
  }
131
134
  }
132
- var RAW_STUB = Symbol("realStub");
135
+ var RAW_STUB = /* @__PURE__ */ Symbol("realStub");
133
136
  var PROXY_HANDLERS = {
134
137
  apply(target, thisArg, argumentsList) {
135
138
  let stub = target.raw;