@vite-hub/realtime 0.3.1 → 0.5.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 (36) hide show
  1. package/README.md +20 -14
  2. package/dist/agent.d.ts +1 -1
  3. package/dist/agent.js +1 -1
  4. package/dist/{bus-Bi-Ec414.js → bus-Bsdanf5O.js} +9 -18
  5. package/dist/bus.js +1 -1
  6. package/dist/client/browser.js +1 -1
  7. package/dist/client.d.ts +2 -2
  8. package/dist/client.js +14 -7
  9. package/dist/{conflict-Bg8vCisd.js → conflict-DYC_qXzM.js} +7 -7
  10. package/dist/{document-DjBrcgpY.js → document-U5rzg2vG.js} +1 -1
  11. package/dist/{document-DPaywuCO.d.ts → document-b1ShR2S1.d.ts} +2 -2
  12. package/dist/durability.js +0 -4
  13. package/dist/{editor-BM5-vHqf.js → editor-IL2jHqn8.js} +0 -1
  14. package/dist/{error-diagnostics-B_66irmt.js → error-diagnostics-Cd-XWNf7.js} +21 -4
  15. package/dist/index.d.ts +4 -4
  16. package/dist/index.js +2 -2
  17. package/dist/internal/runtime/unavailable.d.ts +1 -1
  18. package/dist/internal/server.d.ts +7 -0
  19. package/dist/internal/server.js +6 -0
  20. package/dist/{keepalive-BCs-uXp4.js → keepalive-B4ONG6AD.js} +1 -1
  21. package/dist/{layout-fyQnhwCg.d.ts → layout-VHjYWa-M.d.ts} +1 -1
  22. package/dist/log-E-cH6xkd.js +159 -0
  23. package/dist/{object-CzKyK0hX.js → object-BgPPDLDE.js} +1 -1
  24. package/dist/{presence-Tuy1y_l9.js → presence-B6rvkHqd.js} +2 -2
  25. package/dist/{request-BUwa359H.d.ts → request-DAodIqZ8.d.ts} +1 -1
  26. package/dist/{request-CWaHqW3R.js → request-kM5epeSd.js} +2 -3
  27. package/dist/server-BSXJP45b.js +2606 -0
  28. package/dist/server-Covbc24E.d.ts +140 -0
  29. package/dist/server.d.ts +3 -141
  30. package/dist/server.js +3 -2581
  31. package/dist/{types-1DVVG5ro.d.ts → types-DdY7Vo1H.d.ts} +30 -41
  32. package/dist/vite.d.ts +3 -1
  33. package/dist/vite.js +67 -54
  34. package/dist/vue.d.ts +7 -9
  35. package/dist/vue.js +35 -38
  36. package/package.json +11 -6
