@aria-framework/db-worker 0.28.0 → 0.30.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/README.md +330 -0
- package/bin/tenancy.js +97 -0
- package/client.js +47 -2
- package/customerPin.js +144 -0
- package/customerScope.js +115 -0
- package/database.js +54 -0
- package/index.js +14 -2
- package/package.json +13 -2
- package/sql.js +395 -0
- package/tenancyGenerator.js +970 -0
- package/tenancyManifest.js +183 -0
- package/tenantScope.js +120 -0
- package/tenantSettings.js +301 -0
- package/worker.js +40 -5
package/worker.js
CHANGED
|
@@ -20,6 +20,8 @@
|
|
|
20
20
|
const { parentPort, workerData } = require('worker_threads');
|
|
21
21
|
const { performance } = require('perf_hooks');
|
|
22
22
|
const { runWithTenant, APPLY_SQL, applyParams } = require('./tenantContext');
|
|
23
|
+
const { PIN_SQL, pinParams, runPinned } = require('./customerPin');
|
|
24
|
+
const { isCustomerScope, ownMethod, refused } = require('./customerScope');
|
|
23
25
|
|
|
24
26
|
/**
|
|
25
27
|
* The driver, or null before the app's init() has opened one. Read per call rather than cached:
|
|
@@ -43,7 +45,7 @@ function currentDriver() {
|
|
|
43
45
|
* On a serial driver (SQLite) there is no RLS and nothing to set. The context is still
|
|
44
46
|
* established so a model can read it, and no transaction is forced around a read.
|
|
45
47
|
*/
|
|
46
|
-
async function withTenant(tenant, label, dispatch, { outsideTransaction = false } = {}) {
|
|
48
|
+
async function withTenant(tenant, label, dispatch, { outsideTransaction = false, pin = null } = {}) {
|
|
47
49
|
const driver = currentDriver();
|
|
48
50
|
if (!driver || driver.concurrency !== 'pool') {
|
|
49
51
|
return tenant ? runWithTenant(tenant, dispatch) : dispatch();
|
|
@@ -65,6 +67,9 @@ async function withTenant(tenant, label, dispatch, { outsideTransaction = false
|
|
|
65
67
|
if (outsideTransaction) return runWithTenant(tenant, dispatch);
|
|
66
68
|
return runWithTenant(tenant, () => driver.transaction(async (tx) => {
|
|
67
69
|
await tx.run(APPLY_SQL, applyParams(tenant));
|
|
70
|
+
// 0.30.0 — a customer-pinned invoke (customerPin.js): narrowed to the customer AFTER the tenant's own settings, in
|
|
71
|
+
// the same transaction. Only when a pin came in the message; an unpinned invoke sends exactly what it always did.
|
|
72
|
+
if (pin) await tx.run(PIN_SQL, pinParams(pin));
|
|
68
73
|
return dispatch();
|
|
69
74
|
}));
|
|
70
75
|
}
|
|
@@ -72,11 +77,16 @@ async function withTenant(tenant, label, dispatch, { outsideTransaction = false
|
|
|
72
77
|
/**
|
|
73
78
|
* @param {Object} opts
|
|
74
79
|
* @param {(workerData: any) => Object} opts.init - App boot; returns the model registry
|
|
80
|
+
* @param {(models: Object) => Object} [opts.customerScope] - 0.30.0: given the registry once after init, returns
|
|
81
|
+
* createCustomerScope({ safe, models }). Without it every customer-pinned invoke is refused.
|
|
75
82
|
*/
|
|
76
|
-
async function runWorker({ init }) {
|
|
83
|
+
async function runWorker({ init, customerScope } = {}) {
|
|
77
84
|
if (typeof init !== 'function') {
|
|
78
85
|
throw new Error('runWorker({ init }): init must be a function (workerData) => modelRegistry');
|
|
79
86
|
}
|
|
87
|
+
if (customerScope !== undefined && typeof customerScope !== 'function') {
|
|
88
|
+
throw new Error('runWorker({ customerScope }): customerScope must be a function (models) => createCustomerScope({ safe, models })');
|
|
89
|
+
}
|
|
80
90
|
|
|
81
91
|
try {
|
|
82
92
|
// AWAITED since 0.6.0. An app's init opens the database and runs migrations, and on
|
|
@@ -84,6 +94,11 @@ async function runWorker({ init }) {
|
|
|
84
94
|
// model registry and serve queries against an unmigrated database. `await` on a synchronous
|
|
85
95
|
// init returns the same object it always did, so the SQLite path is unchanged.
|
|
86
96
|
const models = (await init(workerData)) || {};
|
|
97
|
+
let customer = null;
|
|
98
|
+
if (customerScope) {
|
|
99
|
+
customer = await customerScope(models);
|
|
100
|
+
if (!isCustomerScope(customer)) throw new Error('runWorker({ customerScope }): it must return createCustomerScope({ safe, models })');
|
|
101
|
+
}
|
|
87
102
|
|
|
88
103
|
parentPort.postMessage({ type: 'ready' });
|
|
89
104
|
|
|
@@ -120,7 +135,7 @@ async function runWorker({ init }) {
|
|
|
120
135
|
});
|
|
121
136
|
|
|
122
137
|
async function handle(msg) {
|
|
123
|
-
const { id, model, method, args, tenant } = msg;
|
|
138
|
+
const { id, model, method, args, tenant, pin } = msg;
|
|
124
139
|
// Worker-side execution time, shipped back so the client's slow-call log
|
|
125
140
|
// attributes slowness to the query that WAS slow, not to calls that were
|
|
126
141
|
// merely queued behind it. Monotonic clock: a wall-clock (Date.now) delta
|
|
@@ -133,13 +148,33 @@ async function runWorker({ init }) {
|
|
|
133
148
|
throw new Error(`Unknown method: ${model}.${method}`);
|
|
134
149
|
}
|
|
135
150
|
|
|
136
|
-
|
|
151
|
+
// ONE dispatch, from THIS worker's registry, for an unpinned and a pinned call alike.
|
|
152
|
+
const dispatch = async () => {
|
|
137
153
|
let r = target[method](...(args || []));
|
|
138
154
|
if (r && typeof r.then === 'function') {
|
|
139
155
|
r = await r; // async methods (e.g. bcrypt-backed user ops)
|
|
140
156
|
}
|
|
141
157
|
return r;
|
|
142
|
-
}
|
|
158
|
+
};
|
|
159
|
+
let result;
|
|
160
|
+
if (pin !== undefined) {
|
|
161
|
+
// 0.30.0 — CUSTOMER-PINNED (customerPin.js). Every refusal happens here, before a transaction is opened. The
|
|
162
|
+
// scope only DECIDES (pin, tenant, safe list); what runs is target[method] above, from this registry.
|
|
163
|
+
if (!customer) {
|
|
164
|
+
throw refused(`db-worker: ${model}.${method} arrived pinned to a customer, and this worker has no customerScope (runWorker({ customerScope }))`);
|
|
165
|
+
}
|
|
166
|
+
const call = customer.prepare(pin, tenant, model, method);
|
|
167
|
+
if (ownMethod(models, model, method) !== target[method]) {
|
|
168
|
+
throw refused(`CustomerScope: ${model}.${method} is not the model's own method in this worker's registry`);
|
|
169
|
+
}
|
|
170
|
+
if (target[method].outsideTransaction === true) {
|
|
171
|
+
throw refused(`CustomerScope: ${model}.${method} runs outside a transaction, where a customer pin cannot apply`);
|
|
172
|
+
}
|
|
173
|
+
result = await withTenant(tenant, `${model}.${method}`, () => runPinned(call.pin, dispatch), { pin: call.pin });
|
|
174
|
+
} else {
|
|
175
|
+
result = await withTenant(tenant, `${model}.${method}`, dispatch,
|
|
176
|
+
{ outsideTransaction: target[method].outsideTransaction === true });
|
|
177
|
+
}
|
|
143
178
|
|
|
144
179
|
result = sanitizeForTransfer(result);
|
|
145
180
|
parentPort.postMessage({ id, result, execMs: Math.round(performance.now() - t0) });
|