shutro 0.3.7 → 0.3.9
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.
|
@@ -39,6 +39,14 @@ export interface WorkerHost {
|
|
|
39
39
|
readonly company?: number;
|
|
40
40
|
}, fn: (env: Env) => Promise<T>): Promise<T>;
|
|
41
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* How many handlers may be in flight on one driver. A handler runs in a transaction — it holds a connection —
|
|
44
|
+
* and may well open another one (a `bootstrap`, a `run` as somebody else, a sequence). With as many subscribers
|
|
45
|
+
* as topics and a backlog, every connection of the pool would be held by a handler waiting for a second one:
|
|
46
|
+
* nothing would move again, requests included. At most half the pool is in flight, so each handler can have the
|
|
47
|
+
* second connection it asks for; the others wait their turn before they take one.
|
|
48
|
+
*/
|
|
49
|
+
export declare function handlerSlots(max: number | undefined): number;
|
|
42
50
|
/**
|
|
43
51
|
* Delivers events of `topic` to `handler`, in id order, at least once; the cursor advances in the handler's
|
|
44
52
|
* transaction (exactly once for its database work). One active delivery per subscription name across processes.
|
|
@@ -5,11 +5,45 @@ const SKIPPED = Symbol('skipped');
|
|
|
5
5
|
function message(error) {
|
|
6
6
|
return error instanceof Error ? error.message : String(error);
|
|
7
7
|
}
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* How many handlers may be in flight on one driver. A handler runs in a transaction — it holds a connection —
|
|
10
|
+
* and may well open another one (a `bootstrap`, a `run` as somebody else, a sequence). With as many subscribers
|
|
11
|
+
* as topics and a backlog, every connection of the pool would be held by a handler waiting for a second one:
|
|
12
|
+
* nothing would move again, requests included. At most half the pool is in flight, so each handler can have the
|
|
13
|
+
* second connection it asks for; the others wait their turn before they take one.
|
|
14
|
+
*/
|
|
15
|
+
export function handlerSlots(max) {
|
|
16
|
+
return max === undefined ? 4 : Math.max(1, Math.floor(max / 2));
|
|
17
|
+
}
|
|
18
|
+
const gates = new WeakMap();
|
|
19
|
+
/** Runs `fn` when a handler slot of the driver is free; the slot is given on to the next in line after it. */
|
|
20
|
+
async function inSlot(driver, fn) {
|
|
21
|
+
let gate = gates.get(driver);
|
|
22
|
+
if (gate === undefined) {
|
|
23
|
+
gate = { free: handlerSlots(driver.max), waiting: [] };
|
|
24
|
+
gates.set(driver, gate);
|
|
25
|
+
}
|
|
26
|
+
const held = gate;
|
|
27
|
+
if (held.free > 0)
|
|
28
|
+
held.free -= 1;
|
|
29
|
+
else
|
|
30
|
+
await new Promise((resolve) => held.waiting.push(resolve));
|
|
31
|
+
try {
|
|
32
|
+
return await fn();
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
const next = held.waiting.shift();
|
|
36
|
+
if (next === undefined)
|
|
37
|
+
held.free += 1;
|
|
38
|
+
else
|
|
39
|
+
next(); // the slot goes straight to the next in line
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Runs `fn` as the actor of an event or job (the system for bootstrap-time work), in a handler slot. */
|
|
9
43
|
function asActor(host, actor, reason, fn) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
44
|
+
return inSlot(host.driver, () => actor.uid === SYSTEM_UID
|
|
45
|
+
? host.bootstrap({ reason, ...(actor.companyId > 0 ? { company: actor.companyId } : {}) }, fn)
|
|
46
|
+
: host.run({ user: actor.uid, company: actor.companyId, groups: actor.groups, reason }, fn));
|
|
13
47
|
}
|
|
14
48
|
function loop(host, channel, pollMs, step) {
|
|
15
49
|
let running = false;
|
package/dist/core/index.d.ts
CHANGED
|
@@ -48,5 +48,5 @@ export { copyField } from './recordset/writes.js';
|
|
|
48
48
|
export { columnDefault, createTableStatements, historyStatements, historyTable, } from './registry/ddl.js';
|
|
49
49
|
export { type AnyExtension, type Extension, type ExtensionDefinition, extendModel, type FieldOverride, isExtension, type OverrideFn, type Replacement, replace, } from './registry/extension.js';
|
|
50
50
|
export { compareVersions, type DataMigration, defineModule, isModule, type MigrationContext, type Module, type ModuleBase, type ModuleDefinition, } from './registry/module.js';
|
|
51
|
-
export { buildRegistry, type MethodLayer, type Origin, type RegisteredModel, type Registry, type RegistryModelDescription, type RegistryOptions, type SchemaOfModule, type SchemaOfModules, type SecurityOptions, targetOf, } from './registry/registry.js';
|
|
51
|
+
export { buildRegistry, type MethodLayer, type Origin, type RegisteredModel, type Registry, type RegistryModelDescription, type RegistryOptions, type SchemaOfModule, type SchemaOfModules, type SecurityOptions, type TrackingOptions, targetOf, } from './registry/registry.js';
|
|
52
52
|
export { describeRuntimeSecurity, loadRuntimeSecurity, type RuntimeRole, type RuntimeRule, type RuntimeSecurity, type RuntimeSecurityDescription, runtimeSecurityStatements, SECURITY_CHANNEL, } from './security/runtime.js';
|
|
@@ -104,6 +104,29 @@ export interface RegistryOptions<Mods extends readonly ModuleBase[]> {
|
|
|
104
104
|
* to every environment, for prototypes and for tests of other features. Never ship it off.
|
|
105
105
|
*/
|
|
106
106
|
readonly security?: SecurityOptions;
|
|
107
|
+
/**
|
|
108
|
+
* What the audit trail records beyond what models name themselves (RFC 0013). Without it, a model's `tracking`
|
|
109
|
+
* list and its fields' `tracking: true` decide — and a model that names nothing records nothing, not even its
|
|
110
|
+
* creations and deletions.
|
|
111
|
+
*/
|
|
112
|
+
readonly tracking?: TrackingOptions;
|
|
113
|
+
}
|
|
114
|
+
/** Audit-trail options of {@link buildRegistry}. */
|
|
115
|
+
export interface TrackingOptions {
|
|
116
|
+
/**
|
|
117
|
+
* `'all'`: every model records its creations, changes and deletions, and every field a person or a rule sets
|
|
118
|
+
* is tracked — every stored field that is not computed, not an x2many and not encrypted. What a model tracks
|
|
119
|
+
* explicitly stays tracked whatever `except` says.
|
|
120
|
+
*/
|
|
121
|
+
readonly default: 'all';
|
|
122
|
+
/**
|
|
123
|
+
* Leaves out a whole model (called with the model's name alone) or one of its fields: queues, logs and
|
|
124
|
+
* sign-in internals, secrets the server keeps, bulky values.
|
|
125
|
+
*/
|
|
126
|
+
readonly except?: (model: string, field?: {
|
|
127
|
+
readonly key: string;
|
|
128
|
+
readonly kind: string;
|
|
129
|
+
}) => boolean;
|
|
107
130
|
}
|
|
108
131
|
/** Access-control options of {@link buildRegistry}. */
|
|
109
132
|
export interface SecurityOptions {
|
|
@@ -295,8 +295,21 @@ function applyExtension(draft, ext, module, reachable) {
|
|
|
295
295
|
draft.extensions.push(module);
|
|
296
296
|
}
|
|
297
297
|
/** Rebuilds the final model from the draft through `defineModel`, so every model-level check applies to it. */
|
|
298
|
-
function finalize(draft) {
|
|
298
|
+
function finalize(draft, tracking) {
|
|
299
299
|
const def = draft.base.definition;
|
|
300
|
+
// Tracked by default (`tracking: { default: 'all' }`): what is set, not what follows from it.
|
|
301
|
+
if (tracking?.default === 'all' && tracking.except?.(draft.base.name) !== true)
|
|
302
|
+
for (const [key, raw] of Object.entries(draft.fields)) {
|
|
303
|
+
const field = raw;
|
|
304
|
+
if (field.kind === 'one2many' || field.kind === 'many2many')
|
|
305
|
+
continue;
|
|
306
|
+
if (field.store === false || field.computed === true || field.encrypted === true)
|
|
307
|
+
continue;
|
|
308
|
+
if (tracking.except?.(draft.base.name, { key, kind: field.kind }) === true)
|
|
309
|
+
continue;
|
|
310
|
+
if (!draft.tracking.includes(key))
|
|
311
|
+
draft.tracking.push(key);
|
|
312
|
+
}
|
|
300
313
|
const model = defineModel(draft.base.name, {
|
|
301
314
|
...def,
|
|
302
315
|
fields: draft.fields,
|
|
@@ -373,7 +386,7 @@ export function buildRegistry(options) {
|
|
|
373
386
|
}
|
|
374
387
|
const models = new Map();
|
|
375
388
|
for (const [name, draft] of drafts)
|
|
376
|
-
models.set(name, finalize(draft));
|
|
389
|
+
models.set(name, finalize(draft, options.tracking));
|
|
377
390
|
const resolve = (fromModule, target, via) => {
|
|
378
391
|
const found = models.get(target);
|
|
379
392
|
if (found === undefined)
|
package/package.json
CHANGED