@kition/sync 0.0.0-stage → 0.1.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,52 @@
1
+ /**
2
+ * One save of a co-edited room through its save state machine (D3, Kition platform
3
+ * docs/platform/managed-collab.md section 3, rules SV1 to SV4): `save/begin`, the host write of one
4
+ * version with its authors, `save/end`, then the checkpoint (CK1). A host side runs it for the
5
+ * person's Save (`user`), its autosave timer (`timer`), and commands (`command`), and as soon as it
6
+ * joins a room in `must-save`.
7
+ */
8
+ import type { SyncClient } from './client.js';
9
+ import { type SaveAuthor } from './protocol.js';
10
+ import type { SaveFailure, SaveTrigger } from './saveMachine.js';
11
+ export type SaveRunOptions = {
12
+ client: Pick<SyncClient, 'saveBegin' | 'saveEnd'>;
13
+ docId: string;
14
+ trigger: SaveTrigger;
15
+ /**
16
+ * Writes the document as one host version (SV1): the kit's bytes through the host's conditional
17
+ * write, with the server's `receipt` (the host verifies it and takes the authors from it, I6),
18
+ * the `authors` of the changes up to `seq` as shown here, and the `trigger`. Answers the version.
19
+ */
20
+ write: (save: {
21
+ seq: number;
22
+ authors: SaveAuthor[];
23
+ trigger: SaveTrigger;
24
+ receipt: string;
25
+ }) => Promise<string>;
26
+ /** After a saved version: keeps the replica as the file's checkpoint for `version` (CK1). A failure here does not undo the save. */
27
+ checkpoint?: (save: {
28
+ seq: number;
29
+ version: string;
30
+ }) => Promise<void>;
31
+ /** Why `write` failed: `conflict` for a stale or refused version (SV3), `corrupted`, else `failed`. */
32
+ classify?: (error: unknown) => SaveFailure;
33
+ };
34
+ /** `saved`: one version; `clean`: nothing to save; `busy`: another session saves now; `failed`: the edits stay in the room. */
35
+ export type SaveRun = {
36
+ status: 'saved';
37
+ version: string;
38
+ seq: number;
39
+ authors: SaveAuthor[];
40
+ checkpointed: boolean;
41
+ } | {
42
+ status: 'clean';
43
+ } | {
44
+ status: 'busy';
45
+ } | {
46
+ status: 'failed';
47
+ error: SaveFailure;
48
+ cause: unknown;
49
+ };
50
+ /** The default `classify`: an error whose `code` or `reason` is `conflict` or `corrupted` says so; anything else failed. */
51
+ export declare function classifySaveError(error: unknown): SaveFailure;
52
+ export declare function runSave(options: SaveRunOptions): Promise<SaveRun>;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The transport `SyncClient` needs: the slice of the browser `WebSocket` API it uses, so a real
3
+ * `WebSocket` (with `binaryType = "arraybuffer"`) satisfies it directly, and a test can supply an
4
+ * in-memory fake instead. `createTransport(url)` makes a fresh one for each connection attempt;
5
+ * `SyncClient` assigns its own `onopen`/`onclose`/`onerror`/`onmessage`, so a transport MUST
6
+ * support being told them (not just constructor callbacks).
7
+ */
8
+ export interface SyncTransport {
9
+ send(data: string | Uint8Array): void;
10
+ close(): void;
11
+ onopen: (() => void) | null;
12
+ onclose: (() => void) | null;
13
+ onerror: ((error: unknown) => void) | null;
14
+ onmessage: ((event: {
15
+ data: string | ArrayBuffer;
16
+ }) => void) | null;
17
+ }
18
+ export type TransportFactory = (url: string) => SyncTransport;
19
+ export type ConnectionState = 'connecting' | 'online' | 'offline';
package/package.json CHANGED
@@ -1,6 +1,41 @@
1
1
  {
2
2
  "name": "@kition/sync",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Client for the open sync protocol kition-sync/1: JSON-RPC 2.0 over WebSocket, reconnect with backoff, offline push queue, and the relay between a KDM replica and its room.",
5
+ "license": "Apache-2.0",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ },
12
+ "./replicaVersions": {
13
+ "types": "./dist/replicaVersions.d.ts",
14
+ "import": "./dist/replicaVersions.js"
15
+ },
16
+ "./openToken": {
17
+ "types": "./dist/openToken.d.ts",
18
+ "import": "./dist/openToken.js"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "dependencies": {
25
+ "loro-crdt": "1.16.4",
26
+ "@kition/kdm": "0.1.0"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/KitionAI/kition-sdk.git",
31
+ "directory": "packages/sync"
32
+ },
33
+ "types": "./dist/index.d.ts",
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "main": "./dist/index.js",
38
+ "scripts": {
39
+ "build": "node ../../tools/build-package.mjs"
40
+ }
6
41
  }