@genesislcap/mock-server 15.72.0 → 15.73.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.
Files changed (42) hide show
  1. package/README.md +58 -5
  2. package/dist/db/audit.d.ts +12 -0
  3. package/dist/db/audit.js +98 -0
  4. package/dist/db/criteriaDialect.js +15 -8
  5. package/dist/db/criteriaJuxtaposition.d.ts +6 -0
  6. package/dist/db/criteriaJuxtaposition.js +123 -0
  7. package/dist/db/groovySyntax.js +4 -1
  8. package/dist/db/groovyValidator.js +166 -7
  9. package/dist/db/schema.d.ts +2 -0
  10. package/dist/db/schema.js +2 -0
  11. package/dist/db/store.d.ts +14 -2
  12. package/dist/db/store.js +64 -4
  13. package/dist/handlers/commitEvent.js +16 -3
  14. package/dist/handlers/dataLogon.js +8 -4
  15. package/dist/handlers/jsonSchema.js +1 -1
  16. package/dist/handlers/meta.js +1 -1
  17. package/dist/index.d.ts +1 -1
  18. package/dist/protocol/fieldTypes.d.ts +5 -1
  19. package/dist/protocol/fieldTypes.js +9 -2
  20. package/dist/protocol/msgNack.d.ts +2 -0
  21. package/dist/protocol/msgNack.js +14 -0
  22. package/dist/protocol/router.js +12 -1
  23. package/dist/server.js +21 -2
  24. package/dist/types.d.ts +6 -0
  25. package/package.json +1 -1
  26. package/src/db/audit.ts +127 -0
  27. package/src/db/criteriaDialect.ts +15 -8
  28. package/src/db/criteriaJuxtaposition.ts +132 -0
  29. package/src/db/groovySyntax.ts +6 -1
  30. package/src/db/groovyValidator.ts +176 -11
  31. package/src/db/schema.ts +4 -0
  32. package/src/db/store.ts +70 -4
  33. package/src/handlers/commitEvent.ts +18 -3
  34. package/src/handlers/dataLogon.ts +8 -4
  35. package/src/handlers/jsonSchema.ts +1 -1
  36. package/src/handlers/meta.ts +1 -1
  37. package/src/index.ts +1 -0
  38. package/src/protocol/fieldTypes.ts +13 -2
  39. package/src/protocol/msgNack.ts +16 -0
  40. package/src/protocol/router.ts +14 -1
  41. package/src/server.ts +26 -2
  42. package/src/types.ts +17 -0
@@ -4,9 +4,12 @@
4
4
  // (genesis-db v8.15.29) refuses. The criteria is parsed as Groovy reads it
5
5
  // (Groovy 3.0.25's precedence) and the validator's walk is repeated over
6
6
  // it: every error, in the order the validator meets them, with its text.
7
+ // That includes a call's only argument written as operands side by side
8
+ // (`NAME.equals("say "hi"")`), which Groovy reads as a command expression.
7
9
  // Anything this parser doesn't follow — the engine's AND / OR / NOT dialect,
8
10
  // an assignment, a closure's insides, a GString — gets no verdict here.
9
11
 
12
+ import { juxtaposedIndex } from './criteriaJuxtaposition.ts';
10
13
  import { groovyCompileErrorText, groovyTokens, type Token } from './groovySyntax.ts';
11
14
 
12
15
  const VALIDATION_FAILED = 'GenericCriteriaValidationException : Criteria validation failed: ';
@@ -103,6 +106,28 @@ const COMPARISONS = new Set(['==', '!=', '>', '>=', '<', '<=', 'in', '!in']);
103
106
 
104
107
  const NOT_SUPPORTED = (what: string) => `${what} are not supported in criteria`;
105
108
 
109
+ // A name that can stand for a type or a variable: not a keyword operator,
110
+ // a literal word or this engine's dialect.
111
+ const isTypeName = (name: string) =>
112
+ !KEYWORD_OPERATORS.has(name) &&
113
+ !['true', 'false', 'null', 'new'].includes(name) &&
114
+ !DIALECT_WORDS.has(name.toUpperCase());
115
+
116
+ const PRIMITIVES = new Set(['boolean', 'byte', 'char', 'short', 'int', 'long', 'float', 'double']);
117
+
118
+ // Whether Groovy resolves a type a cast or a declaration names: a
119
+ // primitive, `def` / `var`, a class it imports by default, or a qualified
120
+ // JDK or Groovy class.
121
+ function resolvesType(type: string): boolean {
122
+ return (
123
+ PRIMITIVES.has(type) ||
124
+ type === 'def' ||
125
+ type === 'var' ||
126
+ Object.hasOwn(DEFAULT_IMPORT_CLASSES, type) ||
127
+ QUALIFIED_CLASS.test(type)
128
+ );
129
+ }
130
+
106
131
  type Node =
