psionic 0.3.1 → 0.3.2
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 +321 -0
- package/__tests__/async-functions.ts +43 -0
- package/__tests__/async-generator-functions.ts +137 -0
- package/__tests__/convert-from-wire-format.ts +65 -0
- package/__tests__/convert-to-wire-format.ts +66 -0
- package/__tests__/describe.ts +26 -0
- package/__tests__/lib/adapter.ts +47 -0
- package/docs/extending.md +19 -0
- package/docs/protocol.md +154 -0
- package/examples/cli-chat/client.mjs +21 -0
- package/examples/cli-chat/package.json +17 -0
- package/examples/cli-chat/server.mjs +48 -0
- package/examples/cloudflare/README.md +37 -0
- package/examples/cloudflare/eslint.config.js +19 -0
- package/examples/cloudflare/index.html +13 -0
- package/examples/cloudflare/package.json +29 -0
- package/examples/cloudflare/public/vite.svg +1 -0
- package/examples/cloudflare/src/App.svelte +450 -0
- package/examples/cloudflare/src/api.ts +33 -0
- package/examples/cloudflare/src/main.ts +4 -0
- package/examples/cloudflare/tsconfig.app.json +27 -0
- package/examples/cloudflare/tsconfig.json +14 -0
- package/examples/cloudflare/tsconfig.node.json +26 -0
- package/examples/cloudflare/tsconfig.worker.json +8 -0
- package/examples/cloudflare/vite.config.ts +8 -0
- package/examples/cloudflare/worker/cloudflare.ts +115 -0
- package/examples/cloudflare/worker/index.ts +17 -0
- package/examples/cloudflare/worker/peer.ts +47 -0
- package/examples/cloudflare/worker-configuration.d.ts +10922 -0
- package/examples/cloudflare/wrangler.jsonc +58 -0
- package/examples/nodestreams/adapter.mjs +36 -0
- package/examples/nodestreams/child.mjs +20 -0
- package/examples/nodestreams/package.json +14 -0
- package/examples/nodestreams/parent.mjs +20 -0
- package/examples/reconnect/client.mjs +22 -0
- package/examples/reconnect/package.json +15 -0
- package/examples/reconnect/server.mjs +30 -0
- package/examples/tokens/client.mjs +26 -0
- package/examples/tokens/package.json +20 -0
- package/examples/tokens/server.mjs +46 -0
- package/make-package.js +23 -0
- package/package.json +24 -42
- package/src/adapters/reconnect.ts +54 -0
- package/src/adapters/resend.ts +34 -0
- package/src/adapters/socket.ts +183 -0
- package/src/adapters/websocket.ts +157 -0
- package/src/adapters/webworker.ts +48 -0
- package/src/codecs/cbor.ts +239 -0
- package/src/codecs/psionic.ts +75 -0
- package/src/psionic.ts +876 -0
- package/src/session.ts +164 -0
- package/src/utils/react-hooks.ts +48 -0
- package/tests +8 -0
- package/tsdown.config.ts +19 -0
- package/adapters/reconnect.d.mts +0 -10
- package/adapters/reconnect.mjs +0 -42
- package/adapters/reconnect.mjs.map +0 -1
- package/adapters/resend.d.mts +0 -13
- package/adapters/resend.mjs +0 -29
- package/adapters/resend.mjs.map +0 -1
- package/adapters/socket.d.mts +0 -14
- package/adapters/socket.mjs +0 -160
- package/adapters/socket.mjs.map +0 -1
- package/adapters/websocket.d.mts +0 -20
- package/adapters/websocket.mjs +0 -134
- package/adapters/websocket.mjs.map +0 -1
- package/adapters/webworker.d.mts +0 -8
- package/adapters/webworker.mjs +0 -42
- package/adapters/webworker.mjs.map +0 -1
- package/codecs/cbor.d.mts +0 -12
- package/codecs/cbor.mjs +0 -198
- package/codecs/cbor.mjs.map +0 -1
- package/codecs/psionic.d.mts +0 -12
- package/codecs/psionic.mjs +0 -57
- package/codecs/psionic.mjs.map +0 -1
- package/psionic.d.mts +0 -203
- package/psionic.mjs +0 -671
- package/psionic.mjs.map +0 -1
- package/session.d.mts +0 -63
- package/session.mjs +0 -92
- package/session.mjs.map +0 -1
- package/utils/react-hooks.d.mts +0 -24
- package/utils/react-hooks.mjs +0 -37
- package/utils/react-hooks.mjs.map +0 -1
package/psionic.d.mts
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
import { ID, OperationTypes, Session } from "./session.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/psionic.d.ts
|
|
4
|
-
declare const Keyword: "@@psion";
|
|
5
|
-
declare let CallContextSymbol: symbol;
|
|
6
|
-
declare const AsyncFnKey = "f";
|
|
7
|
-
declare const AsyncGenKey = "g";
|
|
8
|
-
declare const STATE_DISCONNECTED: 0;
|
|
9
|
-
declare const STATE_CONNECTING: 1;
|
|
10
|
-
declare const STATE_CONNECTED: 2;
|
|
11
|
-
declare const STATE_LOCAL_DESCRIBED: 3;
|
|
12
|
-
declare const STATE_REMOTE_DESCRIBED: 4;
|
|
13
|
-
declare const STATE_READY: 5;
|
|
14
|
-
type ReadyState = typeof STATE_DISCONNECTED | typeof STATE_CONNECTING | typeof STATE_CONNECTED | typeof STATE_LOCAL_DESCRIBED | typeof STATE_REMOTE_DESCRIBED | typeof STATE_READY;
|
|
15
|
-
declare const OpDescribe: "D";
|
|
16
|
-
declare const OpCall: "C";
|
|
17
|
-
declare const OpOK: "K";
|
|
18
|
-
declare const OpError: "E";
|
|
19
|
-
declare const OpIterNext: ".";
|
|
20
|
-
declare const OpIterReturn: "<";
|
|
21
|
-
declare const OpIterThrow: ">";
|
|
22
|
-
type CallFrame = [type: typeof OpCall, id: ID, name: string[], args: any[]];
|
|
23
|
-
type DescribeFrame = [type: typeof OpDescribe, id: ID, description: any, swap?: boolean];
|
|
24
|
-
type RespondFrame = [type: typeof OpOK, id: ID, result: any];
|
|
25
|
-
type ErrorFrame = [type: typeof OpError, id: ID, code: number, error?: string];
|
|
26
|
-
type IterNextFrame = [type: typeof OpIterNext, id: ID, operationId: ID, value: any];
|
|
27
|
-
type IterReturnFrame = [type: typeof OpIterReturn, id: ID, operationId: ID, value: any];
|
|
28
|
-
type IterThrowFrame = [type: typeof OpIterThrow, id: ID, operationId: ID, code: number, error?: string];
|
|
29
|
-
type Frame = DescribeFrame | CallFrame | RespondFrame | ErrorFrame | IterNextFrame | IterReturnFrame | IterThrowFrame;
|
|
30
|
-
type FrameOps = Frame[0];
|
|
31
|
-
declare const Errors: readonly [{
|
|
32
|
-
readonly code: 0;
|
|
33
|
-
readonly error: "@@psion: Remote error";
|
|
34
|
-
}, {
|
|
35
|
-
readonly code: 1;
|
|
36
|
-
readonly error: "@@psion: No remote assigned";
|
|
37
|
-
}, {
|
|
38
|
-
readonly code: 2;
|
|
39
|
-
readonly error: "@@psion: Disconnected";
|
|
40
|
-
}, {
|
|
41
|
-
readonly code: 3;
|
|
42
|
-
readonly error: "@@psion: Protocol violation";
|
|
43
|
-
}, {
|
|
44
|
-
readonly code: 4;
|
|
45
|
-
readonly error: "@@psion: Function not found";
|
|
46
|
-
}, {
|
|
47
|
-
readonly code: 5;
|
|
48
|
-
readonly error: "@@psion: Expired or invalid";
|
|
49
|
-
}, {
|
|
50
|
-
readonly code: 6;
|
|
51
|
-
readonly error: "@@psion: Rejected";
|
|
52
|
-
}];
|
|
53
|
-
declare const ErrorGeneric: {
|
|
54
|
-
readonly code: 0;
|
|
55
|
-
readonly error: "@@psion: Remote error";
|
|
56
|
-
};
|
|
57
|
-
declare const ErrorNoRemote: {
|
|
58
|
-
readonly code: 1;
|
|
59
|
-
readonly error: "@@psion: No remote assigned";
|
|
60
|
-
};
|
|
61
|
-
declare const ErrorDisconnected: {
|
|
62
|
-
readonly code: 2;
|
|
63
|
-
readonly error: "@@psion: Disconnected";
|
|
64
|
-
};
|
|
65
|
-
declare const ErrorProtocolViolation: {
|
|
66
|
-
readonly code: 3;
|
|
67
|
-
readonly error: "@@psion: Protocol violation";
|
|
68
|
-
};
|
|
69
|
-
declare const ErrorFunctionNotFound: {
|
|
70
|
-
readonly code: 4;
|
|
71
|
-
readonly error: "@@psion: Function not found";
|
|
72
|
-
};
|
|
73
|
-
declare const ErrorExpiredOrInvalid: {
|
|
74
|
-
readonly code: 5;
|
|
75
|
-
readonly error: "@@psion: Expired or invalid";
|
|
76
|
-
};
|
|
77
|
-
declare const ErrorRejected: {
|
|
78
|
-
readonly code: 6;
|
|
79
|
-
readonly error: "@@psion: Rejected";
|
|
80
|
-
};
|
|
81
|
-
type ErrorTypes = typeof Errors[number];
|
|
82
|
-
/** Codec for serializing frames to/from wire format. */
|
|
83
|
-
type WireCodec = {
|
|
84
|
-
type?: 'text' | 'binary';
|
|
85
|
-
messageToWire: (message: any[]) => string | Uint8Array;
|
|
86
|
-
messageFromWire: (wire: string | Uint8Array) => any[];
|
|
87
|
-
};
|
|
88
|
-
type BasicDescription = {
|
|
89
|
-
[key: string]: BasicValue;
|
|
90
|
-
};
|
|
91
|
-
type BasicValue = string | number | boolean | null | undefined | ((...args: any[]) => Promise<any>) | ((...args: any[]) => AsyncGenerator<any, any, any>) | BasicDescription | BasicValue[];
|
|
92
|
-
type DescriptionValue<T> = [T] extends [(string | number | boolean | null | undefined)] ? T : [T] extends [readonly (infer U)[]] ? DescriptionValue<U>[] : [T] extends [object] ? Description<T> : [T] extends [(...args: infer A extends any[]) => Promise<infer R>] ? (...args: A) => Promise<R> : [T] extends [(...args: infer A extends any[]) => AsyncGenerator<infer Y, infer R, infer N>] ? (...args: A) => AsyncGenerator<Y, R, N> : never;
|
|
93
|
-
/** Infers concrete types for functions within a description. */
|
|
94
|
-
type Description<T> = { [K in keyof T]: DescriptionValue<T[K]> };
|
|
95
|
-
type PeerEvents<RemoteAPI> = {
|
|
96
|
-
readyStateChange: (readyState: ReadyState) => void;
|
|
97
|
-
describe: (description: Description<RemoteAPI>) => void;
|
|
98
|
-
error: (err: Error) => void;
|
|
99
|
-
send: (frame: Frame) => void;
|
|
100
|
-
message: (frame: Frame) => void;
|
|
101
|
-
};
|
|
102
|
-
/** Transport-level connection to a remote peer. */
|
|
103
|
-
type Connection = {
|
|
104
|
-
/** Send a frame to the remote. */send: (frame: Frame) => void; /** Close the connection and clean up resources. */
|
|
105
|
-
close: (err?: any) => void;
|
|
106
|
-
};
|
|
107
|
-
/** Adapter interface for middleware or peers */
|
|
108
|
-
type AdapterNext = {
|
|
109
|
-
session: Session;
|
|
110
|
-
handleMessage: Peer["handleMessage"];
|
|
111
|
-
close: Peer["close"];
|
|
112
|
-
};
|
|
113
|
-
type Adapter = (peer: Peer) => Promise<Connection>;
|
|
114
|
-
type PeerOptions<LocalAPI extends BasicDescription> = {
|
|
115
|
-
/** Initial description sent to the remote on connect. */description?: LocalAPI; /** Send full error messages to remote. Default: false. */
|
|
116
|
-
sendErrorMessages?: boolean; /** Accept full error messages from remote. Default: false. */
|
|
117
|
-
receiveErrorMessages?: boolean;
|
|
118
|
-
session?: Session;
|
|
119
|
-
};
|
|
120
|
-
/** Represents a peer in the network. */
|
|
121
|
-
type Peer<RemoteAPI extends BasicDescription = any, LocalAPI extends BasicDescription = any> = {
|
|
122
|
-
session: Session; /** Send a frame to the remote peer. */
|
|
123
|
-
send(...frame: any[]): void; /** Emit an event to local listeners. */
|
|
124
|
-
emit(key: string, ...args: any[]): void; /** Handle an incoming message frame from the remote peer. */
|
|
125
|
-
handleMessage(frame: Frame): void; /** Whether to send full error messages to remote. */
|
|
126
|
-
sendErrorMessages?: boolean; /** Whether to accept full error messages from remote. */
|
|
127
|
-
receiveErrorMessages?: boolean; /** Register an event listener. */
|
|
128
|
-
on: <K extends keyof PeerEvents<RemoteAPI>>(key: K, listener: PeerEvents<RemoteAPI>[K]) => () => void; /** Remove an event listener. */
|
|
129
|
-
off: <K extends keyof PeerEvents<RemoteAPI>>(key: K, listener: PeerEvents<RemoteAPI>[K]) => void; /** Connect to remote, describe local state, optionally wait for remote description. */
|
|
130
|
-
connect(connect: Adapter, waitForRemoteDescribe?: boolean): Promise<void>; /** Remote's described state, with functions replaced by invokers. */
|
|
131
|
-
remote?: Description<RemoteAPI>; /** Describe local state to remote. Accepts an object or an updater function. */
|
|
132
|
-
describe: (stateChange: DescribeCallback<Description<LocalAPI>>) => Promise<void>; /** Close connection and clean up. Peer can reconnect after closing. */
|
|
133
|
-
close(ex?: any): void; /** Copy of the most recently described local state. */
|
|
134
|
-
local?: Description<LocalAPI>;
|
|
135
|
-
/**
|
|
136
|
-
* - `0` STATE_DISCONNECTED: The peer is not connected to any remote.
|
|
137
|
-
* - `1` STATE_CONNECTING: The peer is currently connecting to a new remote.
|
|
138
|
-
* - `2` STATE_CONNECTED: The peer is connected to a remote, but is not yet ready to use.
|
|
139
|
-
* - `3` STATE_LOCAL_DESCRIBED: The peer has described itself, but not received a description.
|
|
140
|
-
* - `4` STATE_REMOTE_DESCRIBED: The peer has received a description, but not described itself.
|
|
141
|
-
* - `5` STATE_READY: The peer is ready to both send and receive messages.
|
|
142
|
-
*/
|
|
143
|
-
readyState: ReadyState; /** Current remote connection, if any. */
|
|
144
|
-
connection?: Connection;
|
|
145
|
-
get ready(): boolean;
|
|
146
|
-
};
|
|
147
|
-
type EventEmitter<T> = {
|
|
148
|
-
on: <K extends keyof T>(key: K, listener: T[K]) => () => void;
|
|
149
|
-
off: <K extends keyof T>(key: K, listener: T[K]) => void;
|
|
150
|
-
};
|
|
151
|
-
/** Create a new event emitter instance. */
|
|
152
|
-
declare function createEventEmitter(): {
|
|
153
|
-
on: (key: string, listener: (...args: any[]) => void) => () => void;
|
|
154
|
-
emit: (key: string, ...args: any[]) => void;
|
|
155
|
-
off: (key: string, listener: (...args: any[]) => void) => void;
|
|
156
|
-
};
|
|
157
|
-
/** Either a full state object, or a state updater function. */
|
|
158
|
-
type DescribeCallback<T> = T | ((current: T) => T);
|
|
159
|
-
/** Events emitted by a Describer instance. */
|
|
160
|
-
type DescriberEvents<T> = {
|
|
161
|
-
/** Emitted when the state changes. */change: (description: Description<T>, previous: Description<T>, swap?: boolean) => void;
|
|
162
|
-
};
|
|
163
|
-
/** A Describer instance manages state and emits events when the state changes. */
|
|
164
|
-
type Describer<T> = EventEmitter<DescriberEvents<T>> & {
|
|
165
|
-
/** Get the complete current state. */currentState(): T; /** Describe a new state change. */
|
|
166
|
-
describe: (stateChange: DescribeCallback<T>) => [prevState: T, newState: T, swap: boolean];
|
|
167
|
-
};
|
|
168
|
-
/** Create a new Describer instance, which holds state and emits events when state changes. */
|
|
169
|
-
declare function createDescription<T>(initialState: T): Describer<T>;
|
|
170
|
-
/** Create a new disconnected peer instance. */
|
|
171
|
-
declare function createPeer<RemoteAPI extends BasicDescription = any, LocalAPI extends BasicDescription = any>(opts?: PeerOptions<LocalAPI>): Peer<RemoteAPI, LocalAPI>;
|
|
172
|
-
/** Resolves when remote has described itself. Rejects on disconnect. */
|
|
173
|
-
declare function waitUntilRemoteReady(peer: Peer): Promise<void>;
|
|
174
|
-
/** Converts a local description to wire-safe codec format (functions → sentinel symbols). */
|
|
175
|
-
declare function toCodecDescription(description: any): any;
|
|
176
|
-
/** Converts a wire-format description into a usable object (sentinel symbols → invoker functions). */
|
|
177
|
-
declare function fromCodecDescription(peer: Peer, description: any): any;
|
|
178
|
-
/** Creates a local async generator that proxies iteration to the remote. */
|
|
179
|
-
declare function createAsyncGeneratorFunction(peer: Peer, name: string[]): (...args: any[]) => AsyncGenerator<any, any, any>;
|
|
180
|
-
/** Marks a function as an async generator, setting the appropriate properties. */
|
|
181
|
-
declare function markAsAsyncGenerator<T>(wrappedAsyncGeneratorFunction: T): T;
|
|
182
|
-
/** Creates a local async function that proxies calls to the remote. */
|
|
183
|
-
declare function createAsyncFunction(peer: Peer, name: string[], waitForPeer?: boolean): (...args: any[]) => Promise<any>;
|
|
184
|
-
/** Context value that can be passed in to described functions */
|
|
185
|
-
type CallContext = {
|
|
186
|
-
peer: Peer;
|
|
187
|
-
id: ID;
|
|
188
|
-
operation: OperationTypes;
|
|
189
|
-
};
|
|
190
|
-
type HasContext<T> = {};
|
|
191
|
-
type RemoveFirstParameter<T> = T extends ((first: CallContext, ...args: infer U) => infer R) ? (...args: U) => R : never;
|
|
192
|
-
/** Wraps a function to automatically provide a call context. */
|
|
193
|
-
declare function withContext<T extends Function>(fn: T): RemoveFirstParameter<T> | HasContext<RemoveFirstParameter<T>>;
|
|
194
|
-
declare class OperationError extends Error {
|
|
195
|
-
code: number;
|
|
196
|
-
constructor(ex: ErrorTypes | string, code?: number);
|
|
197
|
-
}
|
|
198
|
-
declare function isAsyncGeneratorLike(value: any): boolean;
|
|
199
|
-
/** Deep-merges a patch into a description. Null values delete keys. */
|
|
200
|
-
declare function mergeDescriptions(original: any, patch: any): any;
|
|
201
|
-
//#endregion
|
|
202
|
-
export { Adapter, AdapterNext, AsyncFnKey, AsyncGenKey, BasicDescription, BasicValue, CallContext, CallContextSymbol, CallFrame, Connection, DescribeFrame, Description, DescriptionValue, ErrorDisconnected, ErrorExpiredOrInvalid, ErrorFrame, ErrorFunctionNotFound, ErrorGeneric, ErrorNoRemote, ErrorProtocolViolation, ErrorRejected, Errors, Frame, FrameOps, IterNextFrame, IterReturnFrame, IterThrowFrame, Keyword, OpCall, OpDescribe, OpError, OpIterNext, OpIterReturn, OpIterThrow, OpOK, OperationError, Peer, PeerEvents, PeerOptions, ReadyState, RespondFrame, STATE_CONNECTED, STATE_CONNECTING, STATE_DISCONNECTED, STATE_LOCAL_DESCRIBED, STATE_READY, STATE_REMOTE_DESCRIBED, WireCodec, createAsyncFunction, createAsyncGeneratorFunction, createDescription, createEventEmitter, createPeer, fromCodecDescription, isAsyncGeneratorLike, markAsAsyncGenerator, mergeDescriptions, toCodecDescription, waitUntilRemoteReady, withContext };
|
|
203
|
-
//# sourceMappingURL=psionic.d.mts.map
|