@daasboard/doc 0.0.0-stage → 0.1.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/LICENSE +5 -0
- package/README.md +6 -2
- package/dist/DocumentAdapter.d.ts +41 -0
- package/dist/DocumentAdapter.js +34 -0
- package/dist/LoroPageDoc.d.ts +137 -0
- package/dist/LoroPageDoc.js +2662 -0
- package/dist/MemoryDocument.d.ts +42 -0
- package/dist/MemoryDocument.js +868 -0
- package/dist/connectors.d.ts +8 -0
- package/dist/connectors.js +44 -0
- package/dist/imageHashes.d.ts +3 -0
- package/dist/imageHashes.js +61 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +12 -0
- package/dist/normalize.d.ts +50 -0
- package/dist/normalize.js +203 -0
- package/dist/presence/RoomPresence.d.ts +107 -0
- package/dist/presence/RoomPresence.js +515 -0
- package/dist/schema.d.ts +728 -0
- package/dist/schema.js +891 -0
- package/dist/text.d.ts +80 -0
- package/dist/text.js +244 -0
- package/dist/textDelta.d.ts +27 -0
- package/dist/textDelta.js +688 -0
- package/dist/transport/WsTransport.d.ts +178 -0
- package/dist/transport/WsTransport.js +1314 -0
- package/dist/transport/connectRoom.d.ts +47 -0
- package/dist/transport/connectRoom.js +303 -0
- package/dist/transport/prefix.d.ts +4 -0
- package/dist/transport/prefix.js +27 -0
- package/dist/transport/slices.d.ts +9 -0
- package/dist/transport/slices.js +151 -0
- package/dist/transport/snapshot.d.ts +46 -0
- package/dist/transport/snapshot.js +248 -0
- package/dist/transport/token.d.ts +2 -0
- package/dist/transport/token.js +6 -0
- package/package.json +34 -3
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { LoroDoc } from "loro-crdt";
|
|
2
|
+
import { resolveRoomToken } from "./token.js";
|
|
3
|
+
const BUSY = new Set([429, 503]);
|
|
4
|
+
const DEFAULT_RETRIES = 5;
|
|
5
|
+
const BACKOFF_MS = 500;
|
|
6
|
+
const MAX_BACKOFF_MS = 8000;
|
|
7
|
+
const MAX_WAIT_MS = 20_000;
|
|
8
|
+
const MAX_RETRY_AFTER_MS = 60_000;
|
|
9
|
+
const INACTIVITY_MS = 15_000;
|
|
10
|
+
const BASE_MS = 30_000;
|
|
11
|
+
const MIN_BYTES_PER_SECOND = 128 * 1024;
|
|
12
|
+
const MAX_BYTES = 256 * 1024 * 1024;
|
|
13
|
+
export function snapshotUrl(base, room) {
|
|
14
|
+
return `${base.replace(/^ws(s?):/i, "http$1:").replace(/\/$/, "")}/rooms/${encodeURIComponent(room)}/snapshot`;
|
|
15
|
+
}
|
|
16
|
+
export function retryAfterMs(value, now = Date.now()) {
|
|
17
|
+
const v = value?.trim();
|
|
18
|
+
if (!v)
|
|
19
|
+
return 0;
|
|
20
|
+
if (/^\d+$/.test(v))
|
|
21
|
+
return Math.min(MAX_RETRY_AFTER_MS, Number(v) * 1000);
|
|
22
|
+
const at = Date.parse(v);
|
|
23
|
+
return Number.isNaN(at) ? 0 : Math.min(MAX_RETRY_AFTER_MS, Math.max(0, at - now));
|
|
24
|
+
}
|
|
25
|
+
export function frontierText(frontiers) {
|
|
26
|
+
const sorted = [...frontiers].sort((a, b) => {
|
|
27
|
+
const x = BigInt(a.peer);
|
|
28
|
+
const y = BigInt(b.peer);
|
|
29
|
+
return x < y ? -1 : x > y ? 1 : a.counter - b.counter;
|
|
30
|
+
});
|
|
31
|
+
return sorted.map((f) => `${f.peer}:${f.counter}`).join(",") || "empty";
|
|
32
|
+
}
|
|
33
|
+
const U64 = /^\d{1,20}$/;
|
|
34
|
+
const U64_LIMIT = 1n << 64n;
|
|
35
|
+
export async function fetchSnapshot(base, room, opts = {}) {
|
|
36
|
+
const doFetch = opts.fetch ?? globalThis.fetch;
|
|
37
|
+
const maxRetries = opts.maxRetries ?? DEFAULT_RETRIES;
|
|
38
|
+
const maxWaitMs = opts.maxWaitMs ?? MAX_WAIT_MS;
|
|
39
|
+
const limits = { inactivityMs: opts.inactivityMs ?? INACTIVITY_MS, minBytesPerSecond: opts.minBytesPerSecond ?? MIN_BYTES_PER_SECOND };
|
|
40
|
+
const maxBytes = opts.maxBytes ?? MAX_BYTES;
|
|
41
|
+
const random = opts.random ?? Math.random;
|
|
42
|
+
const url = snapshotUrl(base, room);
|
|
43
|
+
const started = performance.now();
|
|
44
|
+
let attempts = 0;
|
|
45
|
+
let waitedMs = 0;
|
|
46
|
+
const none = (reason, retryable = false, retryAfter = 0) => ({ snapshot: null, reason, retryable, retryAfterMs: retryAfter, attempts, waitedMs, fetchMs: performance.now() - started - waitedMs, stageMs: 0 });
|
|
47
|
+
for (;;) {
|
|
48
|
+
attempts++;
|
|
49
|
+
const request = new Bounded(limits, opts.signal);
|
|
50
|
+
try {
|
|
51
|
+
let response;
|
|
52
|
+
try {
|
|
53
|
+
const token = opts.token === undefined ? undefined : await request.race(resolveRoomToken(opts.token, room));
|
|
54
|
+
response = await request.race(doFetch(url, { ...(token === undefined ? {} : { headers: { Authorization: `Bearer ${token}` } }), signal: request.signal, ...(opts.cache ? { cache: opts.cache } : {}) }));
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
if (opts.signal?.aborted)
|
|
58
|
+
throw opts.signal.reason;
|
|
59
|
+
return none(`the snapshot request failed (${request.stalled ? `no response in ${limits.inactivityMs} ms` : (request.slow ?? String(err))})`, true);
|
|
60
|
+
}
|
|
61
|
+
if (BUSY.has(response.status) && attempts <= maxRetries) {
|
|
62
|
+
const after = retryAfterMs(response.headers.get("retry-after"));
|
|
63
|
+
const delayMs = after + random() * Math.min(MAX_BACKOFF_MS, BACKOFF_MS * 2 ** (attempts - 1));
|
|
64
|
+
void response.body?.cancel().catch(() => { });
|
|
65
|
+
if (waitedMs + delayMs > maxWaitMs)
|
|
66
|
+
return none(`the server is busy (${response.status}) and a retry in ${(delayMs / 1000).toFixed(1)} s would wait past the ${(maxWaitMs / 1000).toFixed(0)} s a join waits for a snapshot`, true, after);
|
|
67
|
+
opts.onRetry?.({ attempt: attempts, status: response.status, retryAfterMs: after, delayMs });
|
|
68
|
+
await sleep(delayMs, opts.signal);
|
|
69
|
+
waitedMs += delayMs;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (!response.ok) {
|
|
73
|
+
void response.body?.cancel().catch(() => { });
|
|
74
|
+
if (response.status === 401 || response.status === 403) {
|
|
75
|
+
const detail = response.headers.get("www-authenticate")?.match(/error_description="([^"\r\n]*)"/)?.[1];
|
|
76
|
+
return none(`the snapshot token was ${response.status === 403 ? "forbidden" : "refused"} (${response.status})${detail ? `: ${detail}` : ""}`);
|
|
77
|
+
}
|
|
78
|
+
if (response.status === 404)
|
|
79
|
+
return none("no snapshot is published for the room's current log (404)");
|
|
80
|
+
return none(BUSY.has(response.status) ? `the server stayed busy (${response.status}) through ${maxRetries} retries` : `the server answered ${response.status}`, BUSY.has(response.status) || response.status === 408 || response.status >= 500, retryAfterMs(response.headers.get("retry-after")));
|
|
81
|
+
}
|
|
82
|
+
const head = readHeaders(response.headers);
|
|
83
|
+
if (typeof head === "string") {
|
|
84
|
+
void response.body?.cancel().catch(() => { });
|
|
85
|
+
return none(head);
|
|
86
|
+
}
|
|
87
|
+
const length = Number(response.headers.get("content-length") ?? 0);
|
|
88
|
+
if (length > maxBytes) {
|
|
89
|
+
void response.body?.cancel().catch(() => { });
|
|
90
|
+
return none(`the snapshot is ${length} bytes, more than the ${maxBytes} a join reads (maxBytes)`);
|
|
91
|
+
}
|
|
92
|
+
let body;
|
|
93
|
+
try {
|
|
94
|
+
body = await readBody(response, request, maxBytes);
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
if (opts.signal?.aborted)
|
|
98
|
+
throw opts.signal.reason;
|
|
99
|
+
return none(`the snapshot download failed (${request.stalled ? `no byte in ${limits.inactivityMs} ms` : (request.slow ?? String(err))})`, !(err instanceof SnapshotTooLarge));
|
|
100
|
+
}
|
|
101
|
+
const fetchMs = performance.now() - started - waitedMs;
|
|
102
|
+
const stageStart = performance.now();
|
|
103
|
+
const staged = stage(body, head.frontier);
|
|
104
|
+
const stageMs = performance.now() - stageStart;
|
|
105
|
+
if (typeof staged === "string")
|
|
106
|
+
return { ...none(staged), fetchMs, stageMs };
|
|
107
|
+
return { snapshot: { doc: staged, epoch: head.epoch, seq: head.seq, prefix: head.prefix, frontier: head.frontier, location: head.location, size: body.byteLength }, reason: null, retryable: false, retryAfterMs: 0, attempts, waitedMs, fetchMs, stageMs };
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
request.done();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function readHeaders(h) {
|
|
115
|
+
const format = h.get("x-snapshot-format");
|
|
116
|
+
const epoch = h.get("x-snapshot-epoch");
|
|
117
|
+
const seq = h.get("x-snapshot-seq");
|
|
118
|
+
const frontier = h.get("x-snapshot-frontier");
|
|
119
|
+
const prefix = h.get("x-snapshot-prefix");
|
|
120
|
+
if (format === null || epoch === null || seq === null || frontier === null || prefix === null)
|
|
121
|
+
return "the response lacks the X-Snapshot-* headers (a proxy or CDN that strips them, or a server that does not expose them cross-origin)";
|
|
122
|
+
if (format !== "full")
|
|
123
|
+
return `the snapshot's format is "${format}", not "full"`;
|
|
124
|
+
if (!U64.test(epoch) || BigInt(epoch) === 0n || BigInt(epoch) >= U64_LIMIT)
|
|
125
|
+
return `X-Snapshot-Epoch "${epoch}" is not a nonzero u64`;
|
|
126
|
+
if (!U64.test(seq) || !Number.isSafeInteger(Number(seq)) || Number(seq) < 1)
|
|
127
|
+
return `X-Snapshot-Seq "${seq}" is not a positive integer`;
|
|
128
|
+
if (!/^[0-9a-f]{64}$/.test(prefix))
|
|
129
|
+
return `X-Snapshot-Prefix "${prefix}" is not a lowercase SHA-256 hash`;
|
|
130
|
+
const hash = Uint8Array.from(prefix.match(/../g), (byte) => Number.parseInt(byte, 16));
|
|
131
|
+
return { epoch: BigInt(epoch), seq: Number(seq), prefix: hash, frontier, location: h.get("content-location") };
|
|
132
|
+
}
|
|
133
|
+
function stage(body, frontier) {
|
|
134
|
+
const doc = new LoroDoc();
|
|
135
|
+
const refuse = (reason) => {
|
|
136
|
+
doc.free();
|
|
137
|
+
return reason;
|
|
138
|
+
};
|
|
139
|
+
try {
|
|
140
|
+
const status = doc.import(body);
|
|
141
|
+
if (status.pending)
|
|
142
|
+
return refuse("the snapshot holds updates whose dependencies it lacks");
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
return refuse(`the snapshot does not import (${String(err)})`);
|
|
146
|
+
}
|
|
147
|
+
const trash = doc.getMap("meta").get("trash");
|
|
148
|
+
if (typeof trash !== "string" || trash === "")
|
|
149
|
+
return refuse("the snapshot is not a page document (no trash root)");
|
|
150
|
+
const actual = frontierText(doc.oplogFrontiers());
|
|
151
|
+
if (actual !== frontier)
|
|
152
|
+
return refuse(`the snapshot ends at ${actual}, not at the frontier its headers name (${frontier}): headers and body from different publications`);
|
|
153
|
+
return doc;
|
|
154
|
+
}
|
|
155
|
+
class SnapshotTooLarge extends Error {
|
|
156
|
+
}
|
|
157
|
+
async function readBody(response, request, maxBytes) {
|
|
158
|
+
const tooLarge = () => new SnapshotTooLarge(`the body passed ${maxBytes} bytes (maxBytes)`);
|
|
159
|
+
if (!response.body) {
|
|
160
|
+
const bytes = new Uint8Array(await request.race(response.arrayBuffer()));
|
|
161
|
+
if (bytes.byteLength > maxBytes)
|
|
162
|
+
throw tooLarge();
|
|
163
|
+
return bytes;
|
|
164
|
+
}
|
|
165
|
+
const reader = response.body.getReader();
|
|
166
|
+
const chunks = [];
|
|
167
|
+
let length = 0;
|
|
168
|
+
for (;;) {
|
|
169
|
+
const { done, value } = await request.race(reader.read());
|
|
170
|
+
if (done)
|
|
171
|
+
break;
|
|
172
|
+
length += value.byteLength;
|
|
173
|
+
if (length > maxBytes) {
|
|
174
|
+
void reader.cancel().catch(() => { });
|
|
175
|
+
throw tooLarge();
|
|
176
|
+
}
|
|
177
|
+
request.received = length;
|
|
178
|
+
chunks.push(value);
|
|
179
|
+
}
|
|
180
|
+
if (chunks.length === 1)
|
|
181
|
+
return chunks[0];
|
|
182
|
+
const bytes = new Uint8Array(length);
|
|
183
|
+
let offset = 0;
|
|
184
|
+
for (const chunk of chunks) {
|
|
185
|
+
bytes.set(chunk, offset);
|
|
186
|
+
offset += chunk.byteLength;
|
|
187
|
+
}
|
|
188
|
+
return bytes;
|
|
189
|
+
}
|
|
190
|
+
class Bounded {
|
|
191
|
+
limits;
|
|
192
|
+
outer;
|
|
193
|
+
controller = new AbortController();
|
|
194
|
+
started = performance.now();
|
|
195
|
+
timer;
|
|
196
|
+
rejectStall;
|
|
197
|
+
stall;
|
|
198
|
+
onAbort = () => this.controller.abort(this.outer?.reason);
|
|
199
|
+
received = 0;
|
|
200
|
+
stalled = false;
|
|
201
|
+
slow = null;
|
|
202
|
+
constructor(limits, outer) {
|
|
203
|
+
this.limits = limits;
|
|
204
|
+
this.outer = outer;
|
|
205
|
+
this.stall = new Promise((_, reject) => (this.rejectStall = reject));
|
|
206
|
+
this.stall.catch(() => { });
|
|
207
|
+
if (outer?.aborted)
|
|
208
|
+
this.controller.abort(outer.reason);
|
|
209
|
+
else
|
|
210
|
+
outer?.addEventListener("abort", this.onAbort, { once: true });
|
|
211
|
+
}
|
|
212
|
+
get signal() {
|
|
213
|
+
return this.controller.signal;
|
|
214
|
+
}
|
|
215
|
+
race(p) {
|
|
216
|
+
clearTimeout(this.timer);
|
|
217
|
+
const allowance = BASE_MS + (this.received * 1000) / this.limits.minBytesPerSecond - (performance.now() - this.started);
|
|
218
|
+
const slow = allowance < this.limits.inactivityMs;
|
|
219
|
+
this.timer = setTimeout(() => {
|
|
220
|
+
if (slow)
|
|
221
|
+
this.slow = `${this.received} bytes in ${((performance.now() - this.started) / 1000).toFixed(1)} s, slower than ${this.limits.minBytesPerSecond} bytes/s after the first ${BASE_MS / 1000} s`;
|
|
222
|
+
else
|
|
223
|
+
this.stalled = true;
|
|
224
|
+
this.rejectStall?.(new Error(slow ? "too slow" : "stalled"));
|
|
225
|
+
this.controller.abort();
|
|
226
|
+
}, Math.max(0, slow ? allowance : this.limits.inactivityMs));
|
|
227
|
+
return Promise.race([p, this.stall]);
|
|
228
|
+
}
|
|
229
|
+
done() {
|
|
230
|
+
clearTimeout(this.timer);
|
|
231
|
+
this.outer?.removeEventListener("abort", this.onAbort);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function sleep(ms, signal) {
|
|
235
|
+
return new Promise((resolve, reject) => {
|
|
236
|
+
if (signal?.aborted)
|
|
237
|
+
return reject(signal.reason);
|
|
238
|
+
const onAbort = () => {
|
|
239
|
+
clearTimeout(timer);
|
|
240
|
+
reject(signal.reason);
|
|
241
|
+
};
|
|
242
|
+
const timer = setTimeout(() => {
|
|
243
|
+
signal?.removeEventListener("abort", onAbort);
|
|
244
|
+
resolve();
|
|
245
|
+
}, ms);
|
|
246
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
247
|
+
});
|
|
248
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export async function resolveRoomToken(provider, room) {
|
|
2
|
+
const token = (typeof provider === "string" ? provider : await provider(room)).trim();
|
|
3
|
+
if (!token || token.length > 4096 || !/^[\x21-\x7e]+$/.test(token))
|
|
4
|
+
throw new Error("the room token must be 1..4096 printable ASCII bytes");
|
|
5
|
+
return token;
|
|
6
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@daasboard/doc",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@noble/hashes": "^2.0.1",
|
|
9
|
+
"loro-crdt": "1.16.3",
|
|
10
|
+
"prosemirror-model": "^1.25.0",
|
|
11
|
+
"prosemirror-state": "^1.4.3",
|
|
12
|
+
"prosemirror-transform": "^1.10.0",
|
|
13
|
+
"@daasboard/render-core": "0.1.1"
|
|
14
|
+
},
|
|
15
|
+
"license": "UNLICENSED",
|
|
16
|
+
"description": "Document adapters and collaboration for daasboard.",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE"
|
|
28
|
+
],
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=22.3.0"
|
|
35
|
+
},
|
|
36
|
+
"daasboardCommit": "82dbc88e19d8c5a48f6b318c873289aca2f99b07"
|
|
6
37
|
}
|