@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,7 @@
1
+ import { i as RealtimeRoomRequestResult, n as RealtimeDurableObjectNamespace, o as requestRealtimeRoom, r as RealtimeRoomRequestOptions } from "../request-DAodIqZ8.js";
2
+ import { n as RealtimeBusMessage, r as RealtimeRoomRecord, t as RealtimeBus } from "../bus-protocol-wXkXW28q.js";
3
+ import { n as isRealtimeDocumentConflict, t as RealtimeDocumentConflict } from "../conflict-CmZizTYl.js";
4
+ import { n as realtimeDocumentLayout, t as readRealtimeStatus } from "../layout-VHjYWa-M.js";
5
+ import { C as claimAwarenessClientIds, D as blobDocument, E as RealtimeBlobStore, O as isRealtimeAvailable, S as bindAwarenessIdentity, T as RealtimeBlobDocumentOptions, _ as isRealtimeWorkspaceDocument, a as applyRealtimeSyncMessage, b as readRealtimeRequestState, c as encodeAwarenessState, d as realtimePersistenceDefaults, f as realtimeRoomKey, g as RealtimeWorkspaceDocumentAdapter, h as restoreRealtimeDocument, i as RealtimeSyncOptions, l as encodeSyncStep1, m as resolveRealtimeDefinition, n as RealtimeHandlerOptions, o as assertRealtimeRoomStateQuota, p as refreshRealtimeDocument, r as RealtimeRefreshOptions, s as createRealtimeHandler, t as RealtimeHandler, u as encodeSyncUpdate, v as readRealtimeWorkspaceDocument, w as compactRealtimeAwareness, x as applyRealtimeAwarenessUpdate, y as workspaceDocument } from "../server-Covbc24E.js";
6
+ import { i as readRealtimeYDoc, n as createRealtimeYDoc, r as diffRealtimeYDoc, t as containsRealtimeState } from "../document-b1ShR2S1.js";
7
+ export { type RealtimeBlobDocumentOptions, type RealtimeBlobStore, type RealtimeBus, type RealtimeBusMessage, RealtimeDocumentConflict, type RealtimeDurableObjectNamespace, RealtimeHandler, RealtimeHandlerOptions, RealtimeRefreshOptions, type RealtimeRoomRecord, type RealtimeRoomRequestOptions, type RealtimeRoomRequestResult, RealtimeSyncOptions, type RealtimeWorkspaceDocumentAdapter, applyRealtimeAwarenessUpdate, applyRealtimeSyncMessage, assertRealtimeRoomStateQuota, bindAwarenessIdentity, blobDocument, claimAwarenessClientIds, compactRealtimeAwareness, containsRealtimeState, createRealtimeHandler, createRealtimeYDoc, diffRealtimeYDoc, encodeAwarenessState, encodeSyncStep1, encodeSyncUpdate, isRealtimeAvailable, isRealtimeDocumentConflict, isRealtimeWorkspaceDocument, readRealtimeRequestState, readRealtimeStatus, readRealtimeWorkspaceDocument, readRealtimeYDoc, realtimeDocumentLayout, realtimePersistenceDefaults, realtimeRoomKey, refreshRealtimeDocument, requestRealtimeRoom, resolveRealtimeDefinition, restoreRealtimeDocument, workspaceDocument };
@@ -0,0 +1,6 @@
1
+ import { l as requestRealtimeRoom, m as isRealtimeAvailable } from "../request-kM5epeSd.js";
2
+ import { n as realtimeDocumentLayout, t as readRealtimeStatus } from "../layout-DNaxppTZ.js";
3
+ import { n as isRealtimeDocumentConflict, t as RealtimeDocumentConflict } from "../conflict-DYC_qXzM.js";
4
+ import { _ as applyRealtimeAwarenessUpdate, a as encodeSyncStep1, b as compactRealtimeAwareness, c as realtimeRoomKey, d as restoreRealtimeDocument, f as blobDocument, g as readRealtimeRequestState, h as workspaceDocument, i as encodeAwarenessState, l as refreshRealtimeDocument, m as readRealtimeWorkspaceDocument, n as assertRealtimeRoomStateQuota, o as encodeSyncUpdate, p as isRealtimeWorkspaceDocument, r as createRealtimeHandler, s as realtimePersistenceDefaults, t as applyRealtimeSyncMessage, u as resolveRealtimeDefinition, v as bindAwarenessIdentity, y as claimAwarenessClientIds } from "../server-BSXJP45b.js";
5
+ import { i as diffRealtimeYDoc, n as containsRealtimeState, o as readRealtimeYDoc, r as createRealtimeYDoc } from "../document-U5rzg2vG.js";
6
+ export { RealtimeDocumentConflict, applyRealtimeAwarenessUpdate, applyRealtimeSyncMessage, assertRealtimeRoomStateQuota, bindAwarenessIdentity, blobDocument, claimAwarenessClientIds, compactRealtimeAwareness, containsRealtimeState, createRealtimeHandler, createRealtimeYDoc, diffRealtimeYDoc, encodeAwarenessState, encodeSyncStep1, encodeSyncUpdate, isRealtimeAvailable, isRealtimeDocumentConflict, isRealtimeWorkspaceDocument, readRealtimeRequestState, readRealtimeStatus, readRealtimeWorkspaceDocument, readRealtimeYDoc, realtimeDocumentLayout, realtimePersistenceDefaults, realtimeRoomKey, refreshRealtimeDocument, requestRealtimeRoom, resolveRealtimeDefinition, restoreRealtimeDocument, workspaceDocument };
@@ -1,4 +1,4 @@
1
- import { h as realtimeKeepalivePing } from "./object-CzKyK0hX.js";
1
+ import { h as realtimeKeepalivePing } from "./object-BgPPDLDE.js";
2
2
  import * as time from "lib0/time";
