@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,8 +4,11 @@
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.
11
+ import { juxtaposedIndex } from "./criteriaJuxtaposition.js";
9
12
  import { groovyCompileErrorText, groovyTokens } from "./groovySyntax.js";
10
13
  const VALIDATION_FAILED = 'GenericCriteriaValidationException : Criteria validation failed: ';
11
14
  // Classes Groovy resolves in a criteria without an import (java.lang,
@@ -94,6 +97,22 @@ const ALLOWED_CONSTRUCTORS = new Set([
94
97
  const LOGICAL = new Set(['&&', '||']);
95
98
  const COMPARISONS = new Set(['==', '!=', '>', '>=', '<', '<=', 'in', '!in']);
96
99
  const NOT_SUPPORTED = (what) => `${what} are not supported in criteria`;
100
+ // A name that can stand for a type or a variable: not a keyword operator,
101
+ // a literal word or this engine's dialect.
102
+ const isTypeName = (name) => !KEYWORD_OPERATORS.has(name) &&
103
+ !['true', 'false', 'null', 'new'].includes(name) &&
104
+ !DIALECT_WORDS.has(name.toUpperCase());
105
+ const PRIMITIVES = new Set(['boolean', 'byte', 'char', 'short', 'int', 'long', 'float', 'double']);
106
+ // Whether Groovy resolves a type a cast or a declaration names: a
107
+ // primitive, `def` / `var`, a class it imports by default, or a qualified
108
+ // JDK or Groovy class.
109
+ function resolvesType(type) {
110
+ return (PRIMITIVES.has(type) ||
111
+ type === 'def' ||
112
+ type === 'var' ||
113
+ Object.hasOwn(DEFAULT_IMPORT_CLASSES, type) ||
114
+ QUALIFIED_CLASS.test(type));
115
+ }
97
116
  // Thrown for anything this parser doesn't follow: no verdict.
98
117
  class GiveUp extends Error {
99
118
  }
@@ -140,6 +159,11 @@ const DIALECT_WORDS = new Set(['AND', 'OR', 'NOT']);
140
159
  class Parser {
141
160
  at = 0;
142
161
  tokens;
162
+ // A binary operator read so far, and how deep inside a prefix operator's
163
+ // operand the parser is: either makes a command-expression argument a
164
+ // shape Groovy parses differently (criteriaJuxtaposition.ts answers it).
165
+ sawBinary = false;
166
+ prefixDepth = 0;
143
167
  constructor(tokens) {
144
168
  this.tokens = tokens;
145
169
  }
@@ -183,6 +207,66 @@ class Parser {
183
207
  return { op: token.text, length: 1 };
184
208
  return undefined;
185
209
  }
210
+ // Statements separated by ';', each a declaration or an expression.
211
+ statements() {
212
+ const items = [];
213
+ for (;;) {
214
+ items.push(this.declaration() ?? this.expression());
215
+ if (this.peek()?.kind !== 'semicolon')
216
+ break;
217
+ this.next();
218
+ if (this.done)
219
+ break;
220
+ }
221
+ return items.length === 1 ? items[0] : { kind: 'statements', items };
222
+ }
223
+ // `Type x`, `def x`, then `= value`, ';' or the end: a declaration, which
224
+ // Groovy reads even with a field name for its type (`TRADER x`).
225
+ declaration() {
226
+ let ahead = 0;
227
+ const first = this.peek();
228
+ if (first?.kind !== 'name' || !isTypeName(first.text))
229
+ return undefined;
230
+ let type = first.text;
231
+ while (this.peek(ahead + 1)?.text === '.' && this.peek(ahead + 2)?.kind === 'name') {
232
+ type += `.${this.peek(ahead + 2).text}`;
233
+ ahead += 2;
234
+ }
235
+ const variable = this.peek(ahead + 1);
236
+ if (variable?.kind !== 'name' || !isTypeName(variable.text))
237
+ return undefined;
238
+ const after = this.peek(ahead + 2);
239
+ const assigned = after?.kind === 'operator' && after.text === '=';
240
+ if (after && after.kind !== 'semicolon' && !assigned)
241
+ return undefined;
242
+ this.at += ahead + 2;
243
+ if (assigned) {
244
+ this.next();
245
+ this.expression();
246
+ }
247
+ return { kind: 'declaration', type, offset: variable.offset };
248
+ }
249
+ // `(Type)` before something a cast can apply to: the type and how many
250
+ // tokens it takes, '(' to ')'. `(TRADER) == x` is a parenthesized TRADER.
251
+ castType() {
252
+ let ahead = 1;
253
+ const first = this.peek(ahead);
254
+ if (first?.kind !== 'name' || !isTypeName(first.text))
255
+ return undefined;
256
+ let type = first.text;
257
+ while (this.peek(ahead + 1)?.text === '.' && this.peek(ahead + 2)?.kind === 'name') {
258
+ type += `.${this.peek(ahead + 2).text}`;
259
+ ahead += 2;
260
+ }
261
+ if (this.peek(ahead + 1)?.text !== ')')
262
+ return undefined;
263
+ const operand = this.peek(ahead + 2);
264
+ const startsOperand = operand?.kind === 'literal' ||
265
+ (operand?.kind === 'name' && isTypeName(operand.text)) ||
266
+ (operand?.kind === 'open' && ['(', '[', '{'].includes(operand.text)) ||
267
+ (operand?.kind === 'operator' && (operand.text === '!' || operand.text === '~'));
268
+ return startsOperand ? { type, length: ahead + 2 } : undefined;
269
+ }
186
270
  expression(minPower = 0) {
187
271
  let left = this.unary();
188
272
  for (;;) {
@@ -218,6 +302,7 @@ class Parser {
218
302
  if (power <= minPower)
219
303
  return left;
220
304
  this.at += operator.length;
305
+ this.sawBinary = true;
221
306
  const right = this.expression(RIGHT_ASSOCIATIVE.has(operator.op) ? power - 1 : power);
222
307
  if (operator.op === '..' || operator.op === '..<') {
223
308
  left = { kind: 'refused', message: NOT_SUPPORTED('Range expressions') };
@@ -235,21 +320,30 @@ class Parser {
235
320
  if (token?.kind === 'operator') {
236
321
  if (token.text === '!' || token.text === '-' || token.text === '+') {
237
322
  this.next();
238
- return { kind: 'visitInner', operand: this.expression(UNARY_POWER) };
323
+ return { kind: 'visitInner', operand: this.prefixOperand() };
239
324
  }
240
325
  if (token.text === '~') {
241
326
  this.next();
242
- this.expression(UNARY_POWER);
327
+ this.prefixOperand();
243
328
  return { kind: 'refused', message: NOT_SUPPORTED('Bitwise negation expressions') };
244
329
  }
245
330
  if (token.text === '++' || token.text === '--') {
246
331
  this.next();
247
- this.expression(UNARY_POWER);
332
+ this.prefixOperand();
248
333
  return { kind: 'refused', message: NOT_SUPPORTED('Prefix expressions') };
249
334
  }
250
335
  }
251
336
  return this.postfix(this.primary());
252
337
  }
338
+ prefixOperand() {
339
+ this.prefixDepth += 1;
340
+ try {
341
+ return this.expression(UNARY_POWER);
342
+ }
343
+ finally {
344
+ this.prefixDepth -= 1;
345
+ }
346
+ }
253
347
  postfix(start) {
254
348
  let node = start;
255
349
  for (;;) {
@@ -262,6 +356,10 @@ class Parser {
262
356
  if (this.peek()?.text === '(' && this.peek()?.kind === 'open') {
263
357
  node = { kind: 'call', object: node, name: member.text, args: this.arguments() };
264
358
  }
359
+ else if (this.peek()?.text === '{' && this.peek()?.kind === 'open') {
360
+ // A closure as the only argument: `.any { ... }`.
361
+ node = { kind: 'call', object: node, name: member.text, args: [this.primary()] };
362
+ }
265
363
  else {
266
364
  node = { kind: 'property', object: node, name: member.text };
267
365
  }
@@ -293,6 +391,9 @@ class Parser {
293
391
  this.next();
294
392
  return args;
295
393
  }
394
+ const command = this.commandArgument();
395
+ if (command)
396
+ return [command];
296
397
  for (;;) {
297
398
  args.push(this.expression());
298
399
  const after = this.next();
@@ -302,6 +403,40 @@ class Parser {
302
403
  throw new GiveUp();
303
404
  }
304
405
  }
406
+ // A call's only argument written as operands side by side, up to its ')':
407
+ // the command expression Groovy reads, `a b c d e` as `this.a(b).c(d).e`.
408
+ // Undefined when the argument doesn't start that way.
409
+ commandArgument() {
410
+ const tokens = this.tokens.slice(this.at, this.at + 2);
411
+ if (tokens.length < 2 || juxtaposedIndex(tokens) !== 1)
412
+ return undefined;
413
+ // After a binary operator, inside a prefix operator's operand, or led by
414
+ // a number (`1.call(b)`), Groovy parses it another way.
415
+ if (this.sawBinary || this.prefixDepth > 0)
416
+ throw new GiveUp();
417
+ if (tokens[0].kind === 'literal' && !/^['"]/.test(tokens[0].text))
418
+ throw new GiveUp();
419
+ const run = [this.next(), this.next()];
420
+ while (this.peek() && juxtaposedIndex([run.at(-1), this.peek()]) === 1)
421
+ run.push(this.next());
422
+ this.expect(')');
423
+ const operand = (token) => token.kind === 'name' && !['true', 'false', 'null'].includes(token.text)
424
+ ? { kind: 'variable', name: token.text }
425
+ : { kind: 'constant' };
426
+ let node = {
427
+ kind: 'call',
428
+ object: undefined,
429
+ name: run[0].text,
430
+ args: [operand(run[1])],
431
+ };
432
+ for (let index = 2; index < run.length; index += 2) {
433
+ const argument = run[index + 1];
434
+ node = argument
435
+ ? { kind: 'call', object: node, name: run[index].text, args: [operand(argument)] }
436
+ : { kind: 'property', object: node, name: run[index].text };
437
+ }
438
+ return node;
439
+ }
305
440
  primary() {
306
441
  const token = this.next();
307
442
  if (token.kind === 'literal') {
@@ -325,6 +460,14 @@ class Parser {
325
460
  return { kind: 'variable', name: token.text };
326
461
  }
327
462
  if (token.kind === 'open' && token.text === '(') {
463
+ this.at -= 1;
464
+ const cast = this.castType();
465
+ this.at += 1;
466
+ if (cast) {
467
+ this.at += cast.length - 1;
468
+ this.unary();
469
+ return { kind: 'cast', type: cast.type, offset: token.offset };
470
+ }
328
471
  const inner = this.expression();
329
472
  this.expect(')');
330
473
  return inner;
@@ -458,6 +601,18 @@ function visit(node, allowed, verdict) {
458
601
  }
459
602
  return;
460
603
  }
604
+ case 'cast':
605
+ case 'declaration':
606
+ if (!resolvesType(node.type))
607
+ verdict.unresolved ??= { name: node.type, offset: node.offset };
608
+ else {
609
+ verdict.errors.push(NOT_SUPPORTED(node.kind === 'cast' ? 'Cast expressions' : 'Declaration expressions'));
610
+ }
611
+ return;
612
+ case 'statements':
613
+ for (const item of node.items)
614
+ visit(item, allowed, verdict);
615
+ return;
461
616
  case 'call': {
462
617
  const { object } = node;
463
618
  if (object?.kind === 'variable' && object.name === 'Expr') {
@@ -484,11 +639,11 @@ function visit(node, allowed, verdict) {
484
639
  const SIMPLE_OPERATORS = new Set(['&&', '||', '==', '!=', '<', '<=', '>', '>=', '!', '.']);
485
640
  // Whether the criteria uses Groovy this engine's grammar doesn't read: an
486
641
  // operator other than its own, a bracket or brace, `new`, `in`,
487
- // `instanceof`, `as`.
642
+ // `instanceof`, `as`, or operands side by side.
488
643
  function usesGroovy(tokens) {
489
- return tokens.some((token) => (token.kind === 'operator' && !SIMPLE_OPERATORS.has(token.text)) ||
644
+ return (tokens.some((token) => (token.kind === 'operator' && !SIMPLE_OPERATORS.has(token.text)) ||
490
645
  (token.kind === 'open' && token.text !== '(') ||
491
- (token.kind === 'name' && ['new', 'in', 'instanceof', 'as'].includes(token.text)));
646
+ (token.kind === 'name' && ['new', 'in', 'instanceof', 'as'].includes(token.text))) || juxtaposedIndex(tokens) >= 0);
492
647
  }
493
648
  // GSF's verdict on a criteria in Groovy this engine's grammar doesn't read;
494
649
  // undefined when it reads it all (criteriaDialect.ts answers), or this
@@ -500,7 +655,7 @@ export function groovyValidation(criteria, allowed) {
500
655
  let root;
501
656
  try {
502
657
  const parser = new Parser(tokens);
503
- root = parser.expression();
658
+ root = parser.statements();
504
659
  if (!parser.done)
505
660
  return undefined;
506
661
  }
@@ -519,5 +674,9 @@ export function groovyValidation(criteria, allowed) {
519
674
  if (verdict.errors.length > 0) {
520
675
  return { kind: 'refused', text: `${VALIDATION_FAILED}${verdict.errors.join(',')}` };
521
676
  }
677
+ // Statements that pass the validator still have to compile as a filter,
678
+ // which this parser doesn't follow.
679
+ if (root.kind === 'statements')
680
+ return undefined;
522
681
  return { kind: 'passed' };
523
682
  }
@@ -9,6 +9,8 @@ export interface ResolvedField {
9
9
  default?: unknown;
10
10
  maxSize?: number;
11
11
  minLength?: number;
12
+ min?: number;
13
+ max?: number;
12
14
  scale?: number;
13
15
  description?: string;
14
16
  title?: string;
package/dist/db/schema.js CHANGED
@@ -41,6 +41,8 @@ function fromFieldDef(field, table, notNullByDefault, generated) {
41
41
  ...(field.default !== undefined ? { default: field.default } : {}),
42
42
  ...(field.maxSize !== undefined ? { maxSize: field.maxSize } : {}),
43
43
  ...(field.minLength !== undefined ? { minLength: field.minLength } : {}),
44
+ ...(field.min !== undefined ? { min: field.min } : {}),
45
+ ...(field.max !== undefined ? { max: field.max } : {}),
44
46
  ...(field.scale !== undefined ? { scale: field.scale } : {}),
45
47
  ...(field.description !== undefined ? { description: field.description } : {}),
46
48
  ...(field.title !== undefined ? { title: field.title } : {}),
@@ -1,4 +1,5 @@
1
1
  import type { Row, TableDef, ViewDef } from '../types.ts';
2
+ import { type AuditContext } from './audit.ts';
2
3
  import { type PlannedAntiJoin, type PlannedJoin, type ViewEntry } from './views.ts';
3
4
  export interface ViewRowsSource {
4
5
  base?: Row[];
@@ -7,23 +8,33 @@ export interface ViewRowsSource {
7
8
  type Pk = string;
8
9
  export interface StoreOptions {
9
10
  normalizeValues?: () => boolean;
11
+ holdSharedSequences?: () => boolean;
10
12
  }
11
13
  export declare class Store {
12
14
  private tables;
13
15
  private tableConfig;
14
16
  private sequenceCounters;
17
+ private sequenceUse;
15
18
  private autoIncrementCounters;
16
19
  private clock;
17
20
  private seedRows;
18
21
  private removedRows;
19
22
  private previousRows;
20
23
  private savepoint;
24
+ private auditTables;
25
+ private auditContext;
26
+ private auditWrites;
21
27
  readonly views: Map<string, ViewDef>;
22
28
  private readonly normalizeValues;
29
+ private readonly holdSharedSequences;
23
30
  private readonly unlistedWarned;
24
- constructor({ normalizeValues }?: StoreOptions);
25
- begin(): void;
31
+ constructor({ normalizeValues, holdSharedSequences, }?: StoreOptions);
32
+ begin(audit?: AuditContext): void;
26
33
  commit(): void;
34
+ takeAuditWrites(): Array<{
35
+ tableName: string;
36
+ key: Pk;
37
+ }>;
27
38
  rollback(): void;
28
39
  get inTransaction(): boolean;
29
40
  private save;
@@ -47,6 +58,7 @@ export declare class Store {
47
58
  findRow(tableName: string, criteria: Row): Row | undefined;
48
59
  withGeneratedValues(tableName: string, record: Row): Row;
49
60
  insertRow(tableName: string, record: Row): Row;
61
+ private audit;
50
62
  updateRow(tableName: string, pk: Pk, patch: Row): Row | undefined;
51
63
  deleteRow(tableName: string, pk: Pk): boolean;
52
64
  takeRemovedRows(tableName: string, key: Pk): Row[];
package/dist/db/store.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // In-memory tables + views + sequence generation. No persistence, no external deps.
2
2
  // This is the "database" — disposable and rebuilt from seed data on every server start.
3
+ import { auditRecord, auditTableName, isAudited } from "./audit.js";
3
4
  import { RECORD_ID, RecordClock, recordKey, TIMESTAMP, withoutRecordFields } from "./identity.js";
4
5
  import { columnScale, storedValue, unlistedEnumValue } from "./values.js";
5
6
  import { materializeEntries, planView, } from "./views.js";
@@ -78,6 +79,9 @@ export class Store {
78
79
  tables = new Map();
79
80
  tableConfig = new Map();
80
81
  sequenceCounters = new Map();
82
+ // Per sequence counter and table, the highest value the table has taken
83
+ // from it: its seed rows' and the ones its writes used.
84
+ sequenceUse = new Map();
81
85
  autoIncrementCounters = new Map();
82
86
  clock = new RecordClock();
83
87
  // Immutable snapshot of each table's rows as registered, used only for field
@@ -97,12 +101,20 @@ export class Store {
97
101
  // While a transaction is open (see begin), each table it has written to,
98
102
  // as it was before.
99
103
  savepoint;
104
+ // Each audited table's audit table (db/audit.ts), and while an event's
105
+ // transaction is open, the event its writes are audited for and the audit
106
+ // rows they added.
107
+ auditTables = new Map();
108
+ auditContext;
109
+ auditWrites = [];
100
110
  views = new Map();
101
111
  normalizeValues;
112
+ holdSharedSequences;
102
113
  // ENUM values already warned about, as 'TABLE.FIELD=value'.
103
114
  unlistedWarned = new Set();
104
- constructor({ normalizeValues = () => true } = {}) {
115
+ constructor({ normalizeValues = () => true, holdSharedSequences = () => true, } = {}) {
105
116
  this.normalizeValues = normalizeValues;
117
+ this.holdSharedSequences = holdSharedSequences;
106
118
  }
107
119
  // Opens a transaction: the commit-event path wraps every handler in one
108
120
  // (handlers/commitEvent.ts), so an event that NACKs leaves the tables as
@@ -113,19 +125,31 @@ export class Store {
113
125
  // back: a value used stays used, as a database sequence's does. Not nested,
114
126
  // and the engine's own: an event that finds one open answers INTERNAL_ERROR.
115
127
  // Saving copies the table's row map, O(rows) per event and table written.
116
- begin() {
128
+ // With an audit context, each write to an audited table also adds its
129
+ // audit row (see audit.ts), undone with the rest by rollback().
130
+ begin(audit) {
117
131
  if (this.savepoint)
118
132
  throw new Error('A store transaction is already open');
119
133
  this.savepoint = new Map();
134
+ this.auditContext = audit;
135
+ this.auditWrites = [];
120
136
  }
121
137
  // Keeps every write since begin().
122
138
  commit() {
123
139
  this.savepoint = undefined;
140
+ this.auditContext = undefined;
141
+ }
142
+ // The audit rows the open transaction added (table and key), for the live
143
+ // pushes that announce them once it commits.
144
+ takeAuditWrites() {
145
+ return this.auditWrites.splice(0);
124
146
  }
125
147
  // Undoes every write since begin().
126
148
  rollback() {
127
149
  const savepoint = this.savepoint;
128
150
  this.savepoint = undefined;
151
+ this.auditContext = undefined;
152
+ this.auditWrites = [];
129
153
  for (const [tableName, saved] of savepoint ?? []) {
130
154
  this.tables.set(tableName, saved.rows);
131
155
  restoreEntry(this.removedRows, tableName, saved.removed);
@@ -190,6 +214,10 @@ export class Store {
190
214
  }
191
215
  this.tables.set(tableName, table);
192
216
  this.tableConfig.set(tableName, config);
217
+ if (isAudited(definition))
218
+ this.auditTables.set(tableName, auditTableName(tableName));
219
+ else
220
+ this.auditTables.delete(tableName);
193
221
  // Registering a table again replaces it whole: a rollback leaves it be.
194
222
  this.savepoint?.delete(tableName);
195
223
  this.removedRows.delete(tableName);
@@ -197,7 +225,11 @@ export class Store {
197
225
  this.seedRows.set(tableName, seeded.map((row) => ({ ...row })));
198
226
  // Each counter starts after the highest value the seed rows carry (two
199
227
  // fields of one table sharing a prefix share the counter). The key's
200
- // sequence only exists for a single-field pk.
228
+ // sequence only exists for a single-field pk. A sequence is shared by
229
+ // every table with its prefix: it also starts after the values the other
230
+ // tables on it have taken, so no id repeats across them (an audit
231
+ // table's default sequence AU, an app table on the same prefix). This
232
+ // table's own earlier values don't count: registering it again resets it.
201
233
  const starts = new Map();
202
234
  for (const generator of config.generators) {
203
235
  let maxN = 0;
@@ -211,7 +243,17 @@ export class Store {
211
243
  }
212
244
  for (const generator of config.generators) {
213
245
  const counter = this.counterOf(tableName, config, generator);
214
- this.countersFor(generator).set(counter, starts.get(counter));
246
+ const seeded = starts.get(counter);
247
+ let start = seeded;
248
+ if (generator.kind === 'SEQUENCE') {
249
+ const use = this.sequenceUse.get(counter) ?? new Map();
250
+ this.sequenceUse.set(counter, use.set(tableName, seeded - 1));
251
+ if (this.holdSharedSequences()) {
252
+ const others = [...use].filter(([table]) => table !== tableName).map(([, n]) => n);
253
+ start = Math.max(seeded, ...others.map((n) => n + 1));
254
+ }
255
+ }
256
+ this.countersFor(generator).set(counter, start);
215
257
  }
216
258
  return this;
217
259
  }
@@ -337,6 +379,11 @@ export class Store {
337
379
  const counter = this.counterOf(tableName, config, generator);
338
380
  if (n + 1 > (counters.get(counter) ?? 1))
339
381
  counters.set(counter, n + 1);
382
+ if (generator.kind === 'SEQUENCE') {
383
+ const use = this.sequenceUse.get(counter) ?? new Map();
384
+ if (n > (use.get(tableName) ?? 0))
385
+ this.sequenceUse.set(counter, use.set(tableName, n));
386
+ }
340
387
  }
341
388
  }
342
389
  nextSequenceValue(sequencePrefix, width) {
@@ -445,8 +492,19 @@ export class Store {
445
492
  if (displaced)
446
493
  this.noteRemoved(tableName, key, displaced);
447
494
  table.set(key, row);
495
+ this.audit(tableName, row);
448
496
  return row;
449
497
  }
498
+ // Adds a write's audit row, when the table is audited and an event's
499
+ // transaction is open.
500
+ audit(tableName, record) {
501
+ const auditTable = this.auditTables.get(tableName);
502
+ const context = this.auditContext;
503
+ if (!auditTable || !context || !this.tables.has(auditTable))
504
+ return;
505
+ const row = this.insertRow(auditTable, auditRecord(record, context, Date.now()));
506
+ this.auditWrites.push({ tableName: auditTable, key: this.rowKey(auditTable, row) });
507
+ }
450
508
  // Merges the patch, moves TIMESTAMP on and keeps RECORD_ID (a patch can't
451
509
  // change a row's identity).
452
510
  updateRow(tableName, pk, patch) {
@@ -473,6 +531,7 @@ export class Store {
473
531
  this.noteRemoved(tableName, nextKey, displaced);
474
532
  }
475
533
  table.set(nextKey, updated);
534
+ this.audit(tableName, updated);
476
535
  return updated;
477
536
  }
478
537
  deleteRow(tableName, pk) {
@@ -483,6 +542,7 @@ export class Store {
483
542
  this.save(tableName);
484
543
  table.delete(pk);
485
544
  this.noteRemoved(tableName, pk, existing);
545
+ this.audit(tableName, existing);
486
546
  return true;
487
547
  }
488
548
  // Reads and clears every record a key lost that no broadcast announced
@@ -12,10 +12,10 @@ function resolveEventHandler(eventName, config) {
12
12
  crudEventHandler(eventName, config) ??
13
13
  config.defaultEventHandler);
14
14
  }
15
- function openTransaction(store, ctx) {
15
+ function openTransaction(store, ctx, audit) {
16
16
  const pending = [];
17
17
  let open = true;
18
- store.begin();
18
+ store.begin(audit);
19
19
  const hold = (push) => {
20
20
  if (open)
21
21
  pending.push(push);
@@ -26,6 +26,10 @@ function openTransaction(store, ctx) {
26
26
  broadcast: (resourceName, operation, row) => hold(() => ctx.broadcast(resourceName, operation, row)),
27
27
  broadcastTableChange: (tableName, operation, pk) => hold(() => ctx.broadcastTableChange(tableName, operation, pk)),
28
28
  commit() {
29
+ // The audit rows the writes added go live with them.
30
+ for (const { tableName, key } of store.takeAuditWrites()) {
31
+ pending.push(() => ctx.broadcastTableChange(tableName, 'INSERT', key));
32
+ }
29
33
  open = false;
30
34
  store.commit();
31
35
  },
@@ -48,6 +52,15 @@ function openTransaction(store, ctx) {
48
52
  },
49
53
  };
50
54
  }
55
+ // What GSF audits an event's writes with (TransactionalEventHandlerContainer:
56
+ // entityDb.audited(message.userName, message.messageType, message.reason)).
57
+ function auditContext(message, connection) {
58
+ return {
59
+ eventType: message.MESSAGE_TYPE,
60
+ user: connection.userName ?? '',
61
+ text: typeof message.REASON === 'string' ? message.REASON : null,
62
+ };
63
+ }
51
64
  // Commit events (EVENT_<ENTITY>_INSERT/_AMEND/_DELETE, or whatever suffix
52
65
  // convention your project uses) are looked up by exact MESSAGE_TYPE against
53
66
  // config.eventHandlers, then the tables' generic CRUD events. A handler
@@ -119,7 +132,7 @@ export function handleCommitEvent(message, connection, config, store, ctx) {
119
132
  // Inside the try: a store transaction someone else left open fails this
120
133
  // event (INTERNAL_ERROR) instead of leaving it unanswered.
121
134
  if (!legacy)
122
- transaction = openTransaction(store, ctx);
135
+ transaction = openTransaction(store, ctx, auditContext(message, connection));
123
136
  const schemaErrors = legacy ? [] : validateEventMessage(message, connection, config, store);
124
137
  if (schemaErrors.length > 0)
125
138
  throw new NackError(schemaErrors);
@@ -246,7 +246,8 @@ export function handleDataLogon(message, connection, config, store) {
246
246
  // getCriteriaFilter, v8.15.29): no subscription opens. Refused here: the
247
247
  // syntax errors Groovy would report too, and this engine's own dialect —
248
248
  // AND / OR / NOT, `<>`, a single `=`, dotted names, `$` in double quotes,
249
- // calls off GSF's whitelists (db/criteriaDialect.ts). A criteria outside
249
+ // calls off GSF's whitelists (db/criteriaDialect.ts), operands side by
250
+ // side (db/criteriaJuxtaposition.ts). A criteria outside
250
251
  // this engine's grammar that
251
252
  // may well be valid Groovy still fails open below.
252
253
  const refusal = typeof criteria === 'string'
@@ -369,8 +370,11 @@ export function handleDataLogon(message, connection, config, store) {
369
370
  // createMoreRowsMessage gives the request a SOURCE_REF of its own) or, when
370
371
  // that is absent, in its own SOURCE_REF. The page goes out as a QUERY_UPDATE
371
372
  // on the subscription's SOURCE_REF without ROWS_COUNT; MORE_ROWS_ACK answers
372
- // the request's own SOURCE_REF, and only when DETAILS.SOURCE_REF was given —
373
- // both as the real ConnectionManager.processMoreRows does. The ack goes first
373
+ // the request's own SOURCE_REF, and only when DETAILS.SOURCE_REF names another
374
+ // one. The real router rewrites DETAILS.SOURCE_REF to its own channel-prefixed
375
+ // form only then, so that is the only ack ConnectionManager.processMoreRows
376
+ // sends that it can route back to the socket (MsgType.MoreRowsOrColumns
377
+ // @v8.15.29); on the subscription's own SOURCE_REF the ack is lost. The ack goes first
374
378
  // because the real server writes it while its dataserver is still reading the
375
379
  // page. Subscriptions are per connection, so another socket can never page
376
380
  // this one: its lookup misses and it gets the 404.
@@ -387,7 +391,7 @@ export function handleMoreRows(message, connection, config) {
387
391
  connection.reply(message, nonExistentSubscriptionNack());
388
392
  return;
389
393
  }
390
- if (detailsSourceRef !== undefined) {
394
+ if (detailsSourceRef !== undefined && detailsSourceRef !== message.SOURCE_REF) {
391
395
  connection.reply(message, { MESSAGE_TYPE: MESSAGE_TYPE.MORE_ROWS_ACK });
392
396
  }
393
397
  const page = takePage(subscription);
@@ -40,7 +40,7 @@ function eventProperty(field, annotated, appName) {
40
40
  }
41
41
  if (type === 'BIGDECIMAL')
42
42
  property.pattern = BIGDECIMAL_PATTERN;
43
- const range = annotated ? rangeOf(type) : undefined;
43
+ const range = annotated ? rangeOf(field) : undefined;
44
44
  if (range)
45
45
  [property.minimum, property.maximum] = range;
46
46
  property.readOnly = false;
@@ -96,7 +96,7 @@ export function eventMetaField(field, { annotated, appName }) {
96
96
  if (field.maxSize !== undefined)
97
97
  meta.MAX_LENGTH = field.maxSize;
98
98
  }
99
- const range = annotated ? rangeOf(type) : undefined;
99
+ const range = annotated ? rangeOf(field) : undefined;
100
100
  if (range)
101
101
  [meta.MIN, meta.MAX] = range;
102
102
  if (type === 'ENUM') {
package/dist/index.d.ts CHANGED
@@ -5,4 +5,4 @@ export { RECORD_FIELDS, RECORD_ID, TIMESTAMP } from './db/identity.ts';
5
5
  export { ErrorCode, NackError, statusCodeFor, type ErrorCodeName, type GenesisErrorType, type NackErrorEntry, type NackErrorInput, type NackErrorOptions, } from './protocol/errors.ts';
6
6
  export { compileCriteria, type CriteriaOptions, filterByCriteria, type Predicate, } from './db/criteria.ts';
7
7
  export { criteriaRefusal } from './db/criteriaRefusal.ts';
8
- export type { AntiJoinDef, AuthConfig, AuthUser, BroadcastFn, BroadcastTableChangeFn, CookieAuthenticationOptions, CrudEventVerb, EventHandler, EventHandlerCtx, EventHandlerResult, EventSchemaOverride, FidelityMode, FieldDef, FieldMetadata, GeneratedFieldDef, GenesisFieldType, GenesisMessage, HttpRouteDef, IndexDef, JoinDef, JoinFieldLink, JoinLink, JoinType, JoinValueLink, LiveModifyMode, MockServerConfig, OnMessageCtx, OnMessageHook, ProductDetails, QueryDef, RequestingUser, RequestReplyDef, RequestReplyResolverCtx, ResourceAuthDef, ResourceItem, Row, SequenceFormat, SocketLike, SsoIdentityProvider, TableDef, ViewDef, ViewRowSources, } from './types.ts';
8
+ export type { AntiJoinDef, AuditDef, AuthConfig, AuthUser, BroadcastFn, BroadcastTableChangeFn, CookieAuthenticationOptions, CrudEventVerb, EventHandler, EventHandlerCtx, EventHandlerResult, EventSchemaOverride, FidelityMode, FieldDef, FieldMetadata, GeneratedFieldDef, GenesisFieldType, GenesisMessage, HttpRouteDef, IndexDef, JoinDef, JoinFieldLink, JoinLink, JoinType, JoinValueLink, LiveModifyMode, MockServerConfig, OnMessageCtx, OnMessageHook, ProductDetails, QueryDef, RequestingUser, RequestReplyDef, RequestReplyResolverCtx, ResourceAuthDef, ResourceItem, Row, SequenceFormat, SocketLike, SsoIdentityProvider, TableDef, ViewDef, ViewRowSources, } from './types.ts';
@@ -1,6 +1,10 @@
1
1
  export declare const BIGDECIMAL_PATTERN = "[0-9]+(\\.[0-9]+)?";
2
2
  export declare function jsonTypeOf(type: string): string;
3
- export declare function rangeOf(type: string): [number, number] | undefined;
3
+ export declare function rangeOf(field: {
4
+ type: string;
5
+ min?: number;
6
+ max?: number;
7
+ }): [number, number] | undefined;
4
8
  export declare function eventMetaTypeOf(type: string): string;
5
9
  export declare function titleCase(name: string): string;
6
10
  export declare function pascalCase(name: string): string;
@@ -56,8 +56,15 @@ export const BIGDECIMAL_PATTERN = '[0-9]+(\\.[0-9]+)?';
56
56
  export function jsonTypeOf(type) {
57
57
  return JSON_TYPES[type] ?? 'string';
58
58
  }
59
- export function rangeOf(type) {
60
- return RANGES[type];
59
+ // The types GSF's dictionary gives a `metadata { min; max }`
60
+ // (FieldMetadata.Number @v8.15.29).
61
+ const DECLARED_RANGE_TYPES = new Set(['INT', 'SHORT', 'LONG', 'DOUBLE']);
62
+ // A number field's range: its declared min / max, else its type's.
63
+ export function rangeOf(field) {
64
+ const range = RANGES[field.type];
65
+ if (!range || !DECLARED_RANGE_TYPES.has(field.type))
66
+ return range;
67
+ return [field.min ?? range[0], field.max ?? range[1]];
61
68
  }
62
69
  // TYPE in event META follows the JVM class, not the dictionary: a DATE column
63
70
  // is a DateTime property, so it is TYPE 'DATETIME' (GENESIS_TYPE says 'DATE').
@@ -1,6 +1,8 @@
1
1
  import type { Row } from '../types.ts';
2
2
  import type { NackErrorEntry } from './errors.ts';
3
3
  export declare function serviceMsgNack(errors: NackErrorEntry[], warnings?: NackErrorEntry[]): Row;
4
+ export declare function missingSourceRefNack(): Row;
5
+ export declare function missingMessageTypeNack(): Row;
4
6
  export declare function resourceNotMappedNack(resourceName: string | undefined): Row;
5
7
  export declare function nonExistentSubscriptionNack(): Row;
6
8
  export declare function subscriptionAlreadyExistsNack(): Row;