@nexys/zk-store 0.1.12 → 0.1.14

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 CHANGED
@@ -97,8 +97,51 @@ Everything the ZK Store web app uses is exported too:
97
97
  - **Sharing**: `createShare`, `joinShare`, `downloadSharedFile`, … for
98
98
  file and folder links.
99
99
 
100
+ - **Live channels**: `openLiveChannel`, real-time rooms for a share's
101
+ owner and members or for one vault's devices (see below).
102
+
100
103
  The server's API is documented at `/docs` on any ZK Store server.
101
104
 
105
+ ## Live channels
106
+
107
+ Real-time collaboration on top of shares: everyone who can open a share
108
+ (or every device of one vault) joins a channel and exchanges small
109
+ changes ("ops") and ephemeral messages ("signals"), end-to-end encrypted.
110
+
111
+ ```ts
112
+ import { openLiveChannel, shareKeyFor, loadTree } from "@nexys/zk-store";
113
+
114
+ const key = await shareKeyFor(vault.userKey, await loadTree(vault.userKey), shareId);
115
+ const live = openLiveChannel<Edit, Cursor>(
116
+ { share: shareId, key }, // or { vault: true, key: vault.userKey }
117
+ {
118
+ channel: `myapp:${fileId}`, // hashed with the key; never sent as is
119
+ onOp: (edit, { seq, mine }) => apply(edit),
120
+ onSignal: (cursor, { from }) => showCursor(from, cursor),
121
+ onPeers: (peers) => showWhoIsHere(peers),
122
+ onStatus: (status) => {}, // connecting, live, offline, revoked, …
123
+ onGap: () => reloadSavedCopy(),
124
+ }
125
+ );
126
+ live.send({ cell: "B3", value: 520 }); // numbered by the server, echoed back
127
+ live.signal({ cursor: "B3" }); // to the others, not kept
128
+ live.close();
129
+ ```
130
+
131
+ - The server puts every op in one order (`seq`) and sends it to everyone,
132
+ the sender included, so all clients converge: apply ops in `seq` order
133
+ and keep your own unacknowledged ones (`live.pending()`) on top.
134
+ - Payloads are AES-GCM encrypted with a key derived (HKDF) from the share
135
+ key or vault key, and the channel id is an HMAC of your channel name.
136
+ - It reconnects by itself, resends unacknowledged ops and resumes where it
137
+ left off. If the server no longer has everything that was missed,
138
+ `onGap` asks you to reload the saved file; later ops still follow.
139
+ - Viewers receive ops and may send signals, but their ops are refused.
140
+ Removing a member, or stopping sharing, closes their channel
141
+ (`revoked`).
142
+ - Live channels don't store anything: keep saving the file itself as
143
+ usual (with `saveSharedFile`), as the durable copy.
144
+
102
145
  ## License
103
146
 
104
147
  MIT
