@aria-framework/db-worker 0.4.0 → 0.4.2
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/README.md +18 -0
- package/client.js +35 -6
- package/package.json +1 -1
- package/worker.js +19 -13
package/README.md
CHANGED
|
@@ -112,6 +112,24 @@ Model methods may be sync or async (thenables are awaited). BigInts in results
|
|
|
112
112
|
|
|
113
113
|
## Changelog
|
|
114
114
|
|
|
115
|
+
- **0.4.2** — sanitizeForTransfer passes `Map`/`Set` through untouched (like
|
|
116
|
+
Date/Buffer/typed arrays — structured clone handles them natively; the
|
|
117
|
+
generic object branch would silently mangle them to `{}`). Latent-bug guard
|
|
118
|
+
from an Acc101 review: no current consumer returns one across the boundary,
|
|
119
|
+
but the first future model method that does now works instead of delivering
|
|
120
|
+
empty data with no error signal.
|
|
121
|
+
- **0.4.1** — third-review fixes (all in the 0.4.0 additions). (1) Boot-crash
|
|
122
|
+
suppression latched: a hard boot crash emits 'error' AND 'exit'; the exit
|
|
123
|
+
handler no longer fires the death policy after the error handler consumed
|
|
124
|
+
the boot state. (2) init() resets the death latch and closing flag, so the
|
|
125
|
+
respawn pattern works (worker #2's death notifies again) and close()+re-init
|
|
126
|
+
doesn't inherit stale flags. (3) Queue warnings coalesce: first occurrence
|
|
127
|
+
logs, then at most one summary line per 10s with the suppressed count —
|
|
128
|
+
saturation no longer floods the log it's reporting into; wording clarifies
|
|
129
|
+
the wait includes IPC/event-loop overhead, not pure queue time.
|
|
130
|
+
(4) Sanitizer memoizes shared references (obj → sanitized node): linear
|
|
131
|
+
time on diamond-shaped graphs AND sharing is preserved through structured
|
|
132
|
+
clone; true cycles still throw via a separate ancestor set.
|
|
115
133
|
- **0.4.0** — second-review fixes. (1) sanitizeForTransfer tracks the ancestor
|
|
116
134
|
PATH (unwind delete), so shared/diamond references in results are legal
|
|
117
135
|
again — only true cycles throw; the error path is also guarded so an
|
package/client.js
CHANGED
|
@@ -48,6 +48,12 @@ class DbClient {
|
|
|
48
48
|
this._queueWarnMs = queueWarnMs === undefined ? 1000 : queueWarnMs;
|
|
49
49
|
this._onWorkerDeath = typeof onWorkerDeath === 'function' ? onWorkerDeath : null;
|
|
50
50
|
this._deathNotified = false;
|
|
51
|
+
// Queue-warn coalescing: under real saturation EVERY completed call crosses
|
|
52
|
+
// the threshold — one line each would flood the log (disk I/O during the
|
|
53
|
+
// very pile-up being reported). Log the first, then at most one summary
|
|
54
|
+
// line per window carrying the suppressed count.
|
|
55
|
+
this._queueWarnLastAt = 0;
|
|
56
|
+
this._queueWarnSuppressed = 0;
|
|
51
57
|
this._worker = null;
|
|
52
58
|
this._pending = new Map();
|
|
53
59
|
this._nextId = 0;
|
|
@@ -63,6 +69,12 @@ class DbClient {
|
|
|
63
69
|
*/
|
|
64
70
|
init(workerData) {
|
|
65
71
|
return new Promise((resolve, reject) => {
|
|
72
|
+
// Fresh lifecycle state per worker: a respawn policy re-init()s after a
|
|
73
|
+
// death, and a close()+re-init must not inherit the previous worker's
|
|
74
|
+
// latch/closing flags (they would suppress the NEW worker's crash
|
|
75
|
+
// handling and death notification).
|
|
76
|
+
this._deathNotified = false;
|
|
77
|
+
this._closing = false;
|
|
66
78
|
this._worker = new Worker(this._workerPath, { workerData });
|
|
67
79
|
this._initReject = reject;
|
|
68
80
|
|
|
@@ -94,9 +106,21 @@ class DbClient {
|
|
|
94
106
|
// the queue-wait pile-up.
|
|
95
107
|
this._logger.warn(`Slow DB call (${msg.execMs}ms exec, ${total}ms total): ${pending.label}`);
|
|
96
108
|
} else if (this._queueWarnMs && (total - msg.execMs) >= this._queueWarnMs) {
|
|
97
|
-
// Saturation signal: this call executed fast but
|
|
98
|
-
//
|
|
99
|
-
|
|
109
|
+
// Saturation signal: this call executed fast but its round trip was
|
|
110
|
+
// dominated by wait (worker queue + IPC/event-loop overhead — NOT
|
|
111
|
+
// pure DB queue time; a stalled client event loop shows here too).
|
|
112
|
+
// A worker drowning in fast queries would otherwise log nothing.
|
|
113
|
+
const now = Date.now();
|
|
114
|
+
if (now - this._queueWarnLastAt >= 10_000) {
|
|
115
|
+
const suppressed = this._queueWarnSuppressed;
|
|
116
|
+
this._queueWarnLastAt = now;
|
|
117
|
+
this._queueWarnSuppressed = 0;
|
|
118
|
+
this._logger.warn(`Slow DB queue (${total - msg.execMs}ms wait, ${msg.execMs}ms exec): ${pending.label}` +
|
|
119
|
+
(suppressed ? ` (+${suppressed} more delayed calls in the last 10s)` : '') +
|
|
120
|
+
' — calls backing up behind other work');
|
|
121
|
+
} else {
|
|
122
|
+
this._queueWarnSuppressed++;
|
|
123
|
+
}
|
|
100
124
|
}
|
|
101
125
|
}
|
|
102
126
|
if (msg.error) {
|
|
@@ -127,8 +151,12 @@ class DbClient {
|
|
|
127
151
|
}
|
|
128
152
|
this._pending.clear();
|
|
129
153
|
// Boot failures reject init() — the app's startup error handling owns
|
|
130
|
-
// logging + exit; firing the death policy here would preempt it.
|
|
131
|
-
|
|
154
|
+
// logging + exit; firing the death policy here would preempt it. Latch
|
|
155
|
+
// the suppression: a boot crash emits 'error' AND 'exit', and by the
|
|
156
|
+
// time 'exit' runs _initReject is already cleared — without the latch
|
|
157
|
+
// the exit handler would fire the policy the boot guard just skipped.
|
|
158
|
+
if (wasBooting) this._deathNotified = true;
|
|
159
|
+
else this._notifyDeath({ reason: 'error', error: err });
|
|
132
160
|
});
|
|
133
161
|
|
|
134
162
|
this._worker.on('exit', (code) => {
|
|
@@ -148,7 +176,8 @@ class DbClient {
|
|
|
148
176
|
p.reject(new Error(`Database worker exited with code ${code}`));
|
|
149
177
|
}
|
|
150
178
|
this._pending.clear();
|
|
151
|
-
if (
|
|
179
|
+
if (wasBooting) this._deathNotified = true;
|
|
180
|
+
else this._notifyDeath({ reason: 'exit', code });
|
|
152
181
|
}
|
|
153
182
|
});
|
|
154
183
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aria-framework/db-worker",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Aria App Framework — db-worker module. Async RPC bridge over synchronous better-sqlite3: a worker-thread dispatcher (runWorker), a main-thread client with per-call timeouts and crash handling (DbClient), and a SQLite layer with WAL pragmas + a numbered-migration runner (database).",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"private": false,
|
package/worker.js
CHANGED
|
@@ -84,31 +84,37 @@ function runWorker({ init }) {
|
|
|
84
84
|
* props → {}; a Buffer would become a numeric-keyed plain object). Circular
|
|
85
85
|
* result graphs throw a clear error instead of a RangeError stack overflow.
|
|
86
86
|
*/
|
|
87
|
-
function sanitizeForTransfer(obj,
|
|
87
|
+
function sanitizeForTransfer(obj, ctx) {
|
|
88
88
|
if (obj === null || obj === undefined) return obj;
|
|
89
89
|
if (typeof obj === 'bigint') return Number(obj);
|
|
90
90
|
if (typeof obj !== 'object') return obj;
|
|
91
|
-
if (obj instanceof Date || Buffer.isBuffer(obj) || ArrayBuffer.isView(obj)
|
|
92
|
-
|
|
93
|
-
//
|
|
94
|
-
//
|
|
95
|
-
//
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
|
|
91
|
+
if (obj instanceof Date || Buffer.isBuffer(obj) || ArrayBuffer.isView(obj)
|
|
92
|
+
|| obj instanceof Map || obj instanceof Set) return obj;
|
|
93
|
+
// Two structures: `path` (ancestor set) detects TRUE cycles — an object
|
|
94
|
+
// containing itself must throw; `memo` (obj → sanitized copy) makes shared/
|
|
95
|
+
// diamond references legal AND linear-time: the same source object always
|
|
96
|
+
// maps to the SAME sanitized node, so sharing survives structured clone and
|
|
97
|
+
// a diamond-shaped graph isn't re-walked once per referencing edge
|
|
98
|
+
// (unwind-only tracking was worst-case exponential on nested shared trees).
|
|
99
|
+
ctx = ctx || { path: new WeakSet(), memo: new WeakMap() };
|
|
100
|
+
if (ctx.path.has(obj)) {
|
|
99
101
|
throw new Error('DB worker result contains a circular reference and cannot cross the thread boundary');
|
|
100
102
|
}
|
|
101
|
-
|
|
103
|
+
if (ctx.memo.has(obj)) return ctx.memo.get(obj);
|
|
104
|
+
ctx.path.add(obj);
|
|
102
105
|
let result;
|
|
103
106
|
if (Array.isArray(obj)) {
|
|
104
|
-
result =
|
|
107
|
+
result = [];
|
|
108
|
+
ctx.memo.set(obj, result);
|
|
109
|
+
for (const v of obj) result.push(sanitizeForTransfer(v, ctx));
|
|
105
110
|
} else {
|
|
106
111
|
result = {};
|
|
112
|
+
ctx.memo.set(obj, result);
|
|
107
113
|
for (const [key, val] of Object.entries(obj)) {
|
|
108
|
-
result[key] = sanitizeForTransfer(val,
|
|
114
|
+
result[key] = sanitizeForTransfer(val, ctx);
|
|
109
115
|
}
|
|
110
116
|
}
|
|
111
|
-
|
|
117
|
+
ctx.path.delete(obj);
|
|
112
118
|
return result;
|
|
113
119
|
}
|
|
114
120
|
|