@genesislcap/mock-server 15.3.2-canary.16 → 15.7.3-alpha-94532c4.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 +75 -21
- package/package.json +2 -2
- package/src/db/criteria.ts +153 -13
- package/src/db/metadata.ts +15 -5
- package/src/db/store.ts +28 -6
- package/src/handlers/auth.ts +42 -1
- package/src/handlers/commitEvent.ts +9 -4
- package/src/handlers/http.ts +35 -8
- package/src/handlers/jsonSchema.ts +6 -2
- package/src/handlers/meta.ts +1 -1
- package/src/handlers/requestReply.ts +18 -1
- package/src/protocol/messageTypes.ts +9 -0
- package/src/protocol/router.ts +79 -3
- package/src/server.ts +50 -13
- package/src/types.ts +9 -0
- package/test/criteria.test.ts +174 -0
- package/test/jsonSchema.test.ts +38 -0
- package/test/metadata.test.ts +49 -0
- package/test/server.test.ts +308 -0
- package/test/store.test.ts +181 -0
- package/tsconfig.json +1 -1
package/README.md
CHANGED
|
@@ -97,6 +97,21 @@ CI/local dev. Leave it unset to require an explicit `EVENT_LOGIN_AUTH` POST
|
|
|
97
97
|
(validated against `config.auth.users`, same as the WS flow) before
|
|
98
98
|
`event-login-details` will succeed on a later reload.
|
|
99
99
|
|
|
100
|
+
An **opt-in** auth gate (`config.auth.requireAuth: true`) rejects WS data
|
|
101
|
+
traffic (`DATA_LOGON`, `REQ_*`, `META_REQUEST`, `JSON_SCHEMA_REQUEST`, commit
|
|
102
|
+
events) with a `NOT_LOGGED_ON` error until the connection logs in over WS or
|
|
103
|
+
the message carries a `SESSION_AUTH_TOKEN` this server issued over **either**
|
|
104
|
+
transport. The WS token-refresh login (`EVENT_LOGIN_AUTH` with only
|
|
105
|
+
`DETAILS.REFRESH_AUTH_TOKEN`, no credentials — foundation-comms'
|
|
106
|
+
`createRefreshTokenMessage`) is honored via the same token registry.
|
|
107
|
+
|
|
108
|
+
The gate is **off by default** for a hard-won reason: clients that log in
|
|
109
|
+
over foundation-auth's HTTP transport send **no** `SESSION_AUTH_TOKEN` on any
|
|
110
|
+
WebSocket message — session storage's `authToken` is never populated by that
|
|
111
|
+
flow, so `message.ts`'s token stamp serializes to nothing (verified from
|
|
112
|
+
browser frame captures). A default-on gate would reject every datasource such
|
|
113
|
+
a client opens. Enable it only for clients that authenticate over WS.
|
|
114
|
+
|
|
100
115
|
## Protocol coverage
|
|
101
116
|
|
|
102
117
|
| MESSAGE_TYPE | Handled |
|
|
@@ -109,7 +124,7 @@ CI/local dev. Leave it unset to require an explicit `EVENT_LOGIN_AUTH` POST
|
|
|
109
124
|
| `META_REQUEST` | ✅ auto-derived from seed data |
|
|
110
125
|
| `JSON_SCHEMA_REQUEST` | ✅ auto-derived per event — see below |
|
|
111
126
|
| `RESOURCES_REQUEST` | ✅ |
|
|
112
|
-
| `REQ_<NAME>` / `REP_<NAME>` | ✅ |
|
|
127
|
+
| `REQ_<NAME>` / `REP_<NAME>` | ✅ incl. keyed `REQUEST` field lookups (`'*'` = any) and `CRITERIA_MATCH` |
|
|
113
128
|
| `EVENT_<ENTITY>_<VERB>` commit events | ✅ via `config.eventHandlers` |
|
|
114
129
|
|
|
115
130
|
## Entity-management (`@genesislcap/foundation-entity-management`) support
|
|
@@ -214,7 +229,15 @@ extracted from for how each was actually found:
|
|
|
214
229
|
array, uppercase keys. Throw `NackError` from an event handler or
|
|
215
230
|
request-reply resolver rather than hand-rolling this shape; a lowercase
|
|
216
231
|
`{error, details}` object crashes the real client's `.forEach`.
|
|
217
|
-
3.
|
|
232
|
+
3. **Commit-event acks are the GENERIC `EVENT_ACK`/`EVENT_NACK`** — never
|
|
233
|
+
`<EVENT_NAME>_ACK`/`_NACK`. The reply is correlated purely by
|
|
234
|
+
`SOURCE_REF`; real consumers match the MESSAGE_TYPE string exactly
|
|
235
|
+
(`foundation-comms`' `kv.service` checks `=== 'EVENT_ACK'`,
|
|
236
|
+
entity-management and the showcase's `customEvents.ts` check
|
|
237
|
+
`=== 'EVENT_NACK'`). Get this wrong and every successful commit renders
|
|
238
|
+
as a failure toast while every NACK passes as silent success.
|
|
239
|
+
`handleCommitEvent()` (`src/handlers/commitEvent.ts`) does this for you.
|
|
240
|
+
4. **`DATA_LOGON` snapshot and later live pushes use the identical envelope**
|
|
218
241
|
(`MESSAGE_TYPE: 'QUERY_UPDATE'`, same `SOURCE_REF`) — the client
|
|
219
242
|
distinguishes insert/amend/delete purely via each row's
|
|
220
243
|
`DETAILS.OPERATION` (`'INSERT' | 'MODIFY' | 'DELETE'`, no other vocabulary
|
|
@@ -222,17 +245,35 @@ extracted from for how each was actually found:
|
|
|
222
245
|
(`src/protocol/rowUpdate.ts`) does this for you — always route pushes
|
|
223
246
|
through `broadcast()`/`broadcastTableChange()`, never `connection.send()`
|
|
224
247
|
directly.
|
|
225
|
-
|
|
248
|
+
5. **Datasource `init()` needs `TYPE: 'DATASERVER'` plus a non-empty `FIELD`
|
|
226
249
|
array** or the client silently reports "no fields returned" and gives up.
|
|
227
|
-
`deriveFields()` (`src/db/metadata.ts`) infers this from the
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
6.
|
|
250
|
+
`deriveFields()` (`src/db/metadata.ts`) infers this from the rows' own JS
|
|
251
|
+
types — falling back to the registration-time seed rows when every live
|
|
252
|
+
row has been deleted, so a runtime delete can't collapse a resource's
|
|
253
|
+
schema to zero fields. Numeric inference scans every row (a column is
|
|
254
|
+
`DOUBLE` if any value is fractional), but a column whose values are *all*
|
|
255
|
+
whole numbers still infers as `LONG` — if it's semantically a `DOUBLE`
|
|
256
|
+
(e.g. a NOTIONAL that happens to be round in the seed data), say so via
|
|
257
|
+
the `fieldTypes` override on the query/request-reply.
|
|
258
|
+
6. **`CRITERIA_MATCH` needs more than `==`.** `compileCriteria()`
|
|
259
|
+
(`src/db/criteria.ts`) supports `== != > >= < <= AND OR NOT` (also `&&`/`||`)
|
|
260
|
+
and parentheses via a small typed grammar (not `eval`/`new Function` — a
|
|
261
|
+
malformed criteria string can only fail to parse, never execute arbitrary
|
|
262
|
+
JS). It also understands the `Expr.<fn>(field, value)` function-call
|
|
263
|
+
criteria that grid date/text filters generate — mirroring
|
|
264
|
+
`foundation-criteria`'s `serialisers.ts` and `foundation-ui`'s date-range
|
|
265
|
+
filter: `contains`, `containsIgnoreCase`, `dateIsToday`, `dateIsEqual`,
|
|
266
|
+
`dateIsGreaterEqual`, `dateIsLessEqual`, `dateTimeIsEqual`,
|
|
267
|
+
`dateTimeIsGreaterEqual`, `dateTimeIsLessEqual`, `dateTimeIsAfter`,
|
|
268
|
+
`dateTimeIsBefore` (`dateIs*` compares by calendar day, `dateTimeIs*` by
|
|
269
|
+
full timestamp; date values are `'YYYYMMDD-HH:MM[:SS]'`, matching
|
|
270
|
+
`formatDate()` in `serialisers.ts`). Any other criteria this grammar still
|
|
271
|
+
doesn't cover fails open instead of crashing the handler: `compileCriteria()`
|
|
272
|
+
logs a warning and returns a match-all predicate, so `DATA_LOGON` still
|
|
273
|
+
responds (unfiltered) rather than leaving the client's grid stuck on
|
|
274
|
+
"Loading...". Add new `Expr.*` entries to `EXPR_FUNCTIONS` in `criteria.ts`
|
|
275
|
+
if you hit one that isn't covered yet.
|
|
276
|
+
7. **Composite-key AMEND/DELETE matching needs a fallback beyond the PK.**
|
|
236
277
|
`Store.findRow()` (`src/db/store.ts`) tries the configured PK field, then
|
|
237
278
|
any single field that uniquely identifies one row, then the full
|
|
238
279
|
combination of provided fields — needed for tables where no single field
|
|
@@ -240,15 +281,28 @@ extracted from for how each was actually found:
|
|
|
240
281
|
|
|
241
282
|
## Known, accepted limitation
|
|
242
283
|
|
|
243
|
-
If a live update makes a row fall out of a subscription's
|
|
244
|
-
the correct behavior is to push it as a `DELETE` so the
|
|
245
|
-
out-of-scope row. This engine does not do that
|
|
246
|
-
only checks the *new* row against the
|
|
247
|
-
matching just stops being pushed rather than
|
|
248
|
-
the client's local state. Fix this in your own
|
|
249
|
-
leans harder on live filtering than the
|
|
250
|
-
membership yourself and call
|
|
251
|
-
when a row exits the criteria).
|
|
284
|
+
If a live **update** (MODIFY) makes a row fall out of a subscription's
|
|
285
|
+
`CRITERIA_MATCH`, the correct behavior is to push it as a `DELETE` so the
|
|
286
|
+
client drops the now out-of-scope row. This engine does not do that
|
|
287
|
+
automatically — `broadcast()` only checks the *new* row against the
|
|
288
|
+
predicate, so a row that stops matching just stops being pushed rather than
|
|
289
|
+
being explicitly removed from the client's local state. Fix this in your own
|
|
290
|
+
`eventHandlers` if a project leans harder on live filtering than the
|
|
291
|
+
showcase example does (e.g. re-check membership yourself and call
|
|
292
|
+
`broadcastTableChange(table, 'DELETE', pk)` when a row exits the criteria).
|
|
293
|
+
|
|
294
|
+
Actual **deletes** are not affected: DELETE pushes bypass the criteria
|
|
295
|
+
predicate entirely (the pushed row is a PK-only stub that couldn't satisfy
|
|
296
|
+
one anyway), so every subscriber is told to drop the row — a client that
|
|
297
|
+
never held that `ROW_REF` ignores the message.
|
|
298
|
+
|
|
299
|
+
## Tests
|
|
300
|
+
|
|
301
|
+
`npm run --workspace=@genesislcap/mock-server test` runs the unit tests for
|
|
302
|
+
the criteria grammar, store, metadata inference, and event-table inference
|
|
303
|
+
plus end-to-end WS/HTTP tests (auth gating, filtered-subscription DELETE
|
|
304
|
+
delivery, keyed `REQ_*` lookups, session reuse, clean shutdown) via
|
|
305
|
+
`node --test`, no build step.
|
|
252
306
|
|
|
253
307
|
## What's per-project vs. generic
|
|
254
308
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@genesislcap/mock-server",
|
|
3
|
-
"version": "15.3
|
|
3
|
+
"version": "15.7.3-alpha-94532c4.0",
|
|
4
4
|
"description": "Reusable in-memory Genesis WebSocket protocol mock server for client automation/CI, without a JVM or database.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"publishConfig": {
|
|
34
34
|
"access": "public"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "85a871e7a46382dbfc7cc262a508e0cc95d3b3e3"
|
|
37
37
|
}
|
package/src/db/criteria.ts
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
// Evaluator for Genesis CRITERIA_MATCH expressions, e.g.:
|
|
2
2
|
// TRADE_STATUS == 'CONFIRMED' AND QUANTITY > 100
|
|
3
3
|
// NOT (SIDE == 'SELL') OR PRICE >= 100.5
|
|
4
|
+
// Expr.dateTimeIsGreaterEqual(TRADE_DATE, '20240101-00:00:00') && COUNTERPARTY_ID == 'CPTY001'
|
|
4
5
|
//
|
|
5
|
-
// Supports: == = != <> > >= < <=, AND OR NOT, parentheses,
|
|
6
|
-
// literals,
|
|
7
|
-
//
|
|
8
|
-
//
|
|
6
|
+
// Supports: == = != <> > >= < <=, AND OR NOT (also && ||), parentheses,
|
|
7
|
+
// string/number/boolean literals, bare field identifiers, and the
|
|
8
|
+
// `Expr.<fn>(field, value)` function-call criteria that foundation-criteria's
|
|
9
|
+
// serialisers.ts emits for grid date/text filters (see EXPR_FUNCTIONS below).
|
|
10
|
+
// Deliberately NOT eval()/new Function() — this parses a small typed grammar
|
|
11
|
+
// so a malformed or hostile criteria string can only fail to parse, never
|
|
12
|
+
// execute arbitrary JS.
|
|
9
13
|
|
|
10
14
|
import type { Row } from '../types.ts';
|
|
11
15
|
|
|
12
16
|
export type Predicate = (record: Row) => boolean;
|
|
13
17
|
|
|
14
18
|
const TOKEN_RE =
|
|
15
|
-
/\s*(
|
|
19
|
+
/\s*(&&|\|\||<=|>=|!=|<>|==|=|<|>|\(|\)|,|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|-?\d+(?:\.\d+)?|[A-Za-z_][A-Za-z0-9_.]*)/g;
|
|
16
20
|
|
|
17
21
|
function tokenize(input: string): string[] {
|
|
18
22
|
const tokens: string[] = [];
|
|
@@ -43,6 +47,100 @@ function parseLiteral(token: string): unknown {
|
|
|
43
47
|
|
|
44
48
|
const COMPARATORS = new Set(['==', '=', '!=', '<>', '<', '<=', '>', '>=']);
|
|
45
49
|
|
|
50
|
+
// Genesis date/datetime values serialize as 'YYYYMMDD-HH:MM[:SS]' (see
|
|
51
|
+
// foundation-criteria's serialisers.ts formatDate); row values may already be
|
|
52
|
+
// epoch millis (number) or an ISO string. Returns null if unparseable.
|
|
53
|
+
function parseGenesisDateTime(value: unknown): number | null {
|
|
54
|
+
if (typeof value === 'number') return value;
|
|
55
|
+
if (typeof value !== 'string') return null;
|
|
56
|
+
const match = value.match(/^(\d{4})(\d{2})(\d{2})-(\d{2}):(\d{2})(?::(\d{2}))?$/);
|
|
57
|
+
if (match) {
|
|
58
|
+
const [, y, mo, d, h, mi, s] = match;
|
|
59
|
+
return Date.UTC(Number(y), Number(mo) - 1, Number(d), Number(h), Number(mi), Number(s ?? '0'));
|
|
60
|
+
}
|
|
61
|
+
const parsed = Date.parse(value);
|
|
62
|
+
return Number.isNaN(parsed) ? null : parsed;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const HOURS_PER_DAY = 24;
|
|
66
|
+
const MINUTES_PER_HOUR = 60;
|
|
67
|
+
const SECONDS_PER_MINUTE = 60;
|
|
68
|
+
const MS_PER_SECOND = 1000;
|
|
69
|
+
const MS_PER_DAY = HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE * MS_PER_SECOND;
|
|
70
|
+
|
|
71
|
+
function toEpochDay(value: unknown): number | null {
|
|
72
|
+
const epoch = parseGenesisDateTime(value);
|
|
73
|
+
return epoch === null ? null : Math.floor(epoch / MS_PER_DAY);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
type ExprResolver = (token: string, record: Row) => unknown;
|
|
77
|
+
|
|
78
|
+
// Expr.<fn>(field, value) function-call criteria, as emitted by
|
|
79
|
+
// foundation-criteria's serialisers.ts (contains/containsIgnoreCase/dateIs*)
|
|
80
|
+
// and foundation-ui's date-range filter.ts (dateTimeIsAfter/dateTimeIsBefore,
|
|
81
|
+
// which operator-map.ts maps onto the same >=/<= semantics as
|
|
82
|
+
// dateTimeIsGreaterEqual/dateTimeIsLessEqual). `dateIs*` compares by calendar
|
|
83
|
+
// day (UTC), `dateTimeIs*` compares full timestamps.
|
|
84
|
+
const EXPR_FUNCTIONS: Record<
|
|
85
|
+
string,
|
|
86
|
+
(record: Row, args: string[], resolve: ExprResolver) => boolean
|
|
87
|
+
> = {
|
|
88
|
+
contains: (record, args, resolve) => {
|
|
89
|
+
const value = resolve(args[0], record);
|
|
90
|
+
const needle = resolve(args[1], record);
|
|
91
|
+
return typeof value === 'string' && typeof needle === 'string' && value.includes(needle);
|
|
92
|
+
},
|
|
93
|
+
containsIgnoreCase: (record, args, resolve) => {
|
|
94
|
+
const value = resolve(args[0], record);
|
|
95
|
+
const needle = resolve(args[1], record);
|
|
96
|
+
return (
|
|
97
|
+
typeof value === 'string' &&
|
|
98
|
+
typeof needle === 'string' &&
|
|
99
|
+
value.toLowerCase().includes(needle.toLowerCase())
|
|
100
|
+
);
|
|
101
|
+
},
|
|
102
|
+
dateIsToday: (record, args, resolve) => {
|
|
103
|
+
const day = toEpochDay(resolve(args[0], record));
|
|
104
|
+
return day !== null && day === toEpochDay(Date.now());
|
|
105
|
+
},
|
|
106
|
+
dateIsEqual: (record, args, resolve) => compareByDay(record, args, resolve, (a, b) => a === b),
|
|
107
|
+
dateIsGreaterEqual: (record, args, resolve) =>
|
|
108
|
+
compareByDay(record, args, resolve, (a, b) => a >= b),
|
|
109
|
+
dateIsLessEqual: (record, args, resolve) => compareByDay(record, args, resolve, (a, b) => a <= b),
|
|
110
|
+
dateTimeIsEqual: (record, args, resolve) =>
|
|
111
|
+
compareByTime(record, args, resolve, (a, b) => a === b),
|
|
112
|
+
dateTimeIsGreaterEqual: (record, args, resolve) =>
|
|
113
|
+
compareByTime(record, args, resolve, (a, b) => a >= b),
|
|
114
|
+
dateTimeIsLessEqual: (record, args, resolve) =>
|
|
115
|
+
compareByTime(record, args, resolve, (a, b) => a <= b),
|
|
116
|
+
dateTimeIsAfter: (record, args, resolve) =>
|
|
117
|
+
compareByTime(record, args, resolve, (a, b) => a >= b),
|
|
118
|
+
dateTimeIsBefore: (record, args, resolve) =>
|
|
119
|
+
compareByTime(record, args, resolve, (a, b) => a <= b),
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
function compareByDay(
|
|
123
|
+
record: Row,
|
|
124
|
+
args: string[],
|
|
125
|
+
resolve: ExprResolver,
|
|
126
|
+
cmp: (a: number, b: number) => boolean,
|
|
127
|
+
): boolean {
|
|
128
|
+
const a = toEpochDay(resolve(args[0], record));
|
|
129
|
+
const b = toEpochDay(resolve(args[1], record));
|
|
130
|
+
return a !== null && b !== null && cmp(a, b);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function compareByTime(
|
|
134
|
+
record: Row,
|
|
135
|
+
args: string[],
|
|
136
|
+
resolve: ExprResolver,
|
|
137
|
+
cmp: (a: number, b: number) => boolean,
|
|
138
|
+
): boolean {
|
|
139
|
+
const a = parseGenesisDateTime(resolve(args[0], record));
|
|
140
|
+
const b = parseGenesisDateTime(resolve(args[1], record));
|
|
141
|
+
return a !== null && b !== null && cmp(a, b);
|
|
142
|
+
}
|
|
143
|
+
|
|
46
144
|
class Parser {
|
|
47
145
|
private tokens: string[];
|
|
48
146
|
private pos = 0;
|
|
@@ -71,7 +169,7 @@ class Parser {
|
|
|
71
169
|
|
|
72
170
|
parseOr(): Predicate {
|
|
73
171
|
let left = this.parseAnd();
|
|
74
|
-
while (this.peek()?.toUpperCase() === 'OR') {
|
|
172
|
+
while (this.peek() === '||' || this.peek()?.toUpperCase() === 'OR') {
|
|
75
173
|
this.next();
|
|
76
174
|
const right = this.parseAnd();
|
|
77
175
|
const prev = left;
|
|
@@ -82,7 +180,7 @@ class Parser {
|
|
|
82
180
|
|
|
83
181
|
parseAnd(): Predicate {
|
|
84
182
|
let left = this.parseNot();
|
|
85
|
-
while (this.peek()?.toUpperCase() === 'AND') {
|
|
183
|
+
while (this.peek() === '&&' || this.peek()?.toUpperCase() === 'AND') {
|
|
86
184
|
this.next();
|
|
87
185
|
const right = this.parseNot();
|
|
88
186
|
const prev = left;
|
|
@@ -112,6 +210,11 @@ class Parser {
|
|
|
112
210
|
if (leftToken === undefined) {
|
|
113
211
|
throw new Error('Expected operand on the left side of comparison');
|
|
114
212
|
}
|
|
213
|
+
|
|
214
|
+
if (/^Expr\.[A-Za-z_][A-Za-z0-9_]*$/.test(leftToken) && this.peek() === '(') {
|
|
215
|
+
return this.parseFunctionCall(leftToken);
|
|
216
|
+
}
|
|
217
|
+
|
|
115
218
|
const op = this.peek();
|
|
116
219
|
if (!op || !COMPARATORS.has(op)) {
|
|
117
220
|
throw new Error(`Expected comparator, got: ${op}`);
|
|
@@ -154,6 +257,30 @@ class Parser {
|
|
|
154
257
|
if (literal !== undefined) return literal;
|
|
155
258
|
return record[token];
|
|
156
259
|
}
|
|
260
|
+
|
|
261
|
+
parseFunctionCall(name: string): Predicate {
|
|
262
|
+
this.next(); // consume '('
|
|
263
|
+
const args: string[] = [];
|
|
264
|
+
if (this.peek() !== ')') {
|
|
265
|
+
const arg = this.next();
|
|
266
|
+
if (arg === undefined) throw new Error(`Unterminated argument list in ${name}(...)`);
|
|
267
|
+
args.push(arg);
|
|
268
|
+
while (this.peek() === ',') {
|
|
269
|
+
this.next();
|
|
270
|
+
const nextArg = this.next();
|
|
271
|
+
if (nextArg === undefined) throw new Error(`Unterminated argument list in ${name}(...)`);
|
|
272
|
+
args.push(nextArg);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (this.next() !== ')') throw new Error(`Missing closing parenthesis in ${name}(...)`);
|
|
276
|
+
|
|
277
|
+
const fnName = name.slice('Expr.'.length);
|
|
278
|
+
const handler = EXPR_FUNCTIONS[fnName];
|
|
279
|
+
if (!handler) throw new Error(`Unsupported criteria function: ${name}`);
|
|
280
|
+
|
|
281
|
+
const resolve: ExprResolver = (token, record) => this.resolveValue(token, record);
|
|
282
|
+
return (record: Row) => handler(record, args, resolve);
|
|
283
|
+
}
|
|
157
284
|
}
|
|
158
285
|
|
|
159
286
|
/** Compiles a CRITERIA_MATCH string into a predicate function(record) => boolean. */
|
|
@@ -161,13 +288,26 @@ export function compileCriteria(criteria?: string | null): Predicate {
|
|
|
161
288
|
if (!criteria || typeof criteria !== 'string' || criteria.trim() === '') {
|
|
162
289
|
return () => true;
|
|
163
290
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
291
|
+
// Real Genesis clients can emit CRITERIA_MATCH syntax this grammar doesn't
|
|
292
|
+
// cover yet (e.g. function-call criteria like Expr.dateTimeIsGreaterEqual(...)
|
|
293
|
+
// from date-range filters). Failing open — matching everything and logging —
|
|
294
|
+
// keeps DATA_LOGON/broadcast working (unfiltered) instead of crashing the
|
|
295
|
+
// handler and leaving the client's grid stuck on "Loading...".
|
|
296
|
+
try {
|
|
297
|
+
const tokens = tokenize(criteria.trim());
|
|
298
|
+
const parser = new Parser(tokens);
|
|
299
|
+
const predicate = parser.parseExpression();
|
|
300
|
+
if (parser.position !== tokens.length) {
|
|
301
|
+
throw new Error(`Unexpected trailing tokens in criteria: ${criteria}`);
|
|
302
|
+
}
|
|
303
|
+
return predicate;
|
|
304
|
+
} catch (error) {
|
|
305
|
+
console.warn(
|
|
306
|
+
`[mock-server] Unsupported CRITERIA_MATCH syntax, ignoring filter: ${criteria}`,
|
|
307
|
+
error,
|
|
308
|
+
);
|
|
309
|
+
return () => true;
|
|
169
310
|
}
|
|
170
|
-
return predicate;
|
|
171
311
|
}
|
|
172
312
|
|
|
173
313
|
export function filterByCriteria(rows: Row[], criteria?: string | null): Row[] {
|
package/src/db/metadata.ts
CHANGED
|
@@ -4,10 +4,19 @@
|
|
|
4
4
|
|
|
5
5
|
import type { FieldMetadata, Row } from '../types.ts';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
// Numeric fields scan EVERY row, not just the first non-null value: a column
|
|
8
|
+
// like PRICE is DOUBLE if any row holds a fractional value, even when row one
|
|
9
|
+
// happens to land on a whole number. A column whose values are all whole
|
|
10
|
+
// numbers still infers as LONG — if it's semantically a DOUBLE, say so via
|
|
11
|
+
// the fieldTypes override (see README).
|
|
12
|
+
function inferFieldType(name: string, rows: Row[], sample: unknown): string {
|
|
13
|
+
if (typeof sample === 'number') {
|
|
14
|
+
const anyFractional = rows.some(
|
|
15
|
+
(row) => typeof row[name] === 'number' && !Number.isInteger(row[name]),
|
|
16
|
+
);
|
|
17
|
+
return anyFractional ? 'DOUBLE' : 'LONG';
|
|
18
|
+
}
|
|
19
|
+
if (typeof sample === 'boolean') return 'BOOLEAN';
|
|
11
20
|
return 'STRING';
|
|
12
21
|
}
|
|
13
22
|
|
|
@@ -26,7 +35,8 @@ export function deriveFields(rows: Row[], overrides: Record<string, string> = {}
|
|
|
26
35
|
|
|
27
36
|
return fieldNames.map((name) => {
|
|
28
37
|
const sampleRow = rows.find((row) => row[name] !== undefined && row[name] !== null);
|
|
29
|
-
const type =
|
|
38
|
+
const type =
|
|
39
|
+
overrides[name] ?? (sampleRow ? inferFieldType(name, rows, sampleRow[name]) : 'STRING');
|
|
30
40
|
return { NAME: name, TYPE: type };
|
|
31
41
|
});
|
|
32
42
|
}
|
package/src/db/store.ts
CHANGED
|
@@ -15,6 +15,10 @@ export class Store {
|
|
|
15
15
|
private tables = new Map<string, Map<Pk, Row>>();
|
|
16
16
|
private tableConfig = new Map<string, TableConfig>();
|
|
17
17
|
private sequenceCounters = new Map<string, number>();
|
|
18
|
+
// Immutable snapshot of each table's rows as registered, used only for field
|
|
19
|
+
// metadata derivation (see getMetadataRows) so deleting every live row can't
|
|
20
|
+
// collapse a resource's schema to zero fields.
|
|
21
|
+
private seedRows = new Map<string, Row[]>();
|
|
18
22
|
readonly views = new Map<string, ViewDef>();
|
|
19
23
|
|
|
20
24
|
registerTable(
|
|
@@ -27,6 +31,10 @@ export class Store {
|
|
|
27
31
|
}
|
|
28
32
|
this.tables.set(tableName, table);
|
|
29
33
|
this.tableConfig.set(tableName, { pkField, sequencePrefix, sequenceWidth });
|
|
34
|
+
this.seedRows.set(
|
|
35
|
+
tableName,
|
|
36
|
+
rows.map((row) => ({ ...row })),
|
|
37
|
+
);
|
|
30
38
|
|
|
31
39
|
if (sequencePrefix) {
|
|
32
40
|
let maxN = 0;
|
|
@@ -122,16 +130,16 @@ export class Store {
|
|
|
122
130
|
getViewRows(viewName: string): Row[] {
|
|
123
131
|
const view = this.views.get(viewName);
|
|
124
132
|
if (!view) throw new Error(`Unknown view: ${viewName}`);
|
|
133
|
+
return this.materializeView(view, (tableName) => this.getAllRows(tableName));
|
|
134
|
+
}
|
|
125
135
|
|
|
126
|
-
|
|
136
|
+
private materializeView(view: ViewDef, getRows: (tableName: string) => Row[]): Row[] {
|
|
137
|
+
let rows = getRows(view.base).map((row) => ({ ...row }));
|
|
127
138
|
|
|
128
139
|
for (const join of view.joins ?? []) {
|
|
129
|
-
const joinTable = this.tables.get(join.table);
|
|
130
140
|
const joinIndex = new Map<unknown, Row>();
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
joinIndex.set(row[join.on[1]], row);
|
|
134
|
-
}
|
|
141
|
+
for (const row of getRows(join.table)) {
|
|
142
|
+
joinIndex.set(row[join.on[1]], row);
|
|
135
143
|
}
|
|
136
144
|
rows = rows.map((row) => {
|
|
137
145
|
const match = joinIndex.get(row[join.on[0]]);
|
|
@@ -162,6 +170,20 @@ export class Store {
|
|
|
162
170
|
return this.getAllRows(resourceName);
|
|
163
171
|
}
|
|
164
172
|
|
|
173
|
+
// Rows to derive META_REQUEST/JSON_SCHEMA_REQUEST field metadata from:
|
|
174
|
+
// current rows when there are any, else the registration-time seed rows
|
|
175
|
+
// (re-joined through the view definition for views). Without this fallback,
|
|
176
|
+
// deleting a table's last row would make the next metadata reply say the
|
|
177
|
+
// resource has zero fields — which entity-management renders as a form with
|
|
178
|
+
// no inputs and a delete button that can't build a payload, silently.
|
|
179
|
+
getMetadataRows(resourceName: string): Row[] {
|
|
180
|
+
const live = this.getResourceRows(resourceName);
|
|
181
|
+
if (live.length > 0) return live;
|
|
182
|
+
const view = this.views.get(resourceName);
|
|
183
|
+
if (view) return this.materializeView(view, (tableName) => this.seedRows.get(tableName) ?? []);
|
|
184
|
+
return this.seedRows.get(resourceName) ?? [];
|
|
185
|
+
}
|
|
186
|
+
|
|
165
187
|
// Row-identity field for a table or view, used to stamp DETAILS.ROW_REF on
|
|
166
188
|
// QUERY_UPDATE rows (see handlers/dataLogon.ts and dataServerResultFilter.ts
|
|
167
189
|
// client-side, which keys inserts/amends/deletes off this field).
|
package/src/handlers/auth.ts
CHANGED
|
@@ -5,6 +5,32 @@ import type { AuthUser, GenesisMessage, MockServerConfig, Row } from '../types.t
|
|
|
5
5
|
|
|
6
6
|
const DEFAULT_HEARTBEAT_INTERVAL_SECS = 30;
|
|
7
7
|
|
|
8
|
+
// Every SESSION_AUTH_TOKEN/REFRESH_AUTH_TOKEN issued by this server -> the
|
|
9
|
+
// username it was issued to. Shared across the WS and HTTP transports so a
|
|
10
|
+
// token from an HTTP login authenticates later WS traffic (see router.ts) and
|
|
11
|
+
// foundation-comms' WS token-refresh (EVENT_LOGIN_AUTH with only
|
|
12
|
+
// DETAILS.REFRESH_AUTH_TOKEN, no credentials) can succeed.
|
|
13
|
+
export type TokenRegistry = Map<string, string>;
|
|
14
|
+
|
|
15
|
+
// Both transports mint tokens on every login/silent-login, so a long-lived
|
|
16
|
+
// dev/CI server would otherwise grow these maps one entry per page load.
|
|
17
|
+
// Maps iterate in insertion order, so dropping from the front evicts the
|
|
18
|
+
// oldest sessions/tokens first.
|
|
19
|
+
const MAX_REGISTRY_ENTRIES = 1000;
|
|
20
|
+
export function capRegistry(registry: Map<string, string>): void {
|
|
21
|
+
while (registry.size > MAX_REGISTRY_ENTRIES) {
|
|
22
|
+
const oldest = registry.keys().next().value;
|
|
23
|
+
if (oldest === undefined) return;
|
|
24
|
+
registry.delete(oldest);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function registerAckTokens(ack: Row, username: string, tokens: TokenRegistry): void {
|
|
29
|
+
tokens.set(ack.SESSION_AUTH_TOKEN, username);
|
|
30
|
+
tokens.set(ack.REFRESH_AUTH_TOKEN, username);
|
|
31
|
+
capRegistry(tokens);
|
|
32
|
+
}
|
|
33
|
+
|
|
8
34
|
// Shared by the WS EVENT_LOGIN_AUTH handler and the HTTP login handlers
|
|
9
35
|
// (handlers/http.ts) — EVENT_LOGIN_DETAILS_ACK is byte-for-byte the same shape
|
|
10
36
|
// as EVENT_LOGIN_AUTH_ACK, just under a different MESSAGE_TYPE (see
|
|
@@ -54,9 +80,23 @@ export function handleLogin(
|
|
|
54
80
|
message: GenesisMessage,
|
|
55
81
|
connection: Connection,
|
|
56
82
|
config: MockServerConfig,
|
|
83
|
+
tokens: TokenRegistry,
|
|
57
84
|
): void {
|
|
58
85
|
const details = message.DETAILS ?? {};
|
|
59
|
-
|
|
86
|
+
// Two login shapes share EVENT_LOGIN_AUTH: credentials ({USER_NAME,
|
|
87
|
+
// PASSWORD}) and token refresh — foundation-comms' createRefreshTokenMessage
|
|
88
|
+
// sends only DETAILS.REFRESH_AUTH_TOKEN (plus the stored SESSION_AUTH_TOKEN
|
|
89
|
+
// at the top level), no username at all.
|
|
90
|
+
const tokenUsername =
|
|
91
|
+
details.USER_NAME === undefined
|
|
92
|
+
? (tokens.get(details.REFRESH_AUTH_TOKEN) ??
|
|
93
|
+
tokens.get(details.SESSION_AUTH_TOKEN) ??
|
|
94
|
+
tokens.get(message.SESSION_AUTH_TOKEN))
|
|
95
|
+
: undefined;
|
|
96
|
+
const user =
|
|
97
|
+
details.USER_NAME !== undefined
|
|
98
|
+
? findUser(config, details.USER_NAME, details.PASSWORD)
|
|
99
|
+
: findUser(config, tokenUsername);
|
|
60
100
|
|
|
61
101
|
if (!user) {
|
|
62
102
|
connection.reply(message, {
|
|
@@ -74,6 +114,7 @@ export function handleLogin(
|
|
|
74
114
|
}
|
|
75
115
|
|
|
76
116
|
const ack = buildLoginAckBody(user, config);
|
|
117
|
+
registerAckTokens(ack, user.username, tokens);
|
|
77
118
|
connection.authenticated = true;
|
|
78
119
|
connection.userName = user.username;
|
|
79
120
|
connection.profile = user.profile ?? [];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Store } from '../db/store.ts';
|
|
2
2
|
import type { Connection } from '../protocol/connection.ts';
|
|
3
3
|
import { NackError } from '../protocol/errors.ts';
|
|
4
|
-
import { ERROR_CODE } from '../protocol/messageTypes.ts';
|
|
4
|
+
import { ERROR_CODE, MESSAGE_TYPE } from '../protocol/messageTypes.ts';
|
|
5
5
|
import type {
|
|
6
6
|
BroadcastFn,
|
|
7
7
|
BroadcastTableChangeFn,
|
|
@@ -28,9 +28,14 @@ export function handleCommitEvent(
|
|
|
28
28
|
const eventName = message.MESSAGE_TYPE;
|
|
29
29
|
const handler = config.eventHandlers?.[eventName];
|
|
30
30
|
|
|
31
|
+
// Replies use the GENERIC EVENT_ACK/EVENT_NACK — correlation is purely via
|
|
32
|
+
// SOURCE_REF. See the note on MESSAGE_TYPE in protocol/messageTypes.ts:
|
|
33
|
+
// real consumers (foundation-comms' kv.service, entity-management) match
|
|
34
|
+
// these names exactly, so a name-suffixed <EVENT>_ACK reads as failure and
|
|
35
|
+
// a <EVENT>_NACK passes as silent success.
|
|
31
36
|
if (!handler) {
|
|
32
37
|
connection.reply(message, {
|
|
33
|
-
MESSAGE_TYPE:
|
|
38
|
+
MESSAGE_TYPE: MESSAGE_TYPE.EVENT_NACK,
|
|
34
39
|
ERROR: [
|
|
35
40
|
{ CODE: ERROR_CODE.UNKNOWN_RESOURCE, TEXT: `No event handler registered for ${eventName}` },
|
|
36
41
|
],
|
|
@@ -41,7 +46,7 @@ export function handleCommitEvent(
|
|
|
41
46
|
try {
|
|
42
47
|
const result = handler(message.DETAILS ?? {}, { connection, store, message, ...ctx }) ?? {};
|
|
43
48
|
connection.reply(message, {
|
|
44
|
-
MESSAGE_TYPE:
|
|
49
|
+
MESSAGE_TYPE: MESSAGE_TYPE.EVENT_ACK,
|
|
45
50
|
GENERATED: result.generated ?? [],
|
|
46
51
|
});
|
|
47
52
|
} catch (error) {
|
|
@@ -49,6 +54,6 @@ export function handleCommitEvent(
|
|
|
49
54
|
error instanceof NackError
|
|
50
55
|
? error.errors
|
|
51
56
|
: [{ CODE: ERROR_CODE.VALIDATION_ERROR, TEXT: (error as Error).message }];
|
|
52
|
-
connection.reply(message, { MESSAGE_TYPE:
|
|
57
|
+
connection.reply(message, { MESSAGE_TYPE: MESSAGE_TYPE.EVENT_NACK, ERROR: errors });
|
|
53
58
|
}
|
|
54
59
|
}
|
package/src/handlers/http.ts
CHANGED
|
@@ -2,7 +2,13 @@ import crypto from 'node:crypto';
|
|
|
2
2
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
3
3
|
import { MESSAGE_TYPE } from '../protocol/messageTypes.ts';
|
|
4
4
|
import type { MockServerConfig } from '../types.ts';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
buildLoginAckBody,
|
|
7
|
+
capRegistry,
|
|
8
|
+
findUser,
|
|
9
|
+
registerAckTokens,
|
|
10
|
+
type TokenRegistry,
|
|
11
|
+
} from './auth.ts';
|
|
6
12
|
|
|
7
13
|
const SESSION_COOKIE = 'MOCK_SESSION_ID';
|
|
8
14
|
|
|
@@ -58,7 +64,26 @@ async function readJsonBody(req: IncomingMessage): Promise<Record<string, any>>
|
|
|
58
64
|
return raw ? JSON.parse(raw) : {};
|
|
59
65
|
}
|
|
60
66
|
|
|
61
|
-
|
|
67
|
+
// Reuse the request's own session when its cookie already maps to this user —
|
|
68
|
+
// otherwise every page load would mint (and leak) a fresh session entry on a
|
|
69
|
+
// long-lived dev/CI server.
|
|
70
|
+
function ensureSession(
|
|
71
|
+
sessions: Map<string, string>,
|
|
72
|
+
cookieSessionId: string | undefined,
|
|
73
|
+
username: string,
|
|
74
|
+
): string {
|
|
75
|
+
if (cookieSessionId && sessions.get(cookieSessionId) === username) return cookieSessionId;
|
|
76
|
+
const sessionId = crypto.randomUUID();
|
|
77
|
+
sessions.set(sessionId, username);
|
|
78
|
+
capRegistry(sessions);
|
|
79
|
+
return sessionId;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function createHttpRequestListener(
|
|
83
|
+
config: MockServerConfig,
|
|
84
|
+
sessions: Map<string, string>,
|
|
85
|
+
tokens: TokenRegistry = new Map(),
|
|
86
|
+
) {
|
|
62
87
|
return async function requestListener(req: IncomingMessage, res: ServerResponse): Promise<void> {
|
|
63
88
|
const pathname = (req.url ?? '').split('?')[0];
|
|
64
89
|
|
|
@@ -95,10 +120,11 @@ export function createHttpRequestListener(config: MockServerConfig, sessions: Ma
|
|
|
95
120
|
return;
|
|
96
121
|
}
|
|
97
122
|
|
|
98
|
-
const sessionId =
|
|
99
|
-
sessions.set(sessionId, user.username);
|
|
123
|
+
const sessionId = ensureSession(sessions, cookies[SESSION_COOKIE], user.username);
|
|
100
124
|
setSessionCookie(res, sessionId);
|
|
101
|
-
|
|
125
|
+
const ack = buildLoginAckBody(user, config, 'EVENT_LOGIN_DETAILS_ACK');
|
|
126
|
+
registerAckTokens(ack, user.username, tokens);
|
|
127
|
+
sendJson(res, HTTP_STATUS.OK, ack);
|
|
102
128
|
return;
|
|
103
129
|
}
|
|
104
130
|
|
|
@@ -130,10 +156,11 @@ export function createHttpRequestListener(config: MockServerConfig, sessions: Ma
|
|
|
130
156
|
return;
|
|
131
157
|
}
|
|
132
158
|
|
|
133
|
-
const sessionId =
|
|
134
|
-
sessions.set(sessionId, user.username);
|
|
159
|
+
const sessionId = ensureSession(sessions, cookies[SESSION_COOKIE], user.username);
|
|
135
160
|
setSessionCookie(res, sessionId);
|
|
136
|
-
|
|
161
|
+
const ack = buildLoginAckBody(user, config);
|
|
162
|
+
registerAckTokens(ack, user.username, tokens);
|
|
163
|
+
sendJson(res, HTTP_STATUS.OK, ack);
|
|
137
164
|
return;
|
|
138
165
|
}
|
|
139
166
|
|