@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.
Files changed (58) hide show
  1. package/dist/account.d.ts +14 -0
  2. package/dist/account.js +34 -0
  3. package/dist/accountCalls.d.ts +34 -0
  4. package/dist/accountCalls.js +48 -0
  5. package/dist/adHoc.d.ts +49 -0
  6. package/dist/adHoc.js +52 -0
  7. package/dist/cbor.d.ts +8 -0
  8. package/dist/cbor.js +223 -0
  9. package/dist/connect.d.ts +14 -0
  10. package/dist/connect.js +527 -0
  11. package/dist/connectParts.d.ts +29 -0
  12. package/dist/connectParts.js +10 -0
  13. package/dist/db.d.ts +291 -0
  14. package/dist/db.js +17 -0
  15. package/dist/durability.d.ts +20 -0
  16. package/dist/durability.js +5 -0
  17. package/dist/durabilityReport.d.ts +29 -0
  18. package/dist/durabilityReport.js +128 -0
  19. package/dist/elements.d.ts +5 -0
  20. package/dist/elements.js +13 -0
  21. package/dist/error.d.ts +10 -0
  22. package/dist/error.js +23 -0
  23. package/dist/index.d.ts +19 -0
  24. package/dist/index.js +6 -0
  25. package/dist/lockCheck.d.ts +4 -0
  26. package/dist/lockCheck.js +63 -0
  27. package/dist/members.d.ts +55 -0
  28. package/dist/members.js +38 -0
  29. package/dist/membership.d.ts +5 -0
  30. package/dist/membership.js +29 -0
  31. package/dist/nc.d.ts +153 -0
  32. package/dist/nc.js +211 -0
  33. package/dist/query.d.ts +21 -0
  34. package/dist/query.js +45 -0
  35. package/dist/reports.d.ts +8 -0
  36. package/dist/reports.js +50 -0
  37. package/dist/spaceList.d.ts +14 -0
  38. package/dist/spaceList.js +45 -0
  39. package/dist/status.d.ts +51 -0
  40. package/dist/status.js +5 -0
  41. package/dist/sync.d.ts +15 -0
  42. package/dist/sync.js +4 -0
  43. package/dist/tabs.d.ts +30 -0
  44. package/dist/tabs.js +444 -0
  45. package/dist/texts.d.ts +52 -0
  46. package/dist/texts.js +58 -0
  47. package/dist/waitNotice.d.ts +7 -0
  48. package/dist/waitNotice.js +32 -0
  49. package/dist/wasm/nocentraldb_wasm.d.ts +91 -0
  50. package/dist/wasm/nocentraldb_wasm.js +1058 -0
  51. package/dist/wasm/nocentraldb_wasm_bg.wasm +0 -0
  52. package/dist/wasm/nocentraldb_wasm_bg.wasm.d.ts +36 -0
  53. package/dist/worker.d.ts +1 -0
  54. package/dist/worker.js +142 -0
  55. package/dist/workerHost.d.ts +3 -0
  56. package/dist/workerHost.js +55 -0
  57. package/package.json +21 -4
  58. package/README.md +0 -3
