psionic 0.3.2 → 0.3.4
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/adapters/reconnect.d.mts +10 -0
- package/adapters/reconnect.mjs +42 -0
- package/adapters/reconnect.mjs.map +1 -0
- package/adapters/resend.d.mts +13 -0
- package/adapters/resend.mjs +26 -0
- package/adapters/resend.mjs.map +1 -0
- package/adapters/socket.d.mts +14 -0
- package/adapters/socket.mjs +160 -0
- package/adapters/socket.mjs.map +1 -0
- package/adapters/websocket.d.mts +21 -0
- package/adapters/websocket.mjs +135 -0
- package/adapters/websocket.mjs.map +1 -0
- package/adapters/webworker.d.mts +8 -0
- package/adapters/webworker.mjs +42 -0
- package/adapters/webworker.mjs.map +1 -0
- package/codecs/cbor.d.mts +12 -0
- package/codecs/cbor.mjs +198 -0
- package/codecs/cbor.mjs.map +1 -0
- package/codecs/psionic.d.mts +12 -0
- package/codecs/psionic.mjs +57 -0
- package/codecs/psionic.mjs.map +1 -0
- package/package.json +42 -24
- package/psionic.d.mts +203 -0
- package/psionic.mjs +671 -0
- package/psionic.mjs.map +1 -0
- package/session.d.mts +63 -0
- package/session.mjs +92 -0
- package/session.mjs.map +1 -0
- package/utils/react-hooks.d.mts +24 -0
- package/utils/react-hooks.mjs +37 -0
- package/utils/react-hooks.mjs.map +1 -0
- package/README.md +0 -321
- package/__tests__/async-functions.ts +0 -43
- package/__tests__/async-generator-functions.ts +0 -137
- package/__tests__/convert-from-wire-format.ts +0 -65
- package/__tests__/convert-to-wire-format.ts +0 -66
- package/__tests__/describe.ts +0 -26
- package/__tests__/lib/adapter.ts +0 -47
- package/docs/extending.md +0 -19
- package/docs/protocol.md +0 -154
- package/examples/cli-chat/client.mjs +0 -21
- package/examples/cli-chat/package.json +0 -17
- package/examples/cli-chat/server.mjs +0 -48
- package/examples/cloudflare/README.md +0 -37
- package/examples/cloudflare/eslint.config.js +0 -19
- package/examples/cloudflare/index.html +0 -13
- package/examples/cloudflare/package.json +0 -29
- package/examples/cloudflare/public/vite.svg +0 -1
- package/examples/cloudflare/src/App.svelte +0 -450
- package/examples/cloudflare/src/api.ts +0 -33
- package/examples/cloudflare/src/main.ts +0 -4
- package/examples/cloudflare/tsconfig.app.json +0 -27
- package/examples/cloudflare/tsconfig.json +0 -14
- package/examples/cloudflare/tsconfig.node.json +0 -26
- package/examples/cloudflare/tsconfig.worker.json +0 -8
- package/examples/cloudflare/vite.config.ts +0 -8
- package/examples/cloudflare/worker/cloudflare.ts +0 -115
- package/examples/cloudflare/worker/index.ts +0 -17
- package/examples/cloudflare/worker/peer.ts +0 -47
- package/examples/cloudflare/worker-configuration.d.ts +0 -10922
- package/examples/cloudflare/wrangler.jsonc +0 -58
- package/examples/nodestreams/adapter.mjs +0 -36
- package/examples/nodestreams/child.mjs +0 -20
- package/examples/nodestreams/package.json +0 -14
- package/examples/nodestreams/parent.mjs +0 -20
- package/examples/reconnect/client.mjs +0 -22
- package/examples/reconnect/package.json +0 -15
- package/examples/reconnect/server.mjs +0 -30
- package/examples/tokens/client.mjs +0 -26
- package/examples/tokens/package.json +0 -20
- package/examples/tokens/server.mjs +0 -46
- package/make-package.js +0 -23
- package/src/adapters/reconnect.ts +0 -54
- package/src/adapters/resend.ts +0 -34
- package/src/adapters/socket.ts +0 -183
- package/src/adapters/websocket.ts +0 -157
- package/src/adapters/webworker.ts +0 -48
- package/src/codecs/cbor.ts +0 -239
- package/src/codecs/psionic.ts +0 -75
- package/src/psionic.ts +0 -876
- package/src/session.ts +0 -164
- package/src/utils/react-hooks.ts +0 -48
- package/tests +0 -8
- package/tsdown.config.ts +0 -19
package/src/session.ts
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import { ErrorDisconnected, OperationError, ErrorExpiredOrInvalid, ErrorRejected } from './psionic';
|
|
2
|
-
|
|
3
|
-
export type ID = string;
|
|
4
|
-
|
|
5
|
-
export type Operation<Type extends string, Context> = {
|
|
6
|
-
id: ID,
|
|
7
|
-
type: Type,
|
|
8
|
-
cleanup: () => void,
|
|
9
|
-
reject: (error: OperationError) => void,
|
|
10
|
-
context: Context
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type OperationGenerator = Operation<'generator', {
|
|
14
|
-
name: string[];
|
|
15
|
-
/** True while processing next/throw/return — prevents reentry. */
|
|
16
|
-
busy?: boolean,
|
|
17
|
-
target: any,
|
|
18
|
-
generator?: AsyncGenerator<any, any, any>,
|
|
19
|
-
}>;
|
|
20
|
-
|
|
21
|
-
export type OperationOutboundCall = Operation<'outbound-call', {
|
|
22
|
-
resolve: (value: any) => void,
|
|
23
|
-
reject: (error: any) => void,
|
|
24
|
-
}>;
|
|
25
|
-
|
|
26
|
-
export type OperationInboundCall = Operation<'inbound-call', undefined>;
|
|
27
|
-
|
|
28
|
-
/** Tracks in-flight operations (promises, generators) for cleanup and policy enforcement. */
|
|
29
|
-
export type Session = {
|
|
30
|
-
/** Look up an existing operation by id. */
|
|
31
|
-
use<TypeName extends OperationTypes['type'] | undefined, Type extends Extract<OperationTypes, { type: TypeName }>>(type: TypeName | undefined, id: ID): (TypeName extends undefined ? OperationTypes : Type) | undefined;
|
|
32
|
-
/** Register a new operation. */
|
|
33
|
-
add<TypeName extends OperationTypes['type'], Type extends Extract<OperationTypes, { type: TypeName }>>(type: TypeName, context: Type['context'], reject: Type['reject']): Type;
|
|
34
|
-
/** Clear all operations (e.g. on disconnect). */
|
|
35
|
-
clear(reason?: string): void;
|
|
36
|
-
/** Latest merged description of the peer's remote */
|
|
37
|
-
remoteDescription?: any;
|
|
38
|
-
/** Latest merged description of the peer's own state */
|
|
39
|
-
localDescription?: any;
|
|
40
|
-
/** Last generated ID */
|
|
41
|
-
lastID?: ID;
|
|
42
|
-
/** Persistent metadata for the session */
|
|
43
|
-
meta: Record<string, any>;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export type OperationTypes = OperationGenerator | OperationOutboundCall | OperationInboundCall;
|
|
47
|
-
|
|
48
|
-
export type SessionOptions = {
|
|
49
|
-
/** Timeout for in-flight operations in milliseconds. Default is 6 minutes. */
|
|
50
|
-
timeout?: number;
|
|
51
|
-
/** Custom setTimeout function. */
|
|
52
|
-
setTimeout?: (callback: () => void, ms: number) => ReturnType<typeof setTimeout>;
|
|
53
|
-
/** Custom clearTimeout function. */
|
|
54
|
-
clearTimeout?: (id: ReturnType<typeof setTimeout>) => void;
|
|
55
|
-
/** Maximum in-flight operations. Default is 16 per peer. */
|
|
56
|
-
//maxOperation?: { [K in OperationTypes['type']]?: number };
|
|
57
|
-
permitOperation?<TypeName extends OperationTypes['type'], Type extends Extract<OperationTypes, { type: TypeName }>>(type: TypeName, context: Type['context'], counts: Map<OperationTypes['type'], number>): boolean | Promise<boolean>;
|
|
58
|
-
/** State to restore from a previous session. */
|
|
59
|
-
restore?: {
|
|
60
|
-
/** Latest merged description of the peer's remote */
|
|
61
|
-
remoteDescription?: any;
|
|
62
|
-
/** Latest merged description of the peer's own state */
|
|
63
|
-
localDescription?: any;
|
|
64
|
-
/** Last used ID from the previous session */
|
|
65
|
-
lastID?: ID;
|
|
66
|
-
/** Session metadata from the previous session */
|
|
67
|
-
meta?: Record<string, any>;
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Creates a default session, which tracks in-flight local and remote peer operations as well as current state.
|
|
73
|
-
*
|
|
74
|
-
* The session can be reused across multiple connections to allow for features like automatic resending of frames on reconnect.
|
|
75
|
-
* Note that the operation calls themselves cannot be serialized, but the session can be reattached to a new peer in the same isolate.
|
|
76
|
-
**/
|
|
77
|
-
export function createSession(opts: SessionOptions = {}): Session {
|
|
78
|
-
const idGenerator = monotonicBase36Ids(opts.restore?.lastID);
|
|
79
|
-
const permit = opts.permitOperation ?? ((type, _ctx, counts) => (counts.get(type) ?? 0) < 0x10);
|
|
80
|
-
const timeout = opts.timeout ?? (1000 * 60 * 6); // Default timeout 6 minutes
|
|
81
|
-
const setTimeoutFn = opts.setTimeout ?? setTimeout;
|
|
82
|
-
const clearTimeoutFn = opts.clearTimeout ?? clearTimeout;
|
|
83
|
-
const operation = new Map<ID, OperationTypes>();
|
|
84
|
-
const counts = new Map<OperationTypes['type'], number>();
|
|
85
|
-
const timers = new Map<ID, any>();
|
|
86
|
-
|
|
87
|
-
function touch(id: ID) {
|
|
88
|
-
clearTimeoutFn(timers.get(id));
|
|
89
|
-
timers.set(id, setTimeoutFn(() => {
|
|
90
|
-
const result = operation.get(id);
|
|
91
|
-
if (!result) return;
|
|
92
|
-
result.reject(new OperationError(ErrorExpiredOrInvalid));
|
|
93
|
-
}, timeout));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const session = {
|
|
97
|
-
remoteDescription: opts.restore?.remoteDescription,
|
|
98
|
-
localDescription: opts.restore?.localDescription,
|
|
99
|
-
lastID: opts.restore?.lastID,
|
|
100
|
-
meta: opts.restore?.meta ?? {},
|
|
101
|
-
get operations() { return counts; },
|
|
102
|
-
|
|
103
|
-
use(type, id) {
|
|
104
|
-
const result = operation.get(id);
|
|
105
|
-
if (!result || (type !== undefined && result.type !== type)) return;
|
|
106
|
-
touch(id);
|
|
107
|
-
return result as Extract<OperationTypes, { type: typeof type }>;
|
|
108
|
-
},
|
|
109
|
-
add(type, context, reject) {
|
|
110
|
-
if (!type) throw new Error('Operation type is required');
|
|
111
|
-
if (!permit(type, context, counts)) {
|
|
112
|
-
throw new OperationError(ErrorRejected);
|
|
113
|
-
}
|
|
114
|
-
let done = false;
|
|
115
|
-
const id = idGenerator();
|
|
116
|
-
session.lastID = id;
|
|
117
|
-
const operationObj = {
|
|
118
|
-
id,
|
|
119
|
-
type,
|
|
120
|
-
context,
|
|
121
|
-
cleanup,
|
|
122
|
-
reject: (error: OperationError) => {
|
|
123
|
-
if (done) return;
|
|
124
|
-
reject(error);
|
|
125
|
-
cleanup();
|
|
126
|
-
},
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function cleanup() {
|
|
130
|
-
if (done) return;
|
|
131
|
-
done = true;
|
|
132
|
-
clearTimeoutFn(timers.get(id));
|
|
133
|
-
timers.delete(id);
|
|
134
|
-
operation.delete(id);
|
|
135
|
-
counts.set(type, Math.max(0, (counts.get(type) ?? 1) - 1));
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
touch(id);
|
|
139
|
-
operation.set(id, operationObj as OperationTypes);
|
|
140
|
-
counts.set(type, (counts.get(type) ?? 0) + 1);
|
|
141
|
-
return operationObj;
|
|
142
|
-
},
|
|
143
|
-
clear() {
|
|
144
|
-
const copy = Array.from(operation.values());
|
|
145
|
-
operation.clear();
|
|
146
|
-
for (const value of copy) {
|
|
147
|
-
value.reject(new OperationError(ErrorDisconnected));
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
} as Session;
|
|
151
|
-
|
|
152
|
-
return session;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** Generates monotonic base-36 IDs, 0 - zzzzzzzzzz, before wrapping. */
|
|
156
|
-
export function monotonicBase36Ids(lastID?: ID): () => ID {
|
|
157
|
-
let previous = lastID ? parseInt(lastID, 36) : 0;
|
|
158
|
-
const maxValue = (36**10);
|
|
159
|
-
return () => {
|
|
160
|
-
const nextID = (previous + 1) % maxValue;
|
|
161
|
-
previous = nextID;
|
|
162
|
-
return nextID.toString(36) as ID;
|
|
163
|
-
}
|
|
164
|
-
}
|
package/src/utils/react-hooks.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { type BasicDescription, type Description, type Peer, STATE_READY } from "../psionic";
|
|
2
|
-
|
|
3
|
-
type PeerContext<T extends BasicDescription> = { peer: Peer<T>, remote: Peer<T>['remote'], ready: boolean };
|
|
4
|
-
|
|
5
|
-
type FC<P> = (props: P) => any | Promise<any>;
|
|
6
|
-
type ReactShape = {
|
|
7
|
-
useSyncExternalStore: any
|
|
8
|
-
createContext: any
|
|
9
|
-
useContext: any
|
|
10
|
-
useEffect: any
|
|
11
|
-
createElement: any
|
|
12
|
-
useRef: any;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
type ReactPeerProvider<RemoteAPI extends BasicDescription = any> = {
|
|
16
|
-
PeerProvider: FC<{ children: any }>,
|
|
17
|
-
usePeerRemote:() => Description<RemoteAPI>;
|
|
18
|
-
usePeer: () => Peer<RemoteAPI>;
|
|
19
|
-
usePeerReady: () => boolean;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export function createPeerHooks<T extends BasicDescription>(peer: Peer<T>, React: ReactShape): ReactPeerProvider<T> {
|
|
23
|
-
|
|
24
|
-
const PeerContext = React.createContext(undefined as unknown as PeerContext<any>);
|
|
25
|
-
|
|
26
|
-
function PeerProvider({ children }: { children: any }) {
|
|
27
|
-
const remote = React.useSyncExternalStore((d: any) => peer.on('describe', d), () => peer.remote);
|
|
28
|
-
const ready = React.useSyncExternalStore((d: any) => peer.on('readyStateChange', d), () => peer.readyState >= STATE_READY);
|
|
29
|
-
return React.createElement(PeerContext.Provider, { value: { peer, remote, ready } }, children);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function usePeerRemote() {
|
|
33
|
-
const { remote } = React.useContext(PeerContext);
|
|
34
|
-
return remote as Description<T>;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function usePeer() {
|
|
38
|
-
const { peer } = React.useContext(PeerContext);
|
|
39
|
-
return peer as Peer<T>;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function usePeerReady() {
|
|
43
|
-
const { ready } = React.useContext(PeerContext);
|
|
44
|
-
return ready;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return { PeerProvider, usePeerRemote, usePeer, usePeerReady };
|
|
48
|
-
}
|
package/tests
DELETED
package/tsdown.config.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from 'tsdown'
|
|
2
|
-
|
|
3
|
-
export default defineConfig({
|
|
4
|
-
entry: [
|
|
5
|
-
{
|
|
6
|
-
"*": "./src/*.ts",
|
|
7
|
-
"codecs/*": "./src/codecs/*.ts",
|
|
8
|
-
"adapters/*": "./src/adapters/*.ts",
|
|
9
|
-
"utils/*": "./src/utils/*.ts",
|
|
10
|
-
}
|
|
11
|
-
],
|
|
12
|
-
dts: true,
|
|
13
|
-
clean: true,
|
|
14
|
-
outDir: "dist",
|
|
15
|
-
target: "es2025",
|
|
16
|
-
sourcemap: true,
|
|
17
|
-
minify: true,
|
|
18
|
-
format: ['esm']
|
|
19
|
-
});
|