@nocentraldb/sdk 0.2.0-alpha.3 → 0.2.0-alpha.5
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/coreGuard.d.ts +13 -0
- package/dist/coreGuard.js +68 -0
- package/dist/texts.d.ts +4 -0
- package/dist/texts.js +4 -0
- package/dist/wasm/nocentraldb_wasm.js +3 -3
- package/dist/wasm/nocentraldb_wasm_bg.wasm +0 -0
- package/dist/worker.js +16 -17
- package/package.json +1 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** The core as the worker calls it: one bridge request in, its answer out. */
|
|
2
|
+
export type Handle = (request: Uint8Array) => Promise<Uint8Array>;
|
|
3
|
+
/**
|
|
4
|
+
* Answers each request with the core that `ready` gives, or with
|
|
5
|
+
* `storageFailed` once the core failed. `failed` takes an error the core
|
|
6
|
+
* threw outside a call, which the worker sees as an uncaught one: a trap in
|
|
7
|
+
* a step the core runs after the call returned leaves that call's promise
|
|
8
|
+
* unsettled, so the request it was answering gets its answer here.
|
|
9
|
+
*/
|
|
10
|
+
export declare const guardCore: (ready: Promise<Handle>) => {
|
|
11
|
+
answer: (request: Uint8Array) => Promise<Uint8Array>;
|
|
12
|
+
failed: (error: unknown) => void;
|
|
13
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// What the SDK's worker (worker.ts) answers in the core's place when the
|
|
2
|
+
// core fails: the module did not load, or the core trapped, as WebAssembly
|
|
3
|
+
// does when it runs out of memory. Every request still gets an answer, so
|
|
4
|
+
// the page never waits for one that does not come. A core that failed is
|
|
5
|
+
// not called again: a trap leaves its memory as it was mid-call, a bridge
|
|
6
|
+
// taken out among it, so it is broken for good (NOC-426). Every later
|
|
7
|
+
// request fails with `storageFailed` and a text that tells the app to open
|
|
8
|
+
// the database again, which starts a new worker; a `close` succeeds, so the
|
|
9
|
+
// app can. Internal: the package's entry does not reach it.
|
|
10
|
+
import { decode, encode } from "./cbor.js";
|
|
11
|
+
import * as texts from "./texts.js";
|
|
12
|
+
/** The id and the call of `request`, as far as they can be read. */
|
|
13
|
+
const read = (request) => {
|
|
14
|
+
try {
|
|
15
|
+
const { id, call } = decode(request);
|
|
16
|
+
return { id: typeof id === "number" ? id : 0, call };
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return { id: 0 };
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/** A `storageFailed` answer to `request`. */
|
|
23
|
+
const failure = (request, message) => encode({ id: read(request).id, error: { code: "storageFailed", message } });
|
|
24
|
+
/** The answer to `request` once the core failed. */
|
|
25
|
+
const stopped = (request) => {
|
|
26
|
+
const { id, call } = read(request);
|
|
27
|
+
return call === "close"
|
|
28
|
+
? encode({ id, ok: null })
|
|
29
|
+
: encode({
|
|
30
|
+
id,
|
|
31
|
+
error: { code: "storageFailed", message: texts.CORE_STOPPED },
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Answers each request with the core that `ready` gives, or with
|
|
36
|
+
* `storageFailed` once the core failed. `failed` takes an error the core
|
|
37
|
+
* threw outside a call, which the worker sees as an uncaught one: a trap in
|
|
38
|
+
* a step the core runs after the call returned leaves that call's promise
|
|
39
|
+
* unsettled, so the request it was answering gets its answer here.
|
|
40
|
+
*/
|
|
41
|
+
export const guardCore = (ready) => {
|
|
42
|
+
let broken = false;
|
|
43
|
+
let waiting;
|
|
44
|
+
return {
|
|
45
|
+
answer: async (request) => {
|
|
46
|
+
if (broken)
|
|
47
|
+
return stopped(request);
|
|
48
|
+
try {
|
|
49
|
+
const core = await ready;
|
|
50
|
+
return await new Promise((resolve, reject) => {
|
|
51
|
+
waiting = reject;
|
|
52
|
+
core(request).then(resolve, reject);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
broken = true;
|
|
57
|
+
return failure(request, texts.coreFailed(String(error)));
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
waiting = undefined;
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
failed: (error) => {
|
|
64
|
+
broken = true;
|
|
65
|
+
waiting?.(error);
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
};
|
package/dist/texts.d.ts
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
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
3
|
/** `anotherLock` (modelRefused): nc.open in a tab while another tab of the origin has the database open with another lock. */
|
|
4
4
|
export declare const ANOTHER_LOCK = "another tab has this database open with another lock";
|
|
5
|
+
/** `coreFailed` (storageFailed): The call the SDK's worker was answering when its core failed, such as a write too large for the core's memory, with the browser's own message. */
|
|
6
|
+
export declare const coreFailed: (message: unknown) => string;
|
|
7
|
+
/** `coreStopped` (storageFailed): Every call after the SDK's worker's core failed in this tab, until the app closes the database and opens it again. */
|
|
8
|
+
export declare const CORE_STOPPED = "the database's core stopped after an earlier failure, such as a write too large for its memory: close the database and open it again";
|
|
5
9
|
/** `cursorOfAnotherDatabase` (cursorOfAnotherModel): db.find or space.find, given a page's cursor from another database. */
|
|
6
10
|
export declare const CURSOR_OF_ANOTHER_DATABASE = "the cursor is not of this database; start from the first page";
|
|
7
11
|
/** `databaseClosed` (closed): Any call after db.close(), and the calls still waiting when it closes. */
|
package/dist/texts.js
CHANGED
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
export 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";
|
|
9
9
|
/** `anotherLock` (modelRefused): nc.open in a tab while another tab of the origin has the database open with another lock. */
|
|
10
10
|
export const ANOTHER_LOCK = "another tab has this database open with another lock";
|
|
11
|
+
/** `coreFailed` (storageFailed): The call the SDK's worker was answering when its core failed, such as a write too large for the core's memory, with the browser's own message. */
|
|
12
|
+
export const coreFailed = (message) => `the database's core failed: ${message}`;
|
|
13
|
+
/** `coreStopped` (storageFailed): Every call after the SDK's worker's core failed in this tab, until the app closes the database and opens it again. */
|
|
14
|
+
export const CORE_STOPPED = "the database's core stopped after an earlier failure, such as a write too large for its memory: close the database and open it again";
|
|
11
15
|
/** `cursorOfAnotherDatabase` (cursorOfAnotherModel): db.find or space.find, given a page's cursor from another database. */
|
|
12
16
|
export const CURSOR_OF_ANOTHER_DATABASE = "the cursor is not of this database; start from the first page";
|
|
13
17
|
/** `databaseClosed` (closed): Any call after db.close(), and the calls still waiting when it closes. */
|
|
@@ -667,17 +667,17 @@ function __wbg_get_imports() {
|
|
|
667
667
|
return ret;
|
|
668
668
|
}, arguments); },
|
|
669
669
|
__wbindgen_generic_0000000000000001: function(arg0, arg1) {
|
|
670
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx:
|
|
670
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 187, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
|
|
671
671
|
const ret = makeMutClosure(arg0, arg1, wasm_bindgen_70735ce8c264698d___convert__closures_____invoke___wasm_bindgen_70735ce8c264698d___JsValue______true_);
|
|
672
672
|
return ret;
|
|
673
673
|
},
|
|
674
674
|
__wbindgen_generic_0000000000000002: function(arg0, arg1) {
|
|
675
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx:
|
|
675
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 953, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
|
|
676
676
|
const ret = makeMutClosure(arg0, arg1, wasm_bindgen_70735ce8c264698d___convert__closures_____invoke___wasm_bindgen_70735ce8c264698d___JsValue__core_37ee0ce0c4cd57d5___result__Result_____wasm_bindgen_70735ce8c264698d___JsError___true_);
|
|
677
677
|
return ret;
|
|
678
678
|
},
|
|
679
679
|
__wbindgen_generic_0000000000000003: function(arg0, arg1) {
|
|
680
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("Event")], shim_idx:
|
|
680
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("Event")], shim_idx: 187, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
|
|
681
681
|
const ret = makeMutClosure(arg0, arg1, wasm_bindgen_70735ce8c264698d___convert__closures_____invoke___wasm_bindgen_70735ce8c264698d___JsValue______true__7);
|
|
682
682
|
return ret;
|
|
683
683
|
},
|
|
Binary file
|
package/dist/worker.js
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
// then (docs/getting-started-browser.md, "Safari"), and while it waits its
|
|
20
20
|
// tab reports `waitingForTab` in `db.durability` (NOC-355).
|
|
21
21
|
import { decode, encode } from "./cbor.js";
|
|
22
|
+
import { guardCore } from "./coreGuard.js";
|
|
22
23
|
import { waitNotice } from "./waitNotice.js";
|
|
23
24
|
// @ts-expect-error: generated into dist/wasm/ by the build step.
|
|
24
25
|
import init, { Core } from "./wasm/nocentraldb_wasm.js";
|
|
@@ -35,23 +36,21 @@ const ready = init({
|
|
|
35
36
|
});
|
|
36
37
|
// Requests are answered one at a time, in the order they came.
|
|
37
38
|
let queue = Promise.resolve();
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
};
|
|
39
|
+
const core = guardCore(ready.then((bridge) => (request) => bridge.handle(request)));
|
|
40
|
+
const answer = core.answer;
|
|
41
|
+
// An error nothing caught in this worker comes from the core: a trap in a
|
|
42
|
+
// step it runs after a call returned, as when a write is too large for its
|
|
43
|
+
// memory (NOC-426). The guard answers the waiting request and every later
|
|
44
|
+
// one with `storageFailed`; the error stops here, so the page sees the
|
|
45
|
+
// nc.Error and no uncaught one.
|
|
46
|
+
self.addEventListener("error", (event) => {
|
|
47
|
+
event.preventDefault();
|
|
48
|
+
core.failed(event.error ?? event.message);
|
|
49
|
+
});
|
|
50
|
+
self.addEventListener("unhandledrejection", (event) => {
|
|
51
|
+
event.preventDefault();
|
|
52
|
+
core.failed(event.reason);
|
|
53
|
+
});
|
|
55
54
|
/** What the page sends, besides bridge requests, to end the worker (workerHost.ts). */
|
|
56
55
|
const STOP = "stop";
|
|
57
56
|
// The lock this worker holds while it may hold the database's storage:
|