@articollab/sync 0.0.0-stage → 0.12.0-beta.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.
Files changed (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +6 -2
  3. package/dist/client.d.ts +107 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +568 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/errors.d.ts +18 -0
  8. package/dist/errors.d.ts.map +1 -0
  9. package/dist/errors.js +28 -0
  10. package/dist/errors.js.map +1 -0
  11. package/dist/http-client.d.ts +16 -0
  12. package/dist/http-client.d.ts.map +1 -0
  13. package/dist/http-client.js +82 -0
  14. package/dist/http-client.js.map +1 -0
  15. package/dist/http-handler.d.ts +65 -0
  16. package/dist/http-handler.d.ts.map +1 -0
  17. package/dist/http-handler.js +448 -0
  18. package/dist/http-handler.js.map +1 -0
  19. package/dist/index.d.ts +10 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +10 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/node.d.ts +31 -0
  24. package/dist/node.d.ts.map +1 -0
  25. package/dist/node.js +62 -0
  26. package/dist/node.js.map +1 -0
  27. package/dist/postgres.d.ts +30 -0
  28. package/dist/postgres.d.ts.map +1 -0
  29. package/dist/postgres.js +155 -0
  30. package/dist/postgres.js.map +1 -0
  31. package/dist/presence.d.ts +35 -0
  32. package/dist/presence.d.ts.map +1 -0
  33. package/dist/presence.js +118 -0
  34. package/dist/presence.js.map +1 -0
  35. package/dist/server-file-store.d.ts +19 -0
  36. package/dist/server-file-store.d.ts.map +1 -0
  37. package/dist/server-file-store.js +149 -0
  38. package/dist/server-file-store.js.map +1 -0
  39. package/dist/server-store.d.ts +59 -0
  40. package/dist/server-store.d.ts.map +1 -0
  41. package/dist/server-store.js +97 -0
  42. package/dist/server-store.js.map +1 -0
  43. package/dist/socket-client.d.ts +23 -0
  44. package/dist/socket-client.d.ts.map +1 -0
  45. package/dist/socket-client.js +219 -0
  46. package/dist/socket-client.js.map +1 -0
  47. package/dist/socket-handler.d.ts +74 -0
  48. package/dist/socket-handler.d.ts.map +1 -0
  49. package/dist/socket-handler.js +183 -0
  50. package/dist/socket-handler.js.map +1 -0
  51. package/dist/sse.d.ts +13 -0
  52. package/dist/sse.d.ts.map +1 -0
  53. package/dist/sse.js +61 -0
  54. package/dist/sse.js.map +1 -0
  55. package/dist/store.d.ts +79 -0
  56. package/dist/store.d.ts.map +1 -0
  57. package/dist/store.js +193 -0
  58. package/dist/store.js.map +1 -0
  59. package/package.json +60 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Michael Wang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,7 @@
1
- # Temporary Holding Version
1
+ # @articollab/sync
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ HTTP and WebSocket sync and persistence for Articollab documents.
4
+
5
+ [Read the sync guide](https://eastsun5566.github.io/articollab/#/sync).
6
+
7
+ MIT.
@@ -0,0 +1,107 @@
1
+ import { type ClientBatch, type ClientCheckpoint, type ClientDocument, type DocumentSchema, type JsonValue, type SchemaShape } from "@articollab/core";
2
+ import { type SyncPresenceOptions, type SyncPresence } from "./presence.ts";
3
+ import type { CheckpointStore } from "./store.ts";
4
+ /**
5
+ * The network operations a sync client needs. Each method resolves when the request completed;
6
+ * a submission that resolves is not yet an acknowledgement. Throw an error with
7
+ * `retryable: false` for failures that must not be retried, such as an authorization error.
8
+ */
9
+ export interface SyncTransport {
10
+ /** Opens a new writer session and returns its ID with an encoded snapshot. */
11
+ register(): Promise<{
12
+ readonly clientId: string;
13
+ readonly snapshot: unknown;
14
+ }>;
15
+ submit(batch: ClientBatch): Promise<void>;
16
+ reject(batch: ClientBatch): Promise<void>;
17
+ /** Parsed JSON: the commits after `watermark`, or null when history no longer covers them. */
18
+ commitsSince(watermark: number): Promise<unknown>;
19
+ /**
20
+ * Optional push channel: delivers commits after `watermark` as the server saves them until the
21
+ * returned function closes it. Without it the client polls.
22
+ */
23
+ subscribe?(watermark: number, handlers: SyncSubscription): () => void;
24
+ /** Optional presence: shares a writer's ephemeral state, or null to leave. Needed for `presence`. */
25
+ publishPresence?(clientId: string, state: JsonValue | null): Promise<void>;
26
+ /** Optional: releases a connection the transport holds. `close()` calls it; the transport is not used again. */
27
+ close?(): void;
28
+ }
29
+ /** Callbacks a transport's push channel reports to. */
30
+ export interface SyncSubscription {
31
+ /** The channel is connected. */
32
+ onOpen(): void;
33
+ /** Parsed JSON commits, normally the ones after the last delivered. */
34
+ onCommits(commits: unknown): void;
35
+ /** History no longer covers the watermark; the channel ends after this. */
36
+ onGap(): void;
37
+ /** Parsed JSON `{ clientId, state }`: one writer's presence, or its departure with a null state. */
38
+ onPresence?(update: unknown): void;
39
+ /** Any traffic, including heartbeats; proves the channel is alive. */
40
+ onActivity(): void;
41
+ /** The channel ended, with the reason when it failed. */
42
+ onEnd(error?: unknown): void;
43
+ }
44
+ export type SyncStatus = "idle" | "syncing" | "retrying" | "blocked" | "gap" | "closed" | "failed";
45
+ /** The push channel: closed, opening, or delivering commits (polling pauses while open). */
46
+ export type SyncPushState = "off" | "connecting" | "open";
47
+ export interface SyncRetryOptions {
48
+ /** Delay before the first retry, in milliseconds. Default 500. */
49
+ readonly initialDelay?: number;
50
+ /** Upper bound for the exponentially growing delay, in milliseconds. Default 30000. */
51
+ readonly maxDelay?: number;
52
+ }
53
+ export interface ConnectSyncOptions<S extends SchemaShape> {
54
+ readonly transport: SyncTransport;
55
+ /** Resume this writer. Without it, a new session is registered. */
56
+ readonly checkpoint?: ClientCheckpoint<S>;
57
+ /**
58
+ * Persists the checkpoint after every local edit, before pending work is sent and after commits
59
+ * are received. Saves run one at a time and always save the latest checkpoint.
60
+ */
61
+ readonly save?: (checkpoint: ClientCheckpoint<S>) => void | Promise<void>;
62
+ /**
63
+ * Loads, saves and closes this writer's checkpoint: a saved checkpoint is restored, otherwise a
64
+ * session is registered. `close()` closes the store too. Not with `checkpoint` or `save`.
65
+ */
66
+ readonly store?: CheckpointStore;
67
+ /** Milliseconds between catch-up polls while idle or blocked. Default 1000; 0 disables polling. */
68
+ readonly pollInterval?: number;
69
+ readonly retry?: SyncRetryOptions;
70
+ /** Receive commits over the transport's push channel when it has one. Default true. */
71
+ readonly push?: boolean;
72
+ /** Milliseconds without any push traffic, heartbeats included, before reconnecting. Default 45000; 0 disables. */
73
+ readonly pushTimeout?: number;
74
+ /** Shares ephemeral per-writer state over the push channel; requires a transport with `publishPresence`. */
75
+ readonly presence?: SyncPresenceOptions;
76
+ }
77
+ export interface SyncClient<S extends SchemaShape> {
78
+ readonly doc: ClientDocument<S>;
79
+ readonly status: SyncStatus;
80
+ /** Why the client failed, while `status` is "failed". */
81
+ readonly error: unknown;
82
+ /** The push channel's state; always "off" without push. */
83
+ readonly push: SyncPushState;
84
+ /** Catches up and submits pending work now; true when everything pending was acknowledged. */
85
+ sync(): Promise<boolean>;
86
+ /** Confirms cancellation of the pending batches that exist when it is called. */
87
+ rejectPending(): Promise<boolean>;
88
+ /** After a history gap with no pending work, registers a new session and closes this client. */
89
+ restartFromSnapshot(): Promise<SyncClient<S>>;
90
+ on(event: "status", listener: (status: SyncStatus) => void): () => void;
91
+ on(event: "push", listener: (push: SyncPushState) => void): () => void;
92
+ /** Stops polling, retries and push and closes the store and transport; final. Responses that arrive later are ignored. */
93
+ close(): void;
94
+ }
95
+ /** A sync client with presence enabled. */
96
+ export interface PresenceSyncClient<S extends SchemaShape, P extends JsonValue = JsonValue> extends SyncClient<S> {
97
+ readonly presence: SyncPresence<P>;
98
+ }
99
+ /**
100
+ * Creates or restores a document and keeps it synchronized through `transport`.
101
+ * The client owns the document's `onBatch`: local edits are scheduled for submission automatically.
102
+ */
103
+ export declare function connectSync<S extends SchemaShape, P extends JsonValue = JsonValue>(schema: DocumentSchema<S>, options: ConnectSyncOptions<S> & {
104
+ readonly presence: SyncPresenceOptions<P>;
105
+ }): Promise<PresenceSyncClient<S, P>>;
106
+ export declare function connectSync<S extends SchemaShape>(schema: DocumentSchema<S>, options: ConnectSyncOptions<S>): Promise<SyncClient<S>>;
107
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EAEnB,KAAK,cAAc,EACnB,KAAK,SAAS,EACd,KAAK,WAAW,EAEjB,MAAM,kBAAkB,CAAC;AAK1B,OAAO,EAA2C,KAAK,mBAAmB,EAAE,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC;AACrH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD;;;;GAIG;AACH,MAAM,WAAW,aAAa;IA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