capnweb 0.6.1 → 0.7.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.
@@ -0,0 +1,125 @@
1
+ import { _ as __RPC_TARGET_BRAND, R as RpcTransport, a as RpcTargetBranded, b as RpcSessionOptions, c as RpcCompatible, d as RpcStub$1 } from './index-workers-C1na2_sM.cjs';
2
+ export { e as RpcPromise, f as RpcSession, g as RpcTarget, 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-C1na2_sM.cjs';
3
+ import { ServerWebSocket } from 'bun';
4
+ import 'node:http';
5
+
6
+ interface RpcTarget {
7
+ [__RPC_TARGET_BRAND]: never;
8
+ }
9
+ declare let RpcTarget: any;
10
+ type PropertyPath = (string | number)[];
11
+ declare abstract class StubHook {
12
+ abstract call(path: PropertyPath, args: RpcPayload): StubHook;
13
+ stream(path: PropertyPath, args: RpcPayload): {
14
+ promise: Promise<void>;
15
+ size?: number;
16
+ };
17
+ abstract map(path: PropertyPath, captures: StubHook[], instructions: unknown[]): StubHook;
18
+ abstract get(path: PropertyPath): StubHook;
19
+ abstract dup(): StubHook;
20
+ abstract pull(): RpcPayload | Promise<RpcPayload>;
21
+ abstract ignoreUnhandledRejections(): void;
22
+ abstract dispose(): void;
23
+ abstract onBroken(callback: (error: any) => void): void;
24
+ }
25
+ declare let RAW_STUB: symbol;
26
+ interface RpcStub extends Disposable {
27
+ }
28
+ declare class RpcStub extends RpcTarget {
29
+ [RAW_STUB]: this;
30
+ constructor(hook: StubHook, pathIfPromise?: PropertyPath);
31
+ hook: StubHook;
32
+ pathIfPromise?: PropertyPath;
33
+ dup(): RpcStub;
34
+ onRpcBroken(callback: (error: any) => void): void;
35
+ map(func: (value: RpcPromise) => unknown): RpcPromise;
36
+ toString(): string;
37
+ }
38
+ declare class RpcPromise extends RpcStub {
39
+ constructor(hook: StubHook, pathIfPromise: PropertyPath);
40
+ then(onfulfilled?: ((value: unknown) => unknown) | undefined | null, onrejected?: ((reason: any) => unknown) | undefined | null): Promise<unknown>;
41
+ catch(onrejected?: ((reason: any) => unknown) | undefined | null): Promise<unknown>;
42
+ finally(onfinally?: (() => void) | undefined | null): Promise<unknown>;
43
+ toString(): string;
44
+ }
45
+ type LocatedPromise = {
46
+ parent: object;
47
+ property: string | number;
48
+ promise: RpcPromise;
49
+ };
50
+ declare class RpcPayload {
51
+ value: unknown;
52
+ private source;
53
+ private hooks?;
54
+ private promises?;
55
+ static fromAppParams(value: unknown): RpcPayload;
56
+ static fromAppReturn(value: unknown): RpcPayload;
57
+ static fromArray(array: RpcPayload[]): RpcPayload;
58
+ static forEvaluate(hooks: StubHook[], promises: LocatedPromise[]): RpcPayload;
59
+ static deepCopyFrom(value: unknown, oldParent: object | undefined, owner: RpcPayload | null): RpcPayload;
60
+ private constructor();
61
+ private rpcTargets?;
62
+ getHookForRpcTarget(target: RpcTarget | Function, parent: object | undefined, dupStubs?: boolean): StubHook;
63
+ getHookForWritableStream(stream: WritableStream, parent: object | undefined, dupStubs?: boolean): StubHook;
64
+ getHookForReadableStream(stream: ReadableStream, parent: object | undefined, dupStubs?: boolean): StubHook;
65
+ private deepCopy;
66
+ ensureDeepCopied(): void;
67
+ private deliverTo;
68
+ private static deliverRpcPromiseTo;
69
+ deliverCall(func: Function, thisArg: object | undefined): Promise<RpcPayload>;
70
+ deliverResolve(): Promise<unknown>;
71
+ dispose(): void;
72
+ private disposeImpl;
73
+ ignoreUnhandledRejections(): void;
74
+ private ignoreUnhandledRejectionsImpl;
75
+ }
76
+
77
+ type WsData = {
78
+ __capnwebTransport: BunWebSocketTransport<WsData>;
79
+ __capnwebStub: RpcStub;
80
+ };
81
+ /**
82
+ * Create a Bun `WebSocketHandler` object that manages RPC sessions automatically.
83
+ *
84
+ * The returned object can be passed directly as the `websocket` option to `Bun.serve()`.
85
+ * A fresh `localMain` is created for each connection via the `createMain` callback.
86
+ * The transport is stored on `ws.data.__capnwebTransport`.
87
+ *
88
+ * @param createMain Called once per connection to create the main RPC interface for that client.
89
+ * @param options Optional RPC session options applied to every connection.
90
+ */
91
+ declare function newBunWebSocketRpcHandler(createMain: () => RpcTargetBranded, options?: RpcSessionOptions): {
92
+ open(ws: ServerWebSocket<WsData>): void;
93
+ message(ws: ServerWebSocket<WsData>, message: string | Buffer): void;
94
+ close(ws: ServerWebSocket<WsData>, code: number, reason: string): void;
95
+ error(ws: ServerWebSocket<WsData>, error: Error): void;
96
+ };
97
+ declare class BunWebSocketTransport<T = undefined> implements RpcTransport {
98
+ #private;
99
+ constructor(ws: ServerWebSocket<T>);
100
+ send(message: string): Promise<void>;
101
+ receive(): Promise<string>;
102
+ abort?(reason: any): void;
103
+ dispatchMessage(data: string | Buffer): void;
104
+ dispatchClose(code: number, reason: string): void;
105
+ dispatchError(error: Error): void;
106
+ }
107
+
108
+ interface Empty {
109
+ }
110
+ /**
111
+ * Start an RPC session over a Bun ServerWebSocket.
112
+ *
113
+ * Returns both the RPC stub and the transport. The transport exposes `dispatchMessage`,
114
+ * `dispatchClose`, and `dispatchError` methods that must be wired to Bun's `WebSocketHandler`
115
+ * callbacks. For a zero-wiring alternative, use `newBunWebSocketRpcHandler` instead.
116
+ *
117
+ * @param ws The Bun ServerWebSocket from the `open` callback.
118
+ * @param localMain The main RPC interface to expose to the peer.
119
+ */
120
+ declare let newBunWebSocketRpcSession: <T extends RpcCompatible<T> = Empty, D = undefined>(ws: ServerWebSocket<D>, localMain?: any, options?: RpcSessionOptions) => {
121
+ stub: RpcStub$1<T>;
122
+ transport: BunWebSocketTransport<D>;
123
+ };
124
+
125
+ export { BunWebSocketTransport, RpcCompatible, RpcSessionOptions, RpcStub$1 as RpcStub, RpcTransport, newBunWebSocketRpcHandler, newBunWebSocketRpcSession };
@@ -0,0 +1,125 @@
1
+ import { _ as __RPC_TARGET_BRAND, R as RpcTransport, a as RpcTargetBranded, b as RpcSessionOptions, c as RpcCompatible, d as RpcStub$1 } from './index-workers-C1na2_sM.js';
2
+ export { e as RpcPromise, f as RpcSession, g as RpcTarget, 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-C1na2_sM.js';
3
+ import { ServerWebSocket } from 'bun';
4
+ import 'node:http';
5
+
6
+ interface RpcTarget {
7
+ [__RPC_TARGET_BRAND]: never;
8
+ }
9
+ declare let RpcTarget: any;
10
+ type PropertyPath = (string | number)[];
11
+ declare abstract class StubHook {
12
+ abstract call(path: PropertyPath, args: RpcPayload): StubHook;
13
+ stream(path: PropertyPath, args: RpcPayload): {
14
+ promise: Promise<void>;
15
+ size?: number;
16
+ };
17
+ abstract map(path: PropertyPath, captures: StubHook[], instructions: unknown[]): StubHook;
18
+ abstract get(path: PropertyPath): StubHook;
19
+ abstract dup(): StubHook;
20
+ abstract pull(): RpcPayload | Promise<RpcPayload>;
21
+ abstract ignoreUnhandledRejections(): void;
22
+ abstract dispose(): void;
23
+ abstract onBroken(callback: (error: any) => void): void;
24
+ }
25
+ declare let RAW_STUB: symbol;
26
+ interface RpcStub extends Disposable {
27
+ }
28
+ declare class RpcStub extends RpcTarget {
29
+ [RAW_STUB]: this;
30
+ constructor(hook: StubHook, pathIfPromise?: PropertyPath);
31
+ hook: StubHook;
32
+ pathIfPromise?: PropertyPath;
33
+ dup(): RpcStub;
34
+ onRpcBroken(callback: (error: any) => void): void;
35
+ map(func: (value: RpcPromise) => unknown): RpcPromise;
36
+ toString(): string;
37
+ }
38
+ declare class RpcPromise extends RpcStub {
39
+ constructor(hook: StubHook, pathIfPromise: PropertyPath);
40
+ then(onfulfilled?: ((value: unknown) => unknown) | undefined | null, onrejected?: ((reason: any) => unknown) | undefined | null): Promise<unknown>;
41
+ catch(onrejected?: ((reason: any) => unknown) | undefined | null): Promise<unknown>;
42
+ finally(onfinally?: (() => void) | undefined | null): Promise<unknown>;
43
+ toString(): string;
44
+ }
45
+ type LocatedPromise = {
46
+ parent: object;
47
+ property: string | number;
48
+ promise: RpcPromise;
49
+ };
50
+ declare class RpcPayload {
51
+ value: unknown;
52
+ private source;
53
+ private hooks?;
54
+ private promises?;
55
+ static fromAppParams(value: unknown): RpcPayload;
56
+ static fromAppReturn(value: unknown): RpcPayload;
57
+ static fromArray(array: RpcPayload[]): RpcPayload;
58
+ static forEvaluate(hooks: StubHook[], promises: LocatedPromise[]): RpcPayload;
59
+ static deepCopyFrom(value: unknown, oldParent: object | undefined, owner: RpcPayload | null): RpcPayload;
60
+ private constructor();
61
+ private rpcTargets?;
62
+ getHookForRpcTarget(target: RpcTarget | Function, parent: object | undefined, dupStubs?: boolean): StubHook;
63
+ getHookForWritableStream(stream: WritableStream, parent: object | undefined, dupStubs?: boolean): StubHook;
64
+ getHookForReadableStream(stream: ReadableStream, parent: object | undefined, dupStubs?: boolean): StubHook;
65
+ private deepCopy;
66
+ ensureDeepCopied(): void;
67
+ private deliverTo;
68
+ private static deliverRpcPromiseTo;
69
+ deliverCall(func: Function, thisArg: object | undefined): Promise<RpcPayload>;
70
+ deliverResolve(): Promise<unknown>;
71
+ dispose(): void;
72
+ private disposeImpl;
73
+ ignoreUnhandledRejections(): void;
74
+ private ignoreUnhandledRejectionsImpl;
75
+ }
76
+
77
+ type WsData = {
78
+ __capnwebTransport: BunWebSocketTransport<WsData>;
79
+ __capnwebStub: RpcStub;
80
+ };
81
+ /**
82
+ * Create a Bun `WebSocketHandler` object that manages RPC sessions automatically.
83
+ *
84
+ * The returned object can be passed directly as the `websocket` option to `Bun.serve()`.
85
+ * A fresh `localMain` is created for each connection via the `createMain` callback.
86
+ * The transport is stored on `ws.data.__capnwebTransport`.
87
+ *
88
+ * @param createMain Called once per connection to create the main RPC interface for that client.
89
+ * @param options Optional RPC session options applied to every connection.
90
+ */
91
+ declare function newBunWebSocketRpcHandler(createMain: () => RpcTargetBranded, options?: RpcSessionOptions): {
92
+ open(ws: ServerWebSocket<WsData>): void;
93
+ message(ws: ServerWebSocket<WsData>, message: string | Buffer): void;
94
+ close(ws: ServerWebSocket<WsData>, code: number, reason: string): void;
95
+ error(ws: ServerWebSocket<WsData>, error: Error): void;
96
+ };
97
+ declare class BunWebSocketTransport<T = undefined> implements RpcTransport {
98
+ #private;
99
+ constructor(ws: ServerWebSocket<T>);
100
+ send(message: string): Promise<void>;
101
+ receive(): Promise<string>;
102
+ abort?(reason: any): void;
103
+ dispatchMessage(data: string | Buffer): void;
104
+ dispatchClose(code: number, reason: string): void;
105
+ dispatchError(error: Error): void;
106
+ }
107
+
108
+ interface Empty {
109
+ }
110
+ /**
111
+ * Start an RPC session over a Bun ServerWebSocket.
112
+ *
113
+ * Returns both the RPC stub and the transport. The transport exposes `dispatchMessage`,
114
+ * `dispatchClose`, and `dispatchError` methods that must be wired to Bun's `WebSocketHandler`
115
+ * callbacks. For a zero-wiring alternative, use `newBunWebSocketRpcHandler` instead.
116
+ *
117
+ * @param ws The Bun ServerWebSocket from the `open` callback.
118
+ * @param localMain The main RPC interface to expose to the peer.
119
+ */
120
+ declare let newBunWebSocketRpcSession: <T extends RpcCompatible<T> = Empty, D = undefined>(ws: ServerWebSocket<D>, localMain?: any, options?: RpcSessionOptions) => {
121
+ stub: RpcStub$1<T>;
122
+ transport: BunWebSocketTransport<D>;
123
+ };
124
+
125
+ export { BunWebSocketTransport, RpcCompatible, RpcSessionOptions, RpcStub$1 as RpcStub, RpcTransport, newBunWebSocketRpcHandler, newBunWebSocketRpcSession };