@@ -0,0 +1,140 @@
1
+ import { R as RealtimeWorkspaceDocumentOptions, c as RealtimeDocumentContext, f as RealtimeEditor, l as RealtimeDocumentDefinition, s as RealtimeDocumentAdapter, w as RealtimeRegistry, x as RealtimePerson } from "./types-DdY7Vo1H.js";
2
+ import { n as RealtimeDurableObjectNamespace } from "./request-DAodIqZ8.js";
3
+ import { t as RealtimeBus } from "./bus-protocol-wXkXW28q.js";
4
+ import "./conflict-CmZizTYl.js";
5
+ import "./document-b1ShR2S1.js";
6
+ import * as awarenessProtocol from "y-protocols/awareness";
7
+ import * as Y from "yjs";
8
+ import { Diagnostic } from "nostics";
9
+ import { ReadonlyWorkspaceFiles, WritableWorkspaceFiles } from "@vite-hub/workspace/files";
10
+ //#region src/availability.d.ts
11
+ /**
12
+ * Returns `true` when this build has a Realtime room driver: Durable Objects on Cloudflare, a Node server, or memory
13
+ * on the dev server. Without one, no room opens and `refreshRealtimeDocument()` resolves `false`.
14
+ */
15
+ declare function isRealtimeAvailable(): boolean;
16
+ //#endregion
17
+ //#region src/blob-document.d.ts
18
+ /**
19
+ * The part of a ViteHub Blob store that `blobDocument()` uses, such as `blob` from `vite-hub/blob`. `put()` writes only
20
+ * when its `ifMatch` or `overwrite: false` condition holds, and otherwise returns a `BLOB_CONFLICT` error.
21
+ */
22
+ interface RealtimeBlobStore {
23
+ get(pathname: string): Promise<[error: unknown, value: Blob | null | undefined]>;
24
+ head(pathname: string): Promise<[error: unknown, value: {
25
+ httpEtag: string | undefined;
26
+ } | null | undefined]>;
27
+ put(pathname: string, body: string, options: {
28
+ contentType: string;
29
+ ifMatch: string;
30
+ } | {
31
+ contentType: string;
32
+ overwrite: false;
33
+ }): Promise<[error: unknown, value: unknown]>;
34
+ }
35
+ interface RealtimeBlobDocumentOptions {
36
+ /** Defaults to `text/markdown; charset=utf-8`. */
37
+ contentType?: string;
38
+ /** Prepended to the document id to make the Blob pathname. Defaults to none. */
39
+ prefix?: string;
40
+ }
41
+ /**
42
+ * Stores each Realtime document as one Blob file at `prefix + documentId`. Its digest is the SHA-256 of the
43
+ * body. `save()` reads the stored file and throws `RealtimeDocumentConflict` when its digest is not `ifDigest`. It
44
+ * writes only if the file still has the ETag that it read, so a writer outside the room that writes between that read
45
+ * and the save's write also makes the save throw `RealtimeDocumentConflict`, and keeps its file.
46
+ */
47
+ declare function blobDocument(store: RealtimeBlobStore, options?: RealtimeBlobDocumentOptions): RealtimeDocumentAdapter;
48
+ //#endregion
49
+ //#region src/presence.d.ts
50
+ type RealtimeIdentity = Omit<RealtimePerson, "clientId">;
51
+ //#endregion
52
+ //#region src/awareness.d.ts
53
+ /**
54
+ * Records that `peer` owns `clients`. A client that another peer owns is a conflict, unless `transferable` accepts its
55
+ * owner, such as another server instance that the client left.
56
+ */
57
+ declare function claimAwarenessClientIds<Peer extends object>(owners: Map<number, Peer>, peer: Peer, clients: number[], transferable?: (owner: Peer) => boolean): number[];
58
+ declare function bindAwarenessIdentity(update: Uint8Array, identity: RealtimeIdentity): Uint8Array;
59
+ declare function applyRealtimeAwarenessUpdate(awareness: awarenessProtocol.Awareness, update: Uint8Array, origin: unknown, maxStateBytes?: number): void;
60
+ declare function compactRealtimeAwareness(awareness: awarenessProtocol.Awareness): awarenessProtocol.Awareness;
61
+ //#endregion
62
+ //#region src/transport.d.ts
63
+ declare function readRealtimeRequestState(request: Request, maxBytes?: number): Promise<Uint8Array>;
64
+ //#endregion
65
+ //#region src/workspace-document.d.ts
66
+ interface RealtimeWorkspaceDocumentAdapter extends RealtimeDocumentAdapter {
67
+ kind: "workspace";
68
+ workspace: string;
69
+ }
70
+ declare function readRealtimeWorkspaceDocument(readable: ReadonlyWorkspaceFiles, writable: WritableWorkspaceFiles, documentId: string): Promise<{
71
+ baselineDigest: string | undefined;
72
+ markdown: string;
73
+ }>;
74
+ /**
75
+ * Stores each Realtime document as the Markdown file at its path in a Workspace working tree.
76
+ * `history.checkpoint()` commits the working tree to Workspace history with the Store's author by default.
77
+ * `document: { workspace }` in a Realtime Definition uses this adapter.
78
+ */
79
+ declare function workspaceDocument(workspaceName: string, checkpointAuthor?: RealtimeWorkspaceDocumentOptions["checkpointAuthor"]): RealtimeWorkspaceDocumentAdapter;
80
+ declare function isRealtimeWorkspaceDocument(adapter: RealtimeDocumentAdapter): adapter is RealtimeWorkspaceDocumentAdapter;
81
+ //#endregion
82
+ //#region src/internal/server.d.ts
83
+ interface RealtimeHandler {
84
+ (event: unknown): Promise<unknown>;
85
+ fetch(input: Request | URL | string): Promise<Response>;
86
+ }
87
+ interface RealtimeHandlerOptions {
88
+ /**
89
+ * Shares the rooms of this server with other instances. ViteHub passes `useRealtimeBus()` from
90
+ * `@vite-hub/realtime/bus` in Node builds. It runs once, when the first room opens. `undefined` keeps the rooms in
91
+ * this process.
92
+ */
93
+ bus?: () => Promise<RealtimeBus | undefined> | RealtimeBus | undefined;
94
+ }
95
+ declare const realtimePersistenceDefaults: Readonly<{
96
+ debounce: 2000;
97
+ maxWait: 10000;
98
+ }>;
99
+ declare function encodeSyncUpdate(update: Uint8Array): Uint8Array;
100
+ declare function encodeSyncStep1(document: Y.Doc): Uint8Array;
101
+ declare function encodeAwarenessState(awareness: awarenessProtocol.Awareness, clients: number[]): Uint8Array;
102
+ interface RealtimeSyncOptions {
103
+ maxStateBytes?: number;
104
+ /** Reject any update that changes the document. A string replaces the rejection message. */
105
+ readOnly?: boolean | string;
106
+ /** Reject updates that change the server-written `status` map. Default `true`. */
107
+ protectStatus?: boolean;
108
+ }
109
+ declare function applyRealtimeSyncMessage(data: Uint8Array, document: Y.Doc, origin: unknown, options?: RealtimeSyncOptions): Uint8Array | undefined;
110
+ declare function assertRealtimeRoomStateQuota(document: Y.Doc, maxStateBytes?: number): void;
111
+ declare function realtimeRoomKey(definitionName: string, documentId: string, scope?: string): string;
112
+ declare function restoreRealtimeDocument(markdown: string, baselineDigest: string | undefined, stored: Record<string, unknown> | undefined): Promise<Y.Doc>;
113
+ interface ResolvedRealtimeDefinition {
114
+ adapter: RealtimeDocumentAdapter;
115
+ definition: RealtimeDocumentDefinition;
116
+ editor: (context: RealtimeDocumentContext) => RealtimeEditor;
117
+ persistence: {
118
+ debounce: number;
119
+ maxWait: number;
120
+ } | false;
121
+ }
122
+ /** Resolves the document adapter, editor codec, and persistence timing of a definition. */
123
+ declare function resolveRealtimeDefinition(definition: RealtimeDocumentDefinition): ResolvedRealtimeDefinition;
124
+ interface RealtimeRefreshOptions {
125
+ /**
126
+ * On Cloudflare, the `$DurableObject` binding that holds the rooms. Defaults to the binding of the running Worker.
127
+ * Without one, the rooms of this process refresh.
128
+ */
129
+ durableObjects?: RealtimeDurableObjectNamespace;
130
+ }
131
+ /**
132
+ * Tells the open room of a document that its stored copy changed outside the room, such as a save by an Agent or the
133
+ * CLI. A room without unsaved edits re-seeds at once and resets its clients. A room with unsaved edits finds the change
134
+ * at its next save, which also re-seeds it. A room that is not open loads the stored copy when it opens. Resolves
135
+ * whether a room re-seeded. It never throws: a failed refresh leaves the next save to find the change.
136
+ */
137
+ declare function refreshRealtimeDocument(definition: string, documentId: string, options?: RealtimeRefreshOptions): Promise<boolean>;
138
+ declare function createRealtimeHandler(registry: RealtimeRegistry, options?: RealtimeHandlerOptions): RealtimeHandler;
139
+ //#endregion
140
+ export { claimAwarenessClientIds as C, blobDocument as D, RealtimeBlobStore as E, isRealtimeAvailable as O, bindAwarenessIdentity as S, RealtimeBlobDocumentOptions as T, isRealtimeWorkspaceDocument as _, applyRealtimeSyncMessage as a, readRealtimeRequestState as b, encodeAwarenessState as c, realtimePersistenceDefaults as d, realtimeRoomKey as f, RealtimeWorkspaceDocumentAdapter as g, restoreRealtimeDocument as h, RealtimeSyncOptions as i, encodeSyncStep1 as l, resolveRealtimeDefinition as m, RealtimeHandlerOptions as n, assertRealtimeRoomStateQuota as o, refreshRealtimeDocument as p, RealtimeRefreshOptions as r, createRealtimeHandler as s, RealtimeHandler as t, encodeSyncUpdate as u, readRealtimeWorkspaceDocument as v, compactRealtimeAwareness as w, applyRealtimeAwarenessUpdate as x, workspaceDocument as y };
package/dist/server.d.ts CHANGED
@@ -1,142 +1,4 @@
1
- import { c as RealtimeDocumentContext, f as RealtimeEditor, l as RealtimeDocumentDefinition, s as RealtimeDocumentAdapter, w as RealtimeRegistry, x as RealtimePerson } from "./types-1DVVG5ro.js";
2
- import { i as RealtimeRoomRequestResult, n as RealtimeDurableObjectNamespace, o as requestRealtimeRoom, r as RealtimeRoomRequestOptions } from "./request-BUwa359H.js";
1
+ import { i as RealtimeRoomRequestResult, n as RealtimeDurableObjectNamespace, o as requestRealtimeRoom, r as RealtimeRoomRequestOptions } from "./request-DAodIqZ8.js";
3
2
  import { n as RealtimeBusMessage, r as RealtimeRoomRecord, t as RealtimeBus } from "./bus-protocol-wXkXW28q.js";