107
132
  | { kind: 'binary'; op: string; left: Node; right: Node }
108
133
  | { kind: 'visitInner'; operand: Node } // !x, -x, +x
@@ -111,7 +136,13 @@ type Node =
111
136
  | { kind: 'variable'; name: string }
112
137
  | { kind: 'property'; object: Node; name: string }
113
138
  | { kind: 'call'; object: Node | undefined; name: string; args: Node[] }
114
- | { kind: 'ctor'; name: string; offset: number };
139
+ | { kind: 'ctor'; name: string; offset: number }
140
+ // `(Type) x`, refused whole; an unresolved Type fails compilation at its '('.
141
+ | { kind: 'cast'; type: string; offset: number }
142
+ // `Type x`, `def x = ...`, refused whole; an unresolved Type fails
143
+ // compilation at the variable.
144
+ | { kind: 'declaration'; type: string; offset: number }
145
+ | { kind: 'statements'; items: Node[] };
115
146
 
116
147
  // Thrown for anything this parser doesn't follow: no verdict.
117
148
  class GiveUp extends Error {}
@@ -160,6 +191,11 @@ const DIALECT_WORDS = new Set(['AND', 'OR', 'NOT']);
160
191
  class Parser {
161
192
  private at = 0;
162
193
  private readonly tokens: Token[];
194
+ // A binary operator read so far, and how deep inside a prefix operator's
195
+ // operand the parser is: either makes a command-expression argument a
196
+ // shape Groovy parses differently (criteriaJuxtaposition.ts answers it).
197
+ private sawBinary = false;
198
+ private prefixDepth = 0;
163
199
 
164
200
  constructor(tokens: Token[]) {
165
201
  this.tokens = tokens;
@@ -204,6 +240,63 @@ class Parser {
204
240
  return undefined;
205
241
  }
206
242
 
243
+ // Statements separated by ';', each a declaration or an expression.
244
+ statements(): Node {
245
+ const items: Node[] = [];
246
+ for (;;) {
247
+ items.push(this.declaration() ?? this.expression());
248
+ if (this.peek()?.kind !== 'semicolon') break;
249
+ this.next();
250
+ if (this.done) break;
251
+ }
252
+ return items.length === 1 ? items[0] : { kind: 'statements', items };
253
+ }
254
+
255
+ // `Type x`, `def x`, then `= value`, ';' or the end: a declaration, which
256
+ // Groovy reads even with a field name for its type (`TRADER x`).
257
+ private declaration(): Node | undefined {
258
+ let ahead = 0;
259
+ const first = this.peek();
260
+ if (first?.kind !== 'name' || !isTypeName(first.text)) return undefined;
261
+ let type = first.text;
262
+ while (this.peek(ahead + 1)?.text === '.' && this.peek(ahead + 2)?.kind === 'name') {
263
+ type += `.${this.peek(ahead + 2)!.text}`;
264
+ ahead += 2;
265
+ }
266
+ const variable = this.peek(ahead + 1);
267
+ if (variable?.kind !== 'name' || !isTypeName(variable.text)) return undefined;
268
+ const after = this.peek(ahead + 2);
269
+ const assigned = after?.kind === 'operator' && after.text === '=';
270
+ if (after && after.kind !== 'semicolon' && !assigned) return undefined;
271
+ this.at += ahead + 2;
272
+ if (assigned) {
273
+ this.next();
274
+ this.expression();
275
+ }
276
+ return { kind: 'declaration', type, offset: variable.offset };
277
+ }
278
+
279
+ // `(Type)` before something a cast can apply to: the type and how many
280
+ // tokens it takes, '(' to ')'. `(TRADER) == x` is a parenthesized TRADER.
281
+ private castType(): { type: string; length: number } | undefined {
282
+ let ahead = 1;
283
+ const first = this.peek(ahead);
284
+ if (first?.kind !== 'name' || !isTypeName(first.text)) return undefined;
285
+ let type = first.text;
286
+ while (this.peek(ahead + 1)?.text === '.' && this.peek(ahead + 2)?.kind === 'name') {
287
+ type += `.${this.peek(ahead + 2)!.text}`;
288
+ ahead += 2;
289
+ }
290
+ if (this.peek(ahead + 1)?.text !== ')') return undefined;
291
+ const operand = this.peek(ahead + 2);
292
+ const startsOperand =
293
+ operand?.kind === 'literal' ||
294
+ (operand?.kind === 'name' && isTypeName(operand.text)) ||
295
+ (operand?.kind === 'open' && ['(', '[', '{'].includes(operand.text)) ||
296
+ (operand?.kind === 'operator' && (operand.text === '!' || operand.text === '~'));
297
+ return startsOperand ? { type, length: ahead + 2 } : undefined;
298
+ }
299
+
207
300
  expression(minPower = 0): Node {
208
301
  let left = this.unary();
209
302
  for (;;) {
@@ -235,6 +328,7 @@ class Parser {
235
328
  const power = BINARY_POWER[operator.op];
236
329
  if (power <= minPower) return left;
237
330
  this.at += operator.length;
331
+ this.sawBinary = true;
238
332
  const right = this.expression(RIGHT_ASSOCIATIVE.has(operator.op) ? power - 1 : power);
239
333
  if (operator.op === '..' || operator.op === '..<') {
240
334
  left = { kind: 'refused', message: NOT_SUPPORTED('Range expressions') };
@@ -251,22 +345,31 @@ class Parser {
251
345
  if (token?.kind === 'operator') {
252
346
  if (token.text === '!' || token.text === '-' || token.text === '+') {
253
347
  this.next();
254
- return { kind: 'visitInner', operand: this.expression(UNARY_POWER) };
348
+ return { kind: 'visitInner', operand: this.prefixOperand() };
255
349
  }
256
350
  if (token.text === '~') {
257
351
  this.next();
258
- this.expression(UNARY_POWER);
352
+ this.prefixOperand();
259
353
  return { kind: 'refused', message: NOT_SUPPORTED('Bitwise negation expressions') };
260
354
  }
261
355
  if (token.text === '++' || token.text === '--') {
262
356
  this.next();
263
- this.expression(UNARY_POWER);
357
+ this.prefixOperand();
264
358
  return { kind: 'refused', message: NOT_SUPPORTED('Prefix expressions') };
265
359
  }
266
360
  }
267
361
  return this.postfix(this.primary());
268
362
  }
269
363
 
364
+ private prefixOperand(): Node {
365
+ this.prefixDepth += 1;
366
+ try {
367
+ return this.expression(UNARY_POWER);
368
+ } finally {
369
+ this.prefixDepth -= 1;
370
+ }
371
+ }
372
+
270
373
  private postfix(start: Node): Node {
271
374
  let node = start;
272
375
  for (;;) {
@@ -277,6 +380,9 @@ class Parser {
277
380
  if (member.kind !== 'name') throw new GiveUp();
278
381
  if (this.peek()?.text === '(' && this.peek()?.kind === 'open') {
279
382
  node = { kind: 'call', object: node, name: member.text, args: this.arguments() };
383
+ } else if (this.peek()?.text === '{' && this.peek()?.kind === 'open') {
384
+ // A closure as the only argument: `.any { ... }`.
385
+ node = { kind: 'call', object: node, name: member.text, args: [this.primary()] };
280
386
  } else {
281
387
  node = { kind: 'property', object: node, name: member.text };
282
388
  }
@@ -309,6 +415,8 @@ class Parser {
309
415
  this.next();
310
416
  return args;
311
417
  }
418
+ const command = this.commandArgument();
419
+ if (command) return [command];
312
420
  for (;;) {
313
421
  args.push(this.expression());
314
422
  const after = this.next();
@@ -317,6 +425,38 @@ class Parser {
317
425
  }
318
426
  }
319
427
 
428
+ // A call's only argument written as operands side by side, up to its ')':
429
+ // the command expression Groovy reads, `a b c d e` as `this.a(b).c(d).e`.
430
+ // Undefined when the argument doesn't start that way.
431
+ private commandArgument(): Node | undefined {
432
+ const tokens = this.tokens.slice(this.at, this.at + 2);
433
+ if (tokens.length < 2 || juxtaposedIndex(tokens) !== 1) return undefined;
434
+ // After a binary operator, inside a prefix operator's operand, or led by
435
+ // a number (`1.call(b)`), Groovy parses it another way.
436
+ if (this.sawBinary || this.prefixDepth > 0) throw new GiveUp();
437
+ if (tokens[0].kind === 'literal' && !/^['"]/.test(tokens[0].text)) throw new GiveUp();
438
+ const run: Token[] = [this.next(), this.next()];
439
+ while (this.peek() && juxtaposedIndex([run.at(-1)!, this.peek()!]) === 1) run.push(this.next());
440
+ this.expect(')');
441
+ const operand = (token: Token): Node =>
442
+ token.kind === 'name' && !['true', 'false', 'null'].includes(token.text)
443
+ ? { kind: 'variable', name: token.text }
444
+ : { kind: 'constant' };
445
+ let node: Node = {
446
+ kind: 'call',
447
+ object: undefined,
448
+ name: run[0].text,
449
+ args: [operand(run[1])],
450
+ };
451
+ for (let index = 2; index < run.length; index += 2) {
452
+ const argument = run[index + 1];
453
+ node = argument
454
+ ? { kind: 'call', object: node, name: run[index].text, args: [operand(argument)] }
455
+ : { kind: 'property', object: node, name: run[index].text };
456
+ }
457
+ return node;
458
+ }
459
+
320
460
  private primary(): Node {
321
461
  const token = this.next();
322
462
  if (token.kind === 'literal') {
@@ -337,6 +477,14 @@ class Parser {
337
477
  return { kind: 'variable', name: token.text };
338
478
  }
339
479
  if (token.kind === 'open' && token.text === '(') {
480
+ this.at -= 1;
481
+ const cast = this.castType();
482
+ this.at += 1;
483
+ if (cast) {
484
+ this.at += cast.length - 1;
485
+ this.unary();
486
+ return { kind: 'cast', type: cast.type, offset: token.offset };
487
+ }
340
488
  const inner = this.expression();
341
489
  this.expect(')');
342
490
  return inner;
@@ -470,6 +618,18 @@ function visit(node: Node, allowed: ReadonlySet<string> | undefined, verdict: Ve
470
618
  }
471
619
  return;
472
620
  }
621
+ case 'cast':
622
+ case 'declaration':
623
+ if (!resolvesType(node.type)) verdict.unresolved ??= { name: node.type, offset: node.offset };
624
+ else {
625
+ verdict.errors.push(
626
+ NOT_SUPPORTED(node.kind === 'cast' ? 'Cast expressions' : 'Declaration expressions'),
627
+ );
628
+ }
629
+ return;
630
+ case 'statements':
631
+ for (const item of node.items) visit(item, allowed, verdict);
632
+ return;
473
633
  case 'call': {
474
634
  const { object } = node;
475
635
  if (object?.kind === 'variable' && object.name === 'Expr') {
@@ -494,13 +654,15 @@ const SIMPLE_OPERATORS = new Set(['&&', '||', '==', '!=', '<', '<=', '>', '>=',
494
654
 
495
655
  // Whether the criteria uses Groovy this engine's grammar doesn't read: an
496
656
  // operator other than its own, a bracket or brace, `new`, `in`,
497
- // `instanceof`, `as`.
657
+ // `instanceof`, `as`, or operands side by side.
498
658
  function usesGroovy(tokens: Token[]): boolean {
499
- return tokens.some(
500
- (token) =>
501
- (token.kind === 'operator' && !SIMPLE_OPERATORS.has(token.text)) ||
502
- (token.kind === 'open' && token.text !== '(') ||
503
- (token.kind === 'name' && ['new', 'in', 'instanceof', 'as'].includes(token.text)),
659
+ return (
660
+ tokens.some(
661
+ (token) =>
662
+ (token.kind === 'operator' && !SIMPLE_OPERATORS.has(token.text)) ||
663
+ (token.kind === 'open' && token.text !== '(') ||
664
+ (token.kind === 'name' && ['new', 'in', 'instanceof', 'as'].includes(token.text)),
665
+ ) || juxtaposedIndex(tokens) >= 0
504
666
  );
505
667
  }
506
668
 
@@ -522,7 +684,7 @@ export function groovyValidation(
522
684
  let root: Node;
523
685
  try {
524
686
  const parser = new Parser(tokens);
525
- root = parser.expression();
687
+ root = parser.statements();
526
688
  if (!parser.done) return undefined;
527
689
  } catch (error) {
528
690
  if (error instanceof GiveUp || error instanceof RangeError) return undefined;
@@ -542,5 +704,8 @@ export function groovyValidation(
542
704
  if (verdict.errors.length > 0) {
543
705
  return { kind: 'refused', text: `${VALIDATION_FAILED}${verdict.errors.join(',')}` };
544
706
  }
707
+ // Statements that pass the validator still have to compile as a filter,
708
+ // which this parser doesn't follow.
709
+ if (root.kind === 'statements') return undefined;
545
710
  return { kind: 'passed' };
546
711
  }
package/src/db/schema.ts CHANGED
@@ -36,6 +36,8 @@ export interface ResolvedField {
36
36
  default?: unknown;
37
37
  maxSize?: number;
38
38
  minLength?: number;
39
+ min?: number;
40
+ max?: number;
39
41
  // A BIGDECIMAL's scale: a table column's always (default 5), a derived or
40
42
  // DTO field's only when declared. See db/values.ts.
41
43
  scale?: number;
@@ -94,6 +96,8 @@ function fromFieldDef(
94
96
  ...(field.default !== undefined ? { default: field.default } : {}),
95
97
  ...(field.maxSize !== undefined ? { maxSize: field.maxSize } : {}),
96
98
  ...(field.minLength !== undefined ? { minLength: field.minLength } : {}),
99
+ ...(field.min !== undefined ? { min: field.min } : {}),
100
+ ...(field.max !== undefined ? { max: field.max } : {}),
97
101
  ...(field.scale !== undefined ? { scale: field.scale } : {}),
98
102
  ...(field.description !== undefined ? { description: field.description } : {}),
99
103
  ...(field.title !== undefined ? { title: field.title } : {}),
package/src/db/store.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  // This is the "database" — disposable and rebuilt from seed data on every server start.
3
3
 
4
4
  import type { Row, SequenceFormat, TableDef, ViewDef } from '../types.ts';
5
+ import { type AuditContext, auditRecord, auditTableName, isAudited } from './audit.ts';
5
6
  import { RECORD_ID, RecordClock, recordKey, TIMESTAMP, withoutRecordFields } from './identity.ts';
6
7
  import { columnScale, storedValue, unlistedEnumValue, type ValueField } from './values.ts';
7
8
  import {
@@ -43,6 +44,11 @@ export interface StoreOptions {
43
44
  // db/values.ts). Read on every write; the mock server turns it off under
44
45
  // fidelity: 'legacy', where rows are kept as given. Default: on.
45
46
  normalizeValues?: () => boolean;
47
+ // Whether a sequence another table shares is kept past the values that
48
+ // table has used when one of them is registered (again). The mock server
49
+ // turns it off under fidelity: 'legacy', where registering a table set the
50
+ // counter from its own seed rows alone. Default: on.
51
+ holdSharedSequences?: () => boolean;
46
52
  }
47
53
 
48
54
  // The generators of a table, the key's own sequence first. A SEQUENCE with
@@ -145,6 +151,9 @@ export class Store {
145
151
  private tables = new Map<string, Map<Pk, Row>>();
146
152
  private tableConfig = new Map<string, TableConfig>();
147
153
  private sequenceCounters = new Map<string, number>();
154
+ // Per sequence counter and table, the highest value the table has taken
155
+ // from it: its seed rows' and the ones its writes used.
156
+ private sequenceUse = new Map<string, Map<string, number>>();
148
157
  private autoIncrementCounters = new Map<string, number>();
149
158
  private clock = new RecordClock();
150
159
  // Immutable snapshot of each table's rows as registered, used only for field
@@ -164,13 +173,24 @@ export class Store {
164
173
  // While a transaction is open (see begin), each table it has written to,
165
174
  // as it was before.
166
175
  private savepoint: Map<string, TableSavepoint> | undefined;
176
+ // Each audited table's audit table (db/audit.ts), and while an event's
177
+ // transaction is open, the event its writes are audited for and the audit
178
+ // rows they added.
179
+ private auditTables = new Map<string, string>();
180
+ private auditContext: AuditContext | undefined;
181
+ private auditWrites: Array<{ tableName: string; key: Pk }> = [];
167
182
  readonly views = new Map<string, ViewDef>();
168
183
  private readonly normalizeValues: () => boolean;
184
+ private readonly holdSharedSequences: () => boolean;
169
185
  // ENUM values already warned about, as 'TABLE.FIELD=value'.
170
186
  private readonly unlistedWarned = new Set<string>();
171
187
 
172
- constructor({ normalizeValues = () => true }: StoreOptions = {}) {
188
+ constructor({
189
+ normalizeValues = () => true,
190
+ holdSharedSequences = () => true,
191
+ }: StoreOptions = {}) {
173
192
  this.normalizeValues = normalizeValues;
193
+ this.holdSharedSequences = holdSharedSequences;
174
194
  }
175
195
 
176
196
  // Opens a transaction: the commit-event path wraps every handler in one
@@ -182,20 +202,33 @@ export class Store {
182
202
  // back: a value used stays used, as a database sequence's does. Not nested,
183
203
  // and the engine's own: an event that finds one open answers INTERNAL_ERROR.
184
204
  // Saving copies the table's row map, O(rows) per event and table written.
185
- begin(): void {
205
+ // With an audit context, each write to an audited table also adds its
206
+ // audit row (see audit.ts), undone with the rest by rollback().
207
+ begin(audit?: AuditContext): void {
186
208
  if (this.savepoint) throw new Error('A store transaction is already open');
187
209
  this.savepoint = new Map();
210
+ this.auditContext = audit;
211
+ this.auditWrites = [];
188
212
  }
189
213
 
190
214
  // Keeps every write since begin().
191
215
  commit(): void {
192
216
  this.savepoint = undefined;
217
+ this.auditContext = undefined;
218
+ }
219
+
220
+ // The audit rows the open transaction added (table and key), for the live
221
+ // pushes that announce them once it commits.
222
+ takeAuditWrites(): Array<{ tableName: string; key: Pk }> {
223
+ return this.auditWrites.splice(0);
193
224
  }
194
225
 
195
226
  // Undoes every write since begin().
196
227
  rollback(): void {
197
228
  const savepoint = this.savepoint;
198
229
  this.savepoint = undefined;
230
+ this.auditContext = undefined;
231
+ this.auditWrites = [];
199
232
  for (const [tableName, saved] of savepoint ?? []) {
200
233
  this.tables.set(tableName, saved.rows);
201
234
  restoreEntry(this.removedRows, tableName, saved.removed);
@@ -268,6 +301,8 @@ export class Store {
268
301
  }
269
302
  this.tables.set(tableName, table);
270
303
  this.tableConfig.set(tableName, config);
304
+ if (isAudited(definition)) this.auditTables.set(tableName, auditTableName(tableName));
305
+ else this.auditTables.delete(tableName);
271
306
  // Registering a table again replaces it whole: a rollback leaves it be.
272
307
  this.savepoint?.delete(tableName);
273
308
  this.removedRows.delete(tableName);
@@ -279,7 +314,11 @@ export class Store {
279
314
 
280
315
  // Each counter starts after the highest value the seed rows carry (two
281
316
  // fields of one table sharing a prefix share the counter). The key's
282
- // sequence only exists for a single-field pk.
317
+ // sequence only exists for a single-field pk. A sequence is shared by
318
+ // every table with its prefix: it also starts after the values the other
319
+ // tables on it have taken, so no id repeats across them (an audit
320
+ // table's default sequence AU, an app table on the same prefix). This
321
+ // table's own earlier values don't count: registering it again resets it.
283
322
  const starts = new Map<string, number>();
284
323
  for (const generator of config.generators) {
285
324
  let maxN = 0;
@@ -292,7 +331,17 @@ export class Store {
292
331
  }
293
332
  for (const generator of config.generators) {
294
333
  const counter = this.counterOf(tableName, config, generator);
295
- this.countersFor(generator).set(counter, starts.get(counter)!);
334
+ const seeded = starts.get(counter)!;
335
+ let start = seeded;
336
+ if (generator.kind === 'SEQUENCE') {
337
+ const use = this.sequenceUse.get(counter) ?? new Map<string, number>();
338
+ this.sequenceUse.set(counter, use.set(tableName, seeded - 1));
339
+ if (this.holdSharedSequences()) {
340
+ const others = [...use].filter(([table]) => table !== tableName).map(([, n]) => n);
341
+ start = Math.max(seeded, ...others.map((n) => n + 1));
342
+ }
343
+ }
344
+ this.countersFor(generator).set(counter, start);
296
345
  }
297
346
  return this;
298
347
  }
@@ -424,6 +473,10 @@ export class Store {
424
473
  const counters = this.countersFor(generator);
425
474
  const counter = this.counterOf(tableName, config, generator);
426
475
  if (n + 1 > (counters.get(counter) ?? 1)) counters.set(counter, n + 1);
476
+ if (generator.kind === 'SEQUENCE') {
477
+ const use = this.sequenceUse.get(counter) ?? new Map<string, number>();
478
+ if (n > (use.get(tableName) ?? 0)) this.sequenceUse.set(counter, use.set(tableName, n));
479
+ }
427
480
  }
428
481
  }
429
482
 
@@ -539,9 +592,20 @@ export class Store {
539
592
  const displaced = table.get(key);
540
593
  if (displaced) this.noteRemoved(tableName, key, displaced);
541
594
  table.set(key, row);
595
+ this.audit(tableName, row);
542
596
  return row;
543
597
  }
544
598
 
599
+ // Adds a write's audit row, when the table is audited and an event's
600
+ // transaction is open.
601
+ private audit(tableName: string, record: Row): void {
602
+ const auditTable = this.auditTables.get(tableName);
603
+ const context = this.auditContext;
604
+ if (!auditTable || !context || !this.tables.has(auditTable)) return;
605
+ const row = this.insertRow(auditTable, auditRecord(record, context, Date.now()));
606
+ this.auditWrites.push({ tableName: auditTable, key: this.rowKey(auditTable, row) });
607
+ }
608
+
545
609
  // Merges the patch, moves TIMESTAMP on and keeps RECORD_ID (a patch can't
546
610
  // change a row's identity).
547
611
  updateRow(tableName: string, pk: Pk, patch: Row): Row | undefined {
@@ -566,6 +630,7 @@ export class Store {
566
630
  if (displaced) this.noteRemoved(tableName, nextKey, displaced);
567
631
  }
568
632
  table.set(nextKey, updated);
633
+ this.audit(tableName, updated);
569
634
  return updated;
570
635
  }
571
636
 
@@ -576,6 +641,7 @@ export class Store {
576
641
  this.save(tableName);
577
642
  table.delete(pk);
578
643
  this.noteRemoved(tableName, pk, existing);
644
+ this.audit(tableName, existing);
579
645
  return true;
580
646
  }
581
647
 
@@ -1,3 +1,4 @@
1
+ import type { AuditContext } from '../db/audit.ts';
1
2
  import type { Store } from '../db/store.ts';
2
3
  import type { Connection } from '../protocol/connection.ts';
3
4
  import { NackError, nackPayload, warningPayload } from '../protocol/errors.ts';
@@ -54,10 +55,10 @@ interface EventTransaction {
54
55
  rollback(): void;
55
56
  }
56
57
 
57
- function openTransaction(store: Store, ctx: CommitEventCtx): EventTransaction {
58
+ function openTransaction(store: Store, ctx: CommitEventCtx, audit: AuditContext): EventTransaction {
58
59
  const pending: Array<() => void> = [];
59
60
  let open = true;
60
- store.begin();
61
+ store.begin(audit);
61
62
  const hold = (push: () => void) => {
62
63
  if (open) pending.push(push);
63
64
  else push();
@@ -68,6 +69,10 @@ function openTransaction(store: Store, ctx: CommitEventCtx): EventTransaction {
68
69
  broadcastTableChange: (tableName, operation, pk) =>
69
70
  hold(() => ctx.broadcastTableChange(tableName, operation, pk)),
70
71
  commit() {
72
+ // The audit rows the writes added go live with them.
73
+ for (const { tableName, key } of store.takeAuditWrites()) {
74
+ pending.push(() => ctx.broadcastTableChange(tableName, 'INSERT', key));
75
+ }
71
76
  open = false;
72
77
  store.commit();
73
78
  },
@@ -89,6 +94,16 @@ function openTransaction(store: Store, ctx: CommitEventCtx): EventTransaction {
89
94
  };
90
95
  }
91
96
 
97
+ // What GSF audits an event's writes with (TransactionalEventHandlerContainer:
98
+ // entityDb.audited(message.userName, message.messageType, message.reason)).
99
+ function auditContext(message: GenesisMessage, connection: Connection): AuditContext {
100
+ return {
101
+ eventType: message.MESSAGE_TYPE,
102
+ user: connection.userName ?? '',
103
+ text: typeof message.REASON === 'string' ? message.REASON : null,
104
+ };
105
+ }
106
+
92
107
  // Commit events (EVENT_<ENTITY>_INSERT/_AMEND/_DELETE, or whatever suffix
93
108
  // convention your project uses) are looked up by exact MESSAGE_TYPE against
94
109
  // config.eventHandlers, then the tables' generic CRUD events. A handler
@@ -170,7 +185,7 @@ export function handleCommitEvent(
170
185
  try {
171
186
  // Inside the try: a store transaction someone else left open fails this
172
187
  // event (INTERNAL_ERROR) instead of leaving it unanswered.
173
- if (!legacy) transaction = openTransaction(store, ctx);
188
+ if (!legacy) transaction = openTransaction(store, ctx, auditContext(message, connection));
174
189
  const schemaErrors = legacy ? [] : validateEventMessage(message, connection, config, store);
175
190
  if (schemaErrors.length > 0) throw new NackError(schemaErrors);
176
191
  const details = legacy ? (message.DETAILS ?? {}) : eventDetails(message, config, store);
@@ -318,7 +318,8 @@ export function handleDataLogon(
318
318
  // getCriteriaFilter, v8.15.29): no subscription opens. Refused here: the
319
319
  // syntax errors Groovy would report too, and this engine's own dialect —
320
320
  // AND / OR / NOT, `<>`, a single `=`, dotted names, `$` in double quotes,
321
- // calls off GSF's whitelists (db/criteriaDialect.ts). A criteria outside
321
+ // calls off GSF's whitelists (db/criteriaDialect.ts), operands side by
322
+ // side (db/criteriaJuxtaposition.ts). A criteria outside
322
323
  // this engine's grammar that
323
324
  // may well be valid Groovy still fails open below.
324
325
  const refusal =
@@ -460,8 +461,11 @@ export function handleDataLogon(
460
461
  // createMoreRowsMessage gives the request a SOURCE_REF of its own) or, when
461
462
  // that is absent, in its own SOURCE_REF. The page goes out as a QUERY_UPDATE
462
463
  // on the subscription's SOURCE_REF without ROWS_COUNT; MORE_ROWS_ACK answers
463
- // the request's own SOURCE_REF, and only when DETAILS.SOURCE_REF was given —
464
- // both as the real ConnectionManager.processMoreRows does. The ack goes first
464
+ // the request's own SOURCE_REF, and only when DETAILS.SOURCE_REF names another
465
+ // one. The real router rewrites DETAILS.SOURCE_REF to its own channel-prefixed
466
+ // form only then, so that is the only ack ConnectionManager.processMoreRows
467
+ // sends that it can route back to the socket (MsgType.MoreRowsOrColumns
468
+ // @v8.15.29); on the subscription's own SOURCE_REF the ack is lost. The ack goes first
465
469
  // because the real server writes it while its dataserver is still reading the
466
470
  // page. Subscriptions are per connection, so another socket can never page
467
471
  // this one: its lookup misses and it gets the 404.
@@ -483,7 +487,7 @@ export function handleMoreRows(
483
487
  connection.reply(message, nonExistentSubscriptionNack());
484
488
  return;
485
489
  }
486
- if (detailsSourceRef !== undefined) {
490
+ if (detailsSourceRef !== undefined && detailsSourceRef !== message.SOURCE_REF) {
487
491
  connection.reply(message, { MESSAGE_TYPE: MESSAGE_TYPE.MORE_ROWS_ACK });
488
492
  }
489
493
 
@@ -80,7 +80,7 @@ function eventProperty(field: ResolvedField, annotated: boolean, appName?: strin
80
80
  if (field.maxSize !== undefined) property.maxLength = field.maxSize;
81
81
  }
82
82
  if (type === 'BIGDECIMAL') property.pattern = BIGDECIMAL_PATTERN;
83
- const range = annotated ? rangeOf(type) : undefined;
83
+ const range = annotated ? rangeOf(field) : undefined;
84
84
  if (range) [property.minimum, property.maximum] = range;
85
85
  property.readOnly = false;
86
86
  if (type === 'ENUM' && knownValues(field)) property.enum = knownValues(field);
@@ -150,7 +150,7 @@ export function eventMetaField(
150
150
  if (field.minLength !== undefined) meta.MIN_LENGTH = field.minLength;
151
151
  if (field.maxSize !== undefined) meta.MAX_LENGTH = field.maxSize;
152
152
  }
153
- const range = annotated ? rangeOf(type) : undefined;
153
+ const range = annotated ? rangeOf(field) : undefined;
154
154
  if (range) [meta.MIN, meta.MAX] = range;
155
155
  if (type === 'ENUM') {
156
156
  // The enum class's own default: the declared one, else its first value.
package/src/index.ts CHANGED
@@ -21,6 +21,7 @@ export {
21
21
  export { criteriaRefusal } from './db/criteriaRefusal.ts';
22
22
  export type {
23
23
  AntiJoinDef,
24
+ AuditDef,
24
25
  AuthConfig,
25
26
  AuthUser,
26
27
  BroadcastFn,
@@ -62,8 +62,19 @@ export function jsonTypeOf(type: string): string {
62
62
  return JSON_TYPES[type] ?? 'string';
63
63
  }
64
64
 
65
- export function rangeOf(type: string): [number, number] | undefined {
66
- return RANGES[type];
65
+ // The types GSF's dictionary gives a `metadata { min; max }`
66
+ // (FieldMetadata.Number @v8.15.29).
67
+ const DECLARED_RANGE_TYPES = new Set(['INT', 'SHORT', 'LONG', 'DOUBLE']);
68
+
69
+ // A number field's range: its declared min / max, else its type's.
70
+ export function rangeOf(field: {
71
+ type: string;
72
+ min?: number;
73
+ max?: number;
74
+ }): [number, number] | undefined {
75
+ const range = RANGES[field.type];
76
+ if (!range || !DECLARED_RANGE_TYPES.has(field.type)) return range;
77
+ return [field.min ?? range[0], field.max ?? range[1]];
67
78
  }
68
79
 
69
80
  // TYPE in event META follows the JVM class, not the dictionary: a DATE column
@@ -13,6 +13,7 @@ export function serviceMsgNack(errors: NackErrorEntry[], warnings: NackErrorEntr
13
13
  return { WARNING: warnings, ERROR: errors, MESSAGE_TYPE: MESSAGE_TYPE.MSG_NACK };
14
14
  }
15
15
 
16
+ const HTTP_BAD_REQUEST = 400;
16
17
  const HTTP_NOT_FOUND = 404;
17
18
  const HTTP_CONFLICT = 409;
18
19
 
@@ -32,6 +33,21 @@ function routerNack(code: number, reason: string, text: string): Row {
32
33
  };
33
34
  }
34
35
 
36
+ // The router reads SOURCE_REF and MESSAGE_TYPE before anything else and
37
+ // refuses a message missing either, or naming a blank one
38
+ // (RouterMessageDelegator.processMessage @v8.15.29). With no SOURCE_REF to
39
+ // echo, the first is sent on an empty one.
40
+ export function missingSourceRefNack(): Row {
41
+ return {
42
+ ...routerNack(HTTP_BAD_REQUEST, 'Bad Request', 'No source ref provided'),
43
+ SOURCE_REF: '',
44
+ };
45
+ }
46
+
47
+ export function missingMessageTypeNack(): Row {
48
+ return routerNack(HTTP_BAD_REQUEST, 'Bad Request', 'No message type provided');
49
+ }
50
+
35
51
  // The router resolves every message to a resource name first and answers this
36
52
  // when no service serves it: DETAILS.DATASOURCE_NAME for DATA_LOGON, the name
37
53
  // after REQ_ for request servers, DETAILS.FEATURE for META_REQUEST /