@vite-hub/realtime 0.0.4 → 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 +94 -21
- package/dist/agent.d.ts +2 -0
- package/dist/agent.js +2 -0
- package/dist/bus-Bi-Ec414.js +476 -0
- package/dist/bus-protocol-wXkXW28q.d.ts +66 -0
- package/dist/bus.d.ts +85 -0
- package/dist/bus.js +2 -0
- package/dist/client/browser.js +53 -0
- package/dist/client.d.ts +67 -0
- package/dist/client.js +200 -0
- package/dist/conflict-Bg8vCisd.js +63 -0
- package/dist/conflict-CmZizTYl.d.ts +17 -0
- package/dist/document-DPaywuCO.d.ts +16 -0
- package/dist/document-DjBrcgpY.js +2041 -0
- package/dist/durability.d.ts +10 -0
- package/dist/durability.js +46 -0
- package/dist/editor-BM5-vHqf.js +147 -0
- package/dist/error-diagnostics-B_66irmt.js +141 -0
- package/dist/index.d.ts +22 -4
- package/dist/index.js +4 -6
- package/dist/internal/runtime/unavailable.d.ts +5 -0
- package/dist/internal/runtime/unavailable.js +8 -0
- package/dist/keepalive-BCs-uXp4.js +33 -0
- package/dist/layout-DNaxppTZ.js +34 -0
- package/dist/layout-fyQnhwCg.d.ts +18 -0
- package/dist/object-CzKyK0hX.js +145 -0
- package/dist/presence-Tuy1y_l9.js +53 -0
- package/dist/request-BUwa359H.d.ts +48 -0
- package/dist/request-CWaHqW3R.js +192 -0
- package/dist/server.d.ts +132 -27
- package/dist/server.js +2228 -506
- package/dist/types-1DVVG5ro.d.ts +319 -0
- package/dist/vite.d.ts +177 -6
- package/dist/vite.js +930 -55
- package/dist/vue.d.ts +69 -3807
- package/dist/vue.js +353 -65
- package/package.json +117 -36
- package/dist/editor-extensions-CZEzrJ-t.js +0 -4860
- package/dist/types-BSDtKw5d.d.ts +0 -42
package/dist/client.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { n as resolveRealtimeApplicationPath, t as keepRealtimeSocketAlive } from "./keepalive-BCs-uXp4.js";
|
|
2
|
+
import { a as decodeRealtimeRequestFramePayload, c as encodeRealtimeRequestFrame, i as decodeRealtimeControlPayload, l as encodeWorkspaceChange, o as decodeWorkspaceChangePayload, x as workspaceRoomId } from "./object-CzKyK0hX.js";
|
|
3
|
+
import { n as realtimeDocumentLayout, t as readRealtimeStatus } from "./layout-DNaxppTZ.js";
|
|
4
|
+
import { n as getRealtimePeople, t as createRealtimeIdentity } from "./presence-Tuy1y_l9.js";
|
|
5
|
+
import "lib0/decoding";
|
|
6
|
+
import * as encoding from "lib0/encoding";
|
|
7
|
+
import * as awarenessProtocol from "y-protocols/awareness";
|
|
8
|
+
import { WebsocketProvider } from "y-websocket";
|
|
9
|
+
import * as Y from "yjs";
|
|
10
|
+
//#region src/client.ts
|
|
11
|
+
const maxAnswerBytes = 1047552;
|
|
12
|
+
/**
|
|
13
|
+
* Joins one room of a Realtime Definition for presence: who is there and what each person shares through awareness,
|
|
14
|
+
* such as a cursor. It also reads the room's save status, such as the digest of the stored document. It does not edit
|
|
15
|
+
* the room document and loads no editor. Use it outside Vue, or when a page needs presence or the save status without
|
|
16
|
+
* an editor; `useRealtimeTiptap()` from `@vite-hub/realtime/vue` is the editor client.
|
|
17
|
+
*/
|
|
18
|
+
function connectRealtimeRoom(definition, documentId, options = {}) {
|
|
19
|
+
const updateListeners = /* @__PURE__ */ new Set();
|
|
20
|
+
const changeListeners = /* @__PURE__ */ new Set();
|
|
21
|
+
const pendingChanges = [];
|
|
22
|
+
let requestHandler;
|
|
23
|
+
let currentStatus = "connecting";
|
|
24
|
+
let synced = false;
|
|
25
|
+
let destroyed = false;
|
|
26
|
+
let document;
|
|
27
|
+
let provider;
|
|
28
|
+
const emit = () => {
|
|
29
|
+
for (const listener of updateListeners) listener();
|
|
30
|
+
};
|
|
31
|
+
const url = () => {
|
|
32
|
+
const app = options.origin ? new URL(options.origin) : window.location;
|
|
33
|
+
return `${app.protocol === "https:" ? "wss:" : "ws:"}//${app.host}${resolveRealtimeApplicationPath(`/api/_vitehub/realtime/${encodeURIComponent(definition)}`, options.baseURL)}`;
|
|
34
|
+
};
|
|
35
|
+
const room = documentId.split("/").map(encodeURIComponent).join("/");
|
|
36
|
+
const localState = {};
|
|
37
|
+
function connect() {
|
|
38
|
+
synced = false;
|
|
39
|
+
document = new Y.Doc();
|
|
40
|
+
provider = new WebsocketProvider(url(), room, document, {
|
|
41
|
+
disableBc: true,
|
|
42
|
+
params: {
|
|
43
|
+
...options.params,
|
|
44
|
+
clientId: String(document.clientID)
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
const current = provider;
|
|
48
|
+
keepRealtimeSocketAlive(current);
|
|
49
|
+
current.messageHandlers[5] = (_encoder, decoder) => {
|
|
50
|
+
const control = decodeRealtimeControlPayload(decoder);
|
|
51
|
+
if (!control) return;
|
|
52
|
+
if (control.type === "session") {
|
|
53
|
+
current.params.epoch = control.epoch;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
current.destroy();
|
|
57
|
+
document.destroy();
|
|
58
|
+
if (destroyed) return;
|
|
59
|
+
connect();
|
|
60
|
+
emit();
|
|
61
|
+
};
|
|
62
|
+
current.messageHandlers[6] = (_encoder, decoder) => {
|
|
63
|
+
const frame = decodeRealtimeRequestFramePayload(decoder);
|
|
64
|
+
if (frame?.kind === "request") answer(current, frame.id, frame.body);
|
|
65
|
+
};
|
|
66
|
+
current.awareness.off("update", current._awarenessUpdateHandler);
|
|
67
|
+
current.awareness.on("update", ({ added, removed, updated }) => {
|
|
68
|
+
const own = [
|
|
69
|
+
...added,
|
|
70
|
+
...updated,
|
|
71
|
+
...removed
|
|
72
|
+
].filter((client) => client === current.doc.clientID);
|
|
73
|
+
const socket = current.ws;
|
|
74
|
+
if (!own.length || !current.wsconnected || !socket || socket.readyState !== socket.OPEN) return;
|
|
75
|
+
const encoder = encoding.createEncoder();
|
|
76
|
+
encoding.writeVarUint(encoder, 1);
|
|
77
|
+
encoding.writeVarUint8Array(encoder, awarenessProtocol.encodeAwarenessUpdate(current.awareness, own));
|
|
78
|
+
socket.send(encoding.toUint8Array(encoder));
|
|
79
|
+
});
|
|
80
|
+
const identity = options.identity ?? { id: `guest:${document.clientID}` };
|
|
81
|
+
current.awareness.setLocalState({
|
|
82
|
+
...localState,
|
|
83
|
+
user: createRealtimeIdentity({
|
|
84
|
+
...identity,
|
|
85
|
+
id: identity.id || `guest:${document.clientID}`
|
|
86
|
+
})
|
|
87
|
+
});
|
|
88
|
+
current.awareness.on("change", emit);
|
|
89
|
+
document.getMap(realtimeDocumentLayout.status).observe(emit);
|
|
90
|
+
current.on("sync", (value) => {
|
|
91
|
+
if (provider !== current) return;
|
|
92
|
+
synced = value;
|
|
93
|
+
emit();
|
|
94
|
+
});
|
|
95
|
+
current.on("status", (event) => {
|
|
96
|
+
currentStatus = event.status;
|
|
97
|
+
emit();
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
connect();
|
|
101
|
+
let changes;
|
|
102
|
+
if (options.changes) {
|
|
103
|
+
changes = new WebsocketProvider(url(), encodeURIComponent(workspaceRoomId), new Y.Doc(), {
|
|
104
|
+
disableBc: true,
|
|
105
|
+
params: {
|
|
106
|
+
...options.params,
|
|
107
|
+
workspace: "events"
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
changes.awareness.setLocalState(null);
|
|
111
|
+
keepRealtimeSocketAlive(changes);
|
|
112
|
+
changes.messageHandlers[4] = (_encoder, decoder) => {
|
|
113
|
+
const change = decodeWorkspaceChangePayload(decoder);
|
|
114
|
+
if (!change) return;
|
|
115
|
+
for (const listener of changeListeners) listener(change);
|
|
116
|
+
};
|
|
117
|
+
changes.on("status", (event) => {
|
|
118
|
+
if (event.status === "connected") flush();
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
async function answer(current, id, body) {
|
|
122
|
+
const refusal = (why) => encodeRealtimeRequestFrame({
|
|
123
|
+
kind: "answer",
|
|
124
|
+
id,
|
|
125
|
+
ok: false,
|
|
126
|
+
why
|
|
127
|
+
});
|
|
128
|
+
let frame;
|
|
129
|
+
try {
|
|
130
|
+
frame = requestHandler ? encodeRealtimeRequestFrame({
|
|
131
|
+
kind: "answer",
|
|
132
|
+
id,
|
|
133
|
+
ok: true,
|
|
134
|
+
body: await requestHandler(body)
|
|
135
|
+
}) : refusal("This page does not answer requests.");
|
|
136
|
+
if (frame.byteLength > maxAnswerBytes) frame = refusal(`The answer has ${frame.byteLength} bytes, over the 1 MiB limit.`);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
frame = refusal(error instanceof Error ? error.message : String(error));
|
|
139
|
+
}
|
|
140
|
+
const socket = current.ws;
|
|
141
|
+
if (current.wsconnected && socket && socket.readyState === socket.OPEN) socket.send(Uint8Array.from(frame));
|
|
142
|
+
}
|
|
143
|
+
function flush() {
|
|
144
|
+
const socket = changes?.ws;
|
|
145
|
+
if (!changes?.wsconnected || !socket || socket.readyState !== socket.OPEN) return;
|
|
146
|
+
while (pendingChanges.length) socket.send(Uint8Array.from(encodeWorkspaceChange(pendingChanges.shift())));
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
get awareness() {
|
|
150
|
+
return provider.awareness;
|
|
151
|
+
},
|
|
152
|
+
get clientId() {
|
|
153
|
+
return document.clientID;
|
|
154
|
+
},
|
|
155
|
+
people: () => getRealtimePeople(provider.awareness.getStates()),
|
|
156
|
+
states: () => {
|
|
157
|
+
const states = new Map(provider.awareness.getStates());
|
|
158
|
+
states.delete(document.clientID);
|
|
159
|
+
return states;
|
|
160
|
+
},
|
|
161
|
+
status: () => currentStatus,
|
|
162
|
+
saveStatus: () => synced ? readRealtimeStatus(document) : void 0,
|
|
163
|
+
setLocal(field, value) {
|
|
164
|
+
if (value === null) delete localState[field];
|
|
165
|
+
else localState[field] = value;
|
|
166
|
+
provider.awareness.setLocalStateField(field, value);
|
|
167
|
+
},
|
|
168
|
+
notify(change) {
|
|
169
|
+
if (!changes) return;
|
|
170
|
+
pendingChanges.push(change);
|
|
171
|
+
flush();
|
|
172
|
+
},
|
|
173
|
+
onUpdate(listener) {
|
|
174
|
+
updateListeners.add(listener);
|
|
175
|
+
return () => updateListeners.delete(listener);
|
|
176
|
+
},
|
|
177
|
+
onChange(listener) {
|
|
178
|
+
changeListeners.add(listener);
|
|
179
|
+
return () => changeListeners.delete(listener);
|
|
180
|
+
},
|
|
181
|
+
onRequest(handler) {
|
|
182
|
+
requestHandler = handler;
|
|
183
|
+
return () => {
|
|
184
|
+
if (requestHandler === handler) requestHandler = void 0;
|
|
185
|
+
};
|
|
186
|
+
},
|
|
187
|
+
destroy() {
|
|
188
|
+
destroyed = true;
|
|
189
|
+
provider.destroy();
|
|
190
|
+
document.destroy();
|
|
191
|
+
changes?.destroy();
|
|
192
|
+
changes?.doc.destroy();
|
|
193
|
+
updateListeners.clear();
|
|
194
|
+
changeListeners.clear();
|
|
195
|
+
requestHandler = void 0;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
//#endregion
|
|
200
|
+
export { connectRealtimeRoom };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { t as realtimeErrorDiagnostics } from "./error-diagnostics-B_66irmt.js";
|
|
2
|
+
import { Diagnostic } from "nostics";
|
|
3
|
+
//#region src/definition.ts
|
|
4
|
+
/**
|
|
5
|
+
* Checks a definition when the server loads it. A definition must say who can use its rooms: `auth: true`,
|
|
6
|
+
* `authorize()`, or `access: "public"`. `history.checkpoint` needs a document that can record history: a Workspace
|
|
7
|
+
* document, or an adapter with `checkpoint()`.
|
|
8
|
+
*/
|
|
9
|
+
function defineRealtime(definition) {
|
|
10
|
+
assertRealtimeAccess(definition);
|
|
11
|
+
assertRealtimeCheckpoint(definition);
|
|
12
|
+
return definition;
|
|
13
|
+
}
|
|
14
|
+
/** Rejects a definition that reaches the server without an access rule, such as a plain object in a JavaScript file. */
|
|
15
|
+
function assertRealtimeAccess(definition) {
|
|
16
|
+
const access = definition.access;
|
|
17
|
+
const authorize = definition.authorize;
|
|
18
|
+
const auth = definition.auth === true;
|
|
19
|
+
const hasAuthorize = typeof authorize === "function";
|
|
20
|
+
if (access !== void 0) {
|
|
21
|
+
if (access !== "public") throw realtimeErrorDiagnostics.REALTIME_INVALID_ACCESS({ reason: "not-public" });
|
|
22
|
+
if (definition.auth !== void 0 || authorize !== void 0) throw realtimeErrorDiagnostics.REALTIME_INVALID_ACCESS({ reason: "public-with-rule" });
|
|
23
|
+
} else if (!auth && !hasAuthorize) throw realtimeErrorDiagnostics.REALTIME_MISSING_ACCESS();
|
|
24
|
+
if (auth && !definition.identify && definition.origins) throw realtimeErrorDiagnostics.REALTIME_INVALID_ACCESS({ reason: "origins-without-identify" });
|
|
25
|
+
}
|
|
26
|
+
function assertRealtimeCheckpoint(definition) {
|
|
27
|
+
if (!definition.document || !definition.history?.checkpoint) return;
|
|
28
|
+
const document = definition.document;
|
|
29
|
+
if ("load" in document && typeof document.checkpoint !== "function") throw realtimeErrorDiagnostics.REALTIME_CHECKPOINT_UNSUPPORTED();
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/conflict.ts
|
|
33
|
+
const conflictCode = "REALTIME_DOCUMENT_CONFLICT";
|
|
34
|
+
/** Thrown by a document adapter when the stored digest no longer matches `ifDigest`. */
|
|
35
|
+
var RealtimeDocumentConflict = class RealtimeDocumentConflict extends Diagnostic {
|
|
36
|
+
constructor(why) {
|
|
37
|
+
const { code, docs, fix } = realtimeErrorDiagnostics.REALTIME_DOCUMENT_CONFLICT({ problem: why });
|
|
38
|
+
super({
|
|
39
|
+
code,
|
|
40
|
+
docs,
|
|
41
|
+
fix,
|
|
42
|
+
why: why ?? "The stored document changed since the realtime room loaded it."
|
|
43
|
+
}, RealtimeDocumentConflict);
|
|
44
|
+
this.name = "RealtimeDocumentConflict";
|
|
45
|
+
}
|
|
46
|
+
toJSON() {
|
|
47
|
+
return {
|
|
48
|
+
code: this.code,
|
|
49
|
+
data: void 0,
|
|
50
|
+
docs: this.docs,
|
|
51
|
+
fix: this.fix,
|
|
52
|
+
name: this.name,
|
|
53
|
+
why: this.why
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
function isRealtimeDocumentConflict(error) {
|
|
58
|
+
if (error instanceof RealtimeDocumentConflict) return true;
|
|
59
|
+
if (!error || typeof error !== "object" || !("code" in error)) return false;
|
|
60
|
+
return error.code === conflictCode;
|
|
61
|
+
}
|
|
62
|
+
//#endregion
|
|
63
|
+
export { defineRealtime as a, assertRealtimeCheckpoint as i, isRealtimeDocumentConflict as n, assertRealtimeAccess as r, RealtimeDocumentConflict as t };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Diagnostic } from "nostics";
|
|
2
|
+
//#region src/conflict.d.ts
|
|
3
|
+
/** Thrown by a document adapter when the stored digest no longer matches `ifDigest`. */
|
|
4
|
+
declare class RealtimeDocumentConflict extends Diagnostic {
|
|
5
|
+
constructor(why?: string);
|
|
6
|
+
toJSON(): {
|
|
7
|
+
code: string;
|
|
8
|
+
data: undefined;
|
|
9
|
+
docs: string | undefined;
|
|
10
|
+
fix: string | undefined;
|
|
11
|
+
name: string;
|
|
12
|
+
why: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
declare function isRealtimeDocumentConflict(error: unknown): boolean;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { isRealtimeDocumentConflict as n, RealtimeDocumentConflict as t };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { f as RealtimeEditor, u as RealtimeDocumentSnapshot } from "./types-1DVVG5ro.js";
|
|
2
|
+
import "./layout-fyQnhwCg.js";
|
|
3
|
+
import * as Y from "yjs";
|
|
4
|
+
//#region src/document.d.ts
|
|
5
|
+
/** Builds a new room document from a stored snapshot. Only use it for rooms no client holds yet. */
|
|
6
|
+
declare function createRealtimeYDoc(editor: RealtimeEditor, snapshot: RealtimeDocumentSnapshot): Promise<Y.Doc>;
|
|
7
|
+
declare function readRealtimeYDoc(editor: RealtimeEditor, document: Y.Doc): Promise<Required<RealtimeDocumentSnapshot>>;
|
|
8
|
+
/**
|
|
9
|
+
* Returns the Yjs update that turns `base` into `target` as a structural diff. Applying it to a
|
|
10
|
+
* room that has moved on since `base` keeps concurrent edits instead of duplicating content.
|
|
11
|
+
*/
|
|
12
|
+
declare function diffRealtimeYDoc(editor: RealtimeEditor, base: Y.Doc, target: RealtimeDocumentSnapshot): Promise<Uint8Array>;
|
|
13
|
+
/** Whether `document` already contains every insertion and deletion in `state`. */
|
|
14
|
+
declare function containsRealtimeState(document: Y.Doc, state: Uint8Array): boolean;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { readRealtimeYDoc as i, createRealtimeYDoc as n, diffRealtimeYDoc as r, containsRealtimeState as t };
|