@@ -0,0 +1,36 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export const __wbg_core_free: (a: number, b: number) => void;
5
+ export const core_handle: (a: number, b: number, c: number) => any;
6
+ export const core_listen: (a: number, b: any) => void;
7
+ export const core_new: () => number;
8
+ export const core_release: (a: number) => number;
9
+ export const rust_sqlite_wasm_host_sleep: (a: bigint, b: number) => void;
10
+ export const rust_sqlite_wasm_abort: () => void;
11
+ export const rust_sqlite_wasm_assert_fail: (a: number, b: number, c: number, d: number) => void;
12
+ export const rust_sqlite_wasm_calloc: (a: number, b: number) => number;
13
+ export const rust_sqlite_wasm_free: (a: number) => void;
14
+ export const rust_sqlite_wasm_getentropy: (a: number, b: number) => number;
15
+ export const rust_sqlite_wasm_host_epoch_timestamp_in_ms: (a: number) => number;
16
+ export const rust_sqlite_wasm_host_fill_entropy: (a: number, b: number) => number;
17
+ export const rust_sqlite_wasm_host_localtime: (a: bigint, b: number) => number;
18
+ export const rust_sqlite_wasm_host_random: (a: number, b: number) => number;
19
+ export const rust_sqlite_wasm_localtime: (a: number) => number;
20
+ export const rust_sqlite_wasm_malloc: (a: number) => number;
21
+ export const rust_sqlite_wasm_realloc: (a: number, b: number) => number;
22
+ export const sqlite3_os_end: () => number;
23
+ export const sqlite3_os_init: () => number;
24
+ export const wasm_bindgen_70735ce8c264698d___convert__closures_____invoke___js_sys_7fe34edb73153ca3___Function_fn_wasm_bindgen_70735ce8c264698d___JsValue_____wasm_bindgen_70735ce8c264698d___sys__Undefined___js_sys_7fe34edb73153ca3___Function_fn_wasm_bindgen_70735ce8c264698d___JsValue_____wasm_bindgen_70735ce8c264698d___sys__Undefined_______true_: (a: number, b: number, c: any, d: any) => void;
25
+ export const wasm_bindgen_70735ce8c264698d___convert__closures_____invoke___wasm_bindgen_70735ce8c264698d___JsValue__core_37ee0ce0c4cd57d5___result__Result_____wasm_bindgen_70735ce8c264698d___JsError___true_: (a: number, b: number, c: any) => [number, number];
26
+ export const wasm_bindgen_70735ce8c264698d___convert__closures_____invoke___wasm_bindgen_70735ce8c264698d___JsValue______true_: (a: number, b: number, c: any) => void;
27
+ export const wasm_bindgen_70735ce8c264698d___convert__closures_____invoke___wasm_bindgen_70735ce8c264698d___JsValue______true__7: (a: number, b: number, c: any) => void;
28
+ export const __wbindgen_malloc: (a: number, b: number) => number;
29
+ export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
30
+ export const __wbindgen_free: (a: number, b: number, c: number) => void;
31
+ export const __wbindgen_exn_store: (a: number) => void;
32
+ export const __externref_table_alloc: () => number;
33
+ export const __wbindgen_externrefs: WebAssembly.Table;
34
+ export const __wbindgen_destroy_closure: (a: number, b: number) => void;
35
+ export const __externref_table_dealloc: (a: number) => void;
36
+ export const __wbindgen_start: () => void;
@@ -0,0 +1 @@
1
+ export {};
package/dist/worker.js ADDED
@@ -0,0 +1,142 @@
1
+ // The dedicated worker the SDK starts for the core (RFC 0009, "Open and
2
+ // close"; Carsten, 2026-11-12, NOC-274). It loads the core's WebAssembly
3
+ // module, shipped next to it in the package's build output, and answers
4
+ // each message, one bridge request in CBOR, with the bridge's answer, in
5
+ // order, as `{answer}`. The core's fold events, which the SDK's
6
+ // subscriptions re-run on, go to the page as `{event}`. The module's glue is
7
+ // generated by wasm-bindgen at build time (packages/sdk/build.mjs); it is
8
+ // not in src/.
9
+ //
10
+ // The worker holds the Web Lock `nocentraldb/<name>/worker` from its open
11
+ // on, while it may hold the database's OPFS handles, and the next worker
12
+ // waits for it before it opens (NOC-352). Asked to stop, the worker answers
13
+ // what it took, closes the database, gives the handles back, releases the
14
+ // lock and ends itself. Ending it from the page would leave the handles to
15
+ // the browser, which may free them later than the lock passes on, and the
16
+ // next worker's open would fail. In WebKit the worker can give them back
17
+ // only while its page runs, so a tab that freezes right after it gave the
18
+ // database up keeps them until it runs again; the next worker waits until
19
+ // then (docs/getting-started-browser.md, "Safari"), and while it waits its
20
+ // tab reports `waitingForTab` in `db.durability` (NOC-355).
21
+ import { decode, encode } from "./cbor.js";
22
+ import { waitNotice } from "./waitNotice.js";
23
+ // @ts-expect-error: generated into dist/wasm/ by the build step.
24
+ import init, { Core } from "./wasm/nocentraldb_wasm.js";
25
+ const post = (message) => {
26
+ const bytes = message.answer ?? message.event;
27
+ self.postMessage(message, bytes ? [bytes.buffer] : []);
28
+ };
29
+ const ready = init({
30
+ module_or_path: new URL("./wasm/nocentraldb_wasm_bg.wasm", import.meta.url),
31
+ }).then(() => {
32
+ const core = new Core();
33
+ core.listen((event) => post({ event }));
34
+ return core;
35
+ });
36
+ // Requests are answered one at a time, in the order they came.
37
+ let queue = Promise.resolve();
38
+ const answer = async (request) => {
39
+ try {
40
+ return await (await ready).handle(request);
41
+ }
42
+ catch (error) {
43
+ // The module did not load, or the core failed: the request still gets
44
+ // an answer, so the page never waits for one that does not come.
45
+ const { id } = decode(request);
46
+ return encode({
47
+ id,
48
+ error: {
49
+ code: "storageFailed",
50
+ message: `the core failed: ${String(error)}`,
51
+ },
52
+ });
53
+ }
54
+ };
55
+ /** What the page sends, besides bridge requests, to end the worker (workerHost.ts). */
56
+ const STOP = "stop";
57
+ // The lock this worker holds while it may hold the database's storage:
58
+ // `held` once it has it, `freed` once it gave it back.
59
+ let held;
60
+ let freed = Promise.resolve();
61
+ /** Tells this worker's tab whether it waits for the storage another tab still holds (NOC-355): the tab reports it as `db.durability`'s `waitingForTab`. */
62
+ const waiting = waitNotice((waiting) => post({ event: encode({ event: "waitingForTab", waiting }) }));
63
+ /** Asks for the lock of the database `name`; `holding` runs once it is held, `busy` at once if another worker holds it. */
64
+ const request = (name, ifAvailable, holding) => navigator.locks.request(`nocentraldb/${name}/worker`, { ifAvailable }, (lock) => lock === null
65
+ ? false
66
+ : new Promise((done) => {
67
+ held = done;
68
+ holding();
69
+ }));
70
+ /** Holds the lock of the database `name`; while a worker that is still ending holds it, waits, and says so (NOC-355). */
71
+ const hold = (name) => new Promise((taken) => {
72
+ const tried = request(name, true, taken);
73
+ freed = tried;
74
+ void tried.then((answer) => {
75
+ if (answer !== false)
76
+ return;
77
+ waiting.started();
78
+ freed = request(name, false, () => {
79
+ waiting.ended();
80
+ taken();
81
+ });
82
+ });
83
+ });
84
+ /** The database an `open` request names, or undefined for any other request. */
85
+ const opens = (request) => {
86
+ try {
87
+ const { call, args } = decode(request);
88
+ return call === "open" && typeof args?.name === "string"
89
+ ? args.name
90
+ : undefined;
91
+ }
92
+ catch {
93
+ return undefined;
94
+ }
95
+ };
96
+ /**
97
+ * How often an open that found the storage held is tried again, and how
98
+ * long it waits between tries, in milliseconds. A worker that ended
99
+ * before it gave the storage back, because its tab closed, leaves the
100
+ * handles to the browser, which may free them after the lock (NOC-352).
101
+ */
102
+ const RETRIES = 30;
103
+ const RETRY_AFTER = 100;
104
+ /** Whether `answer` says the storage could not be opened. */
105
+ const storageFailed = (answer) => decode(answer).error?.code ===
106
+ "storageFailed";
107
+ /** The answer to an open of the database `name`: once its lock is held, and tried again while another worker's handles are not yet free. */
108
+ const open = async (name, request) => {
109
+ if (held === undefined && "locks" in navigator)
110
+ await hold(name);
111
+ let answered = await answer(request.slice());
112
+ for (let tried = 0; tried < RETRIES && storageFailed(answered); tried++) {
113
+ await new Promise((resolve) => setTimeout(resolve, RETRY_AFTER));
114
+ answered = await answer(request.slice());
115
+ }
116
+ return answered;
117
+ };
118
+ /** Closes the database, gives its handles and the lock back, and ends the worker. */
119
+ const stop = async () => {
120
+ try {
121
+ (await ready).release();
122
+ }
123
+ catch {
124
+ // The module did not load: there are no handles to give back.
125
+ }
126
+ held?.();
127
+ await freed;
128
+ self.close();
129
+ };
130
+ self.onmessage = (event) => {
131
+ const request = event.data;
132
+ if (request === STOP) {
133
+ queue = queue.then(stop);
134
+ return;
135
+ }
136
+ queue = queue.then(async () => {
137
+ const name = opens(request);
138
+ post({
139
+ answer: name === undefined ? await answer(request) : await open(name, request),
140
+ });
141
+ });
142
+ };
@@ -0,0 +1,3 @@
1
+ import type { Core } from "./connect.ts";
2
+ /** The worker that runs the core, shipped next to this module. */
3
+ export declare const startWorker: () => Core;
@@ -0,0 +1,55 @@
1
+ // The page's side of the worker that runs the core (RFC 0009, "Open and
2
+ // close"): it starts the worker shipped next to this module, sends it one
3
+ // bridge request at a time and hands back its answers, in order, and its
4
+ // events. Asked to end, it lets the worker end itself, so that it gives
5
+ // the database's storage back first (NOC-352). Internal: `connect` and the tabs layer reach it as a `Core`.
6
+ import { NcError } from "./error.js";
7
+ import * as texts from "./texts.js";
8
+ /** What the page sends the worker, besides bridge requests, to end it (worker.ts). */
9
+ const STOP = "stop";
10
+ /** The worker that runs the core, shipped next to this module. */
11
+ export const startWorker = () => {
12
+ const worker = new Worker(new URL("./worker.js", import.meta.url), {
13
+ type: "module",
14
+ name: "nocentraldb",
15
+ });
16
+ const waiting = [];
17
+ let failed;
18
+ const failing = [];
19
+ let listener;
20
+ // The worker sends `{answer}` for each request, in order, and `{event}`
21
+ // for what the core reports unasked.
22
+ worker.onmessage = (message) => {
23
+ const { answer, event } = message.data;
24
+ if (event !== undefined) {
25
+ listener?.(event);
26
+ return;
27
+ }
28
+ failing.shift();
29
+ if (answer !== undefined)
30
+ waiting.shift()?.(answer);
31
+ };
32
+ worker.onerror = (event) => {
33
+ failed = new NcError("storageFailed", texts.workerFailed(event.message));
34
+ for (const fail of failing.splice(0))
35
+ fail(failed);
36
+ waiting.length = 0;
37
+ };
38
+ return {
39
+ call: (request) => new Promise((resolve, reject) => {
40
+ if (failed)
41
+ return reject(failed);
42
+ waiting.push(resolve);
43
+ failing.push(reject);
44
+ worker.postMessage(request, [request.buffer]);
45
+ }),
46
+ // The worker answers what it took, gives the database's storage back
47
+ // and ends itself (NOC-352, worker.ts). Never ended from here, not
48
+ // even late: the browser would free its storage after the lock passes
49
+ // on, and the next tab's open would fail.
50
+ terminate: () => worker.postMessage(STOP),
51
+ listen: (given) => {
52
+ listener = given;
53
+ },
54
+ };
55
+ };
package/package.json CHANGED
@@ -1,6 +1,23 @@
1
1
  {
2
2
  "name": "@nocentraldb/sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-alpha.70",
4
+ "license": "MIT OR Apache-2.0",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/nocentraldb/nocentraldb.git",
8
+ "directory": "packages/sdk"
9
+ },
10
+ "type": "module",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "default": "./dist/index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "build": "node build.mjs"
22
+ }
23
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.