@@ -324,7 +324,14 @@ export interface paths {
324
324
  * for blobs the share lists.
325
325
  */
326
326
  get: operations["getSharedBlob"];
327
- put?: never;
327
+ /**
328
+ * An editor (or the owner) saves a new version of a file the share lists.
329
+ * The bytes must be encrypted with the file's existing key, so the owner
330
+ * and every member can still read it. `If-Match` with the ETag of the
331
+ * version you read is required: a save on top of someone else's newer one
332
+ * gets 412, and the client should load theirs before trying again.
333
+ */
334
+ put: operations["putSharedBlob"];
328
335
  post?: never;
329
336
  delete?: never;
330
337
  options?: never;
@@ -342,8 +349,8 @@ export interface paths {
342
349
  get?: never;
343
350
  put?: never;
344
351
  /**
345
- * A vault holding the link joins the share. Joining again only updates the
346
- * name.
352
+ * A vault holding the link joins the share with the link's role. Joining
353
+ * again updates the name, and raises the role if the link gives more.
347
354
  */
348
355
  post: operations["joinShare"];
349
356
  delete?: never;
@@ -366,8 +373,8 @@ export interface paths {
366
373
  */
367
374
  get: operations["getShareLink"];
368
375
  /**
369
- * The owner replaces the link. The old link stops working; members who
370
- * already joined keep access.
376
+ * The owner replaces the link, optionally with a different role. The old
377
+ * link stops working; members who already joined keep access and role.
371
378
  */
372
379
  put: operations["resetShareLink"];
373
380
  post?: never;
@@ -381,6 +388,26 @@ export interface paths {
381
388
  patch?: never;
382
389
  trace?: never;
383
390
  };
391
+ "/share/{id}/live": {
392
+ parameters: {
393
+ query?: never;
394
+ header?: never;
395
+ path?: never;
396
+ cookie?: never;
397
+ };
398
+ /**
399
+ * A share's live channel: the owner and members exchange encrypted
400
+ * ops and signals in real time.
401
+ */
402
+ get: operations["shareLive"];
403
+ put?: never;
404
+ post?: never;
405
+ delete?: never;
406
+ options?: never;
407
+ head?: never;
408
+ patch?: never;
409
+ trace?: never;
410
+ };
384
411
  "/share/{id}/members/{member}": {
385
412
  parameters: {
386
413
  query?: never;
@@ -389,7 +416,8 @@ export interface paths {
389
416
  cookie?: never;
390
417
  };
391
418
  get?: never;
392
- put?: never;
419
+ /** The owner makes a member a viewer or an editor. */
420
+ put: operations["setShareMemberRole"];
393
421
  post?: never;
394
422
  /** The owner removes someone, or a member leaves. */
395
423
  delete: operations["removeShareMember"];
@@ -415,6 +443,26 @@ export interface paths {
415
443
  patch?: never;
416
444
  trace?: never;
417
445
  };
446
+ "/vault/live": {
447
+ parameters: {
448
+ query?: never;
449
+ header?: never;
450
+ path?: never;
451
+ cookie?: never;
452
+ };
453
+ /**
454
+ * Your own vault's live channel: your devices exchange encrypted ops
455
+ * and signals in real time.
456
+ */
457
+ get: operations["vaultLive"];
458
+ put?: never;
459
+ post?: never;
460
+ delete?: never;
461
+ options?: never;
462
+ head?: never;
463
+ patch?: never;
464
+ trace?: never;
465
+ };
418
466
  "/vault/rotate": {
419
467
  parameters: {
420
468
  query?: never;
@@ -560,6 +608,8 @@ export interface components {
560
608
  * only its SHA-256.
561
609
  */
562
610
  linkProof: string;
611
+ /** @description The role people get by joining through the link. Defaults to viewer. */
612
+ linkRole?: components["schemas"]["MemberRole"];
563
613
  /** @description `shareKey` wrapped with the link-derived key (base64). */
564
614
  linkWrappedKey: string;
565
615
  /**
@@ -600,6 +650,7 @@ export interface components {
600
650
  };
601
651
  JoinResponse: {
602
652
  memberId: string;
653
+ role: components["schemas"]["MemberRole"];
603
654
  };
604
655
  JoinShareRequest: {
605
656
  /** @description The link proof (32 bytes, base64). */
@@ -607,6 +658,97 @@ export interface components {
607
658
  /** @description The joiner's display name, encrypted with `shareKey` (base64). */
608
659
  name: string;
609
660
  };
661
+ /** @description A message from the client. The first must be `auth`. */
662
+ LiveClientMessage: {
663
+ epoch?: string | null;
664
+ /** Format: int64 */
665
+ since?: number | null;
666
+ token: string;
667
+ /** @enum {string} */
668
+ type: "auth";
669
+ } | {
670
+ data: string;
671
+ id?: string | null;
672
+ /** @enum {string} */
673
+ type: "op";
674
+ } | {
675
+ data: string;
676
+ to?: string | null;
677
+ /** @enum {string} */
678
+ type: "signal";
679
+ } | {
680
+ /** @enum {string} */
681
+ type: "ping";
682
+ };
683
+ /** @description One peer in a channel. */
684
+ LivePeer: {
685
+ peer: string;
686
+ role: components["schemas"]["LiveRole"];
687
+ };
688
+ /**
689
+ * @description What a peer may do in a channel.
690
+ * @enum {string}
691
+ */
692
+ LiveRole: "owner" | "editor" | "viewer";
693
+ /** @description A message from the server. */
694
+ LiveServerMessage: {
695
+ epoch: string;
696
+ /**
697
+ * @description Ops after `since` were dropped or the room restarted: reload the
698
+ * saved copy, then apply what follows.
699
+ */
700
+ gap: boolean;
701
+ peer: string;
702
+ peers: components["schemas"]["LivePeer"][];
703
+ role: components["schemas"]["LiveRole"];
704
+ /** Format: int64 */
705
+ seq: number;
706
+ /** @enum {string} */
707
+ type: "welcome";
708
+ } | {
709
+ data: string;
710
+ from: string;
711
+ id?: string | null;
712
+ /** Format: int64 */
713
+ seq: number;
714
+ /** @enum {string} */
715
+ type: "op";
716
+ } | {
717
+ data: string;
718
+ from: string;
719
+ /** @enum {string} */
720
+ type: "signal";
721
+ } | {
722
+ peer: string;
723
+ role: components["schemas"]["LiveRole"];
724
+ /** @enum {string} */
725
+ type: "peer-joined";
726
+ } | {
727
+ peer: string;
728
+ /** @enum {string} */
729
+ type: "peer-left";
730
+ } | {
731
+ peer: string;
732
+ role: components["schemas"]["LiveRole"];
733
+ /** @enum {string} */
734
+ type: "peer-role";
735
+ } | {
736
+ /** @enum {string} */
737
+ type: "pong";
738
+ } | {
739
+ code: string;
740
+ message: string;
741
+ /** @enum {string} */
742
+ type: "error";
743
+ } | {
744
+ /** @enum {string} */
745
+ type: "revoked";
746
+ };
747
+ /**
748
+ * @description What a member may do with the share's files.
749
+ * @enum {string}
750
+ */
751
+ MemberRole: "viewer" | "editor";
610
752
  MemberView: {
611
753
  /**
612
754
  * Format: int64
@@ -616,6 +758,7 @@ export interface components {
616
758
  memberId: string;
617
759
  /** @description Display name, encrypted with `shareKey` (base64). */
618
760
  name: string;
761
+ role: components["schemas"]["MemberRole"];
619
762
  };
620
763
  /** @description The body of a write that has nothing else to report. */
621
764
  OkBody: {
@@ -676,6 +819,7 @@ export interface components {
676
819
  ResetShareLinkRequest: {
677
820
  /** @description Proof for the new link (32 bytes, base64). */
678
821
  linkProof: string;
822
+ linkRole?: components["schemas"]["MemberRole"] | null;
679
823
  /** @description `shareKey` wrapped with the new link's key (base64). */
680
824
  linkWrappedKey: string;
681
825
  };
@@ -705,7 +849,12 @@ export interface components {
705
849
  token: string;
706
850
  vaultId: string;
707
851
  };
852
+ SetMemberRoleRequest: {
853
+ role: components["schemas"]["MemberRole"];
854
+ };
708
855
  ShareLinkView: {
856
+ /** @description The role joining through this link gives. */
857
+ linkRole: components["schemas"]["MemberRole"];
709
858
  /** @description `shareKey` wrapped with the link-derived key (base64). */
710
859
  linkWrappedKey?: string | null;
711
860
  /** @description The encrypted manifest (base64). */
@@ -714,6 +863,8 @@ export interface components {
714
863
  /** @enum {string} */
715
864
  ShareRole: "owner" | "member";
716
865
  ShareView: {
866
+ /** @description Whether the caller may save new versions of the share's files. */
867
+ canEdit: boolean;
717
868
  /**
718
869
  * Format: int64
719
870
  * @description Unix seconds.
@@ -721,6 +872,8 @@ export interface components {
721
872
  createdAt: number;
722
873
  /** @description Whether the share link still works for new joiners. */
723
874
  linkActive: boolean;
875
+ /** @description The role people get by joining through the link. */
876
+ linkRole: components["schemas"]["MemberRole"];
724
877
  /** @description The encrypted manifest (base64). */
725
878
  manifest: string;
726
879
  /** @description Every member for the owner; only the caller's own entry for a member. */
@@ -1717,6 +1870,8 @@ export interface operations {
1717
1870
  /** @description The blob's bytes (ciphertext) */
1718
1871
  200: {
1719
1872
  headers: {
1873
+ /** @description Hex SHA-256 of the bytes, quoted; send it back as `If-Match` to save */
1874
+ ETag?: string;
1720
1875
  [name: string]: unknown;
1721
1876
  };
1722
1877
  content: {
@@ -1750,6 +1905,92 @@ export interface operations {
1750
1905
  };
1751
1906
  };
1752
1907
  };
1908
+ putSharedBlob: {
1909
+ parameters: {
1910
+ query?: never;
1911
+ header: {
1912
+ /** @description ETag of the version this save replaces */
1913
+ "If-Match": string;
1914
+ };
1915
+ path: {
1916
+ /** @description Share id */
1917
+ id: string;
1918
+ /** @description A `file_*` blob id listed in the share */
1919
+ blob: string;
1920
+ };
1921
+ cookie?: never;
1922
+ };
1923
+ /** @description Ciphertext, up to the server's upload limit */
1924
+ requestBody: {
1925
+ content: {
1926
+ "application/octet-stream": components["schemas"]["Binary"];
1927
+ };
1928
+ };
1929
+ responses: {
1930
+ 200: {
1931
+ headers: {
1932
+ /** @description ETag of the stored bytes */
1933
+ ETag?: string;
1934
+ [name: string]: unknown;
1935
+ };
1936
+ content: {
1937
+ "application/json": components["schemas"]["OkBody"];
1938
+ };
1939
+ };
1940
+ 401: {
1941
+ headers: {
1942
+ [name: string]: unknown;
1943
+ };
1944
+ content: {
1945
+ "application/json": components["schemas"]["ApiError"];
1946
+ };
1947
+ };
1948
+ /** @description Not an editor, or no longer a member */
1949
+ 403: {
1950
+ headers: {
1951
+ [name: string]: unknown;
1952
+ };
1953
+ content: {
1954
+ "application/json": components["schemas"]["ApiError"];
1955
+ };
1956
+ };
1957
+ 404: {
1958
+ headers: {
1959
+ [name: string]: unknown;
1960
+ };
1961
+ content: {
1962
+ "application/json": components["schemas"]["ApiError"];
1963
+ };
1964
+ };
1965
+ /** @description Someone saved a newer version */
1966
+ 412: {
1967
+ headers: {
1968
+ [name: string]: unknown;
1969
+ };
1970
+ content: {
1971
+ "application/json": components["schemas"]["ApiError"];
1972
+ };
1973
+ };
1974
+ /** @description Larger than the server's upload limit (`MAX_UPLOAD_MB`) */
1975
+ 413: {
1976
+ headers: {
1977
+ [name: string]: unknown;
1978
+ };
1979
+ content: {
1980
+ "application/json": components["schemas"]["ApiError"];
1981
+ };
1982
+ };
1983
+ /** @description `If-Match` is missing */
1984
+ 428: {
1985
+ headers: {
1986
+ [name: string]: unknown;
1987
+ };
1988
+ content: {
1989
+ "application/json": components["schemas"]["ApiError"];
1990
+ };
1991
+ };
1992
+ };
1993
+ };
1753
1994
  joinShare: {
1754
1995
  parameters: {
1755
1996
  query?: never;
@@ -1935,6 +2176,95 @@ export interface operations {
1935
2176
  };
1936
2177
  };
1937
2178
  };
2179
+ shareLive: {
2180
+ parameters: {
2181
+ query?: {
2182
+ /** @description Channel name (letters, digits, `-`, `_`; at most 64). Default `default` */
2183
+ channel?: string;
2184
+ };
2185
+ header?: never;
2186
+ path: {
2187
+ /** @description Share id */
2188
+ id: string;
2189
+ };
2190
+ cookie?: never;
2191
+ };
2192
+ requestBody?: never;
2193
+ responses: {
2194
+ /** @description Switches to a WebSocket. Send `LiveClientMessage`s (`auth` first); receive `LiveServerMessage`s */
2195
+ 101: {
2196
+ headers: {
2197
+ [name: string]: unknown;
2198
+ };
2199
+ content: {
2200
+ "application/json": components["schemas"]["LiveServerMessage"];
2201
+ };
2202
+ };
2203
+ /** @description Bad channel name, or not a WebSocket request */
2204
+ 400: {
2205
+ headers: {
2206
+ [name: string]: unknown;
2207
+ };
2208
+ content: {
2209
+ "application/json": components["schemas"]["ApiError"];
2210
+ };
2211
+ };
2212
+ /** @description Not supported on the WASI build */
2213
+ 501: {
2214
+ headers: {
2215
+ [name: string]: unknown;
2216
+ };
2217
+ content: {
2218
+ "application/json": components["schemas"]["ApiError"];
2219
+ };
2220
+ };
2221
+ };
2222
+ };
2223
+ setShareMemberRole: {
2224
+ parameters: {
2225
+ query?: never;
2226
+ header?: never;
2227
+ path: {
2228
+ /** @description Share id */
2229
+ id: string;
2230
+ /** @description Member id */
2231
+ member: string;
2232
+ };
2233
+ cookie?: never;
2234
+ };
2235
+ requestBody: {
2236
+ content: {
2237
+ "application/json": components["schemas"]["SetMemberRoleRequest"];
2238
+ };
2239
+ };
2240
+ responses: {
2241
+ 200: {
2242
+ headers: {
2243
+ [name: string]: unknown;
2244
+ };
2245
+ content: {
2246
+ "application/json": components["schemas"]["OkBody"];
2247
+ };
2248
+ };
2249
+ 401: {
2250
+ headers: {
2251
+ [name: string]: unknown;
2252
+ };
2253
+ content: {
2254
+ "application/json": components["schemas"]["ApiError"];
2255
+ };
2256
+ };
2257
+ /** @description No such share or member, or not the owner */
2258
+ 404: {
2259
+ headers: {
2260
+ [name: string]: unknown;
2261
+ };
2262
+ content: {
2263
+ "application/json": components["schemas"]["ApiError"];
2264
+ };
2265
+ };
2266
+ };
2267
+ };
1938
2268
  removeShareMember: {
1939
2269
  parameters: {
1940
2270
  query?: never;
@@ -2011,6 +2341,47 @@ export interface operations {
2011
2341
  };
2012
2342
  };
2013
2343
  };
2344
+ vaultLive: {
2345
+ parameters: {
2346
+ query?: {
2347
+ /** @description Channel name (letters, digits, `-`, `_`; at most 64). Default `default` */
2348
+ channel?: string;
2349
+ };
2350
+ header?: never;
2351
+ path?: never;
2352
+ cookie?: never;
2353
+ };
2354
+ requestBody?: never;
2355
+ responses: {
2356
+ /** @description Switches to a WebSocket. Send `LiveClientMessage`s (`auth` first); receive `LiveServerMessage`s */
2357
+ 101: {
2358
+ headers: {
2359
+ [name: string]: unknown;
2360
+ };
2361
+ content: {
2362
+ "application/json": components["schemas"]["LiveServerMessage"];
2363
+ };
2364
+ };
2365
+ /** @description Bad channel name, or not a WebSocket request */
2366
+ 400: {
2367
+ headers: {
2368
+ [name: string]: unknown;
2369
+ };
2370
+ content: {
2371
+ "application/json": components["schemas"]["ApiError"];
2372
+ };
2373
+ };
2374
+ /** @description Not supported on the WASI build */
2375
+ 501: {
2376
+ headers: {
2377
+ [name: string]: unknown;
2378
+ };
2379
+ content: {
2380
+ "application/json": components["schemas"]["ApiError"];
2381
+ };
2382
+ };
2383
+ };
2384
+ };
2014
2385
  rotateVault: {
2015
2386
  parameters: {
2016
2387
  query?: never;
package/dist/api.d.ts CHANGED
@@ -24,6 +24,8 @@ export declare function getBaseUrl(): string;
24
24
  export declare function apiError(what: string, response: Response, error?: {
25
25
  error?: string;
26
26
  }): Error;
27
+ /** The open vault session's token, or null. Live channels send it themselves. */
28
+ export declare function currentVaultToken(): string | null;
27
29
  export declare function vaultAuth(): Record<string, string>;
28
30
  /** Opens a session for the vault `proof` belongs to, creating it if new. */
29
31
  export declare function openVault(proof: Uint8Array, credentialToken?: string): Promise<string>;
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export * from "./api";
5
5
  export * from "./crypto";
6
6
  export * from "./tree";
7
7
  export * from "./shares";
8
+ export * from "./live";
8
9
  export * from "./utils";
9
10
  export type * from "./types";
10
11
  export type { components, paths } from "./api-schema";