@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
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { t as realtimeErrorDiagnostics } from "./error-diagnostics-B_66irmt.js";
|
|
2
|
+
import { n as isRecord } from "./object-CzKyK0hX.js";
|
|
3
|
+
//#region src/presence.ts
|
|
4
|
+
const personColors = [
|
|
5
|
+
"#E11D48",
|
|
6
|
+
"#D97706",
|
|
7
|
+
"#059669",
|
|
8
|
+
"#0891B2",
|
|
9
|
+
"#2563EB",
|
|
10
|
+
"#7C3AED",
|
|
11
|
+
"#C026D3"
|
|
12
|
+
];
|
|
13
|
+
function isString(value) {
|
|
14
|
+
try {
|
|
15
|
+
return String(value) === value;
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function createRealtimeIdentity(user) {
|
|
21
|
+
if (!isString(user.id) || !user.id || user.id.length > 256) throw realtimeErrorDiagnostics.REALTIME_INVALID_USER();
|
|
22
|
+
let hash = 0;
|
|
23
|
+
for (let index = 0; index < user.id.length; index++) hash = Math.imul(31, hash) + user.id.charCodeAt(index) | 0;
|
|
24
|
+
const name = (isString(user.name) && user.name || isString(user.email) && user.email || "Anonymous").slice(0, 256);
|
|
25
|
+
const identity = {
|
|
26
|
+
color: personColors[Math.abs(hash) % personColors.length],
|
|
27
|
+
id: user.id,
|
|
28
|
+
name
|
|
29
|
+
};
|
|
30
|
+
if (isString(user.image) && user.image && user.image.length <= 2048) identity.image = user.image;
|
|
31
|
+
return identity;
|
|
32
|
+
}
|
|
33
|
+
function isRealtimeIdentity(value) {
|
|
34
|
+
if (!isRecord(value)) return false;
|
|
35
|
+
return isString(value.id) && value.id.length > 0 && value.id.length <= 256 && isString(value.name) && value.name.length > 0 && value.name.length <= 256 && isString(value.color) && /^#[\da-f]{6}$/i.test(value.color) && (value.image === void 0 || isString(value.image) && value.image.length <= 2048);
|
|
36
|
+
}
|
|
37
|
+
function getRealtimePeople(states) {
|
|
38
|
+
const people = [];
|
|
39
|
+
for (const [clientId, state] of states) if (isRealtimeIdentity(state.user)) {
|
|
40
|
+
const { color, id, image, name } = state.user;
|
|
41
|
+
const person = {
|
|
42
|
+
color,
|
|
43
|
+
id,
|
|
44
|
+
name,
|
|
45
|
+
clientId
|
|
46
|
+
};
|
|
47
|
+
if (image !== void 0) person.image = image;
|
|
48
|
+
people.push(person);
|
|
49
|
+
}
|
|
50
|
+
return people;
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { getRealtimePeople as n, createRealtimeIdentity as t };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { k as RealtimeRoomRequestErrorCode } from "./types-1DVVG5ro.js";
|
|
2
|
+
import { Result } from "@vite-hub/runtime";
|
|
3
|
+
//#region src/request.d.ts
|
|
4
|
+
/** The `$DurableObject` namespace binding that holds Realtime rooms on Cloudflare. */
|
|
5
|
+
interface RealtimeDurableObjectNamespace {
|
|
6
|
+
get(id: unknown): {
|
|
7
|
+
fetch(request: Request): Promise<Response>;
|
|
8
|
+
};
|
|
9
|
+
idFromName(name: string): unknown;
|
|
10
|
+
}
|
|
11
|
+
interface RealtimeRoomRequestOptions {
|
|
12
|
+
/** How long to wait for the browser's answer, in milliseconds. Default 30 seconds. */
|
|
13
|
+
timeout?: number;
|
|
14
|
+
/** How long to wait for a browser to join or rejoin the room before the request fails, in milliseconds. Default 5 seconds. */
|
|
15
|
+
wait?: number;
|
|
16
|
+
/** Stops the request, such as when the caller's run is cancelled. */
|
|
17
|
+
signal?: AbortSignal;
|
|
18
|
+
/**
|
|
19
|
+
* The person whose room this is, for a Definition with `scope: "user"`: their Auth user id, or `null` for the shared
|
|
20
|
+
* anonymous scope. Required there; ignored elsewhere.
|
|
21
|
+
*/
|
|
22
|
+
user?: string | null;
|
|
23
|
+
/** On Cloudflare, the `$DurableObject` binding that holds the rooms. Defaults to the binding of the running Worker. */
|
|
24
|
+
durableObjects?: RealtimeDurableObjectNamespace;
|
|
25
|
+
}
|
|
26
|
+
/** `[error, answer]` of `requestRealtimeRoom()`. */
|
|
27
|
+
type RealtimeRoomRequestResult = Result<unknown, RealtimeRoomRequestErrorCode>;
|
|
28
|
+
/**
|
|
29
|
+
* Asks the one browser in a room of a presence Definition with `requests: true`, and resolves its answer. The browser
|
|
30
|
+
* answers with `room.onRequest()` from `@vite-hub/realtime/client`. A room with no browser waits `wait` milliseconds for
|
|
31
|
+
* one to join, such as after a reload, before it returns `REALTIME_ROOM_EMPTY`. A room with more than one browser
|
|
32
|
+
* returns `REALTIME_ROOM_CROWDED`. On Cloudflare the request goes to the Durable Object that holds the rooms. On Node it
|
|
33
|
+
* reaches the rooms of this process; with Redis or Postgres, a browser on another instance gives `REALTIME_ROOM_ELSEWHERE`.
|
|
34
|
+
* A Definition with `scope: "user"` keeps a room per person: pass `{ user }`, and only that person's room is asked.
|
|
35
|
+
* Returns `[error, answer]` and never throws.
|
|
36
|
+
*/
|
|
37
|
+
declare function requestRealtimeRoom(definition: string, documentId: string, body: unknown, options?: RealtimeRoomRequestOptions): Promise<RealtimeRoomRequestResult>;
|
|
38
|
+
/**
|
|
39
|
+
* The Realtime handle of an Agent's `page()` Capability. ViteHub passes it to the Agent routes when the app has
|
|
40
|
+
* Realtime, as `capabilities.realtime`.
|
|
41
|
+
*/
|
|
42
|
+
interface AgentRealtime {
|
|
43
|
+
available: () => boolean;
|
|
44
|
+
request: typeof requestRealtimeRoom;
|
|
45
|
+
}
|
|
46
|
+
declare const agentRealtime: AgentRealtime;
|
|
47
|
+
//#endregion
|
|
48
|
+
export { agentRealtime as a, RealtimeRoomRequestResult as i, RealtimeDurableObjectNamespace as n, requestRealtimeRoom as o, RealtimeRoomRequestOptions as r, AgentRealtime as t };
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { t as realtimeErrorDiagnostics } from "./error-diagnostics-B_66irmt.js";
|
|
2
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
|
+
import availability from "#vitehub/realtime/availability";
|
|
4
|
+
//#region ../internal/src/runtime/cloudflare-env-lookup.ts
|
|
5
|
+
/** The key of the AsyncLocalStorage store on `globalThis`. Every bundled copy of this module shares the store. */
|
|
6
|
+
const activeEnvStorageKey = Symbol.for("vitehub.cloudflare.env-storage");
|
|
7
|
+
let activeEnv;
|
|
8
|
+
/** The store of the current `runWithActiveCloudflareEnv()` call, or undefined outside one. */
|
|
9
|
+
function getScopedCloudflareEnv() {
|
|
10
|
+
return globalThis[activeEnvStorageKey]?.getStore();
|
|
11
|
+
}
|
|
12
|
+
function getActiveCloudflareEnv() {
|
|
13
|
+
const scoped = getScopedCloudflareEnv();
|
|
14
|
+
if (scoped) return scoped.env;
|
|
15
|
+
const host = globalThis;
|
|
16
|
+
return Object.hasOwn(host, "__env__") && host.__env__ !== void 0 ? host.__env__ : activeEnv;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region ../internal/src/runtime/cloudflare-env.ts
|
|
20
|
+
const runtimeGlobal = globalThis;
|
|
21
|
+
runtimeGlobal[activeEnvStorageKey] ||= new AsyncLocalStorage();
|
|
22
|
+
function getActiveCloudflareBinding(name) {
|
|
23
|
+
const env = getActiveCloudflareEnv();
|
|
24
|
+
return env && Object.hasOwn(env, name) ? env[name] : void 0;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/availability.ts
|
|
28
|
+
/**
|
|
29
|
+
* Returns `true` when this build has a Realtime room driver: Durable Objects on Cloudflare, a Node server, or memory
|
|
30
|
+
* on the dev server. Without one, no room opens and `refreshRealtimeDocument()` resolves `false`.
|
|
31
|
+
*/
|
|
32
|
+
function isRealtimeAvailable() {
|
|
33
|
+
return availability.available;
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/request.ts
|
|
37
|
+
const routePrefix = "/api/_vitehub/realtime/";
|
|
38
|
+
/** Marks the request that `requestRealtimeRoom()` sends to a Durable Object. The Worker strips it from client requests. */
|
|
39
|
+
const internalRequestHeader = "x-vitehub-realtime-request";
|
|
40
|
+
const defaultRequestTimeout = 3e4;
|
|
41
|
+
const defaultRequestWait = 5e3;
|
|
42
|
+
/**
|
|
43
|
+
* The one Durable Object instance that holds every room. Nitro's Cloudflare entry hands each WebSocket upgrade
|
|
44
|
+
* to this instance before the application runs, so saves, checkpoints, and refreshes must reach it too: a Durable
|
|
45
|
+
* Object per room would have no peers and never match a room's state.
|
|
46
|
+
*/
|
|
47
|
+
const durableRoomsInstance = "server";
|
|
48
|
+
/**
|
|
49
|
+
* Accepts the namespace that holds Realtime rooms, and nothing that only inherits `get` and `idFromName` from an
|
|
50
|
+
* arbitrary prototype. Workerd's binding owns no methods: they belong to its `DurableObjectNamespace` class prototype,
|
|
51
|
+
* and Workers have no global `DurableObjectNamespace` to compare with. Any other namespace, such as a test double,
|
|
52
|
+
* must own both methods.
|
|
53
|
+
*/
|
|
54
|
+
function isRealtimeDurableObjectNamespace(value) {
|
|
55
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
56
|
+
const methods = (target) => ["get", "idFromName"].map((name) => Object.getOwnPropertyDescriptor(target, name));
|
|
57
|
+
const callable = (method) => !!method && !method.get && typeof method.value === "function" && Function.prototype.toString.call(method.value) !== "function () { [native code] }";
|
|
58
|
+
try {
|
|
59
|
+
const own = methods(value);
|
|
60
|
+
if (own.every(callable)) return true;
|
|
61
|
+
const prototype = Object.getPrototypeOf(value);
|
|
62
|
+
if (own.some(Boolean) || prototype === null || typeof prototype !== "object" || Object.getPrototypeOf(prototype) !== Object.prototype) return false;
|
|
63
|
+
return Object.prototype.toString.call(value) === "[object DurableObjectNamespace]" && methods(prototype).every(callable);
|
|
64
|
+
} catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** The `$DurableObject` binding of the running Worker. Nitro's Cloudflare entry stores each request's bindings in `globalThis.__env__`. */
|
|
69
|
+
function workerDurableObjects() {
|
|
70
|
+
try {
|
|
71
|
+
const binding = getActiveCloudflareBinding("$DurableObject");
|
|
72
|
+
return isRealtimeDurableObjectNamespace(binding) ? binding : void 0;
|
|
73
|
+
} catch {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const localRequestKey = Symbol.for("vitehub.realtime.local-request");
|
|
78
|
+
const requestScope = globalThis;
|
|
79
|
+
function localRequesters() {
|
|
80
|
+
requestScope[localRequestKey] ??= /* @__PURE__ */ new Set();
|
|
81
|
+
return requestScope[localRequestKey];
|
|
82
|
+
}
|
|
83
|
+
/** The parameters each request error was made with, so a Durable Object can send the same error to the Worker. */
|
|
84
|
+
const roomRequestErrorParams = /* @__PURE__ */ new WeakMap();
|
|
85
|
+
function roomRequestError(code, params = {}) {
|
|
86
|
+
const result = createRoomRequestError(code, params);
|
|
87
|
+
if (result[0]) roomRequestErrorParams.set(result[0], params);
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
function createRoomRequestError(code, params) {
|
|
91
|
+
switch (code) {
|
|
92
|
+
case "REALTIME_ROOM_ABORTED": return [realtimeErrorDiagnostics.REALTIME_ROOM_ABORTED(), void 0];
|
|
93
|
+
case "REALTIME_ROOM_CROWDED": return [realtimeErrorDiagnostics.REALTIME_ROOM_CROWDED({ count: params.count ?? 2 }), void 0];
|
|
94
|
+
case "REALTIME_ROOM_ELSEWHERE": return [realtimeErrorDiagnostics.REALTIME_ROOM_ELSEWHERE(), void 0];
|
|
95
|
+
case "REALTIME_ROOM_EMPTY": return [realtimeErrorDiagnostics.REALTIME_ROOM_EMPTY(), void 0];
|
|
96
|
+
case "REALTIME_ROOM_REFUSED": return [realtimeErrorDiagnostics.REALTIME_ROOM_REFUSED({ reason: params.reason ?? "no reason given." }), void 0];
|
|
97
|
+
case "REALTIME_ROOM_REQUESTS_OFF": return [realtimeErrorDiagnostics.REALTIME_ROOM_REQUESTS_OFF({ definition: params.definition ?? "" }), void 0];
|
|
98
|
+
case "REALTIME_ROOM_TIMEOUT": return [realtimeErrorDiagnostics.REALTIME_ROOM_TIMEOUT({ timeout: params.timeout ?? 3e4 }), void 0];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const roomRequestErrorCodes = /* @__PURE__ */ new Set([
|
|
102
|
+
"REALTIME_ROOM_ABORTED",
|
|
103
|
+
"REALTIME_ROOM_CROWDED",
|
|
104
|
+
"REALTIME_ROOM_ELSEWHERE",
|
|
105
|
+
"REALTIME_ROOM_EMPTY",
|
|
106
|
+
"REALTIME_ROOM_REFUSED",
|
|
107
|
+
"REALTIME_ROOM_REQUESTS_OFF",
|
|
108
|
+
"REALTIME_ROOM_TIMEOUT"
|
|
109
|
+
]);
|
|
110
|
+
function isRoomRequestErrorCode(value) {
|
|
111
|
+
return typeof value === "string" && roomRequestErrorCodes.has(value);
|
|
112
|
+
}
|
|
113
|
+
function toDurableRequestReply(result) {
|
|
114
|
+
const [error, body] = result;
|
|
115
|
+
return error ? {
|
|
116
|
+
ok: false,
|
|
117
|
+
code: error.code,
|
|
118
|
+
params: roomRequestErrorParams.get(error) ?? {}
|
|
119
|
+
} : {
|
|
120
|
+
ok: true,
|
|
121
|
+
body
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function isDurableSockets(value) {
|
|
125
|
+
return typeof value === "object" && value !== null && "getWebSockets" in value && typeof value.getWebSockets === "function";
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Asks the one browser in a room of a presence Definition with `requests: true`, and resolves its answer. The browser
|
|
129
|
+
* answers with `room.onRequest()` from `@vite-hub/realtime/client`. A room with no browser waits `wait` milliseconds for
|
|
130
|
+
* one to join, such as after a reload, before it returns `REALTIME_ROOM_EMPTY`. A room with more than one browser
|
|
131
|
+
* returns `REALTIME_ROOM_CROWDED`. On Cloudflare the request goes to the Durable Object that holds the rooms. On Node it
|
|
132
|
+
* reaches the rooms of this process; with Redis or Postgres, a browser on another instance gives `REALTIME_ROOM_ELSEWHERE`.
|
|
133
|
+
* A Definition with `scope: "user"` keeps a room per person: pass `{ user }`, and only that person's room is asked.
|
|
134
|
+
* Returns `[error, answer]` and never throws.
|
|
135
|
+
*/
|
|
136
|
+
async function requestRealtimeRoom(definition, documentId, body, options = {}) {
|
|
137
|
+
const local = {
|
|
138
|
+
timeout: options.timeout ?? 3e4,
|
|
139
|
+
wait: options.wait ?? 5e3,
|
|
140
|
+
...options.signal ? { signal: options.signal } : {},
|
|
141
|
+
...options.user !== void 0 ? { user: options.user } : {}
|
|
142
|
+
};
|
|
143
|
+
if (local.signal?.aborted) return roomRequestError("REALTIME_ROOM_ABORTED");
|
|
144
|
+
const binding = options.durableObjects ?? workerDurableObjects();
|
|
145
|
+
if (binding) return await requestDurableRoom(binding, definition, documentId, body, local);
|
|
146
|
+
for (const reference of [...localRequesters()].reverse()) {
|
|
147
|
+
const request = reference.deref();
|
|
148
|
+
if (!request) {
|
|
149
|
+
localRequesters().delete(reference);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
return await request(definition, documentId, body, local);
|
|
153
|
+
}
|
|
154
|
+
return roomRequestError("REALTIME_ROOM_EMPTY");
|
|
155
|
+
}
|
|
156
|
+
async function requestDurableRoom(binding, definition, documentId, body, options) {
|
|
157
|
+
const room = documentId.split("/").map(encodeURIComponent).join("/");
|
|
158
|
+
let response;
|
|
159
|
+
try {
|
|
160
|
+
response = await binding.get(binding.idFromName(durableRoomsInstance)).fetch(new Request(`https://vitehub.internal${routePrefix}${encodeURIComponent(definition)}/${room}?request=1`, {
|
|
161
|
+
method: "POST",
|
|
162
|
+
headers: {
|
|
163
|
+
[internalRequestHeader]: "1",
|
|
164
|
+
"content-type": "application/json"
|
|
165
|
+
},
|
|
166
|
+
body: JSON.stringify({
|
|
167
|
+
body,
|
|
168
|
+
timeout: options.timeout,
|
|
169
|
+
wait: options.wait,
|
|
170
|
+
...options.user !== void 0 ? { user: options.user } : {}
|
|
171
|
+
}),
|
|
172
|
+
...options.signal ? { signal: options.signal } : {}
|
|
173
|
+
}));
|
|
174
|
+
} catch {
|
|
175
|
+
return roomRequestError(options.signal?.aborted ? "REALTIME_ROOM_ABORTED" : "REALTIME_ROOM_EMPTY");
|
|
176
|
+
}
|
|
177
|
+
const reply = response.ok ? await response.json().catch(() => void 0) : void 0;
|
|
178
|
+
if (reply && typeof reply === "object" && "ok" in reply) {
|
|
179
|
+
if (reply.ok === true) return [void 0, "body" in reply ? reply.body : void 0];
|
|
180
|
+
if ("code" in reply && isRoomRequestErrorCode(reply.code)) {
|
|
181
|
+
const params = "params" in reply && reply.params && typeof reply.params === "object" ? reply.params : {};
|
|
182
|
+
return roomRequestError(reply.code, params);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return roomRequestError("REALTIME_ROOM_REFUSED", { reason: `the Durable Object answered HTTP ${response.status}.` });
|
|
186
|
+
}
|
|
187
|
+
const agentRealtime = {
|
|
188
|
+
available: isRealtimeAvailable,
|
|
189
|
+
request: requestRealtimeRoom
|
|
190
|
+
};
|
|
191
|
+
//#endregion
|
|
192
|
+
export { internalRequestHeader as a, localRequesters as c, routePrefix as d, toDurableRequestReply as f, durableRoomsInstance as i, requestRealtimeRoom as l, isRealtimeAvailable as m, defaultRequestTimeout as n, isDurableSockets as o, workerDurableObjects as p, defaultRequestWait as r, isRealtimeDurableObjectNamespace as s, agentRealtime as t, roomRequestError as u };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,37 +1,142 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
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";
|
|
3
|
+
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";
|
|
3
7
|
import * as awarenessProtocol from "y-protocols/awareness";
|
|
4
|
-
import
|
|
5
|
-
|
|
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
|
|
6
50
|
//#region src/presence.d.ts
|
|
7
51
|
type RealtimeIdentity = Omit<RealtimePerson, "clientId">;
|
|
8
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
|
|
9
83
|
//#region src/server.d.ts
|
|
10
|
-
interface RealtimeHandler {
|
|
84
|
+
export interface RealtimeHandler {
|
|
11
85
|
(event: unknown): Promise<unknown>;
|
|
12
86
|
fetch(input: Request | URL | string): Promise<Response>;
|
|
13
87
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
declare
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
declare function applyRealtimeSyncMessage(data: Uint8Array, document: Y.Doc, origin: unknown, maxStateBytes?: number): Uint8Array | undefined;
|
|
26
|
-
declare function assertRealtimeRoomStateQuota(document: Y.Doc, maxStateBytes?: number): void;
|
|
27
|
-
declare function applyRealtimeAwarenessUpdate(awareness: awarenessProtocol.Awareness, update: Uint8Array, origin: unknown, maxStateBytes?: number): void;
|
|
28
|
-
declare function compactRealtimeAwareness(awareness: awarenessProtocol.Awareness): awarenessProtocol.Awareness;
|
|
29
|
-
declare function restoreRealtimeDocument(markdown: string, baselineDigest: string | undefined, stored: Record<string, unknown> | undefined): Y.Doc;
|
|
30
|
-
declare function readRealtimeWorkspaceDocument(readable: ReadonlyWorkspaceFacade, writable: WritableWorkspaceFacade, documentId: string): Promise<{
|
|
31
|
-
baselineDigest: string | undefined;
|
|
32
|
-
markdown: string;
|
|
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;
|
|
33
99
|
}>;
|
|
34
|
-
declare function
|
|
35
|
-
declare function
|
|
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;
|
|
36
141
|
//#endregion
|
|
37
|
-
export {
|
|
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 };
|