@vite-hub/realtime 0.0.4 → 0.1.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 +24 -10
- package/dist/application-path-CJblgbWK.js +6 -0
- package/dist/client.d.ts +46 -0
- package/dist/client.js +142 -0
- package/dist/conflict-CmZizTYl.d.ts +17 -0
- package/dist/conflict-rguoaHlE.js +34 -0
- package/dist/document-BFaqcCAL.js +2029 -0
- package/dist/document-CtY6umWY.d.ts +16 -0
- package/dist/durable-key-tnSw58ZK.js +8 -0
- package/dist/error-diagnostics-Cvm6vRXQ.js +73 -0
- package/dist/index.d.ts +17 -4
- package/dist/index.js +3 -1
- package/dist/layout-Br1aOL7o.js +153 -0
- package/dist/layout-WNWejwOY.d.ts +18 -0
- package/dist/protocol-9ZDLEcSS.js +146 -0
- package/dist/server.d.ts +95 -26
- package/dist/server.js +1596 -497
- package/dist/types-Bw6-42Se.d.ts +215 -0
- package/dist/vite.d.ts +80 -6
- package/dist/vite.js +582 -38
- package/dist/vue.d.ts +67 -3808
- package/dist/vue.js +338 -64
- package/package.json +61 -32
- package/dist/editor-extensions-CZEzrJ-t.js +0 -4860
- package/dist/types-BSDtKw5d.d.ts +0 -42
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { l as RealtimeEditor, s as RealtimeDocumentSnapshot } from "./types-Bw6-42Se.js";
|
|
2
|
+
import "./layout-WNWejwOY.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 };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region src/durable-key.ts
|
|
2
|
+
/**
|
|
3
|
+
* The `Symbol.for` key of the global slot that holds the isolate's Durable Object for Realtime. The server reads it, and
|
|
4
|
+
* the generated Nitro plugin writes it without importing the server.
|
|
5
|
+
*/
|
|
6
|
+
const realtimeDurableObjectKey = "vitehub.realtime.durable-object";
|
|
7
|
+
//#endregion
|
|
8
|
+
export { realtimeDurableObjectKey as t };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { defineErrors } from "@vite-hub/runtime";
|
|
2
|
+
//#region src/error-diagnostics.ts
|
|
3
|
+
const realtimeErrorDiagnostics = /*#__PURE__*/ defineErrors({
|
|
4
|
+
docs: "https://vitehub.dev/docs/realtime/limits-and-errors",
|
|
5
|
+
codes: {
|
|
6
|
+
REALTIME_DISABLED: {
|
|
7
|
+
why: ({ action }) => `Realtime is disabled, so \`${action}()\` cannot run.`,
|
|
8
|
+
fix: "Set `enabled` to `true` on `useRealtimeTiptap()` before you call the method."
|
|
9
|
+
},
|
|
10
|
+
REALTIME_DISCONNECTED: {
|
|
11
|
+
why: ({ action }) => `The realtime document disconnected or changed during \`${action}()\`.`,
|
|
12
|
+
fix: "Call the method again after the new document connects. Read `synced` to know when it is ready."
|
|
13
|
+
},
|
|
14
|
+
REALTIME_INVALID_RESPONSE: {
|
|
15
|
+
why: ({ action }) => `The realtime server answered \`${action}()\` with a response that the client cannot use.`,
|
|
16
|
+
fix: "Use the same `@vite-hub/realtime` version on the client and the server, and build again."
|
|
17
|
+
},
|
|
18
|
+
REALTIME_MISSING_DOCUMENT: {
|
|
19
|
+
why: ({ action }) => `\`${action}()\` needs a document, and \`documentId\` is empty.`,
|
|
20
|
+
fix: "Pass a document path to `useRealtimeTiptap(definition, documentId)` before you call the method."
|
|
21
|
+
},
|
|
22
|
+
REALTIME_REQUEST_FAILED: {
|
|
23
|
+
why: ({ action, message, status }) => `The realtime \`${action}()\` request failed${status === void 0 ? "" : ` with HTTP ${status}`}${message ? `: ${message}` : "."}`,
|
|
24
|
+
fix: "Read `error.why` for the server message, and `error.cause` for a network error. Retry when the server is available again."
|
|
25
|
+
},
|
|
26
|
+
REALTIME_AWARENESS_OWNERSHIP_CONFLICT: {
|
|
27
|
+
why: "A realtime awareness client id belongs to another peer.",
|
|
28
|
+
fix: "Let each editor tab use its own Yjs awareness client. Do not reuse a client id on two connections."
|
|
29
|
+
},
|
|
30
|
+
REALTIME_AWARENESS_TOO_LARGE: {
|
|
31
|
+
why: "Realtime awareness state exceeds the 8 MiB room quota.",
|
|
32
|
+
fix: "Keep less data in awareness state. Store large values in the document or in Workspace."
|
|
33
|
+
},
|
|
34
|
+
REALTIME_DOCUMENT_TOO_LARGE: {
|
|
35
|
+
why: "The realtime document exceeds the 8 MiB room quota.",
|
|
36
|
+
fix: "Split the document into smaller documents, or keep large assets in Workspace or Blob."
|
|
37
|
+
},
|
|
38
|
+
REALTIME_INVALID_AWARENESS: {
|
|
39
|
+
why: "A realtime awareness update has a state that is not a plain object.",
|
|
40
|
+
fix: "Set awareness state to a plain object, for example `awareness.setLocalStateField('user', { name })`."
|
|
41
|
+
},
|
|
42
|
+
REALTIME_TOO_MANY_AWARENESS_CLIENTS: {
|
|
43
|
+
why: "One realtime peer sent more than 1,024 awareness clients.",
|
|
44
|
+
fix: "Use one awareness client for each editor tab."
|
|
45
|
+
},
|
|
46
|
+
REALTIME_DOCUMENT_CONFLICT: {
|
|
47
|
+
why: ({ problem }) => problem ?? "The stored document changed since the realtime room loaded it.",
|
|
48
|
+
fix: "Realtime re-seeds the room from the stored document. Listen to `onReset()` to recover text that the room did not save."
|
|
49
|
+
},
|
|
50
|
+
REALTIME_INVALID_PERSISTENCE: {
|
|
51
|
+
why: ({ name, reason }) => reason === "below-debounce" ? "Realtime `persistence.maxWait` must be at least `persistence.debounce`." : `Realtime \`persistence.${name}\` must be a non-negative number of milliseconds.`,
|
|
52
|
+
fix: "Set `persistence: { debounce: 2_000, maxWait: 10_000 }`, or remove the option to use these defaults."
|
|
53
|
+
},
|
|
54
|
+
REALTIME_INVALID_USER: {
|
|
55
|
+
why: "The realtime identity needs a user `id` that is a non-empty string of at most 256 characters.",
|
|
56
|
+
fix: "Return a user with an `id` from the Auth session or from `authorize()`."
|
|
57
|
+
},
|
|
58
|
+
REALTIME_MISSING_DIGEST: {
|
|
59
|
+
why: ({ operation }) => operation === "save" ? "Realtime saves need Workspace file digests, and the Workspace Store did not return one." : "Realtime checkpoints need a stored document, and the save returned no digest.",
|
|
60
|
+
fix: "Use a Workspace Store that returns file digests, or return `digest` from the document adapter `save()`."
|
|
61
|
+
},
|
|
62
|
+
REALTIME_AUTHORITY_CONFLICT: {
|
|
63
|
+
why: ({ authority, preset }) => authority === "memory" ? `Realtime authority "memory" is single-process only, and the ${preset} deployment preset runs more than one process.` : `Realtime authority "cloudflare" does not work with the ${preset} deployment preset.`,
|
|
64
|
+
fix: "Set `realtime.authority` to \"cloudflare\" with a Cloudflare preset, or to \"memory\" for one Node.js process."
|
|
65
|
+
},
|
|
66
|
+
REALTIME_MISSING_AUTHORITY: {
|
|
67
|
+
why: "Realtime production builds need one room authority, and ViteHub cannot select one for this deployment.",
|
|
68
|
+
fix: "Deploy with a Cloudflare preset to use Durable Objects, or set `realtime.authority` to \"memory\" for one Node.js process."
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
//#endregion
|
|
73
|
+
export { realtimeErrorDiagnostics as t };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { C as RealtimeSaveOptions, D as RealtimeVerifyResult, E as RealtimeUser, O as RealtimeWorkspaceChange, S as RealtimeResetReason, T as RealtimeSaved, _ as RealtimePerson, a as RealtimeDocumentAdapter, b as RealtimeRequestContext, c as RealtimeDocumentStatus, d as RealtimeHistoryOptions, f as RealtimeHoldOptions, g as RealtimePersistenceOptions, h as RealtimeModuleOptions, i as RealtimeDefinition, k as RealtimeWorkspaceDocumentOptions, l as RealtimeEditor, m as RealtimeLoadedDocument, n as RealtimeAuthorizeContext, o as RealtimeDocumentContext, p as RealtimeIssue, r as RealtimeCheckpoint, s as RealtimeDocumentSnapshot, u as RealtimeFieldValue, v as RealtimeReadOnlyReason, w as RealtimeSaveResult, x as RealtimeRequestErrorCode } from "./types-Bw6-42Se.js";
|
|
2
|
+
import { n as isRealtimeDocumentConflict, t as RealtimeDocumentConflict } from "./conflict-CmZizTYl.js";
|
|
3
|
+
import { n as realtimeDocumentLayout, t as readRealtimeStatus } from "./layout-WNWejwOY.js";
|
|
3
4
|
//#region src/definition.d.ts
|
|
4
|
-
declare function defineRealtime(definition: RealtimeDefinition): RealtimeDefinition;
|
|
5
|
+
export declare function defineRealtime(definition: RealtimeDefinition): RealtimeDefinition;
|
|
5
6
|
//#endregion
|
|
6
|
-
|
|
7
|
+
//#region src/editor.d.ts
|
|
8
|
+
export declare function createRealtimeEditorExtensions(options?: {
|
|
9
|
+
collaborative?: boolean;
|
|
10
|
+
}): (import("@tiptap/core").Extension<import("@tiptap/starter-kit").StarterKitOptions, any> | import("@tiptap/core").Node<any, any> | import("@tiptap/core").Node<import("@tiptap/extension-image").ImageOptions, any> | import("@tiptap/core").Extension<import("@tiptap/extension-table").TableKitOptions, any> | import("@tiptap/core").Extension<import("@tiptap/markdown").MarkdownExtensionOptions, import("@tiptap/markdown").MarkdownExtensionStorage>)[];
|
|
11
|
+
/** Declares an editor schema and Markdown codec that the server and the browser share. */
|
|
12
|
+
export declare function defineRealtimeEditor(editor: RealtimeEditor): RealtimeEditor;
|
|
13
|
+
/**
|
|
14
|
+
* The built-in Markdown kit: StarterKit, images, tables, and raw YAML frontmatter as an
|
|
15
|
+
* atom node, parsed and printed by `@tiptap/markdown`. It runs headless on the server.
|
|
16
|
+
*/
|
|
17
|
+
export declare const realtimeMarkdownEditor: RealtimeEditor;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { type RealtimeAuthorizeContext, type RealtimeCheckpoint, type RealtimeDefinition, type RealtimeDocumentAdapter, RealtimeDocumentConflict, type RealtimeDocumentContext, type RealtimeDocumentSnapshot, type RealtimeDocumentStatus, type RealtimeEditor, type RealtimeFieldValue, type RealtimeHistoryOptions, type RealtimeHoldOptions, type RealtimeIssue, type RealtimeLoadedDocument, type RealtimeModuleOptions, type RealtimePersistenceOptions, type RealtimePerson, type RealtimeReadOnlyReason, type RealtimeRequestContext, type RealtimeRequestErrorCode, type RealtimeResetReason, type RealtimeSaveOptions, type RealtimeSaveResult, type RealtimeSaved, type RealtimeUser, type RealtimeVerifyResult, type RealtimeWorkspaceChange, type RealtimeWorkspaceDocumentOptions, isRealtimeDocumentConflict, readRealtimeStatus, realtimeDocumentLayout };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { n as isRealtimeDocumentConflict, t as RealtimeDocumentConflict } from "./conflict-rguoaHlE.js";
|
|
2
|
+
import { a as realtimeMarkdownEditor, i as defineRealtimeEditor, n as realtimeDocumentLayout, r as createRealtimeEditorExtensions, t as readRealtimeStatus } from "./layout-Br1aOL7o.js";
|
|
1
3
|
//#region src/definition.ts
|
|
2
4
|
function defineRealtime(definition) {
|
|
3
5
|
return definition;
|
|
4
6
|
}
|
|
5
7
|
//#endregion
|
|
6
|
-
export { defineRealtime };
|
|
8
|
+
export { RealtimeDocumentConflict, createRealtimeEditorExtensions, defineRealtime, defineRealtimeEditor, isRealtimeDocumentConflict, readRealtimeStatus, realtimeDocumentLayout, realtimeMarkdownEditor };
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { Editor, Node } from "@tiptap/core";
|
|
2
|
+
import Image from "@tiptap/extension-image";
|
|
3
|
+
import Document from "@tiptap/extension-document";
|
|
4
|
+
import { TableKit } from "@tiptap/extension-table";
|
|
5
|
+
import { Markdown } from "@tiptap/markdown";
|
|
6
|
+
import StarterKit from "@tiptap/starter-kit";
|
|
7
|
+
import { Marked } from "marked";
|
|
8
|
+
//#region src/frontmatter.ts
|
|
9
|
+
const frontmatterPattern = /^---\r?\n(?:---(?:\r?\n|$)|([\s\S]*?)\r?\n---(?:\r?\n|$))/;
|
|
10
|
+
const Frontmatter = Node.create({
|
|
11
|
+
name: "frontmatter",
|
|
12
|
+
priority: 1e3,
|
|
13
|
+
atom: true,
|
|
14
|
+
selectable: false,
|
|
15
|
+
addAttributes() {
|
|
16
|
+
return { value: { default: null } };
|
|
17
|
+
},
|
|
18
|
+
parseHTML() {
|
|
19
|
+
return [{
|
|
20
|
+
tag: "pre[data-frontmatter]",
|
|
21
|
+
getAttrs: (element) => ({ value: element.hasAttribute("data-frontmatter-empty") ? null : element.getAttribute("data-frontmatter-value") || "" })
|
|
22
|
+
}];
|
|
23
|
+
},
|
|
24
|
+
renderHTML({ node }) {
|
|
25
|
+
return [
|
|
26
|
+
"pre",
|
|
27
|
+
{
|
|
28
|
+
"data-frontmatter": "",
|
|
29
|
+
"data-frontmatter-empty": node.attrs.value === null ? "" : void 0,
|
|
30
|
+
"data-frontmatter-value": node.attrs.value ?? void 0,
|
|
31
|
+
contenteditable: "false"
|
|
32
|
+
},
|
|
33
|
+
node.attrs.value ?? ""
|
|
34
|
+
];
|
|
35
|
+
},
|
|
36
|
+
markdownTokenName: "frontmatter",
|
|
37
|
+
markdownTokenizer: {
|
|
38
|
+
name: "frontmatter",
|
|
39
|
+
level: "block",
|
|
40
|
+
start: (src) => frontmatterPattern.test(src) ? 0 : -1,
|
|
41
|
+
tokenize(src, tokens) {
|
|
42
|
+
if (tokens.length) return;
|
|
43
|
+
const match = frontmatterPattern.exec(src);
|
|
44
|
+
if (!match) return;
|
|
45
|
+
return {
|
|
46
|
+
type: "frontmatter",
|
|
47
|
+
raw: match[0],
|
|
48
|
+
text: match[1]
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
parseMarkdown(token, helpers) {
|
|
53
|
+
return helpers.createNode("frontmatter", { value: token.text ?? null });
|
|
54
|
+
},
|
|
55
|
+
renderMarkdown(node) {
|
|
56
|
+
return node.attrs?.value === null ? "---\n---" : `---\n${node.attrs?.value || ""}\n---`;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/editor.ts
|
|
61
|
+
const RealtimeDocument = Document.extend({ content: "frontmatter? block+" });
|
|
62
|
+
const emptyRealtimeEditorDocument = {
|
|
63
|
+
type: "doc",
|
|
64
|
+
content: [{ type: "paragraph" }]
|
|
65
|
+
};
|
|
66
|
+
function createMarked() {
|
|
67
|
+
return new Marked();
|
|
68
|
+
}
|
|
69
|
+
function createRealtimeEditorExtensions(options = {}) {
|
|
70
|
+
return [
|
|
71
|
+
StarterKit.configure({
|
|
72
|
+
document: false,
|
|
73
|
+
trailingNode: options.collaborative === true ? false : void 0,
|
|
74
|
+
undoRedo: false
|
|
75
|
+
}),
|
|
76
|
+
RealtimeDocument,
|
|
77
|
+
Image,
|
|
78
|
+
TableKit,
|
|
79
|
+
Frontmatter,
|
|
80
|
+
Markdown.configure({ marked: createMarked() })
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
/** Declares an editor schema and Markdown codec that the server and the browser share. */
|
|
84
|
+
function defineRealtimeEditor(editor) {
|
|
85
|
+
return editor;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The built-in Markdown kit: StarterKit, images, tables, and raw YAML frontmatter as an
|
|
89
|
+
* atom node, parsed and printed by `@tiptap/markdown`. It runs headless on the server.
|
|
90
|
+
*/
|
|
91
|
+
const realtimeMarkdownEditor = defineRealtimeEditor({
|
|
92
|
+
extensions: createRealtimeEditorExtensions,
|
|
93
|
+
fromMarkdown(markdown) {
|
|
94
|
+
const editor = new Editor({
|
|
95
|
+
content: markdown || emptyRealtimeEditorDocument,
|
|
96
|
+
contentType: "markdown",
|
|
97
|
+
extensions: createRealtimeEditorExtensions(),
|
|
98
|
+
injectCSS: false
|
|
99
|
+
});
|
|
100
|
+
try {
|
|
101
|
+
return editor.getJSON();
|
|
102
|
+
} finally {
|
|
103
|
+
editor.destroy();
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
toMarkdown(document) {
|
|
107
|
+
const editor = new Editor({
|
|
108
|
+
content: document,
|
|
109
|
+
extensions: createRealtimeEditorExtensions(),
|
|
110
|
+
injectCSS: false
|
|
111
|
+
});
|
|
112
|
+
try {
|
|
113
|
+
return editor.getMarkdown();
|
|
114
|
+
} finally {
|
|
115
|
+
editor.destroy();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region src/layout.ts
|
|
121
|
+
/**
|
|
122
|
+
* Root types of every Realtime Y.Doc:
|
|
123
|
+
* - `body`: Y.XmlFragment bound to the editor through TipTap Collaboration.
|
|
124
|
+
* - `fields`: Y.Map with one JSON value per field; concurrent writes to one key resolve last-write-wins.
|
|
125
|
+
* - `status`: Y.Map written only by the server (`digest`, `error`, `issues`, `savedAt`, `savedBy`).
|
|
126
|
+
* The server closes connections whose updates change it.
|
|
127
|
+
*/
|
|
128
|
+
const realtimeDocumentLayout = Object.freeze({
|
|
129
|
+
body: "body",
|
|
130
|
+
fields: "fields",
|
|
131
|
+
status: "status"
|
|
132
|
+
});
|
|
133
|
+
function text(value) {
|
|
134
|
+
return typeof value === "string" ? value : null;
|
|
135
|
+
}
|
|
136
|
+
function readRealtimeStatus(document) {
|
|
137
|
+
const map = document.getMap(realtimeDocumentLayout.status);
|
|
138
|
+
const issues = map.get("issues");
|
|
139
|
+
const savedBy = map.get("savedBy");
|
|
140
|
+
const status = {
|
|
141
|
+
issues: Array.isArray(issues) ? issues : [],
|
|
142
|
+
savedBy: savedBy && !Array.isArray(savedBy) && savedBy instanceof Object ? savedBy : null
|
|
143
|
+
};
|
|
144
|
+
return {
|
|
145
|
+
digest: text(map.get("digest")),
|
|
146
|
+
error: text(map.get("error")),
|
|
147
|
+
issues: status.issues,
|
|
148
|
+
savedAt: text(map.get("savedAt")),
|
|
149
|
+
savedBy: status.savedBy
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
//#endregion
|
|
153
|
+
export { realtimeMarkdownEditor as a, defineRealtimeEditor as i, realtimeDocumentLayout as n, createRealtimeEditorExtensions as r, readRealtimeStatus as t };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { c as RealtimeDocumentStatus } from "./types-Bw6-42Se.js";
|
|
2
|
+
import * as Y from "yjs";
|
|
3
|
+
//#region src/layout.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Root types of every Realtime Y.Doc:
|
|
6
|
+
* - `body`: Y.XmlFragment bound to the editor through TipTap Collaboration.
|
|
7
|
+
* - `fields`: Y.Map with one JSON value per field; concurrent writes to one key resolve last-write-wins.
|
|
8
|
+
* - `status`: Y.Map written only by the server (`digest`, `error`, `issues`, `savedAt`, `savedBy`).
|
|
9
|
+
* The server closes connections whose updates change it.
|
|
10
|
+
*/
|
|
11
|
+
declare const realtimeDocumentLayout: Readonly<{
|
|
12
|
+
readonly body: "body";
|
|
13
|
+
readonly fields: "fields";
|
|
14
|
+
readonly status: "status";
|
|
15
|
+
}>;
|
|
16
|
+
declare function readRealtimeStatus(document: Y.Doc): RealtimeDocumentStatus;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { realtimeDocumentLayout as n, readRealtimeStatus as t };
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { t as realtimeErrorDiagnostics } from "./error-diagnostics-Cvm6vRXQ.js";
|
|
2
|
+
import * as decoding from "lib0/decoding";
|
|
3
|
+
import * as encoding from "lib0/encoding";
|
|
4
|
+
import { normalizeSafeWorkspacePath } from "@vite-hub/workspace/internal/path";
|
|
5
|
+
//#region ../internal/src/object.ts
|
|
6
|
+
function isPlainObject(value) {
|
|
7
|
+
if (value === null || Object(value) !== value || Array.isArray(value)) return false;
|
|
8
|
+
return typeof value === "object";
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/presence.ts
|
|
12
|
+
const personColors = [
|
|
13
|
+
"#E11D48",
|
|
14
|
+
"#D97706",
|
|
15
|
+
"#059669",
|
|
16
|
+
"#0891B2",
|
|
17
|
+
"#2563EB",
|
|
18
|
+
"#7C3AED",
|
|
19
|
+
"#C026D3"
|
|
20
|
+
];
|
|
21
|
+
function isString(value) {
|
|
22
|
+
try {
|
|
23
|
+
return String(value) === value;
|
|
24
|
+
} catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function createRealtimeIdentity(user) {
|
|
29
|
+
if (!isString(user.id) || !user.id || user.id.length > 256) throw realtimeErrorDiagnostics.REALTIME_INVALID_USER();
|
|
30
|
+
let hash = 0;
|
|
31
|
+
for (let index = 0; index < user.id.length; index++) hash = Math.imul(31, hash) + user.id.charCodeAt(index) | 0;
|
|
32
|
+
const name = (isString(user.name) && user.name || isString(user.email) && user.email || "Anonymous").slice(0, 256);
|
|
33
|
+
const identity = {
|
|
34
|
+
color: personColors[Math.abs(hash) % personColors.length],
|
|
35
|
+
id: user.id,
|
|
36
|
+
name
|
|
37
|
+
};
|
|
38
|
+
if (isString(user.image) && user.image && user.image.length <= 2048) identity.image = user.image;
|
|
39
|
+
return identity;
|
|
40
|
+
}
|
|
41
|
+
function isRealtimeIdentity(value) {
|
|
42
|
+
if (!isPlainObject(value)) return false;
|
|
43
|
+
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);
|
|
44
|
+
}
|
|
45
|
+
function getRealtimePeople(states) {
|
|
46
|
+
const people = [];
|
|
47
|
+
for (const [clientId, state] of states) if (isRealtimeIdentity(state.user)) {
|
|
48
|
+
const { color, id, image, name } = state.user;
|
|
49
|
+
const person = {
|
|
50
|
+
color,
|
|
51
|
+
id,
|
|
52
|
+
name,
|
|
53
|
+
clientId
|
|
54
|
+
};
|
|
55
|
+
if (image !== void 0) person.image = image;
|
|
56
|
+
people.push(person);
|
|
57
|
+
}
|
|
58
|
+
return people;
|
|
59
|
+
}
|
|
60
|
+
const workspaceRoomId = "@workspace";
|
|
61
|
+
const maxAwarenessClients = 1024;
|
|
62
|
+
const realtimeSyncPendingCode = "REALTIME_SYNC_PENDING";
|
|
63
|
+
const realtimeSourceChangedCode = "REALTIME_SOURCE_CHANGED";
|
|
64
|
+
/** A save or checkpoint asked a room whose codec cannot edit the stored document without loss. */
|
|
65
|
+
const realtimeReadOnlyCode = "REALTIME_READ_ONLY";
|
|
66
|
+
/** Close code sent after a reset control frame. The client must drop its Y.Doc. */
|
|
67
|
+
const realtimeResetCloseCode = 4409;
|
|
68
|
+
/** Close code for updates a peer may not write: read-only access or the server-written status. */
|
|
69
|
+
const realtimeForbiddenWriteCloseCode = 4403;
|
|
70
|
+
function isRetryableRealtimeRequestCode(value) {
|
|
71
|
+
return value === realtimeSyncPendingCode;
|
|
72
|
+
}
|
|
73
|
+
function encodeRealtimeControl(message) {
|
|
74
|
+
const encoder = encoding.createEncoder();
|
|
75
|
+
encoding.writeVarUint(encoder, 5);
|
|
76
|
+
encoding.writeVarString(encoder, JSON.stringify(message));
|
|
77
|
+
return encoding.toUint8Array(encoder);
|
|
78
|
+
}
|
|
79
|
+
function decodeRealtimeControlPayload(decoder) {
|
|
80
|
+
try {
|
|
81
|
+
const value = JSON.parse(decoding.readVarString(decoder));
|
|
82
|
+
if (!value || typeof value !== "object" || !("epoch" in value) || !("type" in value)) return;
|
|
83
|
+
if (typeof value.epoch !== "string" || !value.epoch) return;
|
|
84
|
+
if (value.type === "session" && "access" in value && (value.access === "read" || value.access === "write")) {
|
|
85
|
+
if (value.access === "read" && "reason" in value && (value.reason === "access" || value.reason === "document")) return {
|
|
86
|
+
type: "session",
|
|
87
|
+
access: value.access,
|
|
88
|
+
epoch: value.epoch,
|
|
89
|
+
reason: value.reason
|
|
90
|
+
};
|
|
91
|
+
return {
|
|
92
|
+
type: "session",
|
|
93
|
+
access: value.access,
|
|
94
|
+
epoch: value.epoch
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
if (value.type === "reset" && "reason" in value && (value.reason === "source-changed" || value.reason === "stale")) return {
|
|
98
|
+
type: "reset",
|
|
99
|
+
epoch: value.epoch,
|
|
100
|
+
reason: value.reason
|
|
101
|
+
};
|
|
102
|
+
} catch {}
|
|
103
|
+
}
|
|
104
|
+
function validPath(value) {
|
|
105
|
+
if (typeof value !== "string" || value.length === 0 || value.length > 4096 || value.includes("\0")) return false;
|
|
106
|
+
if (value.startsWith("/") || value.endsWith("/") || value.includes("\\") || value.includes("//") || /^[a-z]:/i.test(value)) return false;
|
|
107
|
+
try {
|
|
108
|
+
return normalizeSafeWorkspacePath(value) === value;
|
|
109
|
+
} catch {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function decodeWorkspaceChangePayload(decoder) {
|
|
114
|
+
try {
|
|
115
|
+
const value = JSON.parse(decoding.readVarString(decoder));
|
|
116
|
+
if ((value.operation === "create" || value.operation === "delete" || value.operation === "update") && validPath(value.path)) return {
|
|
117
|
+
operation: value.operation,
|
|
118
|
+
path: value.path
|
|
119
|
+
};
|
|
120
|
+
if (value.operation === "move" && validPath(value.from) && validPath(value.to)) return {
|
|
121
|
+
operation: value.operation,
|
|
122
|
+
from: value.from,
|
|
123
|
+
to: value.to
|
|
124
|
+
};
|
|
125
|
+
} catch {}
|
|
126
|
+
}
|
|
127
|
+
function encodeWorkspaceChange(change) {
|
|
128
|
+
const encoder = encoding.createEncoder();
|
|
129
|
+
encoding.writeVarUint(encoder, 4);
|
|
130
|
+
encoding.writeVarString(encoder, JSON.stringify(change));
|
|
131
|
+
return encoding.toUint8Array(encoder);
|
|
132
|
+
}
|
|
133
|
+
function readAwarenessClientIds(update) {
|
|
134
|
+
const decoder = decoding.createDecoder(update);
|
|
135
|
+
const length = decoding.readVarUint(decoder);
|
|
136
|
+
if (length > 1024) throw realtimeErrorDiagnostics.REALTIME_TOO_MANY_AWARENESS_CLIENTS({});
|
|
137
|
+
const clients = [];
|
|
138
|
+
for (let index = 0; index < length; index++) {
|
|
139
|
+
clients.push(decoding.readVarUint(decoder));
|
|
140
|
+
decoding.readVarUint(decoder);
|
|
141
|
+
decoding.readVarString(decoder);
|
|
142
|
+
}
|
|
143
|
+
return clients;
|
|
144
|
+
}
|
|
145
|
+
//#endregion
|
|
146
|
+
export { isRetryableRealtimeRequestCode as a, realtimeForbiddenWriteCloseCode as c, realtimeSourceChangedCode as d, realtimeSyncPendingCode as f, isPlainObject as g, getRealtimePeople as h, encodeWorkspaceChange as i, realtimeReadOnlyCode as l, createRealtimeIdentity as m, decodeWorkspaceChangePayload as n, maxAwarenessClients as o, workspaceRoomId as p, encodeRealtimeControl as r, readAwarenessClientIds as s, decodeRealtimeControlPayload as t, realtimeResetCloseCode as u };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,37 +1,106 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
import
|
|
1
|
+
import { _ as RealtimePerson, a as RealtimeDocumentAdapter, i as RealtimeDefinition, l as RealtimeEditor, o as RealtimeDocumentContext, y as RealtimeRegistry } from "./types-Bw6-42Se.js";
|
|
2
|
+
import { n as isRealtimeDocumentConflict, t as RealtimeDocumentConflict } from "./conflict-CmZizTYl.js";
|
|
3
|
+
import { n as realtimeDocumentLayout, t as readRealtimeStatus } from "./layout-WNWejwOY.js";
|
|
4
|
+
import { i as readRealtimeYDoc, n as createRealtimeYDoc, r as diffRealtimeYDoc, t as containsRealtimeState } from "./document-CtY6umWY.js";
|
|
3
5
|
import * as awarenessProtocol from "y-protocols/awareness";
|
|
6
|
+
import * as Y from "yjs";
|
|
7
|
+
import { Diagnostic } from "nostics";
|
|
4
8
|
import { ReadonlyWorkspaceFacade, WritableWorkspaceFacade } from "@vite-hub/workspace";
|
|
5
|
-
|
|
6
9
|
//#region src/presence.d.ts
|
|
7
10
|
type RealtimeIdentity = Omit<RealtimePerson, "clientId">;
|
|
8
11
|
//#endregion
|
|
12
|
+
//#region src/awareness.d.ts
|
|
13
|
+
export declare function claimAwarenessClientIds<Peer extends object>(owners: Map<number, Peer>, peer: Peer, clients: number[]): number[];
|
|
14
|
+
export declare function bindAwarenessIdentity(update: Uint8Array, identity: RealtimeIdentity): Uint8Array;
|
|
15
|
+
export declare function applyRealtimeAwarenessUpdate(awareness: awarenessProtocol.Awareness, update: Uint8Array, origin: unknown, maxStateBytes?: number): void;
|
|
16
|
+
export declare function compactRealtimeAwareness(awareness: awarenessProtocol.Awareness): awarenessProtocol.Awareness;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/transport.d.ts
|
|
19
|
+
export declare function readRealtimeRequestState(request: Request, maxBytes?: number): Promise<Uint8Array>;
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/workspace-document.d.ts
|
|
22
|
+
interface RealtimeWorkspaceDocumentAdapter extends RealtimeDocumentAdapter {
|
|
23
|
+
kind: "workspace";
|
|
24
|
+
workspace: string;
|
|
25
|
+
}
|
|
26
|
+
export declare function readRealtimeWorkspaceDocument(readable: ReadonlyWorkspaceFacade, writable: WritableWorkspaceFacade, documentId: string): Promise<{
|
|
27
|
+
baselineDigest: string | undefined;
|
|
28
|
+
markdown: string;
|
|
29
|
+
}>;
|
|
30
|
+
/**
|
|
31
|
+
* Stores each Realtime document as the Markdown file at its path in a Workspace working tree.
|
|
32
|
+
* `history.checkpoint()` commits the working tree to Workspace history. `document: { workspace }`
|
|
33
|
+
* in a Realtime Definition uses this adapter.
|
|
34
|
+
*/
|
|
35
|
+
export declare function workspaceDocument(workspaceName: string): RealtimeWorkspaceDocumentAdapter;
|
|
36
|
+
export declare function isRealtimeWorkspaceDocument(adapter: RealtimeDocumentAdapter): adapter is RealtimeWorkspaceDocumentAdapter;
|
|
37
|
+
//#endregion
|
|
9
38
|
//#region src/server.d.ts
|
|
10
|
-
interface RealtimeHandler {
|
|
39
|
+
export interface RealtimeHandler {
|
|
11
40
|
(event: unknown): Promise<unknown>;
|
|
12
41
|
fetch(input: Request | URL | string): Promise<Response>;
|
|
13
42
|
}
|
|
14
|
-
declare
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
declare function claimAwarenessClientIds(owners: Map<number, object>, peer: object, clients: number[]): number[];
|
|
18
|
-
declare function realtimeRoomKey(definitionName: string, documentId: string): string;
|
|
19
|
-
declare function bindAwarenessIdentity(update: Uint8Array, identity: RealtimeIdentity): Uint8Array;
|
|
20
|
-
declare function writeRealtimeDocument(workspace: Pick<WritableWorkspaceFacade, "fs">, documentId: string, document: Y.Doc, ifDigest: string | null): Promise<string>;
|
|
21
|
-
declare function readRealtimeCheckpointState(request: Request, maxBytes?: number): Promise<Uint8Array>;
|
|
22
|
-
declare function encodeSyncUpdate(update: Uint8Array): Uint8Array;
|
|
23
|
-
declare function encodeSyncStep1(document: Y.Doc): Uint8Array;
|
|
24
|
-
declare function matchesRealtimeState(document: Y.Doc, expected: Uint8Array): boolean;
|
|
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;
|
|
43
|
+
export declare const realtimePersistenceDefaults: Readonly<{
|
|
44
|
+
debounce: 2000;
|
|
45
|
+
maxWait: 10000;
|
|
33
46
|
}>;
|
|
34
|
-
declare function
|
|
35
|
-
declare function
|
|
47
|
+
export declare function encodeSyncUpdate(update: Uint8Array): Uint8Array;
|
|
48
|
+
export declare function encodeSyncStep1(document: Y.Doc): Uint8Array;
|
|
49
|
+
export declare function encodeAwarenessState(awareness: awarenessProtocol.Awareness, clients: number[]): Uint8Array;
|
|
50
|
+
/** A peer tried to write what it may not: a read-only connection, or the server-written status map. */
|
|
51
|
+
export declare class RealtimeWriteRejected extends Error {
|
|
52
|
+
constructor(message: string);
|
|
53
|
+
}
|
|
54
|
+
export interface RealtimeSyncOptions {
|
|
55
|
+
maxStateBytes?: number;
|
|
56
|
+
/** Reject any update that changes the document. A string replaces the rejection message. */
|
|
57
|
+
readOnly?: boolean | string;
|
|
58
|
+
/** Reject updates that change the server-written `status` map. Default `true`. */
|
|
59
|
+
protectStatus?: boolean;
|
|
60
|
+
}
|
|
61
|
+
export declare function applyRealtimeSyncMessage(data: Uint8Array, document: Y.Doc, origin: unknown, options?: RealtimeSyncOptions): Uint8Array | undefined;
|
|
62
|
+
export declare function assertRealtimeRoomStateQuota(document: Y.Doc, maxStateBytes?: number): void;
|
|
63
|
+
export declare function realtimeRoomKey(definitionName: string, documentId: string): string;
|
|
64
|
+
export declare function restoreRealtimeDocument(markdown: string, baselineDigest: string | undefined, stored: Record<string, unknown> | undefined): Promise<Y.Doc>;
|
|
65
|
+
interface ResolvedRealtimeDefinition {
|
|
66
|
+
adapter: RealtimeDocumentAdapter;
|
|
67
|
+
definition: RealtimeDefinition;
|
|
68
|
+
/** The codec of one document: the definition's editor, or the one its editor function picks. */
|
|
69
|
+
editor: (context: RealtimeDocumentContext) => RealtimeEditor;
|
|
70
|
+
persistence: {
|
|
71
|
+
debounce: number;
|
|
72
|
+
maxWait: number;
|
|
73
|
+
} | false;
|
|
74
|
+
}
|
|
75
|
+
/** Resolves the document adapter, editor codec, and persistence timing of a definition. */
|
|
76
|
+
export declare function resolveRealtimeDefinition(definition: RealtimeDefinition): ResolvedRealtimeDefinition;
|
|
77
|
+
/** The `$DurableObject` namespace binding that holds Realtime rooms on Cloudflare. */
|
|
78
|
+
export interface RealtimeDurableObjectNamespace {
|
|
79
|
+
get(id: unknown): {
|
|
80
|
+
fetch(request: Request): Promise<Response>;
|
|
81
|
+
};
|
|
82
|
+
idFromName(name: string): unknown;
|
|
83
|
+
}
|
|
84
|
+
export interface RealtimeRefreshOptions {
|
|
85
|
+
/**
|
|
86
|
+
* On Cloudflare, the `$DurableObject` binding that holds the rooms. Defaults to the binding of the running Worker.
|
|
87
|
+
* Without one, the rooms of this process refresh.
|
|
88
|
+
*/
|
|
89
|
+
durableObjects?: RealtimeDurableObjectNamespace;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Tells the open room of a document that its stored copy changed outside the room, such as a save by an Agent or the
|
|
93
|
+
* CLI. A room without unsaved edits re-seeds at once and resets its clients. A room with unsaved edits finds the change
|
|
94
|
+
* at its next save, which also re-seeds it. A room that is not open loads the stored copy when it opens. Resolves
|
|
95
|
+
* whether a room re-seeded. It never throws: a failed refresh leaves the next save to find the change.
|
|
96
|
+
*/
|
|
97
|
+
export declare function refreshRealtimeDocument(definition: string, documentId: string, options?: RealtimeRefreshOptions): Promise<boolean>;
|
|
98
|
+
/**
|
|
99
|
+
* Remembers the Durable Object of this isolate, for the sockets that Nitro 2 upgrades in it: its upgrade hook gets only
|
|
100
|
+
* the request, and the rooms keep their tables in the Durable Object's storage. Realtime's Nitro plugin calls it at
|
|
101
|
+
* `cloudflare:durable:init`.
|
|
102
|
+
*/
|
|
103
|
+
export declare function registerRealtimeDurableObject(state: unknown, env: unknown): void;
|
|
104
|
+
export declare function createRealtimeHandler(registry: RealtimeRegistry): RealtimeHandler;
|
|
36
105
|
//#endregion
|
|
37
|
-
export {
|
|
106
|
+
export { RealtimeDocumentConflict, type RealtimeWorkspaceDocumentAdapter, containsRealtimeState, createRealtimeYDoc, diffRealtimeYDoc, isRealtimeDocumentConflict, readRealtimeStatus, readRealtimeYDoc, realtimeDocumentLayout };
|