@synapse-sdk/client 0.1.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/README.md +1 -111
- package/dist/calls.d.ts +37 -0
- package/dist/calls.js +192 -0
- package/dist/channel.d.ts +84 -0
- package/dist/channel.js +215 -0
- package/dist/fec.d.ts +85 -0
- package/dist/fec.js +281 -0
- package/dist/index.d.ts +10 -74
- package/dist/index.js +10 -15
- package/dist/moq_log.d.ts +23 -0
- package/dist/moq_log.js +176 -0
- package/dist/node.d.ts +145 -0
- package/dist/node.js +407 -0
- package/dist/scene.d.ts +299 -0
- package/dist/scene.js +1531 -0
- package/dist/scene_frames.d.ts +51 -0
- package/dist/scene_frames.js +215 -0
- package/dist/serve.d.ts +46 -0
- package/dist/serve.js +281 -0
- package/dist/session.d.ts +189 -0
- package/dist/session.js +745 -0
- package/dist/signals.d.ts +10 -0
- package/dist/signals.js +63 -0
- package/dist/store.d.ts +145 -0
- package/dist/store.js +898 -0
- package/dist/synapse.d.ts +89 -0
- package/dist/synapse.js +256 -0
- package/dist/topics.d.ts +136 -0
- package/dist/topics.js +938 -0
- package/dist/types.d.ts +679 -0
- package/dist/types.js +1 -0
- package/dist/util.d.ts +67 -0
- package/dist/util.js +227 -0
- package/dist/view.d.ts +19 -0
- package/dist/view.js +145 -0
- package/package.json +18 -39
- package/dist/node-CSSIXG4k.js +0 -1501
- package/dist/node-DbD7G0Nx.d.ts +0 -466
- package/dist/react.d.ts +0 -63
- package/dist/react.js +0 -141
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
interface Closable {
|
|
2
|
+
close(): Promise<void>;
|
|
3
|
+
}
|
|
4
|
+
/** True in Bun/Node (a `process` with `versions.node`), false in browsers and workers. */
|
|
5
|
+
export declare function isServerRuntime(): boolean;
|
|
6
|
+
/** Adds a node to the set closed on SIGINT/SIGTERM; installs the handler on first use. No-op outside Bun/Node. */
|
|
7
|
+
export declare function closeOnSignals(node: Closable): void;
|
|
8
|
+
/** Removes a node (it closed on its own); the handler goes with the last node so a finished process keeps the default disposition. */
|
|
9
|
+
export declare function forgetOnSignals(node: Closable): void;
|
|
10
|
+
export {};
|
package/dist/signals.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Clean close on SIGINT/SIGTERM in Bun/Node (SDK §2, D 2026-09-27): a killed client must tell the
|
|
2
|
+
// broker it is leaving, so its cid and index entries are released at once instead of lingering
|
|
3
|
+
// until the relay's idle timeout reaps the session. Browsers have no signals; nothing installs there.
|
|
4
|
+
//
|
|
5
|
+
// One process-wide handler serves every open node: on the first signal it closes all of them
|
|
6
|
+
// (bounded by CLOSE_GRACE_MS), removes itself, and re-raises the same signal so the process ends
|
|
7
|
+
// with the default disposition and exit status (130 for SIGINT, 143 for SIGTERM) — the SDK never
|
|
8
|
+
// picks an exit code of its own. Opt out per node with `signals: false`.
|
|
9
|
+
const SIGNALS = ["SIGINT", "SIGTERM"];
|
|
10
|
+
const EXIT_CODE = { SIGINT: 130, SIGTERM: 143 };
|
|
11
|
+
/** Upper bound on the flush: `close()` waits up to 500 ms for the session's close to settle. */
|
|
12
|
+
const CLOSE_GRACE_MS = 1000;
|
|
13
|
+
const nodes = new Set();
|
|
14
|
+
let installed = false;
|
|
15
|
+
function proc() {
|
|
16
|
+
const p = globalThis.process;
|
|
17
|
+
return p && typeof p.versions?.node === "string" && typeof p.on === "function" ? p : undefined;
|
|
18
|
+
}
|
|
19
|
+
/** True in Bun/Node (a `process` with `versions.node`), false in browsers and workers. */
|
|
20
|
+
export function isServerRuntime() { return proc() !== undefined; }
|
|
21
|
+
/** Adds a node to the set closed on SIGINT/SIGTERM; installs the handler on first use. No-op outside Bun/Node. */
|
|
22
|
+
export function closeOnSignals(node) {
|
|
23
|
+
const p = proc();
|
|
24
|
+
if (!p)
|
|
25
|
+
return;
|
|
26
|
+
nodes.add(node);
|
|
27
|
+
if (installed)
|
|
28
|
+
return;
|
|
29
|
+
installed = true;
|
|
30
|
+
for (const sig of SIGNALS)
|
|
31
|
+
p.on(sig, onSignal);
|
|
32
|
+
}
|
|
33
|
+
/** Removes a node (it closed on its own); the handler goes with the last node so a finished process keeps the default disposition. */
|
|
34
|
+
export function forgetOnSignals(node) {
|
|
35
|
+
nodes.delete(node);
|
|
36
|
+
if (nodes.size > 0 || !installed)
|
|
37
|
+
return;
|
|
38
|
+
installed = false;
|
|
39
|
+
const p = proc();
|
|
40
|
+
if (p)
|
|
41
|
+
for (const sig of SIGNALS)
|
|
42
|
+
p.off(sig, onSignal);
|
|
43
|
+
}
|
|
44
|
+
function onSignal(sig) {
|
|
45
|
+
const p = proc();
|
|
46
|
+
for (const s of SIGNALS)
|
|
47
|
+
p.off(s, onSignal);
|
|
48
|
+
installed = false;
|
|
49
|
+
const live = [...nodes];
|
|
50
|
+
nodes.clear();
|
|
51
|
+
const closed = Promise.allSettled(live.map(n => n.close()));
|
|
52
|
+
const grace = new Promise(r => setTimeout(r, CLOSE_GRACE_MS));
|
|
53
|
+
void Promise.race([closed, grace]).then(() => {
|
|
54
|
+
try {
|
|
55
|
+
p.kill(p.pid, sig);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
p.exit(EXIT_CODE[sig]);
|
|
59
|
+
}
|
|
60
|
+
// The default disposition ends the process asynchronously; if the runtime ignores the re-raise, fall back.
|
|
61
|
+
setTimeout(() => p.exit(EXIT_CODE[sig]), 200);
|
|
62
|
+
});
|
|
63
|
+
}
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { SynapseError, type ActiveProject, type Role, type StoreAccess, type StoreDiff, type StoreMode, type StoreOnDeath, type StoreRow, type StoreSnapshot } from "@synapse-sdk/core";
|
|
2
|
+
import type { Node } from "./node.ts";
|
|
3
|
+
import { type Session } from "./session.ts";
|
|
4
|
+
import type { Warn } from "./topics.ts";
|
|
5
|
+
import type { StoreChange, StoreHandle, StoreOptions, StoreStatus } from "./types.ts";
|
|
6
|
+
import { AsyncQueue, type Deferred } from "./util.ts";
|
|
7
|
+
export declare const ENDED_DESTROYED = "destroyed";
|
|
8
|
+
export declare const ENDED_CONNECTION_LOST = "connection_lost";
|
|
9
|
+
export declare const ENDED_CLIENT = "client";
|
|
10
|
+
/** A watcher this many batches behind is ended, never grown. */
|
|
11
|
+
export declare const STORE_FEED_CAP = 256;
|
|
12
|
+
/** The mirror and what the runtime said about the namespace; written by the task and its reader only. */
|
|
13
|
+
export declare class State {
|
|
14
|
+
#private;
|
|
15
|
+
epoch: string | undefined;
|
|
16
|
+
version: number;
|
|
17
|
+
rows: Map<string, StoreRow>;
|
|
18
|
+
flat: ReadonlyMap<string, unknown>;
|
|
19
|
+
mode: StoreMode | undefined;
|
|
20
|
+
owner: string | undefined;
|
|
21
|
+
mine: boolean;
|
|
22
|
+
claim: string | undefined;
|
|
23
|
+
minRole: Role | undefined;
|
|
24
|
+
onDeath: StoreOnDeath | undefined;
|
|
25
|
+
absent: boolean;
|
|
26
|
+
loaded: boolean;
|
|
27
|
+
gap: boolean;
|
|
28
|
+
pending: number;
|
|
29
|
+
ended: string | undefined;
|
|
30
|
+
subscribe(l: () => void): () => void;
|
|
31
|
+
watch(): AsyncQueue<StoreChange[]>;
|
|
32
|
+
/** A feed its reader has left: it is closed and gets nothing more. */
|
|
33
|
+
unwatch(q: AsyncQueue<StoreChange[]>): void;
|
|
34
|
+
applySnapshot(s: StoreSnapshot): void;
|
|
35
|
+
applyDiff(d: StoreDiff, warn: Warn): void;
|
|
36
|
+
/** Nothing of the incarnation is known until the runtime answers the next open. */
|
|
37
|
+
reset(): void;
|
|
38
|
+
opened(a: StoreAccess | undefined): void;
|
|
39
|
+
end(reason: string): void;
|
|
40
|
+
setPending(n: number): void;
|
|
41
|
+
/** The status a handle reports: a closed handle (`closed`) has ended (`client`) whatever the namespace does. */
|
|
42
|
+
status(nodeConnected: boolean, closed?: boolean): StoreStatus;
|
|
43
|
+
}
|
|
44
|
+
type Result<T> = {
|
|
45
|
+
ok: T;
|
|
46
|
+
} | {
|
|
47
|
+
err: SynapseError;
|
|
48
|
+
};
|
|
49
|
+
interface WriteOp {
|
|
50
|
+
key?: string;
|
|
51
|
+
value?: unknown;
|
|
52
|
+
values?: Record<string, unknown>;
|
|
53
|
+
}
|
|
54
|
+
type Event = {
|
|
55
|
+
kind: "session";
|
|
56
|
+
session: Session;
|
|
57
|
+
} | {
|
|
58
|
+
kind: "sessionEnded";
|
|
59
|
+
} | {
|
|
60
|
+
kind: "nodeEnded";
|
|
61
|
+
} | {
|
|
62
|
+
kind: "opened";
|
|
63
|
+
attempt: number;
|
|
64
|
+
result: Result<unknown>;
|
|
65
|
+
} | {
|
|
66
|
+
kind: "applied";
|
|
67
|
+
epoch: string;
|
|
68
|
+
version: number;
|
|
69
|
+
} | {
|
|
70
|
+
kind: "followEnded";
|
|
71
|
+
attempt: number;
|
|
72
|
+
reason: string | undefined;
|
|
73
|
+
} | {
|
|
74
|
+
kind: "join";
|
|
75
|
+
reply: Deferred<void>;
|
|
76
|
+
} | {
|
|
77
|
+
kind: "waitVersion";
|
|
78
|
+
epoch: string;
|
|
79
|
+
version: number;
|
|
80
|
+
reply: Deferred<void>;
|
|
81
|
+
} | {
|
|
82
|
+
kind: "write";
|
|
83
|
+
op: string;
|
|
84
|
+
args: WriteOp;
|
|
85
|
+
reply: Deferred<unknown>;
|
|
86
|
+
} | {
|
|
87
|
+
kind: "writeDone";
|
|
88
|
+
op: string;
|
|
89
|
+
result: Result<unknown>;
|
|
90
|
+
reply: Deferred<unknown>;
|
|
91
|
+
} | {
|
|
92
|
+
kind: "projectChanged";
|
|
93
|
+
now: ActiveProject | null;
|
|
94
|
+
} | {
|
|
95
|
+
kind: "paced";
|
|
96
|
+
attempt: number;
|
|
97
|
+
} | {
|
|
98
|
+
kind: "close";
|
|
99
|
+
};
|
|
100
|
+
/** The namespace shared by its handles and its task. */
|
|
101
|
+
export declare class Namespace {
|
|
102
|
+
readonly name: string;
|
|
103
|
+
readonly opts: StoreOptions;
|
|
104
|
+
readonly node: Node;
|
|
105
|
+
readonly warn: Warn;
|
|
106
|
+
readonly state: State;
|
|
107
|
+
readonly mailbox: AsyncQueue<Event>;
|
|
108
|
+
readonly exited: Deferred<void>;
|
|
109
|
+
handles: number;
|
|
110
|
+
constructor(name: string, opts: StoreOptions, node: Node, warn: Warn);
|
|
111
|
+
post(e: Event): boolean;
|
|
112
|
+
request<T>(make: (reply: Deferred<T>) => Event): Promise<T>;
|
|
113
|
+
}
|
|
114
|
+
export declare function startNamespace(ns: Namespace, registry: Map<string, Namespace>): void;
|
|
115
|
+
/** The application's handle on a namespace: the last one closed ends the task. */
|
|
116
|
+
export declare class StoreHandleImpl implements StoreHandle {
|
|
117
|
+
#private;
|
|
118
|
+
private readonly ns;
|
|
119
|
+
constructor(ns: Namespace);
|
|
120
|
+
get name(): string;
|
|
121
|
+
/** The whole value, what the runtime has; the same object while nothing changed (reference-stable for React). */
|
|
122
|
+
get value(): Record<string, unknown>;
|
|
123
|
+
get(key: string): unknown;
|
|
124
|
+
get synced(): boolean;
|
|
125
|
+
get status(): StoreStatus;
|
|
126
|
+
/** `set(key, value)` or `set({ ...several keys })` (one atomic write); resolves once `value` shows it. */
|
|
127
|
+
set(keyOrValues: string | Record<string, unknown>, value?: unknown): Promise<void>;
|
|
128
|
+
delete(key: string): Promise<void>;
|
|
129
|
+
clear(): Promise<void>;
|
|
130
|
+
destroy(): Promise<void>;
|
|
131
|
+
/** `for await (const value of store.watch())`: the whole value after each change; `store.watch(key)`: that key's value each time it changes. Ends with the handle; leaving the loop removes the feed. */
|
|
132
|
+
watch(key?: string): AsyncGenerator<unknown, void, undefined>;
|
|
133
|
+
/** The framework bridge (React's `useStore`): called on every change of the value or the status. */
|
|
134
|
+
subscribe(listener: () => void): () => void;
|
|
135
|
+
/** Waits for the namespace to be in (the first snapshot applied, or absent). */
|
|
136
|
+
join(): Promise<void>;
|
|
137
|
+
close(): void;
|
|
138
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* `store(name, opts)`: one task per namespace per node; a second `store` with the same options joins
|
|
142
|
+
* it (no open written), with other options `invalid_input`; one exiting is waited out.
|
|
143
|
+
*/
|
|
144
|
+
export declare function openStore(node: Node, registry: Map<string, Namespace>, name: string, opts: StoreOptions, warn: Warn): Promise<StoreHandleImpl>;
|
|
145
|
+
export {};
|