@mistriwork/engine 1.0.0-rc.33 → 1.0.0-rc.35

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.
@@ -95,6 +95,8 @@ interface SpanOptions {
95
95
  links?: readonly SpanLink[];
96
96
  /** Start only inside an existing span: a statement outside any request, job or command is not worth a trace. */
97
97
  childOnly?: boolean;
98
+ /** Start only beneath a sampled trace: the internal reads a statement span would otherwise open by the dozen (drivers.ts). */
99
+ sampledOnly?: boolean;
98
100
  }
99
101
  /** Begin a span; the caller ends it. Prefer `inSpan`, which also makes it current. */
100
102
  export declare function startSpan(name: string, options?: SpanOptions): Span;
@@ -151,6 +151,8 @@ export function startSpan(name, options = {}) {
151
151
  // Beneath a dropped span nothing is traced: the observer's own EXPLAIN is not a trace.
152
152
  if (parent?.discarded)
153
153
  return NOOP;
154
+ if (options.sampledOnly && !parent?.record.sampled)
155
+ return NOOP;
154
156
  const remote = parent ? undefined : options.remote;
155
157
  const traceId = parent?.traceId ?? remote?.traceId ?? hex(16);
156
158
  const record = {
@@ -94,17 +94,19 @@ export type ResourceChecks = ReturnType<typeof resourceChecks>;
94
94
  * App's validation, transitions, stamps, fixed actions and what a line
95
95
  * consumes). Each runs once the grant's own checks have passed, and writes
96
96
  * through the transaction the checks were given; left out, the write is that
97
- * transaction's own.
97
+ * transaction's own. Where a rule is handed `write`, the row's own write goes
98
+ * through it rather than the transaction: that is how a batch carries each
99
+ * row's rules around one statement (adapter.ts `gathered`).
98
100
  */
99
101
  export interface WriteRules<G extends ColumnGrant> {
100
102
  /** This call's grant, kept stable across its awaits: `captureGrant`, with the product's own rules copied beside it. */
101
103
  capture(grant: G): G;
102
104
  /** A lock on a table the grant does not write, which the reader may still update some other way. */
103
105
  locksUnwritable?(table: string, grant: G): boolean;
104
- insert?(request: InsertRequest, grant: G, server: boolean): ReturnType<WriteTransaction['insert']>;
106
+ insert?(request: InsertRequest, grant: G, server: boolean, write?: WriteTransaction['insert']): ReturnType<WriteTransaction['insert']>;
105
107
  /** An update to a table outside `writable`: what answers it instead of the grant's refusal, or undefined. */
106
- unwritable?(request: UpdateRequest, grant: G): ReturnType<WriteTransaction['update']> | undefined;
107
- update?(request: UpdateRequest, grant: G): ReturnType<WriteTransaction['update']>;
108
+ unwritable?(request: UpdateRequest, grant: G, write?: WriteTransaction['update']): ReturnType<WriteTransaction['update']> | undefined;
109
+ update?(request: UpdateRequest, grant: G, write?: WriteTransaction['update']): ReturnType<WriteTransaction['update']>;
108
110
  remove?(request: DeleteRequest, grant: G): ReturnType<WriteTransaction['remove']>;
109
111
  }
110
112
  /** Apply the same graph-aware checks to an already-owned transaction handle. */
@@ -8,7 +8,7 @@
8
8
  * `WriteRules`) and a connection's hidden columns (hiddenColumns.ts), which
9
9
  * has no App in it to ask.
10
10
  */
11
- import { ANSI_STYLE, Refusal, } from '../adapters/adapter.js';
11
+ import { ANSI_STYLE, gathered, Refusal, } from '../adapters/adapter.js';
12
12
  import { freezeDerivedCatalog, isCapturedScopeCatalog, isFixedScopeCatalog } from '../shared/scopeCatalog.js';
13
13
  import { compileView, resolveAggregate, tryResolvePath } from '../shared/view.js';
14
14
  import { rollupReads } from '../shared/rollups.js';