3
3
  //#region src/application-path.ts
4
4
  function resolveRealtimeApplicationPath(path, baseURL = typeof __VITEHUB_APP_BASE_URL__ === "undefined" ? "/" : __VITEHUB_APP_BASE_URL__) {
@@ -1,4 +1,4 @@
1
- import { d as RealtimeDocumentStatus } from "./types-1DVVG5ro.js";
1
+ import { d as RealtimeDocumentStatus } from "./types-DdY7Vo1H.js";
2
2
  import * as Y from "yjs";
3
3
  //#region src/layout.d.ts
4
4
  /**
@@ -0,0 +1,159 @@
1
+ //#region ../internal/src/runtime/request-log.ts
2
+ function isRequestLog(value) {
3
+ return value !== null && typeof value === "object" && typeof Reflect.get(value, "set") === "function" && typeof Reflect.get(value, "warn") === "function" && typeof Reflect.get(value, "error") === "function";
4
+ }
5
+ function contextLog(value) {
6
+ const context = value !== null && typeof value === "object" ? Reflect.get(value, "context") : void 0;
7
+ return context !== null && typeof context === "object" ? Reflect.get(context, "log") : void 0;
8
+ }
9
+ /**
10
+ * The evlog request logger of a request, or `undefined` when the app does not register evlog. Pass a Nitro v3 event
11
+ * (evlog sets `event.req.context.log`) or its `Request` (`request.context.log`).
12
+ */
13
+ function requestLog(input) {
14
+ const log = contextLog(input !== null && typeof input === "object" ? Reflect.get(input, "req") : void 0) ?? contextLog(input);
15
+ return isRequestLog(log) ? log : void 0;
16
+ }
17
+ /**
18
+ * The `Symbol.for()` key of the bridge on `globalThis`. Every bundled copy of this module and the Nitro plugin that
19
+ * `vitehub()` generates use the same key.
20
+ */
21
+ const evlogBridgeKey = "vitehub.evlog";
22
+ function isEvlogBridge(value) {
23
+ return value !== null && typeof value === "object" && typeof Reflect.get(value, "createLogger") === "function";
24
+ }
25
+ /** The evlog bridge of this process, or `undefined` when the app does not register evlog. */
26
+ function evlogBridge() {
27
+ const bridge = globalThis[Symbol.for(evlogBridgeKey)];
28
+ return isEvlogBridge(bridge) ? bridge : void 0;
29
+ }
30
+ function activeBridge() {
31
+ const bridge = evlogBridge();
32
+ try {
33
+ return bridge && bridge.isEnabled?.() !== false ? bridge : void 0;
34
+ } catch {
35
+ return;
36
+ }
37
+ }
38
+ /**
39
+ * A short name for the cause of a failure, safe to put in a log field: the string `code` of the error, such as
40
+ * `ENOSPC` or a ViteHub diagnostic code, else its class name. Never the message, which can hold paths or values.
41
+ */
42
+ function errorCode(error) {
43
+ if (error === null || typeof error !== "object") return void 0;
44
+ const code = Reflect.get(error, "code");
45
+ if (typeof code === "string" && code) return code;
46
+ const name = Reflect.get(error, "name");
47
+ return typeof name === "string" && name && name !== "Error" ? name : void 0;
48
+ }
49
+ /**
50
+ * Emits one evlog event of its own, outside a request's wide event. The event also reaches the app's `evlog:drain`
51
+ * hooks. Returns `false` when the app does not register evlog or turned it off, and never throws.
52
+ */
53
+ function emitLogEvent(event) {
54
+ const bridge = activeBridge();
55
+ if (!bridge) return false;
56
+ try {
57
+ const log = bridge.createLogger({
58
+ action: event.field,
59
+ message: event.message,
60
+ [event.field]: event.fields
61
+ }, event.waitUntil ? { waitUntil: event.waitUntil } : void 0);
62
+ if (event.level === "error") log.error(event.withError && event.error instanceof Error ? event.error : event.message);
63
+ else log[event.level](event.message);
64
+ const emitted = log.emit();
65
+ if (emitted && bridge.drain) {
66
+ const drained = Promise.resolve().then(() => bridge.drain?.(emitted)).catch((error) => {
67
+ console.error("[evlog] drain failed:", error);
68
+ });
69
+ event.waitUntil?.(drained);
70
+ }
71
+ return true;
72
+ } catch {
73
+ return false;
74
+ }
75
+ }
76
+ /**
77
+ * Logs a failure that the caller survives, with a fixed message and the `code` of the error. With the evlog request
78
+ * logger of `request`, it replaces `[field]` on the request's wide event and adds a warning. Pass `request` only when the
79
+ * request has not answered yet: evlog drops fields after it emits the wide event. Otherwise it emits an event of its
80
+ * own. Without evlog, or with evlog off, it calls `fallback`, which prints to the server console as before. It never
81
+ * throws.
82
+ */
83
+ function logFailure(failure, fallback) {
84
+ const code = errorCode(failure.error);
85
+ const fields = {
86
+ ...failure.fields,
87
+ ...code ? { code } : {}
88
+ };
89
+ const log = failure.request !== void 0 && activeBridge() ? requestLog(failure.request) : void 0;
90
+ if (log) try {
91
+ log.set({ [failure.field]: false });
92
+ log.warn(failure.message, { [failure.field]: fields });
93
+ return;
94
+ } catch {}
95
+ if (emitLogEvent({
96
+ ...failure,
97
+ fields,
98
+ level: failure.level ?? "warn"
99
+ })) return;
100
+ try {
101
+ fallback();
102
+ } catch {}
103
+ }
104
+ //#endregion
105
+ //#region src/log.ts
106
+ /**
107
+ * The label of a room in logs. A `document: false` room id can be a random token that lets a person join, such as a
108
+ * page id, so the log gets a short hash of it. A cryptographic hash adds nothing here: a short or guessable id can be
109
+ * found from any hash by trying values.
110
+ */
111
+ function realtimeRoomLabel(documentId, presence) {
112
+ if (!presence) return documentId;
113
+ let hash = 2166136261;
114
+ for (let index = 0; index < documentId.length; index++) {
115
+ hash ^= documentId.charCodeAt(index);
116
+ hash = Math.imul(hash, 16777619);
117
+ }
118
+ return `#${(hash >>> 0).toString(16).padStart(8, "0")}`;
119
+ }
120
+ /** The definition and room of a room key from `realtimeRoomKey()`. Workspace event rooms have no room id. */
121
+ function realtimeRoomFields(key, presence) {
122
+ let parts;
123
+ try {
124
+ parts = JSON.parse(key);
125
+ } catch {
126
+ return {};
127
+ }
128
+ if (!Array.isArray(parts)) return {};
129
+ const [definition, documentId] = parts;
130
+ return {
131
+ ...typeof definition === "string" ? { definition } : {},
132
+ ...typeof documentId === "string" ? { room: realtimeRoomLabel(documentId, presence) } : {}
133
+ };
134
+ }
135
+ /**
136
+ * Logs one Realtime failure as its own evlog event, `{ action: "realtime", message, realtime: fields }`, when the app
137
+ * registers evlog and has it on. The event has the fixed `message` and the error's `code`, never its message. Otherwise
138
+ * it prints to the console, with the error unless the room is private. It never throws: it also runs inside a Durable
139
+ * Object and in socket callbacks, which have no `waitUntil()`.
140
+ */
141
+ function logRealtime(level, message, fields, error, options = {}) {
142
+ logFailure({
143
+ field: "realtime",
144
+ fields: { ...fields },
145
+ level,
146
+ message,
147
+ ...error === void 0 ? {} : { error }
148
+ }, () => {
149
+ if (options.console === false) return;
150
+ const text = `[vitehub] ${message}`;
151
+ if (options.privateRoom) {
152
+ if (level === "error") console.error(text, fields);
153
+ else console.warn(text, fields);
154
+ } else if (level === "error") console.error(text, ...error === void 0 ? [] : [error]);
155
+ else console.warn(text, ...error === void 0 ? [] : [error instanceof Error ? error.message : String(error)]);
156
+ });
157
+ }
158
+ //#endregion
159
+ export { requestLog as i, realtimeRoomFields as n, realtimeRoomLabel as r, logRealtime as t };
@@ -1,4 +1,4 @@
1
- import { t as realtimeErrorDiagnostics } from "./error-diagnostics-B_66irmt.js";
1
+ import { t as realtimeErrorDiagnostics } from "./error-diagnostics-Cd-XWNf7.js";
2
2
  import * as decoding from "lib0/decoding";
3
3
  import * as encoding from "lib0/encoding";
4
4
  import { normalizeSafeWorkspacePath } from "@vite-hub/workspace/internal/path";
@@ -1,5 +1,5 @@
1
- import { t as realtimeErrorDiagnostics } from "./error-diagnostics-B_66irmt.js";
2
- import { n as isRecord } from "./object-CzKyK0hX.js";
1
+ import { t as realtimeErrorDiagnostics } from "./error-diagnostics-Cd-XWNf7.js";
2
+ import { n as isRecord } from "./object-BgPPDLDE.js";
3
3
  //#region src/presence.ts
4
4
  const personColors = [
5
5
  "#E11D48",
@@ -1,4 +1,4 @@
1
- import { k as RealtimeRoomRequestErrorCode } from "./types-1DVVG5ro.js";
1
+ import { j as RealtimeRoomRequestErrorCode } from "./types-DdY7Vo1H.js";
2
2
  import { Result } from "@vite-hub/runtime";
3
3
  //#region src/request.d.ts
4
4
  /** The `$DurableObject` namespace binding that holds Realtime rooms on Cloudflare. */
@@ -1,4 +1,4 @@
1
- import { t as realtimeErrorDiagnostics } from "./error-diagnostics-B_66irmt.js";
1
+ import { t as realtimeErrorDiagnostics } from "./error-diagnostics-Cd-XWNf7.js";
2
2
  import { AsyncLocalStorage } from "node:async_hooks";
3
3
  import availability from "#vitehub/realtime/availability";
4
4
  //#region ../internal/src/runtime/cloudflare-env-lookup.ts
@@ -34,7 +34,7 @@ function isRealtimeAvailable() {
34
34
  }
35
35
  //#endregion
36
36
  //#region src/request.ts
37
- const routePrefix = "/api/_vitehub/realtime/";
37
+ const routePrefix = "/_vitehub/realtime/";
38
38
  /** Marks the request that `requestRealtimeRoom()` sends to a Durable Object. The Worker strips it from client requests. */
39
39
  const internalRequestHeader = "x-vitehub-realtime-request";
40
40
  const defaultRequestTimeout = 3e4;
@@ -80,7 +80,6 @@ function localRequesters() {
80
80
  requestScope[localRequestKey] ??= /* @__PURE__ */ new Set();
81
81
  return requestScope[localRequestKey];
82
82
  }
83
- /** The parameters each request error was made with, so a Durable Object can send the same error to the Worker. */
84
83
  const roomRequestErrorParams = /* @__PURE__ */ new WeakMap();
85
84
  function roomRequestError(code, params = {}) {
86
85
  const result = createRoomRequestError(code, params);