4
- import { n as isRealtimeDocumentConflict, t as RealtimeDocumentConflict } from "./conflict-CmZizTYl.js";
5
- import { n as realtimeDocumentLayout, t as readRealtimeStatus } from "./layout-fyQnhwCg.js";
6
- import { i as readRealtimeYDoc, n as createRealtimeYDoc, r as diffRealtimeYDoc, t as containsRealtimeState } from "./document-DPaywuCO.js";
7
- import * as awarenessProtocol from "y-protocols/awareness";
8
- import * as Y from "yjs";
9
- import { Diagnostic } from "nostics";
10
- import { ReadonlyWorkspaceFiles, WritableWorkspaceFiles } from "@vite-hub/workspace/files";
11
- //#region src/availability.d.ts
12
- /**
13
- * Returns `true` when this build has a Realtime room driver: Durable Objects on Cloudflare, a Node server, or memory
14
- * on the dev server. Without one, no room opens and `refreshRealtimeDocument()` resolves `false`.
15
- */
16
- export declare function isRealtimeAvailable(): boolean;
17
- //#endregion
18
- //#region src/blob-document.d.ts
19
- /**
20
- * The part of a ViteHub Blob store that `blobDocument()` uses, such as `blob` from `vite-hub/blob`. `put()` writes only
21
- * when its `ifMatch` or `overwrite: false` condition holds, and otherwise returns a `BLOB_CONFLICT` error.
22
- */
23
- interface RealtimeBlobStore {
24
- get(pathname: string): Promise<[error: unknown, value: Blob | null | undefined]>;
25
- head(pathname: string): Promise<[error: unknown, value: {
26
- httpEtag: string | undefined;
27
- } | null | undefined]>;
28
- put(pathname: string, body: string, options: {
29
- contentType: string;
30
- ifMatch: string;
31
- } | {
32
- contentType: string;
33
- overwrite: false;
34
- }): Promise<[error: unknown, value: unknown]>;
35
- }
36
- interface RealtimeBlobDocumentOptions {
37
- /** Defaults to `text/markdown; charset=utf-8`. */
38
- contentType?: string;
39
- /** Prepended to the document id to make the Blob pathname. Defaults to none. */
40
- prefix?: string;
41
- }
42
- /**
43
- * Stores each Realtime document as one Blob file at `prefix + documentId`. Its digest is the SHA-256 of the
44
- * body. `save()` reads the stored file and throws `RealtimeDocumentConflict` when its digest is not `ifDigest`. It
45
- * writes only if the file still has the ETag that it read, so a writer outside the room that writes between that read
46
- * and the save's write also makes the save throw `RealtimeDocumentConflict`, and keeps its file.
47
- */
48
- export declare function blobDocument(store: RealtimeBlobStore, options?: RealtimeBlobDocumentOptions): RealtimeDocumentAdapter;
49
- //#endregion
50
- //#region src/presence.d.ts
51
- type RealtimeIdentity = Omit<RealtimePerson, "clientId">;
52
- //#endregion
53
- //#region src/awareness.d.ts
54
- /**
55
- * Records that `peer` owns `clients`. A client that another peer owns is a conflict, unless `transferable` accepts its
56
- * owner, such as another server instance that the client left.
57
- */
58
- export declare function claimAwarenessClientIds<Peer extends object>(owners: Map<number, Peer>, peer: Peer, clients: number[], transferable?: (owner: Peer) => boolean): number[];
59
- export declare function bindAwarenessIdentity(update: Uint8Array, identity: RealtimeIdentity): Uint8Array;
60
- export declare function applyRealtimeAwarenessUpdate(awareness: awarenessProtocol.Awareness, update: Uint8Array, origin: unknown, maxStateBytes?: number): void;
61
- export declare function compactRealtimeAwareness(awareness: awarenessProtocol.Awareness): awarenessProtocol.Awareness;
62
- //#endregion
63
- //#region src/transport.d.ts
64
- export declare function readRealtimeRequestState(request: Request, maxBytes?: number): Promise<Uint8Array>;
65
- //#endregion
66
- //#region src/workspace-document.d.ts
67
- interface RealtimeWorkspaceDocumentAdapter extends RealtimeDocumentAdapter {
68
- kind: "workspace";
69
- workspace: string;
70
- }
71
- export declare function readRealtimeWorkspaceDocument(readable: ReadonlyWorkspaceFiles, writable: WritableWorkspaceFiles, documentId: string): Promise<{
72
- baselineDigest: string | undefined;
73
- markdown: string;
74
- }>;
75
- /**
76
- * Stores each Realtime document as the Markdown file at its path in a Workspace working tree.
77
- * `history.checkpoint()` commits the working tree to Workspace history. `document: { workspace }`
78
- * in a Realtime Definition uses this adapter.
79
- */
80
- export declare function workspaceDocument(workspaceName: string): RealtimeWorkspaceDocumentAdapter;
81
- export declare function isRealtimeWorkspaceDocument(adapter: RealtimeDocumentAdapter): adapter is RealtimeWorkspaceDocumentAdapter;
82
- //#endregion
83
- //#region src/server.d.ts
84
- export interface RealtimeHandler {
85
- (event: unknown): Promise<unknown>;
86
- fetch(input: Request | URL | string): Promise<Response>;
87
- }
88
- export interface RealtimeHandlerOptions {
89
- /**
90
- * Shares the rooms of this server with other instances. ViteHub passes `useRealtimeBus()` from
91
- * `@vite-hub/realtime/bus` in Node builds. It runs once, when the first room opens. `undefined` keeps the rooms in
92
- * this process.
93
- */
94
- bus?: () => Promise<RealtimeBus | undefined> | RealtimeBus | undefined;
95
- }
96
- export declare const realtimePersistenceDefaults: Readonly<{
97
- debounce: 2000;
98
- maxWait: 10000;
99
- }>;
100
- export declare function encodeSyncUpdate(update: Uint8Array): Uint8Array;
101
- export declare function encodeSyncStep1(document: Y.Doc): Uint8Array;
102
- export declare function encodeAwarenessState(awareness: awarenessProtocol.Awareness, clients: number[]): Uint8Array;
103
- export interface RealtimeSyncOptions {
104
- maxStateBytes?: number;
105
- /** Reject any update that changes the document. A string replaces the rejection message. */
106
- readOnly?: boolean | string;
107
- /** Reject updates that change the server-written `status` map. Default `true`. */
108
- protectStatus?: boolean;
109
- }
110
- export declare function applyRealtimeSyncMessage(data: Uint8Array, document: Y.Doc, origin: unknown, options?: RealtimeSyncOptions): Uint8Array | undefined;
111
- export declare function assertRealtimeRoomStateQuota(document: Y.Doc, maxStateBytes?: number): void;
112
- export declare function realtimeRoomKey(definitionName: string, documentId: string, scope?: string): string;
113
- export declare function restoreRealtimeDocument(markdown: string, baselineDigest: string | undefined, stored: Record<string, unknown> | undefined): Promise<Y.Doc>;
114
- interface ResolvedRealtimeDefinition {
115
- adapter: RealtimeDocumentAdapter;
116
- definition: RealtimeDocumentDefinition;
117
- /** The codec of one document: the definition's editor, or the one its editor function picks. */
118
- editor: (context: RealtimeDocumentContext) => RealtimeEditor;
119
- persistence: {
120
- debounce: number;
121
- maxWait: number;
122
- } | false;
123
- }
124
- /** Resolves the document adapter, editor codec, and persistence timing of a definition. */
125
- export declare function resolveRealtimeDefinition(definition: RealtimeDocumentDefinition): ResolvedRealtimeDefinition;
126
- export interface RealtimeRefreshOptions {
127
- /**
128
- * On Cloudflare, the `$DurableObject` binding that holds the rooms. Defaults to the binding of the running Worker.
129
- * Without one, the rooms of this process refresh.
130
- */
131
- durableObjects?: RealtimeDurableObjectNamespace;
132
- }
133
- /**
134
- * Tells the open room of a document that its stored copy changed outside the room, such as a save by an Agent or the
135
- * CLI. A room without unsaved edits re-seeds at once and resets its clients. A room with unsaved edits finds the change
136
- * at its next save, which also re-seeds it. A room that is not open loads the stored copy when it opens. Resolves
137
- * whether a room re-seeded. It never throws: a failed refresh leaves the next save to find the change.
138
- */
139
- export declare function refreshRealtimeDocument(definition: string, documentId: string, options?: RealtimeRefreshOptions): Promise<boolean>;
140
- export declare function createRealtimeHandler(registry: RealtimeRegistry, options?: RealtimeHandlerOptions): RealtimeHandler;
141
- //#endregion
142
- export { type RealtimeBlobDocumentOptions, type RealtimeBlobStore, type RealtimeBus, type RealtimeBusMessage, RealtimeDocumentConflict, type RealtimeDurableObjectNamespace, type RealtimeRoomRecord, type RealtimeRoomRequestOptions, type RealtimeRoomRequestResult, type RealtimeWorkspaceDocumentAdapter, containsRealtimeState, createRealtimeYDoc, diffRealtimeYDoc, isRealtimeDocumentConflict, readRealtimeStatus, readRealtimeYDoc, realtimeDocumentLayout, requestRealtimeRoom };
3
+ import { D as blobDocument, E as RealtimeBlobStore, O as isRealtimeAvailable, T as RealtimeBlobDocumentOptions, g as RealtimeWorkspaceDocumentAdapter, n as RealtimeHandlerOptions, p as refreshRealtimeDocument, r as RealtimeRefreshOptions, s as createRealtimeHandler, t as RealtimeHandler, y as workspaceDocument } from "./server-Covbc24E.js";
4
+ export { type RealtimeBlobDocumentOptions, type RealtimeBlobStore, type RealtimeBus, type RealtimeBusMessage, type RealtimeDurableObjectNamespace, type RealtimeHandler, type RealtimeHandlerOptions, type RealtimeRefreshOptions, type RealtimeRoomRecord, type RealtimeRoomRequestOptions, type RealtimeRoomRequestResult, type RealtimeWorkspaceDocumentAdapter, blobDocument, createRealtimeHandler, isRealtimeAvailable, refreshRealtimeDocument, requestRealtimeRoom, workspaceDocument };