@@ -669,6 +669,41 @@ export function authorizeColumnWrites(full, grant, writer, rules) {
669
669
  const product = rules?.(checks);
670
670
  // Keep each call's rules stable across its awaits; the next call reads current grants.
671
671
  const capture = (current) => (product ? product.capture(current) : captureGrant(current));
672
+ const insertVia = (input, write) => {
673
+ const server = isServerRequest(input);
674
+ const request = jsonWritten(full, requestData(input));
675
+ const operation = capture(grant);
676
+ assertWrite(request.table);
677
+ if (operation.creatable && !operation.creatable.has(request.table))
678
+ throw new AppAccessDenied();
679
+ recordFields(request.table, request.values, 'write', server);
680
+ const written = product?.insert ? product.insert(request, operation, server, write) : (async () => write(request))();
681
+ return written.then((result) => {
682
+ const readable = operation.fields?.get(request.table)?.read;
683
+ if (!readable || !result.row)
684
+ return result;
685
+ return { ...result, row: Object.fromEntries(Object.entries(result.row).filter(([column]) => readable.has(column))) };
686
+ });
687
+ };
688
+ const updateVia = (input, write) => {
689
+ const request = jsonWritten(full, requestData(input));
690
+ const operation = capture(grant);
691
+ if (!operation.writable.has(request.table)) {
692
+ const answered = product?.unwritable?.(request, operation, write);
693
+ if (answered)
694
+ return answered;
695
+ }
696
+ assertWrite(request.table);
697
+ recordFields(request.table, request.values, 'write', isServerRequest(input));
698
+ keyFields(request.table, request.key);
699
+ if (request.expected !== undefined) {
700
+ recordFields(request.table, request.expected, 'read');
701
+ if (!expectedCovers(full, request))
702
+ throw new AppAccessDenied();
703
+ }
704
+ assertRelatedWrites(request.table, Object.keys(request.values));
705
+ return product?.update ? product.update(request, operation, write) : (async () => write(request))();
706
+ };
672
707
  return {
673
708
  ...(writer.lockRecord
674
709
  ? {
@@ -697,41 +732,15 @@ export function authorizeColumnWrites(full, grant, writer, rules) {
697
732
  query: (request) => writer.query(queryFor(request)),
698
733
  count: (table, filter) => writer.count(table, filterFor(table, filter)),
699
734
  ...(writer.aggregate ? { aggregate: (request) => writer.aggregate(aggregateFor(request)) } : {}),
700
- insert: (input) => {
701
- const server = isServerRequest(input);
702
- const request = jsonWritten(full, requestData(input));
703
- const operation = capture(grant);
704
- assertWrite(request.table);
705
- if (operation.creatable && !operation.creatable.has(request.table))
706
- throw new AppAccessDenied();
707
- recordFields(request.table, request.values, 'write', server);
708
- const written = product?.insert ? product.insert(request, operation, server) : (async () => writer.insert(request))();
709
- return written.then((result) => {
710
- const readable = operation.fields?.get(request.table)?.read;
711
- if (!readable || !result.row)
712
- return result;
713
- return { ...result, row: Object.fromEntries(Object.entries(result.row).filter(([column]) => readable.has(column))) };
714
- });
715
- },
716
- update: (input) => {
717
- const request = jsonWritten(full, requestData(input));
718
- const operation = capture(grant);
719
- if (!operation.writable.has(request.table)) {
720
- const answered = product?.unwritable?.(request, operation);
721
- if (answered)
722
- return answered;
723
- }
724
- assertWrite(request.table);
725
- recordFields(request.table, request.values, 'write', isServerRequest(input));
726
- keyFields(request.table, request.key);
727
- if (request.expected !== undefined) {
728
- recordFields(request.table, request.expected, 'read');
729
- if (!expectedCovers(full, request))
730
- throw new AppAccessDenied();
731
- }
732
- assertRelatedWrites(request.table, Object.keys(request.values));
733
- return product?.update ? product.update(request, operation) : (async () => writer.update(request))();
734
- },
735
+ insert: (input) => insertVia(input, (request) => writer.insert(request)),
736
+ update: (input) => updateVia(input, (request) => writer.update(request)),
737
+ // A batch: each row through the same checks, its write one row of one statement.
738
+ ...(writer.insertMany
739
+ ? { insertMany: (inputs) => gathered(inputs, insertVia, (requests) => writer.insertMany(requests)) }
740
+ : {}),
741
+ ...(writer.updateMany
742
+ ? { updateMany: (inputs) => gathered(inputs, updateVia, (requests) => writer.updateMany(requests)) }
743
+ : {}),
735
744
  remove: (input) => {
736
745
  const request = requestData(input);
737
746
  assertWrite(request.table);
@@ -19,6 +19,13 @@ const frozenDerived = new WeakSet();
19
19
  const derivedCaptures = new WeakMap();
20
20
  /** Test-only: how many catalogs were copied and checked in full. */
21
21
  export const scopeCatalogCounters = { captured: 0 };
22
+ /* Every object inside a catalog captured below, with what copying it would
23
+ count. A catalog made from a captured one (a mapping, a pruning) shares its
24
+ frozen parts, and copying those again for each write was most of a capture's
25
+ work (a field report). They are detached plain data that nothing can change,
26
+ so copyData keeps them; the checks after it still read every one. Only seal()
27
+ adds to it, so a caller's object, frozen or not, is always copied. */
28
+ const sealed = new WeakMap();
22
29
  /** Validate descriptors before reading values, including non-authoritative metadata. */
23
30
  function copyData(input) {
24
31
  let nodes = 0;
@@ -38,6 +45,12 @@ function copyData(input) {
38
45
  return value;
39
46
  if (typeof value !== 'object' || ancestors.has(value))
40
47
  return refuse();
48
+ const kept = sealed.get(value);
49
+ if (kept) {
50
+ if ((nodes += kept.nodes - 1) > 262_144 || depth + kept.height > 16 || (characters += kept.characters) > 16_777_216)
51
+ return refuse();
52
+ return value;
53
+ }
41
54
  const array = Array.isArray(value);
42
55
  const proto = Object.getPrototypeOf(value);
43
56
  if (array ? proto !== Array.prototype : proto !== Object.prototype && proto !== null)
@@ -89,6 +102,26 @@ function freeze(value) {
89
102
  }
90
103
  return value;
91
104
  }
105
+ /** Freeze a checked catalog all the way down and remember each part's size for copyData. */
106
+ function seal(value) {
107
+ if (typeof value === 'string')
108
+ return { nodes: 1, characters: value.length, height: 0 };
109
+ if (!value || typeof value !== 'object')
110
+ return { nodes: 1, characters: 0, height: 0 };
111
+ const known = sealed.get(value);
112
+ if (known)
113
+ return known;
114
+ const size = { nodes: 1, characters: 0, height: 0 };
115
+ for (const [key, child] of Object.entries(value)) {
116
+ const inner = seal(child);
117
+ size.nodes += inner.nodes;
118
+ size.characters += inner.characters + (Array.isArray(value) ? 0 : key.length);
119
+ size.height = Math.max(size.height, inner.height + 1);
120
+ }
121
+ Object.freeze(value);
122
+ sealed.set(value, size);
123
+ return size;
124
+ }
92
125
  const fkOrder = (a, b) => order(a.from.table, b.from.table) || order(a.name, b.name);
93
126
  /** Whether `schema` is a catalog this module captured: detached, validated
94
127
  * and deeply frozen, so nothing can change it after a reader binds to it. */
@@ -268,7 +301,8 @@ export function captureScopeCatalog(schema) {
268
301
  endpoint(column.references.table, [column.references.column]);
269
302
  }
270
303
  }
271
- const frozen = freeze(captured);
304
+ seal(captured);
305
+ const frozen = captured;
272
306
  capturedCatalogs.add(frozen);
273
307
  if (frozenDerived.has(schema))
274
308
  derivedCaptures.set(schema, frozen);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mistriwork/engine",
3
- "version": "1.0.0-rc.33",
3
+ "version": "1.0.0-rc.35",
4
4
  "description": "The engine both products stand on: the adapters (SQLite, Postgres), the catalog, the query language, keyset pages, the HTTP kit and the shared rules. No React.",
5
5
  "homepage": "https://mistriwork.dev",
6
6
  "bugs": {