@vite-hub/realtime 0.4.1 → 0.5.1
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 +8 -4
- package/dist/agent.d.ts +1 -1
- package/dist/agent.js +1 -2
- package/dist/{bus-CKv8gQZf.js → bus-DJ3dOQAZ.js} +11 -22
- package/dist/{bus-protocol-wXkXW28q.d.ts → bus-protocol-juNrVJDF.d.ts} +0 -2
- package/dist/bus.d.ts +1 -3
- package/dist/bus.js +1 -2
- package/dist/client/browser.js +3 -3
- package/dist/client.d.ts +2 -4
- package/dist/client.js +15 -9
- package/dist/{conflict-DYC_qXzM.js → conflict-BqdKFrkE.js} +3 -5
- package/dist/{conflict-CmZizTYl.d.ts → conflict-C5KRv-J1.d.ts} +0 -2
- package/dist/{document-BfMW1zYU.d.ts → document-CbqgNO_0.d.ts} +2 -4
- package/dist/document-CfrQbB03.js +1736 -0
- package/dist/durability.d.ts +1 -3
- package/dist/durability.js +1 -6
- package/dist/{editor-BM5-vHqf.js → editor-DzhKM4Gt.js} +2 -5
- package/dist/{error-diagnostics-Cd-XWNf7.js → error-diagnostics-we8V1O-X.js} +1 -2
- package/dist/index.d.ts +3 -7
- package/dist/index.js +3 -3
- package/dist/internal/runtime/unavailable.d.ts +1 -3
- package/dist/internal/runtime/unavailable.js +1 -2
- package/dist/internal/server.d.ts +7 -0
- package/dist/internal/server.js +6 -0
- package/dist/{keepalive-B4ONG6AD.js → keepalive-BYkk6Ax8.js} +3 -5
- package/dist/{layout-DNaxppTZ.js → layout-BQwRLTH0.js} +1 -2
- package/dist/{layout-B7i6-FnH.d.ts → layout-BZtKNqiC.d.ts} +1 -3
- package/dist/log-NlQi6kow.js +157 -0
- package/dist/{object-BgPPDLDE.js → object-DfrneieE.js} +2 -4
- package/dist/{presence-B6rvkHqd.js → presence-DZ21pPKm.js} +3 -4
- package/dist/{request-C_bQc4NE.js → request-DM6xHTaU.js} +5 -10
- package/dist/{request-BwaohsJS.d.ts → request-PrMHOYFK.d.ts} +1 -3
- package/dist/server-CLdYV7Md.d.ts +126 -0
- package/dist/server-yQp02XNp.js +2599 -0
- package/dist/server.d.ts +4 -142
- package/dist/server.js +3 -2588
- package/dist/{types-Cvm0G3VJ.d.ts → types-oUyN-CJR.d.ts} +1 -27
- package/dist/vite.d.ts +4 -15
- package/dist/vite.js +64 -63
- package/dist/vue.d.ts +2 -7
- package/dist/vue.js +47 -40
- package/package.json +44 -39
- package/dist/document-U5rzg2vG.js +0 -2041
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Realtime is for collaborative documents, presence, explicit Workspace checkpoint
|
|
|
14
14
|
## Install
|
|
15
15
|
|
|
16
16
|
```sh
|
|
17
|
-
pnpm add @vite-hub/realtime @vite-hub/workspace @tiptap/vue-3 drizzle-orm@1.0.0-rc.
|
|
17
|
+
pnpm add @vite-hub/realtime @vite-hub/workspace @tiptap/vue-3 drizzle-orm@1.0.0-rc.6 h3 nitro vite vue
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
ViteHub Realtime requires Node.js 24.15 or newer. The published client adapter is for Vue. The package declares `vite` and `vue` as optional peers so server-only integrations do not install unused framework tooling; this walkthrough uses both. It also declares `drizzle-orm` as a peer: Realtime depends on Auth, and Auth uses the app's copy of Drizzle.
|
|
@@ -167,7 +167,7 @@ const [error, answer] = await requestRealtimeRoom("tabs", tabId, { want: "title"
|
|
|
167
167
|
- The Vue adapter connects to the generated same-origin `/_vitehub/realtime/**` WebSocket route. The Vite integration registers that route and discovers `server/realtime` definitions. In a component it prepares the editor extensions during setup and connects when the component mounts, so the server render and the first browser render both have `status` `disconnected`.
|
|
168
168
|
- `driver: "memory"` is single-process and loses room state when the process stops. ViteHub rejects it for known distributed hosts. It retains at most 128 active rooms.
|
|
169
169
|
- `driver: "cloudflare"` uses a SQLite-backed Cloudflare Durable Object. `driver: "auto"` selects it when a Cloudflare host or hosting environment can be resolved.
|
|
170
|
-
- On a Node build, `driver: "auto"` picks when the server starts: `redis` with `REDIS_URL`, else `postgres`
|
|
170
|
+
- On a Node build, `driver: "auto"` picks when the server starts: `redis` with `REDIS_URL`, else `postgres` with `REALTIME_POSTGRES_URL` or the postgres.js client of a Postgres Default Database (`DATABASE_URL` is not read), else `memory`. A borrowed Database client is never closed. It prints the choice at boot. With `redis` or `postgres`, each instance holds its rooms in memory and shares Yjs updates and awareness with the other instances through Redis pub/sub (Redis 6 or later) or Postgres `LISTEN`/`NOTIFY`. The instances start each room from one shared record (`vitehub_realtime_rooms` on Postgres), and saves of one room run one at a time under a lock, so `ifDigest` stays correct across instances. `NOTIFY` payloads over 8,000 bytes go through the `vitehub_realtime_messages` table. Each app has its own namespace on the bus (`realtime.namespace`, else the app name; `REALTIME_NAMESPACE` replaces it at runtime), so two apps on one Redis or Postgres never share a room. Presence of a stopped instance leaves at once, and that of a crashed instance after the 30 second Yjs awareness timeout. Other production builds fail with `REALTIME_MISSING_DRIVER`.
|
|
171
171
|
- Durable rooms do not make Workspace files durable. Production checkpoints need a durable Workspace Store with conditional writes; memory Workspace Stores are rejected for durable checkpoints.
|
|
172
172
|
- Each Realtime Definition must set `authorize: true`, `authorize()`, or `access: "public"`. Otherwise the server rejects it with `REALTIME_MISSING_ACCESS`. `authorize()` allows only when it returns `true`; `undefined` and `false` deny. It also runs for signed-out callers, with `user: null`. `authorize: true` needs ViteHub Auth or `identify()`; it verifies the user and replaces client-supplied presence identity. `access: "public"` lets each person read and write each Realtime. Presence in a public definition is untrusted and must not authorize application actions.
|
|
173
173
|
- Requests with an `Origin` other than the app's get 403 on each definition, unless `origins()` accepts the origin.
|
|
@@ -177,6 +177,10 @@ const [error, answer] = await requestRealtimeRoom("tabs", tabId, { want: "title"
|
|
|
177
177
|
- One WebSocket message is limited to 1 MiB. Document state and awareness state are each limited to 8 MiB per room, and one peer can own at most 1,024 awareness clients.
|
|
178
178
|
- Changing or disabling the document, or disposing its Vue scope, destroys the document provider. `save()`, `history.checkpoint()`, and `refresh()` return `[error, data]` with a `RealtimeRequestErrorCode`. A `REALTIME_REQUEST_FAILED` error has the HTTP status in `error.data.status`. A refused request returns the code that the server names, such as `REALTIME_SOURCE_CHANGED` when the stored document changed, or `REALTIME_SYNC_PENDING` after 20 retries. A checkpoint returns `REALTIME_DISCONNECTED` during a document switch until the Vue watcher creates the new document, and when its original document is disconnected or replaced during a retry. The application still owns editor disposal, checkpoint error handling, and reconnect UX.
|
|
179
179
|
|
|
180
|
+
## Refusals and failures in logs
|
|
181
|
+
|
|
182
|
+
With the evlog Nitro module registered, a refused connection, save, checkpoint, or refresh gets a `realtime` field on its request's wide event: `{ definition, room, refused, status, code? }`. A room id of a `document: false` Definition is a short hash, because it can be a token. After the WebSocket opens, close codes, connection errors, failed sync between Node instances, and failed background saves are their own evlog events, `{ action: "realtime", message, realtime: { definition, room, closed | failed } }`. Without evlog, Realtime prints the sync and save failures to the console. See [Limits and errors](https://vitehub.dev/docs/realtime/limits-and-errors#see-refusals-and-failures-in-logs).
|
|
183
|
+
|
|
180
184
|
## Public imports
|
|
181
185
|
|
|
182
186
|
| Import | Purpose |
|
|
@@ -187,14 +191,14 @@ const [error, answer] = await requestRealtimeRoom("tabs", tabId, { want: "title"
|
|
|
187
191
|
| `@vite-hub/realtime/agent` | `agentRealtime`, the handle that ViteHub passes to the Agent `page()` Capability. Apps do not import it. |
|
|
188
192
|
| `@vite-hub/realtime/client` | `connectRealtimeRoom()` to share presence without an editor, in any framework. |
|
|
189
193
|
| `@vite-hub/realtime/client/browser.js` | The same client as one prebuilt ES module with no imports, for a script that another site loads from your app without a bundler. |
|
|
190
|
-
| `@vite-hub/realtime/server` | `
|
|
194
|
+
| `@vite-hub/realtime/server` | `refreshRealtimeDocument()`, `isRealtimeAvailable()`, `requestRealtimeRoom()`, `workspaceDocument()`, `blobDocument()`, and `createRealtimeHandler()` for a manual H3 server integration. Normal ViteHub apps use the generated route. |
|
|
191
195
|
| `@vite-hub/realtime/bus` | `useRealtimeBus()` and `closeRealtimeBuses()`, the Postgres and Redis buses that the generated Node handler passes to `createRealtimeHandler({ bus })`. |
|
|
192
196
|
|
|
193
197
|
Generated files under `.vitehub/nitro/realtime` are inspectable build output, not application imports.
|
|
194
198
|
|
|
195
199
|
## Learn more
|
|
196
200
|
|
|
197
|
-
- [Realtime collaboration](https://vitehub.dev/docs/realtime) covers
|
|
201
|
+
- [Realtime collaboration](https://vitehub.dev/docs/realtime) covers drivers, checkpoints, workspace events, quotas, and generated output.
|
|
198
202
|
- [Workspace](https://vitehub.dev/docs/workspace) covers stores, write rules, snapshots, and persistence.
|
|
199
203
|
- [Auth](https://vitehub.dev/docs/auth) covers session setup and trusted origins.
|
|
200
204
|
- [Configuration options](https://vitehub.dev/docs/reference/config-options) lists every Realtime module option.
|
package/dist/agent.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as agentRealtime, t as AgentRealtime } from "./request-
|
|
1
|
+
import { a as agentRealtime, t as AgentRealtime } from "./request-PrMHOYFK.js";
|
|
2
2
|
export { type AgentRealtime, agentRealtime };
|
package/dist/agent.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { agentRealtime };
|
|
1
|
+
export { t as agentRealtime } from "./request-DM6xHTaU.js";
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { t as realtimeErrorDiagnostics } from "./error-diagnostics-
|
|
1
|
+
import { t as realtimeErrorDiagnostics } from "./error-diagnostics-we8V1O-X.js";
|
|
2
|
+
import { n as realtimeRoomFields, t as logRealtime } from "./log-NlQi6kow.js";
|
|
2
3
|
import { createHash } from "node:crypto";
|
|
3
4
|
import * as decoding from "lib0/decoding";
|
|
4
5
|
import * as encoding from "lib0/encoding";
|
|
5
|
-
|
|
6
|
+
// src/bus-protocol.ts
|
|
6
7
|
/**
|
|
7
8
|
* What one instance tells the other instances about a room:
|
|
8
9
|
* - `update`: a Yjs update of the room document.
|
|
@@ -52,8 +53,7 @@ function decodeRealtimeBusMessage(bytes) {
|
|
|
52
53
|
return;
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
|
-
|
|
56
|
-
//#region src/bus.ts
|
|
56
|
+
// src/bus.ts
|
|
57
57
|
/** The variable that points the Postgres bus at its own database, instead of the connection of the Default Database. */
|
|
58
58
|
const realtimePostgresUrlVariable = "REALTIME_POSTGRES_URL";
|
|
59
59
|
/** The variable that replaces the namespace of the build, such as to keep staging and production apart on one Redis. */
|
|
@@ -104,7 +104,6 @@ function resolveRealtimeNodeDriver(driver, options = {}) {
|
|
|
104
104
|
function describeChoice(choice) {
|
|
105
105
|
return choice.driver === "memory" ? "[vitehub] Realtime keeps rooms in this process. Set REDIS_URL, or use a Postgres Default Database, to share rooms across instances." : `[vitehub] Realtime shares rooms across instances through ${choice.driver === "redis" ? "Redis" : "Postgres"} (${choice.source}).`;
|
|
106
106
|
}
|
|
107
|
-
/** The postgres.js client of the Default Database, or undefined when it is not one. */
|
|
108
107
|
async function defaultDatabaseClient(databases) {
|
|
109
108
|
const entry = databases ? (await databases()).default : void 0;
|
|
110
109
|
const client = entry ? Reflect.get(entry.db, "$client") : void 0;
|
|
@@ -181,7 +180,6 @@ function createBusBase() {
|
|
|
181
180
|
onClose(hook) {
|
|
182
181
|
hooks.push(hook);
|
|
183
182
|
},
|
|
184
|
-
/** Runs the close hooks once, then `end`. */
|
|
185
183
|
close(end) {
|
|
186
184
|
closing ??= (async () => {
|
|
187
185
|
await Promise.allSettled(hooks.map((hook) => hook()));
|
|
@@ -191,10 +189,6 @@ function createBusBase() {
|
|
|
191
189
|
}
|
|
192
190
|
};
|
|
193
191
|
}
|
|
194
|
-
/**
|
|
195
|
-
* Runs the listeners on each message in arrival order, also when one message needs an async step to read. `start`
|
|
196
|
-
* subscribes once, for the first listener.
|
|
197
|
-
*/
|
|
198
192
|
function createDelivery(instance, read, start) {
|
|
199
193
|
let queue = Promise.resolve();
|
|
200
194
|
const listeners = /* @__PURE__ */ new Set();
|
|
@@ -216,12 +210,11 @@ function createDelivery(instance, read, start) {
|
|
|
216
210
|
if (!message || message.instance === instance) return;
|
|
217
211
|
for (const listener of listeners) await listener(message);
|
|
218
212
|
}).catch((error) => {
|
|
219
|
-
|
|
213
|
+
logRealtime("error", "Realtime could not apply a message from another instance.", { failed: "bus-receive" }, error);
|
|
220
214
|
});
|
|
221
215
|
}
|
|
222
216
|
};
|
|
223
217
|
}
|
|
224
|
-
/** Sends messages one at a time, so that the other instances receive them in the order this instance sent them. */
|
|
225
218
|
function createSendQueue() {
|
|
226
219
|
let queue = Promise.resolve();
|
|
227
220
|
return (send) => {
|
|
@@ -230,7 +223,6 @@ function createSendQueue() {
|
|
|
230
223
|
return sent;
|
|
231
224
|
};
|
|
232
225
|
}
|
|
233
|
-
/** Runs at most `limit` tasks at a time. */
|
|
234
226
|
function createLimit(limit) {
|
|
235
227
|
let active = 0;
|
|
236
228
|
const waiting = [];
|
|
@@ -246,14 +238,10 @@ function createLimit(limit) {
|
|
|
246
238
|
};
|
|
247
239
|
}
|
|
248
240
|
const postgresChannel = "vitehub_realtime";
|
|
249
|
-
/** `NOTIFY` payloads stop at 8000 bytes. A larger message goes to a table, and the notification carries its id. */
|
|
250
241
|
const maxNotifyPayload = 7900;
|
|
251
|
-
/** Large messages stay this long for instances that read them late. */
|
|
252
242
|
const postgresMessageRetentionSeconds = 300;
|
|
253
|
-
/** Pool size for room records and notifications. Locks use their own pool so adapter queries cannot wait on them. */
|
|
254
243
|
const postgresConnections = 6;
|
|
255
244
|
const postgresLockedSaves = 3;
|
|
256
|
-
/** A stable number for `pg_advisory_xact_lock()` while the tables are created: "vhrt" in ASCII. */
|
|
257
245
|
const postgresSetupLock = 1986556532;
|
|
258
246
|
function isPostgresClient(value) {
|
|
259
247
|
return typeof value === "function" && [
|
|
@@ -374,9 +362,7 @@ async function createPostgresRealtimeBus(connection) {
|
|
|
374
362
|
};
|
|
375
363
|
}
|
|
376
364
|
const redisPrefix = "vitehub:realtime:";
|
|
377
|
-
/** A lock whose holder stopped expires after this time. */
|
|
378
365
|
const redisLockMs = 3e4;
|
|
379
|
-
/** Writes the record when the stored epoch is ARGV[1], or when there is no record and ARGV[1] is empty. */
|
|
380
366
|
const redisWriteRoomScript = `
|
|
381
367
|
local current = redis.call('HGET', KEYS[1], 'epoch')
|
|
382
368
|
if (ARGV[1] == '' and current == false) or (current ~= false and current == ARGV[1]) then
|
|
@@ -385,7 +371,6 @@ if (ARGV[1] == '' and current == false) or (current ~= false and current == ARGV
|
|
|
385
371
|
end
|
|
386
372
|
return 0
|
|
387
373
|
`;
|
|
388
|
-
/** Extends the lock while its holder still runs, so a save that takes longer than 30 seconds keeps it. */
|
|
389
374
|
const redisRenewLockScript = `
|
|
390
375
|
if redis.call('GET', KEYS[1]) == ARGV[1] then
|
|
391
376
|
return redis.call('PEXPIRE', KEYS[1], ARGV[2])
|
|
@@ -458,7 +443,12 @@ async function createRedisRealtimeBus(url, options = {}) {
|
|
|
458
443
|
await new Promise((resolve) => setTimeout(resolve, 25 + Math.random() * 50));
|
|
459
444
|
}
|
|
460
445
|
const renew = setInterval(() => {
|
|
461
|
-
client.eval(redisRenewLockScript, 1, key, token, redisLockMs).catch(() => {
|
|
446
|
+
client.eval(redisRenewLockScript, 1, key, token, redisLockMs).catch((error) => {
|
|
447
|
+
logRealtime("warn", "Realtime could not renew a room lock.", {
|
|
448
|
+
...realtimeRoomFields(room, false),
|
|
449
|
+
failed: "bus-lock"
|
|
450
|
+
}, error);
|
|
451
|
+
});
|
|
462
452
|
}, redisLockRenewMs);
|
|
463
453
|
try {
|
|
464
454
|
return await task();
|
|
@@ -472,5 +462,4 @@ async function createRedisRealtimeBus(url, options = {}) {
|
|
|
472
462
|
})
|
|
473
463
|
};
|
|
474
464
|
}
|
|
475
|
-
//#endregion
|
|
476
465
|
export { logRealtimeNodeDriver as a, resolveRealtimeNamespace as c, createRedisRealtimeBus as i, resolveRealtimeNodeDriver as l, createPostgresRealtimeBus as n, realtimeNamespaceVariable as o, createRealtimeNodeBus as r, realtimePostgresUrlVariable as s, closeRealtimeBuses as t, useRealtimeBus as u };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
//#region src/bus-protocol.d.ts
|
|
2
1
|
/**
|
|
3
2
|
* What one instance tells the other instances about a room:
|
|
4
3
|
* - `update`: a Yjs update of the room document.
|
|
@@ -62,5 +61,4 @@ interface RealtimeBus {
|
|
|
62
61
|
onClose(hook: () => Promise<void>): void;
|
|
63
62
|
close(): Promise<void>;
|
|
64
63
|
}
|
|
65
|
-
//#endregion
|
|
66
64
|
export { RealtimeBusMessage as n, RealtimeRoomRecord as r, RealtimeBus as t };
|
package/dist/bus.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { n as RealtimeBusMessage, r as RealtimeRoomRecord, t as RealtimeBus } from "./bus-protocol-
|
|
2
|
-
//#region src/bus.d.ts
|
|
1
|
+
import { n as RealtimeBusMessage, r as RealtimeRoomRecord, t as RealtimeBus } from "./bus-protocol-juNrVJDF.js";
|
|
3
2
|
/** The driver that a Node build asks for: `auto` reads the environment when the server starts. */
|
|
4
3
|
export type RealtimeNodeDriver = "auto" | "postgres" | "redis";
|
|
5
4
|
/** The variable that points the Postgres bus at its own database, instead of the connection of the Default Database. */
|
|
@@ -81,5 +80,4 @@ export declare function createPostgresRealtimeBus(connection: ({
|
|
|
81
80
|
export declare function createRedisRealtimeBus(url: string, options?: {
|
|
82
81
|
namespace?: string;
|
|
83
82
|
}): Promise<RealtimeBus>;
|
|
84
|
-
//#endregion
|
|
85
83
|
export type { RealtimeBus, RealtimeBusMessage, RealtimeRoomRecord };
|
package/dist/bus.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { closeRealtimeBuses, createPostgresRealtimeBus, createRealtimeNodeBus, createRedisRealtimeBus, logRealtimeNodeDriver, realtimeNamespaceVariable, realtimePostgresUrlVariable, resolveRealtimeNamespace, resolveRealtimeNodeDriver, useRealtimeBus };
|
|
1
|
+
export { t as closeRealtimeBuses, n as createPostgresRealtimeBus, r as createRealtimeNodeBus, i as createRedisRealtimeBus, a as logRealtimeNodeDriver, o as realtimeNamespaceVariable, s as realtimePostgresUrlVariable, c as resolveRealtimeNamespace, l as resolveRealtimeNodeDriver, u as useRealtimeBus } from "./bus-DJ3dOQAZ.js";
|