@genesislcap/mock-server 15.64.1 → 15.65.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 +93 -44
- package/dist/db/criteria.js +2 -2
- package/dist/db/criteriaTokens.d.ts +1 -0
- package/dist/db/criteriaTokens.js +11 -0
- package/dist/db/legacyCriteria.js +2 -2
- package/dist/handlers/dataLogon.d.ts +4 -1
- package/dist/handlers/dataLogon.js +75 -23
- package/dist/index.d.ts +1 -1
- package/dist/protocol/connection.d.ts +11 -2
- package/dist/protocol/fidelity.d.ts +2 -1
- package/dist/protocol/fidelity.js +5 -0
- package/dist/server.js +127 -62
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
- package/src/db/criteria.ts +2 -1
- package/src/db/criteriaTokens.ts +16 -0
- package/src/db/legacyCriteria.ts +2 -1
- package/src/handlers/dataLogon.ts +94 -28
- package/src/index.ts +1 -0
- package/src/protocol/connection.ts +33 -7
- package/src/protocol/fidelity.ts +7 -1
- package/src/server.ts +126 -59
- package/src/types.ts +10 -0
package/dist/server.js
CHANGED
|
@@ -1,27 +1,63 @@
|
|
|
1
1
|
import http from 'node:http';
|
|
2
2
|
import { WebSocketServer } from 'ws';
|
|
3
3
|
import { recordKey } from "./db/identity.js";
|
|
4
|
+
import { compareRecordIds } from "./db/ordering.js";
|
|
4
5
|
import { Store } from "./db/store.js";
|
|
5
6
|
import { entriesReading, planView, } from "./db/views.js";
|
|
6
7
|
import { crudEventHandler } from "./handlers/crudEvents.js";
|
|
7
|
-
import { pendingKey } from "./handlers/dataLogon.js";
|
|
8
|
+
import { pendingKey, viewRow, waitForPage } from "./handlers/dataLogon.js";
|
|
8
9
|
import { createHttpRequestListener } from "./handlers/http.js";
|
|
9
10
|
import { permittedQueryRow, requestingUser } from "./handlers/resourceAuth.js";
|
|
10
11
|
import { Connection } from "./protocol/connection.js";
|
|
11
|
-
import { isLegacyFidelity } from "./protocol/fidelity.js";
|
|
12
|
+
import { isLegacyFidelity, liveModifyMode } from "./protocol/fidelity.js";
|
|
12
13
|
import { routeMessage } from "./protocol/router.js";
|
|
13
14
|
import { dataserverRow, stampRow } from "./protocol/rowUpdate.js";
|
|
14
|
-
// Whether two
|
|
15
|
-
|
|
15
|
+
// Whether two values are the same: the same primitive, or the same JSON.
|
|
16
|
+
function sameValue(a, b) {
|
|
17
|
+
return Object.is(a, b) || JSON.stringify(a) === JSON.stringify(b);
|
|
18
|
+
}
|
|
19
|
+
// Whether two rows carry the same columns and values.
|
|
16
20
|
function sameValues(a, b) {
|
|
17
21
|
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
18
22
|
for (const key of keys) {
|
|
19
|
-
if (!
|
|
23
|
+
if (!sameValue(a[key], b[key]))
|
|
20
24
|
return false;
|
|
21
|
-
}
|
|
22
25
|
}
|
|
23
26
|
return true;
|
|
24
27
|
}
|
|
28
|
+
// What a live MODIFY sends a client that holds `held`: each field whose value
|
|
29
|
+
// differs in `next`, a field that became null (or that `next` no longer
|
|
30
|
+
// carries, as a column now hidden from the user) as null — as GSF's
|
|
31
|
+
// diffToGenesisSet and the hidden-field handling of
|
|
32
|
+
// QueryUpdate.Modified.setModifyFromQueryRow write it (v8.15.29). Null and
|
|
33
|
+
// absent are the same value. In column order.
|
|
34
|
+
function changedFields(held, next) {
|
|
35
|
+
const changed = {};
|
|
36
|
+
for (const field of new Set([...Object.keys(held), ...Object.keys(next)])) {
|
|
37
|
+
const value = next[field] ?? null;
|
|
38
|
+
if (!sameValue(held[field] ?? null, value))
|
|
39
|
+
changed[field] = value;
|
|
40
|
+
}
|
|
41
|
+
return changed;
|
|
42
|
+
}
|
|
43
|
+
// The held row a moving view evicts: the oldest version the client holds, by
|
|
44
|
+
// TIMESTAMP and then RECORD_ID, as MovingViewClientIndex's timestampIndex
|
|
45
|
+
// orders them (v8.15.29) — not the row last in the snapshot order. Rows with
|
|
46
|
+
// no ROW_REF can't be named in a DELETE, so are never chosen.
|
|
47
|
+
function oldestHeld(held) {
|
|
48
|
+
let oldest;
|
|
49
|
+
for (const candidate of held) {
|
|
50
|
+
const [, entry] = candidate;
|
|
51
|
+
if (entry.rowRef === undefined || entry.rowRef === null)
|
|
52
|
+
continue;
|
|
53
|
+
if (!oldest ||
|
|
54
|
+
(compareRecordIds(entry.timestamp, oldest[1].timestamp) ||
|
|
55
|
+
compareRecordIds(entry.rowRef, oldest[1].rowRef)) < 0) {
|
|
56
|
+
oldest = candidate;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return oldest;
|
|
60
|
+
}
|
|
25
61
|
export function createMockServer(config) {
|
|
26
62
|
// Declared columns are held by type (db/values.ts), except under
|
|
27
63
|
// fidelity: 'legacy', which keeps rows as given.
|
|
@@ -41,7 +77,7 @@ export function createMockServer(config) {
|
|
|
41
77
|
// predicate would evaluate against missing fields and drop the push —
|
|
42
78
|
// leaving the client holding a ghost row. A DELETE therefore skips the
|
|
43
79
|
// predicate; which subscriptions it reaches is decided by what each client
|
|
44
|
-
// holds (
|
|
80
|
+
// holds (viewUpdate), or, in legacy mode, it reaches all of them.
|
|
45
81
|
// 'error' is kept distinct from 'no-match' because only a clean no-match may
|
|
46
82
|
// be turned into a DELETE below; a broken predicate must not evict rows.
|
|
47
83
|
function evaluateSubscription(subscription, sourceRef, row) {
|
|
@@ -74,55 +110,80 @@ export function createMockServer(config) {
|
|
|
74
110
|
return null;
|
|
75
111
|
return operation === 'MODIFY' ? 'DELETE' : null;
|
|
76
112
|
}
|
|
77
|
-
//
|
|
78
|
-
// what that client holds
|
|
79
|
-
//
|
|
80
|
-
//
|
|
81
|
-
//
|
|
82
|
-
//
|
|
83
|
-
//
|
|
84
|
-
//
|
|
85
|
-
//
|
|
86
|
-
//
|
|
87
|
-
//
|
|
113
|
+
// The rows of the QUERY_UPDATE one 'gsf' subscription's client gets for a
|
|
114
|
+
// live change, decided from what that client holds as the real
|
|
115
|
+
// dataserver's client index decides (ClientIndex and MovingViewClientIndex,
|
|
116
|
+
// v8.15.29); none to push nothing. Keeps the subscription's pending and
|
|
117
|
+
// held rows up to date. `operation` is the change after the query filter
|
|
118
|
+
// and CRITERIA_MATCH (a MODIFY that left them arrives as a DELETE); the
|
|
119
|
+
// user's rowFilter is applied here too, so leaving it counts as leaving the
|
|
120
|
+
// subscription.
|
|
121
|
+
// - A held row gets a MODIFY of what changed since the client last got it
|
|
122
|
+
// (liveModify 'full': the whole row), and nothing when nothing it can
|
|
123
|
+
// see changed; a DELETE when it is gone or out of scope. An INSERT of a
|
|
124
|
+
// row it holds is ignored.
|
|
88
125
|
// - A row the client doesn't hold that is (now) in scope is pushed as an
|
|
89
|
-
// INSERT and becomes held: a
|
|
90
|
-
// into the criteria
|
|
91
|
-
// snapshot's order doesn't matter
|
|
92
|
-
//
|
|
93
|
-
//
|
|
94
|
-
//
|
|
95
|
-
//
|
|
96
|
-
//
|
|
126
|
+
// INSERT and becomes held: a new row, one waiting for MORE_ROWS, one a
|
|
127
|
+
// MODIFY brought back into the criteria (processInsertUpdate, and
|
|
128
|
+
// putNewRowEntry for a MODIFY). The snapshot's order doesn't matter, and
|
|
129
|
+
// no later page repeats it. A full view takes it only when it moves:
|
|
130
|
+
// the held row with the oldest TIMESTAMP is evicted, as a DELETE after
|
|
131
|
+
// the INSERT in the same frame (nothing at all when that is the new row
|
|
132
|
+
// itself). Without a moving view a full view takes nothing
|
|
133
|
+
// (MaxViewReached), and the row waits for a page if it sorts after the
|
|
134
|
+
// read position (waitForPage) — as an evicted row does too.
|
|
135
|
+
// - Out of scope or deleted, a waiting row is dropped and a row the client
|
|
136
|
+
// never held gets nothing.
|
|
97
137
|
// A row without a ROW_REF can't be matched to anything the client holds,
|
|
98
138
|
// so changes to it aren't pushed.
|
|
99
|
-
|
|
100
|
-
function trackClientView(subscription, connection, query, operation, row, rowRef) {
|
|
139
|
+
function viewUpdate(subscription, connection, query, operation, row, rowRef) {
|
|
101
140
|
const key = pendingKey(rowRef);
|
|
102
|
-
const pending = subscription
|
|
103
|
-
const held = subscription.held;
|
|
141
|
+
const { pending, held } = subscription;
|
|
104
142
|
if (key === undefined || !pending || !held)
|
|
105
|
-
return
|
|
143
|
+
return [];
|
|
106
144
|
const permitted = operation === 'DELETE'
|
|
107
145
|
? undefined
|
|
108
146
|
: permittedQueryRow(row, query?.auth, requestingUser(connection), subscription.fieldNames);
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return null;
|
|
115
|
-
}
|
|
116
|
-
if (held.has(key)) {
|
|
117
|
-
if (permitted)
|
|
118
|
-
return 'MODIFY';
|
|
147
|
+
const current = held.get(key);
|
|
148
|
+
if (!permitted) {
|
|
149
|
+
pending.delete(key);
|
|
150
|
+
if (!current)
|
|
151
|
+
return [];
|
|
119
152
|
held.delete(key);
|
|
120
|
-
return 'DELETE';
|
|
153
|
+
return [stampRow({}, rowRef, 'DELETE')];
|
|
121
154
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
155
|
+
const entry = viewRow(wireRow(subscription, permitted, row), rowRef, row);
|
|
156
|
+
if (current) {
|
|
157
|
+
if (operation === 'INSERT')
|
|
158
|
+
return [];
|
|
159
|
+
held.set(key, entry);
|
|
160
|
+
const changed = changedFields(current.row, entry.row);
|
|
161
|
+
if (Object.keys(changed).length === 0)
|
|
162
|
+
return [];
|
|
163
|
+
return [stampRow(liveModifyMode(config) === 'full' ? entry.row : changed, rowRef, 'MODIFY')];
|
|
164
|
+
}
|
|
165
|
+
const waiting = (view) => ({
|
|
166
|
+
...view,
|
|
167
|
+
row: stampRow(view.row, view.rowRef, 'INSERT'),
|
|
168
|
+
});
|
|
169
|
+
const maxView = subscription.maxView ?? Infinity;
|
|
170
|
+
if (!subscription.movingView && held.size >= maxView) {
|
|
171
|
+
waitForPage(subscription, key, waiting(entry));
|
|
172
|
+
return [];
|
|
173
|
+
}
|
|
174
|
+
pending.delete(key);
|
|
175
|
+
held.set(key, entry);
|
|
176
|
+
const rows = [stampRow(entry.row, rowRef, 'INSERT')];
|
|
177
|
+
const evicted = held.size > maxView ? oldestHeld(held) : undefined;
|
|
178
|
+
if (evicted) {
|
|
179
|
+
const [evictedKey, evictedRow] = evicted;
|
|
180
|
+
held.delete(evictedKey);
|
|
181
|
+
waitForPage(subscription, evictedKey, waiting(evictedRow));
|
|
182
|
+
if (evictedKey === key)
|
|
183
|
+
return [];
|
|
184
|
+
rows.push(stampRow({}, evictedRow.rowRef, 'DELETE'));
|
|
185
|
+
}
|
|
186
|
+
return rows;
|
|
126
187
|
}
|
|
127
188
|
function pushChange(resourceName, operation, row) {
|
|
128
189
|
const query = config.queries?.[resourceName];
|
|
@@ -149,38 +210,42 @@ export function createMockServer(config) {
|
|
|
149
210
|
// ROW_REF is computed from the unstripped row, since pk fields may
|
|
150
211
|
// themselves be hidden from this user.
|
|
151
212
|
const rowRef = subscription.getRowRef(row);
|
|
152
|
-
const rowOperation = subscription.held
|
|
153
|
-
? trackClientView(subscription, connection, query, resolved, row, rowRef)
|
|
154
|
-
: resolved;
|
|
155
|
-
if (rowOperation === null)
|
|
156
|
-
continue;
|
|
157
213
|
const legacy = isLegacyFidelity(config);
|
|
158
|
-
let
|
|
159
|
-
if (
|
|
214
|
+
let rows;
|
|
215
|
+
if (subscription.held) {
|
|
216
|
+
rows = viewUpdate(subscription, connection, query, resolved, row, rowRef);
|
|
217
|
+
}
|
|
218
|
+
else if (resolved === 'DELETE') {
|
|
160
219
|
// GSF's DELETE row is DETAILS {OPERATION, ROW_REF} and nothing else.
|
|
161
220
|
// Legacy mode sent the pk fields: the stub it was given for a real
|
|
162
221
|
// DELETE, or one rebuilt from the key for a change that left the
|
|
163
222
|
// subscription (the full row may carry columns hideFields strips).
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
223
|
+
let outRow = {};
|
|
224
|
+
if (legacy) {
|
|
225
|
+
outRow =
|
|
226
|
+
operation !== 'DELETE' && query
|
|
227
|
+
? store.rowStubFromKey(query.source, rowRef)
|
|
228
|
+
: row;
|
|
168
229
|
}
|
|
230
|
+
rows = [stampRow(outRow, rowRef, resolved, legacy)];
|
|
169
231
|
}
|
|
170
232
|
else {
|
|
171
|
-
// Only the subscription's columns, minus the ones this user can't
|
|
233
|
+
// Only the subscription's columns, minus the ones this user can't
|
|
234
|
+
// see; always the whole row.
|
|
172
235
|
const permitted = permittedQueryRow(row, query?.auth, requestingUser(connection), subscription.fieldNames);
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
236
|
+
rows = permitted
|
|
237
|
+
? [stampRow(wireRow(subscription, permitted, row), rowRef, resolved, legacy)]
|
|
238
|
+
: [];
|
|
176
239
|
}
|
|
240
|
+
if (rows.length === 0)
|
|
241
|
+
continue;
|
|
177
242
|
subscription.sequenceId += 1;
|
|
178
243
|
// A live push carries neither MORE_ROWS nor ROWS_COUNT on the real
|
|
179
244
|
// server — those describe the snapshot. Legacy mode sent ROWS_COUNT: 1.
|
|
180
245
|
connection.send({
|
|
181
246
|
MESSAGE_TYPE: 'QUERY_UPDATE',
|
|
182
247
|
SOURCE_REF: sourceRef,
|
|
183
|
-
ROW:
|
|
248
|
+
ROW: rows,
|
|
184
249
|
...(legacy ? { ROWS_COUNT: 1 } : {}),
|
|
185
250
|
SEQUENCE_ID: subscription.sequenceId,
|
|
186
251
|
});
|
package/dist/types.d.ts
CHANGED
|
@@ -185,8 +185,10 @@ export interface OnMessageCtx {
|
|
|
185
185
|
}
|
|
186
186
|
export type OnMessageHook = (message: GenesisMessage, ctx: OnMessageCtx) => GenesisMessage | false | void;
|
|
187
187
|
export type FidelityMode = 'gsf' | 'legacy';
|
|
188
|
+
export type LiveModifyMode = 'diff' | 'full';
|
|
188
189
|
export interface MockServerConfig {
|
|
189
190
|
fidelity?: FidelityMode;
|
|
191
|
+
liveModify?: LiveModifyMode;
|
|
190
192
|
tables?: Record<string, TableDef>;
|
|
191
193
|
views?: Record<string, ViewDef>;
|
|
192
194
|
queries?: Record<string, QueryDef>;
|
package/package.json
CHANGED
package/src/db/criteria.ts
CHANGED
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
import type { Row } from '../types.ts';
|
|
38
38
|
import {
|
|
39
39
|
CriteriaSyntaxError,
|
|
40
|
+
decodeStringLiteral,
|
|
40
41
|
tokenizeCriteria,
|
|
41
42
|
UnsupportedCriteriaError,
|
|
42
43
|
} from './criteriaTokens.ts';
|
|
@@ -150,7 +151,7 @@ const NULL_POINTER = 'java.lang.NullPointerException';
|
|
|
150
151
|
|
|
151
152
|
function parseLiteral(token: string): { value: unknown; type: JavaType } | undefined {
|
|
152
153
|
if (/^['"]/.test(token)) {
|
|
153
|
-
return { value: token
|
|
154
|
+
return { value: decodeStringLiteral(token), type: 'String' };
|
|
154
155
|
}
|
|
155
156
|
if (/^-?\d+$/.test(token)) {
|
|
156
157
|
const value = Number(token);
|
package/src/db/criteriaTokens.ts
CHANGED
|
@@ -45,6 +45,22 @@ export function tokenizeCriteria(input: string): CriteriaToken[] {
|
|
|
45
45
|
return tokens;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
// Groovy's character escapes; any other escaped character stands for itself
|
|
49
|
+
// (\\, \', \", \$).
|
|
50
|
+
const ESCAPES: Record<string, string> = { b: '\b', t: '\t', n: '\n', f: '\f', r: '\r' };
|
|
51
|
+
|
|
52
|
+
const HEX = 16;
|
|
53
|
+
|
|
54
|
+
// The value of a quoted string token: its quotes off and its escapes decoded
|
|
55
|
+
// as Groovy decodes them, so '\n' is a line break and '\u0041' is A.
|
|
56
|
+
export function decodeStringLiteral(token: string): string {
|
|
57
|
+
return token
|
|
58
|
+
.slice(1, -1)
|
|
59
|
+
.replace(/\\(?:u([0-9a-fA-F]{4})|([\s\S]))/g, (_, unicode: string | undefined, char: string) =>
|
|
60
|
+
unicode ? String.fromCharCode(parseInt(unicode, HEX)) : (ESCAPES[char] ?? char),
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
48
64
|
const MS_DIGITS = 3; // '.5' means 500ms, not 5ms — right-pad to full precision
|
|
49
65
|
|
|
50
66
|
// Genesis date/datetime values serialize as 'YYYYMMDD-HH:MM[:SS[.mmm]]'. Row
|
package/src/db/legacyCriteria.ts
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
import type { Row } from '../types.ts';
|
|
15
15
|
import {
|
|
16
16
|
CriteriaSyntaxError,
|
|
17
|
+
decodeStringLiteral,
|
|
17
18
|
parseGenesisDateTime,
|
|
18
19
|
tokenizeCriteria,
|
|
19
20
|
UnsupportedCriteriaError,
|
|
@@ -23,7 +24,7 @@ import * as gsf from './gsfExpr.ts';
|
|
|
23
24
|
type Predicate = (record: Row) => boolean;
|
|
24
25
|
|
|
25
26
|
function parseLiteral(token: string): unknown {
|
|
26
|
-
if (/^['"]/.test(token)) return token
|
|
27
|
+
if (/^['"]/.test(token)) return decodeStringLiteral(token);
|
|
27
28
|
if (/^-?\d+(?:\.\d+)?$/.test(token)) return Number(token);
|
|
28
29
|
if (token === 'true') return true;
|
|
29
30
|
if (token === 'false') return false;
|
|
@@ -10,7 +10,7 @@ import { byRecordId, rowComparator } from '../db/ordering.ts';
|
|
|
10
10
|
import { isDeclared, queryCriteriaTypes, queryFieldNames, querySchema } from '../db/schema.ts';
|
|
11
11
|
import type { Store } from '../db/store.ts';
|
|
12
12
|
import { bigDecimalFields, encodeRow } from '../db/values.ts';
|
|
13
|
-
import type { Connection, Subscription } from '../protocol/connection.ts';
|
|
13
|
+
import type { Connection, Subscription, ViewRow } from '../protocol/connection.ts';
|
|
14
14
|
import { isLegacyFidelity } from '../protocol/fidelity.ts';
|
|
15
15
|
import { MESSAGE_TYPE, ERROR_CODE } from '../protocol/messageTypes.ts';
|
|
16
16
|
import {
|
|
@@ -35,11 +35,21 @@ function readCount(value: unknown, fallback: number): number {
|
|
|
35
35
|
|
|
36
36
|
// MAX_ROWS is the page size, MAX_VIEW the most rows the client's view may
|
|
37
37
|
// hold across all pages. 0 means "no limit" for either, and MAX_ROWS is
|
|
38
|
-
// clamped to MAX_VIEW
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
// clamped to MAX_VIEW. MOVING_VIEW defaults to true. A logon with a
|
|
39
|
+
// VIEW_NUMBER other than 0 asks for numbered pages: its view is one page,
|
|
40
|
+
// with no moving view. All of it as ClientOptions.parse does (v8.15.29).
|
|
41
|
+
export function pagingOptions(details: Row): {
|
|
42
|
+
maxRows: number;
|
|
43
|
+
maxView: number;
|
|
44
|
+
movingView: boolean;
|
|
45
|
+
} {
|
|
46
|
+
const pageNumbered = readCount(details.VIEW_NUMBER, 0) !== 0;
|
|
47
|
+
const requestedRows = readCount(details.MAX_ROWS, DEFAULT_MAX_ROWS);
|
|
48
|
+
const maxView =
|
|
49
|
+
(pageNumbered ? requestedRows : readCount(details.MAX_VIEW, DEFAULT_MAX_VIEW)) || Infinity;
|
|
50
|
+
const maxRows = requestedRows || Infinity;
|
|
51
|
+
const movingView = !pageNumbered && readFlag(details.MOVING_VIEW, true);
|
|
52
|
+
return { maxRows: Math.min(maxRows, maxView), maxView, movingView };
|
|
43
53
|
}
|
|
44
54
|
|
|
45
55
|
// The key a row is tracked under in Subscription.pending / held — undefined
|
|
@@ -49,23 +59,56 @@ export function pendingKey(rowRef: unknown): string | undefined {
|
|
|
49
59
|
return rowRef === undefined || rowRef === null ? undefined : String(rowRef);
|
|
50
60
|
}
|
|
51
61
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
62
|
+
// A row of the view, built from its stored (or view) row. `row` is its wire
|
|
63
|
+
// form: stamped as an INSERT while it waits, plain once held.
|
|
64
|
+
export function viewRow(row: Row, rowRef: unknown, source: Row): ViewRow {
|
|
65
|
+
return { row, rowRef, source, timestamp: source.TIMESTAMP ?? source.RECORD_ID };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// What the client holds once a waiting row is sent: its values, no DETAILS.
|
|
69
|
+
function heldRow(waiting: ViewRow): ViewRow {
|
|
70
|
+
const { DETAILS: _details, ...row } = waiting.row;
|
|
71
|
+
return { ...waiting, row };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function buildPending(rows: ViewRow[]): Map<string, ViewRow> {
|
|
75
|
+
const pending = new Map<string, ViewRow>();
|
|
76
|
+
rows.forEach((entry, index) => {
|
|
77
|
+
const key = pendingKey(entry.rowRef);
|
|
56
78
|
// A row with no ROW_REF (or a duplicate one) still has to be delivered;
|
|
57
79
|
// park it under a key no ROW_REF can produce rather than dropping it.
|
|
58
80
|
// Live changes can't reach it there, which is as much as the client
|
|
59
81
|
// could make of them: to it, rows sharing a ROW_REF are one row.
|
|
60
|
-
pending.set(key === undefined || pending.has(key) ? `\u0000${index}` : key,
|
|
82
|
+
pending.set(key === undefined || pending.has(key) ? `\u0000${index}` : key, entry);
|
|
61
83
|
});
|
|
62
84
|
return pending;
|
|
63
85
|
}
|
|
64
86
|
|
|
87
|
+
// A matched row the client doesn't hold and isn't sent now. It waits for a
|
|
88
|
+
// page when it sorts after the last row a page delivered, since the real
|
|
89
|
+
// NextRowsStrategy reads on from there; one that sorts before it is never
|
|
90
|
+
// read again. Pending stays in delivery order.
|
|
91
|
+
export function waitForPage(subscription: Subscription, key: string, entry: ViewRow): void {
|
|
92
|
+
const { pending, order, cursor } = subscription;
|
|
93
|
+
if (!pending) return;
|
|
94
|
+
pending.delete(key);
|
|
95
|
+
if (!order) {
|
|
96
|
+
pending.set(key, entry);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (cursor && order(entry.source, cursor) <= 0) return;
|
|
100
|
+
const waiting = [...pending];
|
|
101
|
+
const at = waiting.findIndex(([, other]) => order(entry.source, other.source) < 0);
|
|
102
|
+
waiting.splice(at === -1 ? waiting.length : at, 0, [key, entry]);
|
|
103
|
+
pending.clear();
|
|
104
|
+
for (const [waitingKey, other] of waiting) pending.set(waitingKey, other);
|
|
105
|
+
}
|
|
106
|
+
|
|
65
107
|
// The next page: up to MAX_ROWS waiting rows, but never more than the view
|
|
66
108
|
// has room for. The real NextRowsStrategy reads while the client index is
|
|
67
109
|
// below MAX_VIEW, so a full view gets an empty page, and a slot freed by a
|
|
68
|
-
// DELETE is filled by the next row waiting. Delivered rows move to `held
|
|
110
|
+
// DELETE is filled by the next row waiting. Delivered rows move to `held`,
|
|
111
|
+
// and the last one is the new read position.
|
|
69
112
|
function takePage(subscription: Subscription): Row[] {
|
|
70
113
|
const { pending, held } = subscription;
|
|
71
114
|
if (!pending) return [];
|
|
@@ -77,11 +120,12 @@ function takePage(subscription: Subscription): Row[] {
|
|
|
77
120
|
),
|
|
78
121
|
);
|
|
79
122
|
const page: Row[] = [];
|
|
80
|
-
for (const [key,
|
|
123
|
+
for (const [key, entry] of pending) {
|
|
81
124
|
if (page.length >= room) break;
|
|
82
|
-
page.push(row);
|
|
125
|
+
page.push(entry.row);
|
|
83
126
|
pending.delete(key);
|
|
84
|
-
held?.
|
|
127
|
+
held?.set(key, heldRow(entry));
|
|
128
|
+
subscription.cursor = entry.source;
|
|
85
129
|
}
|
|
86
130
|
return page;
|
|
87
131
|
}
|
|
@@ -89,7 +133,10 @@ function takePage(subscription: Subscription): Row[] {
|
|
|
89
133
|
// The dataserver's own refusal of a logon (genesis-pal-dataserver): a
|
|
90
134
|
// LOGON_NACK with a StandardError, sent instead of LOGON_ACK. No subscription
|
|
91
135
|
// is opened, so a later DATA_LOGOFF on it gets the router's 404.
|
|
92
|
-
function logonNack(
|
|
136
|
+
function logonNack(
|
|
137
|
+
code: 'INVALID_CRITERIA' | 'INVALID_INDEX' | 'INVALID_PARAMETER',
|
|
138
|
+
text: string,
|
|
139
|
+
): Row {
|
|
93
140
|
return {
|
|
94
141
|
MESSAGE_TYPE: MESSAGE_TYPE.LOGON_NACK,
|
|
95
142
|
ERROR: [{ '@type': 'StandardError', CODE: code, TEXT: text, STATUS_CODE: '400 Bad Request' }],
|
|
@@ -156,6 +203,12 @@ function readBoolean(value: unknown): boolean {
|
|
|
156
203
|
return value === true || (value !== undefined && value !== null && /^true$/i.test(String(value)));
|
|
157
204
|
}
|
|
158
205
|
|
|
206
|
+
// A flag with a default, as GenesisSet.getBoolean(key, default) reads one:
|
|
207
|
+
// the default when it is absent, otherwise as readBoolean.
|
|
208
|
+
function readFlag(value: unknown, fallback: boolean): boolean {
|
|
209
|
+
return value === undefined || value === null ? fallback : readBoolean(value);
|
|
210
|
+
}
|
|
211
|
+
|
|
159
212
|
// How a query's rows go on the wire: each BIGDECIMAL column as a string with
|
|
160
213
|
// its scale, as GSF's serializer writes a BigDecimal (db/values.ts).
|
|
161
214
|
// undefined when the query has none — known without resolving the schema
|
|
@@ -231,7 +284,18 @@ export function handleDataLogon(
|
|
|
231
284
|
const criteriaOptions: CriteriaOptions = legacy
|
|
232
285
|
? { lenient: true }
|
|
233
286
|
: { types: queryCriteriaTypes(query, config, store) };
|
|
287
|
+
const paging = pagingOptions(details);
|
|
234
288
|
if (!legacy) {
|
|
289
|
+
// A moving view needs a MAX_VIEW to move in, and MOVING_VIEW defaults to
|
|
290
|
+
// true, so MAX_VIEW: 0 alone is refused. Checked before ORDER_BY
|
|
291
|
+
// (ConnectionManager.verifyClientOptions, v8.15.29).
|
|
292
|
+
if (paging.movingView && paging.maxView === Infinity) {
|
|
293
|
+
connection.reply(
|
|
294
|
+
message,
|
|
295
|
+
logonNack('INVALID_PARAMETER', "MOVING_VIEW can't be true if maxView is set to 0"),
|
|
296
|
+
);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
235
299
|
// ORDER_BY names one of the query's own indexes (QueryDef.indexes). GSF
|
|
236
300
|
// checks it before it looks at the criteria
|
|
237
301
|
// (ConnectionManager.verifyClientOptions, v8.15.29).
|
|
@@ -322,13 +386,15 @@ export function handleDataLogon(
|
|
|
322
386
|
: requestedFieldNames(details, query, exposed, config, store);
|
|
323
387
|
// A BigDecimal goes out as a string with its column's scale (db/values.ts).
|
|
324
388
|
const encode = legacy ? undefined : rowEncoder(query, config, store);
|
|
325
|
-
const
|
|
389
|
+
const snapshotRows: ViewRow[] = [];
|
|
326
390
|
for (const row of rows) {
|
|
327
391
|
const permitted = permittedQueryRow(row, query.auth, user, fieldNames);
|
|
328
392
|
if (!permitted) continue;
|
|
329
393
|
const rowRef = getRowRef(row);
|
|
330
394
|
const sent = dataserverRow(permitted, row, query.fields);
|
|
331
|
-
|
|
395
|
+
snapshotRows.push(
|
|
396
|
+
viewRow(stampRow(encode ? encode(sent) : sent, rowRef, 'INSERT', legacy), rowRef, row),
|
|
397
|
+
);
|
|
332
398
|
}
|
|
333
399
|
|
|
334
400
|
const subscription: Subscription = {
|
|
@@ -343,8 +409,8 @@ export function handleDataLogon(
|
|
|
343
409
|
connection.subscribe(sourceRef, subscription);
|
|
344
410
|
connection.reply(message, {
|
|
345
411
|
MESSAGE_TYPE: MESSAGE_TYPE.QUERY_UPDATE,
|
|
346
|
-
ROW:
|
|
347
|
-
ROWS_COUNT:
|
|
412
|
+
ROW: snapshotRows.map((entry) => entry.row),
|
|
413
|
+
ROWS_COUNT: snapshotRows.length,
|
|
348
414
|
SEQUENCE_ID: 1,
|
|
349
415
|
MORE_ROWS: false,
|
|
350
416
|
});
|
|
@@ -357,16 +423,16 @@ export function handleDataLogon(
|
|
|
357
423
|
// which then answers every MORE_ROWS with an empty {MORE_ROWS: true} page.
|
|
358
424
|
// A live INSERT is pushed straight away whatever the order, even when it
|
|
359
425
|
// sorts among the rows still waiting, and a later page doesn't repeat it:
|
|
360
|
-
// the real ClientIndex.processInsertUpdate sends it as soon as it arrives
|
|
361
|
-
//
|
|
362
|
-
// v8.15.29). See server.ts
|
|
363
|
-
|
|
426
|
+
// the real ClientIndex.processInsertUpdate sends it as soon as it arrives
|
|
427
|
+
// (a full view takes it only when it moves), and NextRowsStrategy skips
|
|
428
|
+
// rows the client index already holds (GSF v8.15.29). See server.ts
|
|
429
|
+
// viewUpdate.
|
|
364
430
|
const tracked: Subscription = {
|
|
365
431
|
...subscription,
|
|
366
|
-
pending: buildPending(
|
|
367
|
-
held: new
|
|
368
|
-
|
|
369
|
-
|
|
432
|
+
pending: buildPending(snapshotRows),
|
|
433
|
+
held: new Map<string, ViewRow>(),
|
|
434
|
+
...paging,
|
|
435
|
+
...(compare ? { order: compare } : {}),
|
|
370
436
|
fieldNames,
|
|
371
437
|
};
|
|
372
438
|
const firstPage = takePage(tracked);
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,23 @@ import type { GenesisMessage, Row, SocketLike } from '../types.ts';
|
|
|
4
4
|
|
|
5
5
|
const WS_OPEN = 1;
|
|
6
6
|
|
|
7
|
+
// One row of a subscription's view (see Subscription.pending / held).
|
|
8
|
+
export interface ViewRow {
|
|
9
|
+
// pending: the row as a page will send it, stamped as an INSERT. held: the
|
|
10
|
+
// values the client holds, without DETAILS — what a MODIFY is diffed
|
|
11
|
+
// against.
|
|
12
|
+
row: Row;
|
|
13
|
+
// The DETAILS.ROW_REF it is sent under.
|
|
14
|
+
rowRef: unknown;
|
|
15
|
+
// The stored (or view) row it was built from: what the snapshot order
|
|
16
|
+
// sorts and what a waiting row is placed by.
|
|
17
|
+
source: Row;
|
|
18
|
+
// The TIMESTAMP of the version the client holds (the RECORD_ID when the
|
|
19
|
+
// row has none). A moving view evicts the held row with the oldest one, as
|
|
20
|
+
// MovingViewClientIndex's timestampIndex does.
|
|
21
|
+
timestamp: unknown;
|
|
22
|
+
}
|
|
23
|
+
|
|
7
24
|
export interface Subscription {
|
|
8
25
|
resourceName: string;
|
|
9
26
|
// Computes the DETAILS.ROW_REF value for a pushed row (its RECORD_ID as a
|
|
@@ -18,16 +35,25 @@ export interface Subscription {
|
|
|
18
35
|
// on 'gsf'-fidelity subscriptions; legacy ones have none). Every row is in
|
|
19
36
|
// at most one of:
|
|
20
37
|
// - pending: matched but not sent yet — past this page, or past MAX_VIEW —
|
|
21
|
-
// in delivery order, stamped and permission-stripped
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
// - held: sent to the client (on a page, or pushed as an INSERT)
|
|
25
|
-
// these get MODIFY and DELETE pushes.
|
|
38
|
+
// in delivery order, stamped and permission-stripped: the rows the real
|
|
39
|
+
// dataserver's next read would find past its read position. MORE_ROWS
|
|
40
|
+
// drains it, maxRows at a time, while the view has room.
|
|
41
|
+
// - held: sent to the client (on a page, or pushed as an INSERT), with the
|
|
42
|
+
// values it was last sent. Only these get MODIFY and DELETE pushes.
|
|
26
43
|
// Both are keyed by String(ROW_REF) (see dataLogon.ts pendingKey).
|
|
27
|
-
pending?: Map<string,
|
|
28
|
-
held?:
|
|
44
|
+
pending?: Map<string, ViewRow>;
|
|
45
|
+
held?: Map<string, ViewRow>;
|
|
29
46
|
maxRows?: number;
|
|
30
47
|
maxView?: number;
|
|
48
|
+
// DETAILS.MOVING_VIEW (GSF's default: true). A new row for a full view is
|
|
49
|
+
// still pushed, with a DELETE of the oldest held row in the same frame;
|
|
50
|
+
// without it, a full view takes no new rows.
|
|
51
|
+
movingView?: boolean;
|
|
52
|
+
// The snapshot order, over source rows, and the source of the last row a
|
|
53
|
+
// page delivered: the real dataserver's read position. A row the client
|
|
54
|
+
// doesn't hold waits for a page only when it sorts after it.
|
|
55
|
+
order?: (a: Row, b: Row) => number;
|
|
56
|
+
cursor?: Row;
|
|
31
57
|
// The columns this subscription's rows carry (the query's fields block or
|
|
32
58
|
// declared columns, narrowed by DETAILS.FIELDS); undefined: rows as stored.
|
|
33
59
|
fieldNames?: string[];
|
package/src/protocol/fidelity.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MockServerConfig } from '../types.ts';
|
|
1
|
+
import type { LiveModifyMode, MockServerConfig } from '../types.ts';
|
|
2
2
|
|
|
3
3
|
// True when the config opts back into the envelopes this engine sent up to
|
|
4
4
|
// 15.47 (see FidelityMode in types.ts). Read per message, like every other
|
|
@@ -6,3 +6,9 @@ import type { MockServerConfig } from '../types.ts';
|
|
|
6
6
|
export function isLegacyFidelity(config: MockServerConfig): boolean {
|
|
7
7
|
return config.fidelity === 'legacy';
|
|
8
8
|
}
|
|
9
|
+
|
|
10
|
+
// How a live MODIFY's row is written (see LiveModifyMode in types.ts): as
|
|
11
|
+
// GSF's diff unless the config asks for whole rows.
|
|
12
|
+
export function liveModifyMode(config: MockServerConfig): LiveModifyMode {
|
|
13
|
+
return config.liveModify ?? 'diff';
|
|
14
|
+
}
|