@open-artifact/live-store 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +53 -4
- package/src/cache.d.ts +8 -0
- package/src/cache.js +40 -0
- package/src/doc-hash.d.ts +13 -0
- package/src/doc-hash.js +67 -0
- package/src/engine.d.ts +260 -0
- package/src/engine.js +1270 -0
- package/src/index.d.ts +10 -0
- package/src/index.js +10 -0
- package/src/json.d.ts +56 -0
- package/src/json.js +221 -0
- package/src/transport.d.ts +88 -0
- package/src/transport.js +35 -0
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@open-artifact/live-store`: the live document engine shared by the
|
|
3
|
+
* document-store types (Whiteboard, Data). Pure TypeScript; the store is
|
|
4
|
+
* any {@link SyncTransport} (the runtime's `db` capability satisfies it).
|
|
5
|
+
*/
|
|
6
|
+
export * from './json.ts';
|
|
7
|
+
export * from './transport.ts';
|
|
8
|
+
export * from './doc-hash.ts';
|
|
9
|
+
export * from './engine.ts';
|
|
10
|
+
export * from './cache.ts';
|
package/src/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@open-artifact/live-store`: the live document engine shared by the
|
|
3
|
+
* document-store types (Whiteboard, Data). Pure TypeScript; the store is
|
|
4
|
+
* any {@link SyncTransport} (the runtime's `db` capability satisfies it).
|
|
5
|
+
*/
|
|
6
|
+
export * from "./json.js";
|
|
7
|
+
export * from "./transport.js";
|
|
8
|
+
export * from "./doc-hash.js";
|
|
9
|
+
export * from "./engine.js";
|
|
10
|
+
export * from "./cache.js";
|
package/src/json.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON helpers shared by the codec, the sync engine and history: canonical
|
|
3
|
+
* comparison, JSON merge patches (RFC 7396), depth and byte measures.
|
|
4
|
+
*
|
|
5
|
+
* A merge patch sets fields it names, recurses into objects, and deletes a
|
|
6
|
+
* field whose patch value is `null`. Arrays are replaced whole.
|
|
7
|
+
*/
|
|
8
|
+
/** A JSON value. */
|
|
9
|
+
export type Json = null | boolean | number | string | Json[] | {
|
|
10
|
+
[key: string]: Json;
|
|
11
|
+
};
|
|
12
|
+
/** A JSON object. */
|
|
13
|
+
export type JsonObject = {
|
|
14
|
+
[key: string]: Json;
|
|
15
|
+
};
|
|
16
|
+
/** A JSON merge patch: like the object, with `null` meaning "delete". */
|
|
17
|
+
export type MergePatch = {
|
|
18
|
+
[key: string]: Json | undefined;
|
|
19
|
+
};
|
|
20
|
+
/** True for a plain object (not an array, not null). */
|
|
21
|
+
export declare function isPlainRecord(value: unknown): value is Record<string, unknown>;
|
|
22
|
+
/** JSON text with object keys sorted (cached per object). */
|
|
23
|
+
export declare function stableStringify(value: unknown): string;
|
|
24
|
+
/** True when two values are the same JSON, ignoring key order. */
|
|
25
|
+
export declare function canonicalValuesEqual(a: unknown, b: unknown): boolean;
|
|
26
|
+
/** Deep merge of `patch` into `base` (no deletes; `undefined` is skipped). */
|
|
27
|
+
export declare function deepMerge<T extends Record<string, unknown>>(base: T, patch: Record<string, unknown>): T;
|
|
28
|
+
/**
|
|
29
|
+
* Merges two partial records when they do not conflict in shape (an object
|
|
30
|
+
* field never meets a non-object); `null` when they do.
|
|
31
|
+
*/
|
|
32
|
+
export declare function mergeCompatibleRecords(a: Record<string, unknown>, b: Record<string, unknown>): Record<string, unknown> | null;
|
|
33
|
+
/**
|
|
34
|
+
* The merge patch that turns `from` into `to`, or `null` when they are equal.
|
|
35
|
+
* With `from` undefined the patch is `to` itself.
|
|
36
|
+
*/
|
|
37
|
+
export declare function createJsonMergePatch(from: Record<string, unknown> | undefined, to: Record<string, unknown>): MergePatch | null;
|
|
38
|
+
/** Applies a merge patch to a record (a missing record starts empty). */
|
|
39
|
+
export declare function applyJsonMergePatch(target: Record<string, unknown> | undefined, patch: Record<string, unknown>): Record<string, unknown>;
|
|
40
|
+
/** Composes two merge patches into one with the same effect (`a` then `b`). */
|
|
41
|
+
export declare function composeMergePatches(a: Record<string, unknown>, b: Record<string, unknown>): Record<string, unknown>;
|
|
42
|
+
/** The patch with every `null` (delete) removed: what the patch *sets*. */
|
|
43
|
+
export declare function removeNullMergePatchFields<T extends Record<string, unknown>>(patch: T): T;
|
|
44
|
+
/** How many `null` deletes a patch carries. */
|
|
45
|
+
export declare function countMergePatchTombstones(patch: Record<string, unknown>): number;
|
|
46
|
+
/** True when applying `patch` to `target` changes nothing. */
|
|
47
|
+
export declare function isMergePatchSubset(patch: Record<string, unknown>, target: Record<string, unknown> | undefined): boolean;
|
|
48
|
+
/** Nesting depth of a JSON value (a scalar is 0); stops counting at `limit`. */
|
|
49
|
+
export declare function jsonValueDepth(value: unknown, limit?: number): number;
|
|
50
|
+
/**
|
|
51
|
+
* Bytes of a value's JSON as a server that escapes `<`, `>`, `&` (as
|
|
52
|
+
* `<`…, +5 bytes each) and U+2028/2029 (+3 bytes) would store it.
|
|
53
|
+
*/
|
|
54
|
+
export declare function escapedJsonByteLength(value: unknown): number;
|
|
55
|
+
/** A deep copy of a JSON value. */
|
|
56
|
+
export declare function cloneJson<T>(value: T): T;
|
package/src/json.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON helpers shared by the codec, the sync engine and history: canonical
|
|
3
|
+
* comparison, JSON merge patches (RFC 7396), depth and byte measures.
|
|
4
|
+
*
|
|
5
|
+
* A merge patch sets fields it names, recurses into objects, and deletes a
|
|
6
|
+
* field whose patch value is `null`. Arrays are replaced whole.
|
|
7
|
+
*/
|
|
8
|
+
const PROTO_KEY = '__proto__';
|
|
9
|
+
/** True for a plain object (not an array, not null). */
|
|
10
|
+
export function isPlainRecord(value) {
|
|
11
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
12
|
+
}
|
|
13
|
+
const stableJsonCache = new WeakMap();
|
|
14
|
+
function sortKeys(_key, value) {
|
|
15
|
+
if (!isPlainRecord(value))
|
|
16
|
+
return value;
|
|
17
|
+
const sorted = {};
|
|
18
|
+
for (const key of Object.keys(value).sort())
|
|
19
|
+
sorted[key] = value[key];
|
|
20
|
+
return sorted;
|
|
21
|
+
}
|
|
22
|
+
/** JSON text with object keys sorted (cached per object). */
|
|
23
|
+
export function stableStringify(value) {
|
|
24
|
+
if (typeof value === 'object' && value !== null) {
|
|
25
|
+
const cached = stableJsonCache.get(value);
|
|
26
|
+
if (cached !== undefined)
|
|
27
|
+
return cached;
|
|
28
|
+
const json = JSON.stringify(value, sortKeys) ?? 'undefined';
|
|
29
|
+
stableJsonCache.set(value, json);
|
|
30
|
+
return json;
|
|
31
|
+
}
|
|
32
|
+
return JSON.stringify(value, sortKeys) ?? 'undefined';
|
|
33
|
+
}
|
|
34
|
+
/** True when two values are the same JSON, ignoring key order. */
|
|
35
|
+
export function canonicalValuesEqual(a, b) {
|
|
36
|
+
if (a === b)
|
|
37
|
+
return true;
|
|
38
|
+
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null)
|
|
39
|
+
return false;
|
|
40
|
+
return stableStringify(a) === stableStringify(b);
|
|
41
|
+
}
|
|
42
|
+
/** Deep merge of `patch` into `base` (no deletes; `undefined` is skipped). */
|
|
43
|
+
export function deepMerge(base, patch) {
|
|
44
|
+
const merged = { ...base };
|
|
45
|
+
for (const key of Object.keys(patch)) {
|
|
46
|
+
const value = patch[key];
|
|
47
|
+
if (value === undefined || key === PROTO_KEY)
|
|
48
|
+
continue;
|
|
49
|
+
const current = merged[key];
|
|
50
|
+
merged[key] = isPlainRecord(current) && isPlainRecord(value) ? deepMerge(current, value) : value;
|
|
51
|
+
}
|
|
52
|
+
return merged;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Merges two partial records when they do not conflict in shape (an object
|
|
56
|
+
* field never meets a non-object); `null` when they do.
|
|
57
|
+
*/
|
|
58
|
+
export function mergeCompatibleRecords(a, b) {
|
|
59
|
+
const merged = { ...a };
|
|
60
|
+
for (const key of Object.keys(b)) {
|
|
61
|
+
const value = b[key];
|
|
62
|
+
if (value === undefined || key === PROTO_KEY)
|
|
63
|
+
continue;
|
|
64
|
+
const current = merged[key];
|
|
65
|
+
if (isPlainRecord(value) && current !== undefined) {
|
|
66
|
+
if (!isPlainRecord(current))
|
|
67
|
+
return null;
|
|
68
|
+
const inner = mergeCompatibleRecords(current, value);
|
|
69
|
+
if (inner === null)
|
|
70
|
+
return null;
|
|
71
|
+
merged[key] = inner;
|
|
72
|
+
}
|
|
73
|
+
else
|
|
74
|
+
merged[key] = value;
|
|
75
|
+
}
|
|
76
|
+
return merged;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The merge patch that turns `from` into `to`, or `null` when they are equal.
|
|
80
|
+
* With `from` undefined the patch is `to` itself.
|
|
81
|
+
*/
|
|
82
|
+
export function createJsonMergePatch(from, to) {
|
|
83
|
+
if (from === to)
|
|
84
|
+
return null;
|
|
85
|
+
if (from === undefined)
|
|
86
|
+
return to;
|
|
87
|
+
let patch = null;
|
|
88
|
+
for (const key of Object.keys(to)) {
|
|
89
|
+
if (key === PROTO_KEY)
|
|
90
|
+
continue;
|
|
91
|
+
const next = to[key];
|
|
92
|
+
const previous = from[key];
|
|
93
|
+
if (next === undefined) {
|
|
94
|
+
if (previous !== undefined)
|
|
95
|
+
(patch ??= {})[key] = null;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (previous === next)
|
|
99
|
+
continue;
|
|
100
|
+
if (isPlainRecord(previous) && isPlainRecord(next)) {
|
|
101
|
+
const inner = createJsonMergePatch(previous, next);
|
|
102
|
+
if (inner !== null)
|
|
103
|
+
(patch ??= {})[key] = inner;
|
|
104
|
+
}
|
|
105
|
+
else if (previous === undefined || !canonicalValuesEqual(previous, next))
|
|
106
|
+
(patch ??= {})[key] = next;
|
|
107
|
+
}
|
|
108
|
+
for (const key of Object.keys(from)) {
|
|
109
|
+
if (from[key] !== undefined && to[key] === undefined)
|
|
110
|
+
(patch ??= {})[key] = null;
|
|
111
|
+
}
|
|
112
|
+
return patch;
|
|
113
|
+
}
|
|
114
|
+
/** Applies a merge patch to a record (a missing record starts empty). */
|
|
115
|
+
export function applyJsonMergePatch(target, patch) {
|
|
116
|
+
const result = { ...(target ?? {}) };
|
|
117
|
+
for (const key of Object.keys(patch)) {
|
|
118
|
+
if (key === PROTO_KEY)
|
|
119
|
+
continue;
|
|
120
|
+
const value = patch[key];
|
|
121
|
+
if (value === undefined)
|
|
122
|
+
continue;
|
|
123
|
+
if (value === null)
|
|
124
|
+
delete result[key];
|
|
125
|
+
else if (isPlainRecord(value))
|
|
126
|
+
result[key] = applyJsonMergePatch(isPlainRecord(result[key]) ? result[key] : undefined, value);
|
|
127
|
+
else
|
|
128
|
+
result[key] = value;
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
/** Composes two merge patches into one with the same effect (`a` then `b`). */
|
|
133
|
+
export function composeMergePatches(a, b) {
|
|
134
|
+
const result = { ...a };
|
|
135
|
+
for (const key of Object.keys(b)) {
|
|
136
|
+
if (key === PROTO_KEY)
|
|
137
|
+
continue;
|
|
138
|
+
const value = b[key];
|
|
139
|
+
if (value === undefined)
|
|
140
|
+
continue;
|
|
141
|
+
const previous = result[key];
|
|
142
|
+
result[key] = isPlainRecord(value) && isPlainRecord(previous) ? composeMergePatches(previous, value) : value;
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
/** The patch with every `null` (delete) removed: what the patch *sets*. */
|
|
147
|
+
export function removeNullMergePatchFields(patch) {
|
|
148
|
+
let result;
|
|
149
|
+
for (const key of Object.keys(patch)) {
|
|
150
|
+
const value = patch[key];
|
|
151
|
+
let next = value;
|
|
152
|
+
if (value === null || value === undefined || key === PROTO_KEY)
|
|
153
|
+
next = undefined;
|
|
154
|
+
else if (isPlainRecord(value))
|
|
155
|
+
next = removeNullMergePatchFields(value);
|
|
156
|
+
if (next !== value) {
|
|
157
|
+
result ??= { ...patch };
|
|
158
|
+
if (next === undefined)
|
|
159
|
+
delete result[key];
|
|
160
|
+
else
|
|
161
|
+
result[key] = next;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return (result ?? patch);
|
|
165
|
+
}
|
|
166
|
+
/** How many `null` deletes a patch carries. */
|
|
167
|
+
export function countMergePatchTombstones(patch) {
|
|
168
|
+
let count = 0;
|
|
169
|
+
for (const value of Object.values(patch)) {
|
|
170
|
+
if (value === null)
|
|
171
|
+
count += 1;
|
|
172
|
+
else if (isPlainRecord(value))
|
|
173
|
+
count += countMergePatchTombstones(value);
|
|
174
|
+
}
|
|
175
|
+
return count;
|
|
176
|
+
}
|
|
177
|
+
/** True when applying `patch` to `target` changes nothing. */
|
|
178
|
+
export function isMergePatchSubset(patch, target) {
|
|
179
|
+
for (const key of Object.keys(patch)) {
|
|
180
|
+
const value = patch[key];
|
|
181
|
+
if (value === undefined)
|
|
182
|
+
continue;
|
|
183
|
+
const current = target?.[key];
|
|
184
|
+
if (value === null) {
|
|
185
|
+
if (current !== undefined)
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
else if (isPlainRecord(value)) {
|
|
189
|
+
if (!isPlainRecord(current) || !isMergePatchSubset(value, current))
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
else if (!canonicalValuesEqual(value, current))
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
/** Nesting depth of a JSON value (a scalar is 0); stops counting at `limit`. */
|
|
198
|
+
export function jsonValueDepth(value, limit = 64) {
|
|
199
|
+
if (typeof value !== 'object' || value === null)
|
|
200
|
+
return 0;
|
|
201
|
+
let depth = 0;
|
|
202
|
+
for (const child of Array.isArray(value) ? value : Object.values(value)) {
|
|
203
|
+
depth = Math.max(depth, jsonValueDepth(child, limit - 1));
|
|
204
|
+
if (depth >= limit)
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
return depth + 1;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Bytes of a value's JSON as a server that escapes `<`, `>`, `&` (as
|
|
211
|
+
* `<`…, +5 bytes each) and U+2028/2029 (+3 bytes) would store it.
|
|
212
|
+
*/
|
|
213
|
+
export function escapedJsonByteLength(value) {
|
|
214
|
+
const json = JSON.stringify(value) ?? '';
|
|
215
|
+
const extra = 5 * (json.match(/[<>&]/g)?.length ?? 0) + 3 * (json.match(/[\u2028\u2029]/g)?.length ?? 0);
|
|
216
|
+
return new TextEncoder().encode(json).length + extra;
|
|
217
|
+
}
|
|
218
|
+
/** A deep copy of a JSON value. */
|
|
219
|
+
export function cloneJson(value) {
|
|
220
|
+
return value === undefined ? value : JSON.parse(JSON.stringify(value));
|
|
221
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The document store the live sync engine talks to, and the clock it runs on.
|
|
3
|
+
*
|
|
4
|
+
* {@link SyncTransport} is structural: the runtime's `db` capability
|
|
5
|
+
* (`DbCapability`) satisfies it as-is, and tests pass an in-memory fake. The
|
|
6
|
+
* engine never imports the runtime.
|
|
7
|
+
*/
|
|
8
|
+
/** One stored document. */
|
|
9
|
+
export interface SyncDocument {
|
|
10
|
+
/** Even segment count: `elements/<id>`, `board/meta`. */
|
|
11
|
+
path: string;
|
|
12
|
+
data: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A write's precondition: only while the document still has this hash
|
|
16
|
+
* (`documentSha256` of what was read), or `null`: only while it is absent.
|
|
17
|
+
* A failed one refuses the whole batch with `conflict`.
|
|
18
|
+
*/
|
|
19
|
+
export interface SyncWriteGuard {
|
|
20
|
+
ifMatch?: string | null;
|
|
21
|
+
}
|
|
22
|
+
/** One write of a batch. */
|
|
23
|
+
export type SyncWrite =
|
|
24
|
+
/** Replaces the document (creating it when absent). */
|
|
25
|
+
({
|
|
26
|
+
op: 'set';
|
|
27
|
+
path: string;
|
|
28
|
+
data: Record<string, unknown>;
|
|
29
|
+
} & SyncWriteGuard)
|
|
30
|
+
/** Applies a JSON merge patch; an absent document starts empty. */
|
|
31
|
+
| ({
|
|
32
|
+
op: 'update';
|
|
33
|
+
path: string;
|
|
34
|
+
patch: Record<string, unknown>;
|
|
35
|
+
} & SyncWriteGuard)
|
|
36
|
+
/** Removes the document (a no-op when absent). */
|
|
37
|
+
| ({
|
|
38
|
+
op: 'delete';
|
|
39
|
+
path: string;
|
|
40
|
+
} & SyncWriteGuard);
|
|
41
|
+
/** Changes made by someone else. */
|
|
42
|
+
export interface SyncChangeEvent {
|
|
43
|
+
/** Path → the document's new data, or `null` when it was deleted. */
|
|
44
|
+
changes: Record<string, Record<string, unknown> | null>;
|
|
45
|
+
/** Who made the change, when the host knows. */
|
|
46
|
+
author?: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A document store with change notifications. Writes are all or nothing and
|
|
50
|
+
* are never echoed back through {@link SyncTransport.onChange}.
|
|
51
|
+
*/
|
|
52
|
+
export interface SyncTransport {
|
|
53
|
+
/** Reads one document; `null` when absent. */
|
|
54
|
+
get(path: string): Promise<Record<string, unknown> | null>;
|
|
55
|
+
/** Every document of one collection. */
|
|
56
|
+
list(collection: string): Promise<SyncDocument[]>;
|
|
57
|
+
/** Applies a batch in order, all or nothing. */
|
|
58
|
+
write(writes: SyncWrite[]): Promise<void>;
|
|
59
|
+
/** Subscribes to changes made by anyone else; returns an unsubscribe function. */
|
|
60
|
+
onChange(listener: (event: SyncChangeEvent) => void): () => void;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Why a store call failed. Hosts reject with an error whose `code` is one of
|
|
64
|
+
* these (the runtime's `ArtifactError` codes plus `resource_exhausted`);
|
|
65
|
+
* anything else counts as `unavailable`.
|
|
66
|
+
*/
|
|
67
|
+
export type SyncErrorCode = 'conflict' | 'not_writer' | 'not_granted' | 'invalid' | 'not_found' | 'unavailable' | 'resource_exhausted';
|
|
68
|
+
/** The code of a store failure (`unavailable` for anything unrecognized). */
|
|
69
|
+
export declare function syncErrorCode(error: unknown): SyncErrorCode;
|
|
70
|
+
/** The message of a store failure, for status text. */
|
|
71
|
+
export declare function syncErrorMessage(error: unknown): string;
|
|
72
|
+
/** True for failures that mean the viewer may no longer write. */
|
|
73
|
+
export declare function isAccessError(code: SyncErrorCode): boolean;
|
|
74
|
+
/** An opaque timer handle. */
|
|
75
|
+
export type TimerHandle = unknown;
|
|
76
|
+
/** The clock and timers the engine uses; tests inject a fake. */
|
|
77
|
+
export interface SyncTimers {
|
|
78
|
+
setTimeout(callback: () => void, ms: number): TimerHandle;
|
|
79
|
+
clearTimeout(handle: TimerHandle): void;
|
|
80
|
+
/** Epoch milliseconds. */
|
|
81
|
+
now(): number;
|
|
82
|
+
/** A number in [0, 1). */
|
|
83
|
+
random(): number;
|
|
84
|
+
}
|
|
85
|
+
/** Real timers. */
|
|
86
|
+
export declare const SYSTEM_TIMERS: SyncTimers;
|
|
87
|
+
/** The collection of a document path (`elements/a1` → `elements`). */
|
|
88
|
+
export declare function collectionOfPath(path: string): string;
|
package/src/transport.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The document store the live sync engine talks to, and the clock it runs on.
|
|
3
|
+
*
|
|
4
|
+
* {@link SyncTransport} is structural: the runtime's `db` capability
|
|
5
|
+
* (`DbCapability`) satisfies it as-is, and tests pass an in-memory fake. The
|
|
6
|
+
* engine never imports the runtime.
|
|
7
|
+
*/
|
|
8
|
+
const SYNC_ERROR_CODES = ['conflict', 'not_writer', 'not_granted', 'invalid', 'not_found', 'unavailable', 'resource_exhausted'];
|
|
9
|
+
/** The code of a store failure (`unavailable` for anything unrecognized). */
|
|
10
|
+
export function syncErrorCode(error) {
|
|
11
|
+
const code = typeof error === 'object' && error !== null ? error.code : undefined;
|
|
12
|
+
return typeof code === 'string' && SYNC_ERROR_CODES.includes(code) ? code : 'unavailable';
|
|
13
|
+
}
|
|
14
|
+
/** The message of a store failure, for status text. */
|
|
15
|
+
export function syncErrorMessage(error) {
|
|
16
|
+
if (error instanceof Error && error.message !== '')
|
|
17
|
+
return error.message;
|
|
18
|
+
return typeof error === 'string' && error !== '' ? error : 'unknown error';
|
|
19
|
+
}
|
|
20
|
+
/** True for failures that mean the viewer may no longer write. */
|
|
21
|
+
export function isAccessError(code) {
|
|
22
|
+
return code === 'not_writer' || code === 'not_granted';
|
|
23
|
+
}
|
|
24
|
+
/** Real timers. */
|
|
25
|
+
export const SYSTEM_TIMERS = {
|
|
26
|
+
setTimeout: (callback, ms) => globalThis.setTimeout(callback, ms),
|
|
27
|
+
clearTimeout: (handle) => globalThis.clearTimeout(handle),
|
|
28
|
+
now: () => Date.now(),
|
|
29
|
+
random: () => Math.random(),
|
|
30
|
+
};
|
|
31
|
+
/** The collection of a document path (`elements/a1` → `elements`). */
|
|
32
|
+
export function collectionOfPath(path) {
|
|
33
|
+
const slash = path.lastIndexOf('/');
|
|
34
|
+
return slash < 0 ? '' : path.slice(0, slash);
|
|
35
|
+
}
|