@nocentraldb/sdk 0.0.0-stage → 0.1.0-alpha.70
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/dist/account.d.ts +14 -0
- package/dist/account.js +34 -0
- package/dist/accountCalls.d.ts +34 -0
- package/dist/accountCalls.js +48 -0
- package/dist/adHoc.d.ts +49 -0
- package/dist/adHoc.js +52 -0
- package/dist/cbor.d.ts +8 -0
- package/dist/cbor.js +223 -0
- package/dist/connect.d.ts +14 -0
- package/dist/connect.js +527 -0
- package/dist/connectParts.d.ts +29 -0
- package/dist/connectParts.js +10 -0
- package/dist/db.d.ts +291 -0
- package/dist/db.js +17 -0
- package/dist/durability.d.ts +20 -0
- package/dist/durability.js +5 -0
- package/dist/durabilityReport.d.ts +29 -0
- package/dist/durabilityReport.js +128 -0
- package/dist/elements.d.ts +5 -0
- package/dist/elements.js +13 -0
- package/dist/error.d.ts +10 -0
- package/dist/error.js +23 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +6 -0
- package/dist/lockCheck.d.ts +4 -0
- package/dist/lockCheck.js +63 -0
- package/dist/members.d.ts +55 -0
- package/dist/members.js +38 -0
- package/dist/membership.d.ts +5 -0
- package/dist/membership.js +29 -0
- package/dist/nc.d.ts +153 -0
- package/dist/nc.js +211 -0
- package/dist/query.d.ts +21 -0
- package/dist/query.js +45 -0
- package/dist/reports.d.ts +8 -0
- package/dist/reports.js +50 -0
- package/dist/spaceList.d.ts +14 -0
- package/dist/spaceList.js +45 -0
- package/dist/status.d.ts +51 -0
- package/dist/status.js +5 -0
- package/dist/sync.d.ts +15 -0
- package/dist/sync.js +4 -0
- package/dist/tabs.d.ts +30 -0
- package/dist/tabs.js +444 -0
- package/dist/texts.d.ts +52 -0
- package/dist/texts.js +58 -0
- package/dist/waitNotice.d.ts +7 -0
- package/dist/waitNotice.js +32 -0
- package/dist/wasm/nocentraldb_wasm.d.ts +91 -0
- package/dist/wasm/nocentraldb_wasm.js +1058 -0
- package/dist/wasm/nocentraldb_wasm_bg.wasm +0 -0
- package/dist/wasm/nocentraldb_wasm_bg.wasm.d.ts +36 -0
- package/dist/worker.d.ts +1 -0
- package/dist/worker.js +142 -0
- package/dist/workerHost.d.ts +3 -0
- package/dist/workerHost.js +55 -0
- package/package.json +21 -4
- package/README.md +0 -3
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// The space list as a report (NOC-315; RFC 0009, "Spaces"): the first
|
|
2
|
+
// `db.spaces.subscribe` asks the core's `spaces.follow`, and the core's
|
|
3
|
+
// `spaces` events keep the newest list, so a later subscriber gets it at
|
|
4
|
+
// once. Internal: the package's entry does not reach it.
|
|
5
|
+
import { notify } from "./reports.js";
|
|
6
|
+
/** The follower of the space list of one database. */
|
|
7
|
+
export const spaceListOf = (ask) => {
|
|
8
|
+
const listeners = new Set();
|
|
9
|
+
let newest;
|
|
10
|
+
let following = false;
|
|
11
|
+
const changed = (raw) => {
|
|
12
|
+
newest = (Array.isArray(raw) ? raw : []);
|
|
13
|
+
notify(listeners, newest);
|
|
14
|
+
};
|
|
15
|
+
const follow = () => ask("spaces.follow", {}).then(changed, () => {
|
|
16
|
+
// A closed database answers nothing more; its listeners are gone.
|
|
17
|
+
});
|
|
18
|
+
return {
|
|
19
|
+
/** Calls `listener` with the list at once, or once the core answered the first follow, and with each new list. */
|
|
20
|
+
subscribe(listener) {
|
|
21
|
+
listeners.add(listener);
|
|
22
|
+
if (!following) {
|
|
23
|
+
following = true;
|
|
24
|
+
void follow();
|
|
25
|
+
}
|
|
26
|
+
else if (newest !== undefined) {
|
|
27
|
+
notify(new Set([listener]), newest);
|
|
28
|
+
}
|
|
29
|
+
return () => {
|
|
30
|
+
listeners.delete(listener);
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
/** A `spaces` event of the core. */
|
|
34
|
+
changed,
|
|
35
|
+
/** Another tab took the database over (NOC-177): its core follows no list yet. */
|
|
36
|
+
reopened() {
|
|
37
|
+
if (following)
|
|
38
|
+
void follow();
|
|
39
|
+
},
|
|
40
|
+
/** The database closed: no listener hears anything more. */
|
|
41
|
+
clear() {
|
|
42
|
+
listeners.clear();
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
};
|
package/dist/status.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** What a space holds that a read does not show, as `space.status.current()` gives it (RFC 0009, "Sync and status"). */
|
|
2
|
+
export interface SpaceStatus {
|
|
3
|
+
/** This member's role, or `"removed"`; a removed member keeps reading its copy. */
|
|
4
|
+
readonly role: "reader" | "writer" | "admin" | "removed";
|
|
5
|
+
/** Whether the first pull reached the relay's head. */
|
|
6
|
+
readonly initialSyncComplete: boolean;
|
|
7
|
+
/** How many relay positions the member has heard of but not folded. */
|
|
8
|
+
readonly behind: number;
|
|
9
|
+
/** Dependencies the member does not hold, by change id. */
|
|
10
|
+
readonly gaps: readonly string[];
|
|
11
|
+
/** The parked changes and operations, per reason, such as `anotherModel`. */
|
|
12
|
+
readonly parked: Readonly<Record<string, number>>;
|
|
13
|
+
/** The highest version of the app's model that parked data needs; absent when none names one. An app that sees it above its own version can say that some data needs a newer version of the app. */
|
|
14
|
+
readonly newestModel?: number;
|
|
15
|
+
/** The rejected changes, per reason, such as `badSignature`. */
|
|
16
|
+
readonly rejected: Readonly<Record<string, number>>;
|
|
17
|
+
/** This member's changes in this space the relay refused, by change id and RFC 0007 code. */
|
|
18
|
+
readonly refused: readonly {
|
|
19
|
+
readonly change: string;
|
|
20
|
+
readonly code: string;
|
|
21
|
+
}[];
|
|
22
|
+
/** Grants a concurrent removal of their member capped, by change id. */
|
|
23
|
+
readonly lostGrants: readonly string[];
|
|
24
|
+
/** Changes of members that wrote two changes under one sequence, by change id. */
|
|
25
|
+
readonly equivocations: readonly string[];
|
|
26
|
+
/** Whether members write under a key generation a removed member may hold. */
|
|
27
|
+
readonly exposed: boolean;
|
|
28
|
+
/** Whether this member waits for its key envelope. */
|
|
29
|
+
readonly waitingForKey: boolean;
|
|
30
|
+
/** The fingerprints of accounts whose person-key records failed their checks. */
|
|
31
|
+
readonly badPersonKeyRecords: readonly string[];
|
|
32
|
+
/** The erasures of this space that are still provisional; an app must show them. */
|
|
33
|
+
readonly provisionalErasures: readonly ProvisionalErasure[];
|
|
34
|
+
}
|
|
35
|
+
/** A provisional erasure in a space with one account (RFC 0008, "A provisional erasure in a space with one account"). */
|
|
36
|
+
interface ProvisionalErasure {
|
|
37
|
+
/** The shred, by change id. */
|
|
38
|
+
readonly change: string;
|
|
39
|
+
/** How long ago this device first held the shred, in milliseconds. */
|
|
40
|
+
readonly age: number;
|
|
41
|
+
/** Whether this device may take the erasure back. */
|
|
42
|
+
readonly canUndo: boolean;
|
|
43
|
+
/** The member that performed it. */
|
|
44
|
+
readonly performer: string;
|
|
45
|
+
/** The notice `erasurePending`. */
|
|
46
|
+
readonly notice: {
|
|
47
|
+
readonly code: string;
|
|
48
|
+
readonly text: string;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export {};
|
package/dist/status.js
ADDED
package/dist/sync.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** The relay connection and what syncing found, as `db.sync.current()` gives it (RFC 0009, "Sync and status"). */
|
|
2
|
+
export interface SyncStatus {
|
|
3
|
+
/** `"offline"`: no relay reachable, and the SDK retries; `"none"`: opened without a relay. */
|
|
4
|
+
readonly connection: "connected" | "connecting" | "offline" | "paused" | "none";
|
|
5
|
+
/** The relay `nc.open` named; absent without one. */
|
|
6
|
+
readonly relay?: string;
|
|
7
|
+
/** This member's changes the relay has not confirmed. */
|
|
8
|
+
readonly unconfirmed: number;
|
|
9
|
+
/** This member's changes the relay refused. */
|
|
10
|
+
readonly refused: number;
|
|
11
|
+
/** Whether the relay was restored and the SDK is pushing again what it lost. */
|
|
12
|
+
readonly epochChanged: boolean;
|
|
13
|
+
/** The last error of syncing; never content. An error that stops syncing, by its RFC 0007 code, such as `unsupported_version`, or `unknown_error`; or `upload_failed` while a large change's upload is refused, which keeps syncing and retries the upload after a backoff. Absent while syncing runs. */
|
|
14
|
+
readonly error?: string;
|
|
15
|
+
}
|
package/dist/sync.js
ADDED
package/dist/tabs.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Core } from "./connect.ts";
|
|
2
|
+
/** What a tab reaches the others with; the browser's, or a test's fakes. */
|
|
3
|
+
export interface Platform {
|
|
4
|
+
/** The Web Locks API, as `navigator.locks` gives it. */
|
|
5
|
+
locks: {
|
|
6
|
+
request(name: string, options: {
|
|
7
|
+
ifAvailable?: boolean;
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
}, callback: (lock: unknown) => Promise<void> | void): Promise<unknown>;
|
|
10
|
+
};
|
|
11
|
+
/** A channel to every other tab of the origin, as `new BroadcastChannel(name)`. */
|
|
12
|
+
channel(name: string): {
|
|
13
|
+
postMessage(message: unknown): void;
|
|
14
|
+
onmessage: ((event: {
|
|
15
|
+
data: unknown;
|
|
16
|
+
}) => void) | null;
|
|
17
|
+
close(): void;
|
|
18
|
+
};
|
|
19
|
+
/** Starts the worker that runs the core. */
|
|
20
|
+
startWorker(): Core;
|
|
21
|
+
/** The page's visibility: hidden from `visibilitychange` or `pagehide`, visible again on `visibilitychange` or `pageshow`. Without it, the page counts as visible. */
|
|
22
|
+
page?: {
|
|
23
|
+
visible(): boolean;
|
|
24
|
+
onVisibility(listener: (visible: boolean) => void): void;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/** The page's visibility for the tabs layer (NOC-332): hidden on `visibilitychange` or `pagehide`, visible again on `visibilitychange` or `pageshow`. */
|
|
28
|
+
export declare const browserPage: () => Platform["page"];
|
|
29
|
+
/** The core of the database `name` as this tab reaches it: its own worker while it owns the database, the owner tab's otherwise. */
|
|
30
|
+
export declare const tabCore: (name: string, platform: Platform) => Promise<Core>;
|
package/dist/tabs.js
ADDED
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
// One tab owns a browser database, the others reach it (NOC-177; RFC 0006,
|
|
2
|
+
// "The browser": "One tab owns the database"; RFC 0009, "Open and close").
|
|
3
|
+
// A pool of OPFS access handles is exclusive, so only one tab's worker can
|
|
4
|
+
// open a database. The first tab to open it takes the Web Lock
|
|
5
|
+
// `nocentraldb/<name>` and runs the worker; every other tab queues for the
|
|
6
|
+
// lock and, meanwhile, sends its calls to the owner over the
|
|
7
|
+
// BroadcastChannel `nocentraldb/<name>`. The owner runs them through its
|
|
8
|
+
// worker, in order with its own, and passes every event of the core to
|
|
9
|
+
// every tab. When the owner closes or its tab goes away, the lock passes to
|
|
10
|
+
// the next tab in the queue, which starts its own worker and opens the
|
|
11
|
+
// database with its own options.
|
|
12
|
+
//
|
|
13
|
+
// A phone freezes a hidden tab but leaves it its lock (NOC-332), so the
|
|
14
|
+
// owner gives the database up when its page is hidden: it answers the calls
|
|
15
|
+
// it already took, tells the others, and releases the lock. A tab waits for
|
|
16
|
+
// the lock only while its page is visible or it has calls to send, so the
|
|
17
|
+
// visible tab takes over. A call that reaches a tab that no longer owns the
|
|
18
|
+
// database is declined, never run, and goes to the next owner.
|
|
19
|
+
//
|
|
20
|
+
// What this module gives `connect` is a `Core` like the worker's: the
|
|
21
|
+
// bridge's bytes pass through unchanged. Internal: the package's entry
|
|
22
|
+
// does not reach it.
|
|
23
|
+
import { decode, encode } from "./cbor.js";
|
|
24
|
+
import { NcError } from "./error.js";
|
|
25
|
+
import * as texts from "./texts.js";
|
|
26
|
+
/** The page's visibility for the tabs layer (NOC-332): hidden on `visibilitychange` or `pagehide`, visible again on `visibilitychange` or `pageshow`. */
|
|
27
|
+
export const browserPage = () => {
|
|
28
|
+
if (typeof document === "undefined" || typeof window === "undefined")
|
|
29
|
+
return undefined;
|
|
30
|
+
const visible = () => document.visibilityState !== "hidden";
|
|
31
|
+
return {
|
|
32
|
+
visible,
|
|
33
|
+
onVisibility: (listener) => {
|
|
34
|
+
document.addEventListener("visibilitychange", () => listener(visible()));
|
|
35
|
+
window.addEventListener("pagehide", () => listener(false));
|
|
36
|
+
window.addEventListener("pageshow", () => listener(visible()));
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
/** Calls that change the database or its syncing: a tab never sends one twice, since it cannot know whether a lost owner ran it. */
|
|
41
|
+
const CHANGES = new Set([
|
|
42
|
+
"write",
|
|
43
|
+
"spaces.create",
|
|
44
|
+
"sync.pause",
|
|
45
|
+
"sync.resume",
|
|
46
|
+
"members.add",
|
|
47
|
+
"members.remove",
|
|
48
|
+
"members.setRole",
|
|
49
|
+
"space.leave",
|
|
50
|
+
"account.pair",
|
|
51
|
+
"account.completePairing",
|
|
52
|
+
"account.removeMember",
|
|
53
|
+
]);
|
|
54
|
+
const failure = (id, code, message) => encode({ id, error: { code, message } });
|
|
55
|
+
/** The core of the database `name` as this tab reaches it: its own worker while it owns the database, the owner tab's otherwise. */
|
|
56
|
+
export const tabCore = async (name, platform) => {
|
|
57
|
+
const me = crypto.randomUUID();
|
|
58
|
+
const lockName = `nocentraldb/${name}`;
|
|
59
|
+
const channel = platform.channel(lockName);
|
|
60
|
+
const post = (message) => channel.postMessage(message);
|
|
61
|
+
let listener;
|
|
62
|
+
const emit = (bytes) => listener?.(bytes);
|
|
63
|
+
// This tab while it owns the database.
|
|
64
|
+
let worker;
|
|
65
|
+
let release;
|
|
66
|
+
let ownOpen;
|
|
67
|
+
// Whether this tab's worker opened the database, so it takes calls now.
|
|
68
|
+
let isOpen = false;
|
|
69
|
+
let opened = () => { };
|
|
70
|
+
let ready = new Promise((resolve) => {
|
|
71
|
+
opened = () => {
|
|
72
|
+
isOpen = true;
|
|
73
|
+
resolve();
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
let sync = null;
|
|
77
|
+
// The calls this tab's worker is answering, its own and the others'.
|
|
78
|
+
const serving = new Set();
|
|
79
|
+
// This tab is giving the database up: it takes no new call.
|
|
80
|
+
let yielding = false;
|
|
81
|
+
const visible = () => platform.page?.visible() ?? true;
|
|
82
|
+
// This tab while it follows another.
|
|
83
|
+
let owner;
|
|
84
|
+
let next = 0;
|
|
85
|
+
const sent = new Map();
|
|
86
|
+
const waiting = new AbortController();
|
|
87
|
+
let closed = false;
|
|
88
|
+
// Why every call fails: this tab could not open the database it took over.
|
|
89
|
+
let failed;
|
|
90
|
+
// The open request of this tab, kept to open the database when it takes
|
|
91
|
+
// over, which may come before `connect` sends it.
|
|
92
|
+
let openRequest;
|
|
93
|
+
let openGiven = () => { };
|
|
94
|
+
const openSent = new Promise((resolve) => {
|
|
95
|
+
openGiven = resolve;
|
|
96
|
+
});
|
|
97
|
+
/** The newest sync status, from an answer to `open` or a `sync` event. */
|
|
98
|
+
const noteSync = (bytes) => {
|
|
99
|
+
const message = decode(bytes);
|
|
100
|
+
if (message.event === "sync")
|
|
101
|
+
sync = message.status ?? null;
|
|
102
|
+
else if (message.ok?.sync !== undefined)
|
|
103
|
+
sync = message.ok.sync;
|
|
104
|
+
};
|
|
105
|
+
const workerEvent = (bytes) => {
|
|
106
|
+
noteSync(bytes);
|
|
107
|
+
emit(bytes);
|
|
108
|
+
// This tab's own wait for the storage (NOC-355, worker.ts) is no event
|
|
109
|
+
// of the database: the other tabs do not wait for it.
|
|
110
|
+
if (decode(bytes).event === "waitingForTab")
|
|
111
|
+
return;
|
|
112
|
+
post({ kind: "event", bytes });
|
|
113
|
+
};
|
|
114
|
+
/** The relay this tab opened with, which serves every tab while it owns the database. */
|
|
115
|
+
const relayOfOwner = () => typeof ownOpen?.args.relay === "string"
|
|
116
|
+
? { relay: ownOpen.args.relay }
|
|
117
|
+
: {};
|
|
118
|
+
/** `call`, counted among the calls the worker is answering until it settles. */
|
|
119
|
+
const track = (call) => {
|
|
120
|
+
serving.add(call);
|
|
121
|
+
const done = () => serving.delete(call);
|
|
122
|
+
call.then(done, done);
|
|
123
|
+
return call;
|
|
124
|
+
};
|
|
125
|
+
/** The owner's answer to another tab's call. */
|
|
126
|
+
const serve = async (from, id, bytes) => {
|
|
127
|
+
await ready;
|
|
128
|
+
const request = decode(bytes);
|
|
129
|
+
let answer;
|
|
130
|
+
if (request.call === "open") {
|
|
131
|
+
answer =
|
|
132
|
+
ownOpen?.args.lock === request.args.lock
|
|
133
|
+
? encode({ id: request.id, ok: { sync, ...relayOfOwner() } })
|
|
134
|
+
: failure(request.id, "modelRefused", texts.ANOTHER_LOCK);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
answer = await track(worker
|
|
138
|
+
.call(bytes)
|
|
139
|
+
.catch((error) => failure(request.id, "storageFailed", String(error))));
|
|
140
|
+
}
|
|
141
|
+
post({ kind: "answer", to: from, id, bytes: answer });
|
|
142
|
+
};
|
|
143
|
+
/** Sends this tab's call to the owner, or holds it while none is known. */
|
|
144
|
+
const send = (bytes, call) => new Promise((resolve, reject) => {
|
|
145
|
+
next += 1;
|
|
146
|
+
const entry = { bytes, call, resolve, reject, to: owner };
|
|
147
|
+
sent.set(next, entry);
|
|
148
|
+
if (owner !== undefined) {
|
|
149
|
+
post({ kind: "request", from: me, to: owner, id: next, bytes });
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
queue();
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
/** A new owner, `tab`: a call the old one never answered goes to it again, unless it changes the database. */
|
|
156
|
+
const ownerIs = (tab) => {
|
|
157
|
+
owner = tab;
|
|
158
|
+
for (const [id, entry] of sent) {
|
|
159
|
+
if (entry.to !== undefined &&
|
|
160
|
+
entry.to !== tab &&
|
|
161
|
+
CHANGES.has(entry.call)) {
|
|
162
|
+
sent.delete(id);
|
|
163
|
+
entry.reject(new NcError("storageFailed", texts.OWNER_TAB_CLOSED));
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
entry.to = tab;
|
|
167
|
+
if (tab === me) {
|
|
168
|
+
sent.delete(id);
|
|
169
|
+
if (entry.call === "open") {
|
|
170
|
+
// This tab opened the database itself as it took over.
|
|
171
|
+
const { id: asked } = decode(entry.bytes);
|
|
172
|
+
entry.resolve(encode({ id: asked, ok: { sync } }));
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
worker.call(entry.bytes).then(entry.resolve, entry.reject);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
post({ kind: "request", from: me, to: tab, id, bytes: entry.bytes });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
channel.onmessage = ({ data }) => {
|
|
184
|
+
if (closed)
|
|
185
|
+
return;
|
|
186
|
+
const message = data;
|
|
187
|
+
switch (message.kind) {
|
|
188
|
+
case "request":
|
|
189
|
+
if (worker !== undefined && !yielding)
|
|
190
|
+
void serve(message.from, message.id, message.bytes);
|
|
191
|
+
else if (message.to === me)
|
|
192
|
+
// Not run: the sender gives it to the next owner.
|
|
193
|
+
post({ kind: "declined", to: message.from, id: message.id });
|
|
194
|
+
return;
|
|
195
|
+
case "declined": {
|
|
196
|
+
if (message.to !== me)
|
|
197
|
+
return;
|
|
198
|
+
const entry = sent.get(message.id);
|
|
199
|
+
if (entry === undefined)
|
|
200
|
+
return;
|
|
201
|
+
entry.to = undefined;
|
|
202
|
+
if (owner !== undefined && worker === undefined) {
|
|
203
|
+
entry.to = owner;
|
|
204
|
+
post({
|
|
205
|
+
kind: "request",
|
|
206
|
+
from: me,
|
|
207
|
+
to: owner,
|
|
208
|
+
id: message.id,
|
|
209
|
+
bytes: entry.bytes,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
else
|
|
213
|
+
queue();
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
case "answer": {
|
|
217
|
+
if (message.to !== me)
|
|
218
|
+
return;
|
|
219
|
+
const entry = sent.get(message.id);
|
|
220
|
+
if (entry === undefined)
|
|
221
|
+
return;
|
|
222
|
+
sent.delete(message.id);
|
|
223
|
+
entry.resolve(message.bytes);
|
|
224
|
+
unqueueIfIdle();
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
case "event":
|
|
228
|
+
if (worker === undefined)
|
|
229
|
+
emit(message.bytes);
|
|
230
|
+
return;
|
|
231
|
+
case "who":
|
|
232
|
+
if (worker !== undefined)
|
|
233
|
+
post({ kind: "owner", tab: me });
|
|
234
|
+
return;
|
|
235
|
+
case "owner":
|
|
236
|
+
if (worker === undefined && message.tab !== owner)
|
|
237
|
+
ownerIs(message.tab);
|
|
238
|
+
return;
|
|
239
|
+
case "gone":
|
|
240
|
+
if (message.yielded) {
|
|
241
|
+
// It answered every call it took: the rest never ran.
|
|
242
|
+
for (const entry of sent.values()) {
|
|
243
|
+
if (entry.to === message.tab)
|
|
244
|
+
entry.to = undefined;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (message.tab === owner)
|
|
248
|
+
owner = undefined;
|
|
249
|
+
if (sent.size > 0)
|
|
250
|
+
queue();
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
/** Starts this tab's worker as the owner; `open` is the request it opens with. */
|
|
255
|
+
const own = () => {
|
|
256
|
+
const started = platform.startWorker();
|
|
257
|
+
started.listen(workerEvent);
|
|
258
|
+
worker = started;
|
|
259
|
+
return started;
|
|
260
|
+
};
|
|
261
|
+
/** Takes the database over from an owner that went away. */
|
|
262
|
+
const takeOver = async () => {
|
|
263
|
+
await openSent;
|
|
264
|
+
if (closed || openRequest === undefined)
|
|
265
|
+
return;
|
|
266
|
+
const started = own();
|
|
267
|
+
isOpen = false;
|
|
268
|
+
ready = new Promise((resolve) => {
|
|
269
|
+
opened = () => {
|
|
270
|
+
isOpen = true;
|
|
271
|
+
resolve();
|
|
272
|
+
};
|
|
273
|
+
});
|
|
274
|
+
const answer = await started.call(openRequest.slice());
|
|
275
|
+
noteSync(answer);
|
|
276
|
+
const result = decode(answer);
|
|
277
|
+
if (result.error) {
|
|
278
|
+
// This tab cannot open the database: it gives the lock to the next.
|
|
279
|
+
const error = new NcError(result.error.code, result.error.message);
|
|
280
|
+
for (const entry of sent.values())
|
|
281
|
+
entry.reject(error);
|
|
282
|
+
sent.clear();
|
|
283
|
+
failed = error;
|
|
284
|
+
closed = true;
|
|
285
|
+
worker = undefined;
|
|
286
|
+
started.terminate();
|
|
287
|
+
release?.();
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
ownOpen = decode(openRequest);
|
|
291
|
+
opened();
|
|
292
|
+
post({ kind: "owner", tab: me });
|
|
293
|
+
ownerIs(me);
|
|
294
|
+
// Every tab re-reads what it follows from the new owner.
|
|
295
|
+
const reopened = encode({ event: "reopened", sync, ...relayOfOwner() });
|
|
296
|
+
emit(reopened);
|
|
297
|
+
post({ kind: "event", bytes: reopened });
|
|
298
|
+
// Taken over while hidden, for calls to send: give it up again once they are answered.
|
|
299
|
+
if (!visible())
|
|
300
|
+
giveUp();
|
|
301
|
+
};
|
|
302
|
+
const held = (lock) => {
|
|
303
|
+
if (!lock)
|
|
304
|
+
return;
|
|
305
|
+
return new Promise((resolve) => {
|
|
306
|
+
release = resolve;
|
|
307
|
+
});
|
|
308
|
+
};
|
|
309
|
+
// This tab's wait for the lock, while it waits.
|
|
310
|
+
let queued;
|
|
311
|
+
/** Waits for the lock, unless this tab owns the database or already waits: only while its page is visible or it has calls to send, so the visible tab takes over first. */
|
|
312
|
+
function queue() {
|
|
313
|
+
if (closed || worker !== undefined || queued !== undefined)
|
|
314
|
+
return;
|
|
315
|
+
if (!visible() && sent.size === 0)
|
|
316
|
+
return;
|
|
317
|
+
const wait = new AbortController();
|
|
318
|
+
queued = wait;
|
|
319
|
+
waiting.signal.addEventListener("abort", () => wait.abort());
|
|
320
|
+
void platform.locks
|
|
321
|
+
.request(lockName, { signal: wait.signal }, (lock) => {
|
|
322
|
+
queued = undefined;
|
|
323
|
+
const holding = held(lock);
|
|
324
|
+
void takeOver();
|
|
325
|
+
return holding;
|
|
326
|
+
})
|
|
327
|
+
.catch(() => {
|
|
328
|
+
// Aborted: this tab closed, or was hidden with nothing to send.
|
|
329
|
+
if (queued === wait)
|
|
330
|
+
queued = undefined;
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
/** Stops waiting for the lock while this tab is hidden and has nothing to send. */
|
|
334
|
+
function unqueueIfIdle() {
|
|
335
|
+
if (queued !== undefined && !visible() && sent.size === 0) {
|
|
336
|
+
queued.abort();
|
|
337
|
+
queued = undefined;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
/** Gives the database up (NOC-332): answers what this tab's worker took, tells the others, and releases the lock. With nothing in flight it does so at once, inside the hide handler, since a phone runs nothing after it once it freezes the page. */
|
|
341
|
+
function giveUp() {
|
|
342
|
+
if (worker === undefined || yielding)
|
|
343
|
+
return;
|
|
344
|
+
yielding = true;
|
|
345
|
+
if (isOpen && serving.size === 0)
|
|
346
|
+
return handOver();
|
|
347
|
+
void (async () => {
|
|
348
|
+
await ready;
|
|
349
|
+
while (serving.size > 0)
|
|
350
|
+
await Promise.allSettled([...serving]);
|
|
351
|
+
handOver();
|
|
352
|
+
})();
|
|
353
|
+
}
|
|
354
|
+
/** Tells the others this tab answered all it took, stops its worker and releases the lock. */
|
|
355
|
+
function handOver() {
|
|
356
|
+
const stopping = worker;
|
|
357
|
+
post({ kind: "gone", tab: me, yielded: true });
|
|
358
|
+
worker = undefined;
|
|
359
|
+
owner = undefined;
|
|
360
|
+
isOpen = false;
|
|
361
|
+
ready = new Promise((resolve) => {
|
|
362
|
+
opened = () => {
|
|
363
|
+
isOpen = true;
|
|
364
|
+
resolve();
|
|
365
|
+
};
|
|
366
|
+
});
|
|
367
|
+
stopping?.terminate();
|
|
368
|
+
const releasing = release;
|
|
369
|
+
release = undefined;
|
|
370
|
+
yielding = false;
|
|
371
|
+
releasing?.();
|
|
372
|
+
queue();
|
|
373
|
+
}
|
|
374
|
+
const owns = await new Promise((resolve) => {
|
|
375
|
+
void platform.locks.request(lockName, { ifAvailable: true }, (lock) => {
|
|
376
|
+
resolve(Boolean(lock));
|
|
377
|
+
return held(lock);
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
if (owns) {
|
|
381
|
+
own();
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
post({ kind: "who" });
|
|
385
|
+
queue();
|
|
386
|
+
}
|
|
387
|
+
platform.page?.onVisibility((now) => {
|
|
388
|
+
if (closed)
|
|
389
|
+
return;
|
|
390
|
+
if (!now) {
|
|
391
|
+
if (worker !== undefined)
|
|
392
|
+
giveUp();
|
|
393
|
+
else
|
|
394
|
+
unqueueIfIdle();
|
|
395
|
+
}
|
|
396
|
+
else
|
|
397
|
+
queue();
|
|
398
|
+
});
|
|
399
|
+
const shutDown = () => {
|
|
400
|
+
if (closed)
|
|
401
|
+
return;
|
|
402
|
+
closed = true;
|
|
403
|
+
waiting.abort();
|
|
404
|
+
if (worker !== undefined) {
|
|
405
|
+
post({ kind: "gone", tab: me });
|
|
406
|
+
worker.terminate();
|
|
407
|
+
release?.();
|
|
408
|
+
}
|
|
409
|
+
for (const entry of sent.values()) {
|
|
410
|
+
entry.reject(new NcError("closed", texts.DATABASE_CLOSED));
|
|
411
|
+
}
|
|
412
|
+
sent.clear();
|
|
413
|
+
channel.close();
|
|
414
|
+
};
|
|
415
|
+
return {
|
|
416
|
+
call: async (bytes) => {
|
|
417
|
+
if (failed)
|
|
418
|
+
throw failed;
|
|
419
|
+
const request = decode(bytes);
|
|
420
|
+
if (request.call === "open") {
|
|
421
|
+
openRequest = bytes.slice();
|
|
422
|
+
openGiven();
|
|
423
|
+
}
|
|
424
|
+
if (worker !== undefined && !yielding) {
|
|
425
|
+
const answer = await track(worker.call(bytes));
|
|
426
|
+
if (request.call === "open") {
|
|
427
|
+
noteSync(answer);
|
|
428
|
+
ownOpen = request;
|
|
429
|
+
opened();
|
|
430
|
+
}
|
|
431
|
+
return answer;
|
|
432
|
+
}
|
|
433
|
+
if (request.call === "close") {
|
|
434
|
+
shutDown();
|
|
435
|
+
return encode({ id: request.id, ok: null });
|
|
436
|
+
}
|
|
437
|
+
return send(bytes, request.call);
|
|
438
|
+
},
|
|
439
|
+
terminate: shutDown,
|
|
440
|
+
listen: (given) => {
|
|
441
|
+
listener = given;
|
|
442
|
+
},
|
|
443
|
+
};
|
|
444
|
+
};
|
package/dist/texts.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** `adHocCopyNotRead` (invalidValue): db.spaces.createAdHoc, given in copy an object the SDK of this database did not read, such as a spread of one or an object of another database. */
|
|
2
|
+
export declare const AD_HOC_COPY_NOT_READ = "copy takes objects this database read, as space.get, find or subscribe gives them, not a copy of one";
|
|
3
|
+
/** `anotherLock` (modelRefused): nc.open in a tab while another tab of the origin has the database open with another lock. */
|
|
4
|
+
export declare const ANOTHER_LOCK = "another tab has this database open with another lock";
|
|
5
|
+
/** `cursorOfAnotherDatabase` (cursorOfAnotherModel): db.find or space.find, given a page's cursor from another database. */
|
|
6
|
+
export declare const CURSOR_OF_ANOTHER_DATABASE = "the cursor is not of this database; start from the first page";
|
|
7
|
+
/** `databaseClosed` (closed): Any call after db.close(), and the calls still waiting when it closes. */
|
|
8
|
+
export declare const DATABASE_CLOSED = "the database is closed";
|
|
9
|
+
/** `lockFieldDiffers` (lockOutOfDate): nc.open, on a model whose field has another type or other options in the lock, as when the lock was not built again after the field changed. */
|
|
10
|
+
export declare const lockFieldDiffers: (collection: unknown, field: unknown) => string;
|
|
11
|
+
/** `lockHasNoCollection` (lockOutOfDate): nc.open, on a model that names a collection the lock does not have, as when the lock was not built again after the collection was added. */
|
|
12
|
+
export declare const lockHasNoCollection: (collection: unknown) => string;
|
|
13
|
+
/** `lockHasNoField` (lockOutOfDate): nc.open, on a model that names a field the lock does not have, as when the lock was not built again after the field was added. */
|
|
14
|
+
export declare const lockHasNoField: (collection: unknown, field: unknown) => string;
|
|
15
|
+
/** `lockHasNoIndex` (lockOutOfDate): nc.open, on a model that names an index the lock does not have, as when the lock was not built again after the index was added. */
|
|
16
|
+
export declare const lockHasNoIndex: (collection: unknown, index: unknown) => string;
|
|
17
|
+
/** `lockIndexDiffers` (lockOutOfDate): nc.open, on a model whose index has other fields, another equality prefix or another scope in the lock. */
|
|
18
|
+
export declare const lockIndexDiffers: (collection: unknown, index: unknown) => string;
|
|
19
|
+
/** `noSuchCollection` (notFound): A call given a collection that is not a handle of the model nc.open was given. */
|
|
20
|
+
export declare const NO_SUCH_COLLECTION = "the model has no such collection; pass a handle such as model.tasks";
|
|
21
|
+
/** `notACollection`: ncdb model build, on a model whose collection is not declared with nc.collection. */
|
|
22
|
+
export declare const notACollection: (collection: unknown) => string;
|
|
23
|
+
/** `notAComputedField`: ncdb model build, on a model whose computed field is not declared with nc.reach. */
|
|
24
|
+
export declare const notAComputedField: (collection: unknown, key: unknown) => string;
|
|
25
|
+
/** `notADatabaseName` (invalidValue): nc.open, given a name that breaks RFC 0006's rule for a browser database's name. */
|
|
26
|
+
export declare const NOT_ADATABASE_NAME = "a database name is 1 to 64 characters of a-z, 0-9, - and _";
|
|
27
|
+
/** `notAField`: ncdb model build, on a model whose field is not declared with a field function. */
|
|
28
|
+
export declare const notAField: (path: unknown) => string;
|
|
29
|
+
/** `notAPairingAnswer` (invalidValue): nc.pairingAnswer, given a text that does not start with ncdb-pair-1: or is not unpadded base64url. */
|
|
30
|
+
export declare const NOT_APAIRING_ANSWER = "the text is not a pairing answer: ncdb-pair-1: and unpadded base64url";
|
|
31
|
+
/** `notAQuery` (invalidValue): A subscription to db.find or space.find, given something that is not a query. */
|
|
32
|
+
export declare const NOT_AQUERY = "not a query";
|
|
33
|
+
/** `notAQueryStartOne` (invalidValue): db.find or space.find, given something that is not a query. */
|
|
34
|
+
export declare const NOT_AQUERY_START_ONE = "not a query; start one from an index, such as model.tasks.indexes.byPriority";
|
|
35
|
+
/** `notASafeInteger` (invalidValue): A write of an nc.int() field given a number that is not a safe integer. */
|
|
36
|
+
export declare const notASafeInteger: (collection: unknown, field: unknown) => string;
|
|
37
|
+
/** `notAView`: ncdb model build, on a model whose view is not declared with nc.view. */
|
|
38
|
+
export declare const notAView: (collection: unknown, key: unknown) => string;
|
|
39
|
+
/** `notAnIndex`: ncdb model build, on a model whose index is not declared with nc.index. */
|
|
40
|
+
export declare const notAnIndex: (collection: unknown, key: unknown) => string;
|
|
41
|
+
/** `notAnInvitation` (invalidValue): nc.invitation, given a text that does not start with ncdb-invite-1: or is not unpadded base64url. */
|
|
42
|
+
export declare const NOT_AN_INVITATION = "the text is not an invitation: ncdb-invite-1: and unpadded base64url";
|
|
43
|
+
/** `ownerTabClosed` (storageFailed): A call that changes the database, sent to the tab that owned it, when that tab ended before it answered (NOC-177). */
|
|
44
|
+
export declare const OWNER_TAB_CLOSED = "the tab that held the database closed before it answered; the call may or may not have run";
|
|
45
|
+
/** `reservedFieldType`: ncdb model build, on a model with nc.richText or nc.stream, whose field types a later RFC designs. */
|
|
46
|
+
export declare const reservedFieldType: (path: unknown, type: unknown) => string;
|
|
47
|
+
/** `setElementModified`: ncdb model build, on a model whose nc.set element is personal, described or deprecated. */
|
|
48
|
+
export declare const setElementModified: (path: unknown) => string;
|
|
49
|
+
/** `setElementNotBasic`: ncdb model build, on a model whose nc.set is of something other than a basic value. */
|
|
50
|
+
export declare const setElementNotBasic: (path: unknown) => string;
|
|
51
|
+
/** `workerFailed` (storageFailed): Every call waiting on the SDK's worker when the worker fails, with the browser's own message. */
|
|
52
|
+
export declare const workerFailed: (message: unknown) => string;
|