@stonyx/orm 0.3.2-beta.154 → 0.3.2-beta.156
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 +301 -85
- package/dist/manage-record.js +234 -9
- package/dist/orm-request.d.ts +60 -25
- package/dist/orm-request.js +118 -30
- package/dist/standalone-db.js +17 -5
- package/dist/utils.d.ts +44 -0
- package/dist/utils.js +47 -0
- package/package.json +1 -1
- package/src/manage-record.ts +253 -9
- package/src/orm-request.ts +120 -30
- package/src/standalone-db.ts +17 -6
- package/src/utils.ts +50 -0
package/dist/utils.d.ts
CHANGED
|
@@ -5,3 +5,47 @@ export declare function isDbError(error: unknown): error is {
|
|
|
5
5
|
};
|
|
6
6
|
export declare function isOrmRecord(value: unknown): value is OrmRecord;
|
|
7
7
|
export declare function pluralize(word: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* The highest NUMERIC id held by a set of records, or `0` when there is none.
|
|
10
|
+
*
|
|
11
|
+
* ONE COPY, and the duplication it replaces is the reason it lives here. Three
|
|
12
|
+
* near-identical reduces existed at once: `assignRecordId` (server-assigned id
|
|
13
|
+
* selection), `StandaloneDB.create` (src/standalone-db.ts) and the #203 test
|
|
14
|
+
* helper. `docs/improvements.md`'s standing WET Code category prescribes
|
|
15
|
+
* exactly this remedy -- extract into the module that already acts as the
|
|
16
|
+
* shared utility -- and `assignRecordId` already imported `isOrmRecord` from
|
|
17
|
+
* here.
|
|
18
|
+
*
|
|
19
|
+
* NON-NUMBERS ARE SKIPPED RATHER THAN COERCED TO `0`, AND THAT IS STYLISTIC.
|
|
20
|
+
* `StandaloneDB`'s shape mapped them to `0`, which can never beat a seed of
|
|
21
|
+
* `0`. Measured over eleven input classes (`[]`, `1`, `NaN`, `'5'`, `'abc'`,
|
|
22
|
+
* `-3`, `0`, `null`, `undefined`, `Infinity`, and mixed arrays) the two shapes
|
|
23
|
+
* produce IDENTICAL output on every one. In particular `typeof NaN` is
|
|
24
|
+
* `'number'`, so NEITHER shape coerces `NaN` -- both reject it on `NaN > max`,
|
|
25
|
+
* which is `false`. An earlier revision of this code asserted that the skip was
|
|
26
|
+
* what made the `NaN` case work; it is not, the comparison is, and that claim
|
|
27
|
+
* has been removed rather than left standing.
|
|
28
|
+
*
|
|
29
|
+
* WHAT IS LOAD-BEARING is that this is not `Math.max(...ids)`. `Math.max`
|
|
30
|
+
* returns `NaN` if any operand is `NaN`, and a record CAN be held under the key
|
|
31
|
+
* `NaN` -- so the obvious fix assigns `NaN`, lands on that slot and overwrites
|
|
32
|
+
* it, which is abofs/stonyx-orm#203 in a new disguise. Pinned by
|
|
33
|
+
* test/unit/assign-record-id-test.ts AC2; before that file existed the whole
|
|
34
|
+
* suite scored 951/0 under exactly that fix.
|
|
35
|
+
*/
|
|
36
|
+
export declare function maxNumericId(records: {
|
|
37
|
+
id?: unknown;
|
|
38
|
+
}[]): number;
|
|
39
|
+
/**
|
|
40
|
+
* The message prefix `assignRecordId` throws with when no free id can be
|
|
41
|
+
* derived for a model, and the ONE string `createHandler` matches on to answer
|
|
42
|
+
* `409` instead of letting the rejection reach express's default handler.
|
|
43
|
+
*
|
|
44
|
+
* It lives here rather than in either file because both need it and neither
|
|
45
|
+
* should own a copy: a literal in two places is how the two id coercions in
|
|
46
|
+
* orm-request.ts drifted apart (see `coerceId`). The repo has no error codes
|
|
47
|
+
* and no custom error classes -- 24 bare `throw new Error` sites across `src/`
|
|
48
|
+
* -- so a shared prefix is the narrowest way to make ONE failure distinguishable
|
|
49
|
+
* without inventing an error taxonomy this codebase does not use.
|
|
50
|
+
*/
|
|
51
|
+
export declare const NO_FREE_ID_ERROR = "Cannot assign record ID: no free id available";
|
package/dist/utils.js
CHANGED
|
@@ -15,3 +15,50 @@ export function pluralize(word) {
|
|
|
15
15
|
}
|
|
16
16
|
return basePluralize(word);
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* The highest NUMERIC id held by a set of records, or `0` when there is none.
|
|
20
|
+
*
|
|
21
|
+
* ONE COPY, and the duplication it replaces is the reason it lives here. Three
|
|
22
|
+
* near-identical reduces existed at once: `assignRecordId` (server-assigned id
|
|
23
|
+
* selection), `StandaloneDB.create` (src/standalone-db.ts) and the #203 test
|
|
24
|
+
* helper. `docs/improvements.md`'s standing WET Code category prescribes
|
|
25
|
+
* exactly this remedy -- extract into the module that already acts as the
|
|
26
|
+
* shared utility -- and `assignRecordId` already imported `isOrmRecord` from
|
|
27
|
+
* here.
|
|
28
|
+
*
|
|
29
|
+
* NON-NUMBERS ARE SKIPPED RATHER THAN COERCED TO `0`, AND THAT IS STYLISTIC.
|
|
30
|
+
* `StandaloneDB`'s shape mapped them to `0`, which can never beat a seed of
|
|
31
|
+
* `0`. Measured over eleven input classes (`[]`, `1`, `NaN`, `'5'`, `'abc'`,
|
|
32
|
+
* `-3`, `0`, `null`, `undefined`, `Infinity`, and mixed arrays) the two shapes
|
|
33
|
+
* produce IDENTICAL output on every one. In particular `typeof NaN` is
|
|
34
|
+
* `'number'`, so NEITHER shape coerces `NaN` -- both reject it on `NaN > max`,
|
|
35
|
+
* which is `false`. An earlier revision of this code asserted that the skip was
|
|
36
|
+
* what made the `NaN` case work; it is not, the comparison is, and that claim
|
|
37
|
+
* has been removed rather than left standing.
|
|
38
|
+
*
|
|
39
|
+
* WHAT IS LOAD-BEARING is that this is not `Math.max(...ids)`. `Math.max`
|
|
40
|
+
* returns `NaN` if any operand is `NaN`, and a record CAN be held under the key
|
|
41
|
+
* `NaN` -- so the obvious fix assigns `NaN`, lands on that slot and overwrites
|
|
42
|
+
* it, which is abofs/stonyx-orm#203 in a new disguise. Pinned by
|
|
43
|
+
* test/unit/assign-record-id-test.ts AC2; before that file existed the whole
|
|
44
|
+
* suite scored 951/0 under exactly that fix.
|
|
45
|
+
*/
|
|
46
|
+
export function maxNumericId(records) {
|
|
47
|
+
return records.reduce((max, record) => {
|
|
48
|
+
const { id } = record;
|
|
49
|
+
return typeof id === 'number' && id > max ? id : max;
|
|
50
|
+
}, 0);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The message prefix `assignRecordId` throws with when no free id can be
|
|
54
|
+
* derived for a model, and the ONE string `createHandler` matches on to answer
|
|
55
|
+
* `409` instead of letting the rejection reach express's default handler.
|
|
56
|
+
*
|
|
57
|
+
* It lives here rather than in either file because both need it and neither
|
|
58
|
+
* should own a copy: a literal in two places is how the two id coercions in
|
|
59
|
+
* orm-request.ts drifted apart (see `coerceId`). The repo has no error codes
|
|
60
|
+
* and no custom error classes -- 24 bare `throw new Error` sites across `src/`
|
|
61
|
+
* -- so a shared prefix is the narrowest way to make ONE failure distinguishable
|
|
62
|
+
* without inventing an error taxonomy this codebase does not use.
|
|
63
|
+
*/
|
|
64
|
+
export const NO_FREE_ID_ERROR = 'Cannot assign record ID: no free id available';
|
package/package.json
CHANGED
package/src/manage-record.ts
CHANGED
|
@@ -2,7 +2,7 @@ import Orm, { store } from '@stonyx/orm';
|
|
|
2
2
|
import OrmRecord from './record.js';
|
|
3
3
|
import { getGlobalRegistry, getPendingRegistry, getPendingBelongsToRegistry, getBelongsToRegistry, getHasManyRegistry } from './relationships.js';
|
|
4
4
|
import type Serializer from './serializer.js';
|
|
5
|
-
import { isOrmRecord } from './utils.js';
|
|
5
|
+
import { isOrmRecord, maxNumericId, NO_FREE_ID_ERROR } from './utils.js';
|
|
6
6
|
|
|
7
7
|
interface CreateRecordOptions {
|
|
8
8
|
isDbRecord?: boolean;
|
|
@@ -195,17 +195,58 @@ export function updateRecord(record: OrmRecord, rawData: unknown, userOptions: C
|
|
|
195
195
|
}
|
|
196
196
|
|
|
197
197
|
/**
|
|
198
|
-
* gets the next available id based on
|
|
198
|
+
* gets the next available id, based on the HIGHEST id present.
|
|
199
199
|
*
|
|
200
200
|
* In MySQL mode with numeric IDs, assigns a temporary pending ID.
|
|
201
201
|
* MySQL's AUTO_INCREMENT provides the real ID after INSERT.
|
|
202
|
+
*
|
|
203
|
+
* ---------------------------------------------------------------------------
|
|
204
|
+
* WAS: `Array.from(storeMap.values()).at(-1).id + 1` — the LAST INSERTED id,
|
|
205
|
+
* not the maximum (abofs/stonyx-orm#203). The store is a Map, so insertion
|
|
206
|
+
* order stops being ascending the moment a record is deleted and recreated, a
|
|
207
|
+
* db.json is written out of order, a directory-mode store is read back in file
|
|
208
|
+
* order, or a caller POSTs a high id and then a low one. After that, every
|
|
209
|
+
* server-assigned id is one that is ALREADY TAKEN — and `createRecord`'s
|
|
210
|
+
* last-entry-wins branch then overwrites that record IN PLACE and answers 200.
|
|
211
|
+
* No error, no 409, and the store's size does not change. That is the whole
|
|
212
|
+
* defect, and it is reachable from a create with NO id at all, which is the
|
|
213
|
+
* most ordinary write a consumer performs.
|
|
214
|
+
*
|
|
215
|
+
* Covered by test/unit/assign-record-id-test.ts. Note for anyone changing this
|
|
216
|
+
* function: before that file existed, the whole suite scored 951/0 both on the
|
|
217
|
+
* defect and on a naive `Math.max` fix that introduced a second one. A green
|
|
218
|
+
* suite is not evidence here; those assertions are.
|
|
219
|
+
* ---------------------------------------------------------------------------
|
|
202
220
|
*/
|
|
203
221
|
function assignRecordId(modelName: string, rawData: { [key: string]: unknown }): void {
|
|
204
|
-
|
|
222
|
+
// PRESENCE, not truthiness. `0` is a legal value for an `attr('number')` id,
|
|
223
|
+
// and `if (rawData.id) return` silently reassigned it, handing the caller back
|
|
224
|
+
// a different record than the one it named (#203).
|
|
225
|
+
//
|
|
226
|
+
// `''` is deliberately NOT honoured here and this is not an oversight: it is
|
|
227
|
+
// the one string that means "no id". `parseInt('')` is `NaN`, a record CAN be
|
|
228
|
+
// held under the key `NaN`, and orm-request.ts's body-id normalisation relies
|
|
229
|
+
// on `''` staying absent — otherwise `POST {"id":""}` answers 409 against a
|
|
230
|
+
// record it never named.
|
|
231
|
+
//
|
|
232
|
+
// WHAT WIDENING THIS TO `!== undefined` ACTUALLY DOES, measured rather than
|
|
233
|
+
// asserted: `{id: ''}` early-returns, `parseInt('')` NaNs it, and the record
|
|
234
|
+
// lands on the store's `NaN` slot and OVERWRITES whatever is there — #203's
|
|
235
|
+
// own defect class. It does NOT turn access-filter-enforcement-test.ts
|
|
236
|
+
// assertion 44 red; an earlier revision of this comment claimed it did, which
|
|
237
|
+
// converted an unknown into a false assurance. AC6's BOUNDARY assertions are
|
|
238
|
+
// what catch it, and they only do so because they seed the `NaN` slot first.
|
|
239
|
+
if (rawData.id || rawData.id === 0) return;
|
|
205
240
|
|
|
206
241
|
// In SQL mode with numeric IDs, defer to database auto-increment.
|
|
207
242
|
// Use unique negative integers — they survive the number transform (parseInt preserves negatives)
|
|
208
243
|
// and avoid NaN store-key collisions that string pending IDs caused.
|
|
244
|
+
//
|
|
245
|
+
// This early return is ABOVE the max computation on purpose: a pending
|
|
246
|
+
// negative must never be a candidate for, or be perturbed by, the max path.
|
|
247
|
+
// Pinned directly (AC5.3) rather than by asserting the max is unaffected —
|
|
248
|
+
// that assertion could not have failed, because nothing negative ever reaches
|
|
249
|
+
// the code below.
|
|
209
250
|
if (Orm.instance?.sqlDb && !isStringIdModel(modelName)) {
|
|
210
251
|
rawData.id = -(++pendingIdCounter);
|
|
211
252
|
rawData.__pendingSqlId = true;
|
|
@@ -215,15 +256,218 @@ function assignRecordId(modelName: string, rawData: { [key: string]: unknown }):
|
|
|
215
256
|
const storeMap = store.get(modelName);
|
|
216
257
|
if (!storeMap) throw new Error(`Cannot assign record ID: model "${modelName}" not found in store`);
|
|
217
258
|
const modelStore = Array.from(storeMap.values()).filter(isOrmRecord);
|
|
218
|
-
|
|
219
|
-
|
|
259
|
+
|
|
260
|
+
// ONE COPY of the max-numeric-id reduce, in src/utils.ts. There were three
|
|
261
|
+
// (here, StandaloneDB.create, and the #203 test helper) and `docs/
|
|
262
|
+
// improvements.md`'s WET Code category prescribes the extraction. What that
|
|
263
|
+
// helper must NOT be is `Math.max(...ids)`; the reason is measured and it is
|
|
264
|
+
// documented at the helper rather than duplicated here. Pinned by AC2.
|
|
265
|
+
const maxId = maxNumericId(modelStore);
|
|
266
|
+
|
|
267
|
+
// THE OCCUPANCY CHECK RUNS ON THE LANDING KEY, NOT ON THE RAW CANDIDATE, and
|
|
268
|
+
// the difference is a silent data loss rather than a nicety.
|
|
269
|
+
//
|
|
270
|
+
// `createRecord` looks the record up under `rawData.id` (:50) but WRITES it
|
|
271
|
+
// under `record.id` (:69) — the value after the model's declared id transform
|
|
272
|
+
// has run inside `serialize`. When the transform is not the identity those two
|
|
273
|
+
// differ, so a guard written as `storeMap.has(rawData.id)` checks a key the
|
|
274
|
+
// record will never occupy, misses an occupied slot and overwrites it —
|
|
275
|
+
// measured on an `uppercase`-id model: the guard checks `owner-1`, the record
|
|
276
|
+
// lands under `OWNER-1`, store size unchanged, no error. That is
|
|
277
|
+
// abofs/stonyx-orm#205's lookup-key/landing-key divergence reappearing inside
|
|
278
|
+
// #203's own fix, which is why AC4 exists and why `rawData.id` is set to the
|
|
279
|
+
// LANDING key below.
|
|
280
|
+
//
|
|
281
|
+
// THE SCOPE OF THAT CLAIM, stated rather than implied. Setting `rawData.id` to
|
|
282
|
+
// the landing key makes :50 and :69 agree for every IDEMPOTENT id transform —
|
|
283
|
+
// `number`, `float`, `string`, `passthrough`, `uppercase`, `trim`. It does NOT
|
|
284
|
+
// make them agree for `date` or `timestamp`: `transforms.date` returns a NEW
|
|
285
|
+
// object every call and a `Map` keys by identity, so `storeMap.has(landingKey)`
|
|
286
|
+
// is always `false` there and the occupancy check is vacuous. `dev` is broken
|
|
287
|
+
// for those types too — this is not a regression — but no comment here may
|
|
288
|
+
// claim a property it was not measured to have (#212 § AC5).
|
|
289
|
+
//
|
|
290
|
+
// Resolved ONCE, outside the walk: `getIdType` instantiates the model class,
|
|
291
|
+
// so resolving it per candidate would put a model construction on every
|
|
292
|
+
// iteration of a loop that exists to walk past occupied slots.
|
|
293
|
+
const toStoreKey = storeKeyDeriver(modelName);
|
|
294
|
+
|
|
295
|
+
// DOES THIS MODEL FILE ITS RECORDS UNDER STRING KEYS? Decided by RUNNING the
|
|
296
|
+
// model's own id transform once, not by matching a type NAME against a list:
|
|
297
|
+
// `Orm.instance.transforms` (main.ts:70) is a public, MUTABLE instance
|
|
298
|
+
// property, so any enumeration of "the string-ish types" written here would be
|
|
299
|
+
// wrong the moment a consumer registers one.
|
|
300
|
+
const stringKeyed = typeof toStoreKey(maxId + 1) === 'string';
|
|
301
|
+
|
|
302
|
+
// THE CANDIDATE FOR A STRING-KEYED MODEL IS NOT A BARE NUMBER, and this is
|
|
303
|
+
// abofs/stonyx-orm#209 — which is REOPENED — not aesthetics.
|
|
304
|
+
//
|
|
305
|
+
// `orm-request.ts`'s `coerceId` (:322) resolves a NUMERIC-LOOKING string to a
|
|
306
|
+
// NUMBER on every id-bearing surface, while a model declaring
|
|
307
|
+
// `id = attr('string')` files under the STRING key. So a server-assigned `'1'`
|
|
308
|
+
// produces a record that is created and then NOT ADDRESSABLE. Measured over
|
|
309
|
+
// the route, owner store `{'1': ...}`:
|
|
310
|
+
//
|
|
311
|
+
// GET /owners/1 -> 404 (the record exists)
|
|
312
|
+
// DELETE /owners/1 -> 404
|
|
313
|
+
// GET /owners/owner-1 -> 200
|
|
314
|
+
//
|
|
315
|
+
// and `_withHooks` (:1185) hands an after-`create` hook
|
|
316
|
+
// `context.record === undefined` for the same reason. `dev` assigned `'bob1'`,
|
|
317
|
+
// which is not numeric-looking, so `dev` has neither problem: a bare-number
|
|
318
|
+
// candidate would move #209 from "a caller supplied a numeric-looking id" onto
|
|
319
|
+
// the DEFAULT path for every server-assigned create on every string-id model.
|
|
320
|
+
// Prefixing with the model name keeps #209's population exactly as narrow as
|
|
321
|
+
// it already was, without touching the one shared coercion or the assertion
|
|
322
|
+
// that pins #209 open. Pinned by AC3.
|
|
323
|
+
const toCandidate = stringKeyed
|
|
324
|
+
? (value: number) => `${modelName}-${value}`
|
|
325
|
+
: (value: number) => value;
|
|
326
|
+
|
|
327
|
+
// `maxId + 1` is the id AC1 pins: strictly greater than every numeric key
|
|
328
|
+
// present. IT IS NOT ALWAYS AVAILABLE, and that gap was a live denial of
|
|
329
|
+
// service. Float64 has no integer successor at or above 2^53, so
|
|
330
|
+
// `maxId + 1 === maxId` for every `maxId >= 9007199254740992` and `+ 1` inside
|
|
331
|
+
// the walk is a NO-OP there. One record filed under that key — which an
|
|
332
|
+
// unauthenticated `POST {"id":9007199254740992}` puts there, and which reaches
|
|
333
|
+
// even a filter-protected collection through has-many.ts:65 (#207), a channel
|
|
334
|
+
// GATE 0 does not cover — made the walk unable to advance, so it exhausted its
|
|
335
|
+
// budget and threw on EVERY subsequent server-assigned create, permanently,
|
|
336
|
+
// until that record was deleted. Measured over the route: 200, then 500 for
|
|
337
|
+
// every no-id create. `dev` answers 200.
|
|
338
|
+
//
|
|
339
|
+
// So a store holding one adversarial record must not disable its collection.
|
|
340
|
+
// When "above the max" is not a usable strategy the walk RESTARTS FROM 1: the
|
|
341
|
+
// store holds at most `size` keys, so one of `1 .. size + 1` is always free
|
|
342
|
+
// under an injective id transform. Pinned by AC7.
|
|
343
|
+
const start = maxId + 1;
|
|
344
|
+
let landingKey = firstFreeKey(storeMap, toStoreKey, toCandidate, start);
|
|
345
|
+
|
|
346
|
+
// THE RESTART, and it is the whole of the ceiling fix. Killing mutation:
|
|
347
|
+
// delete this block -> AC7 goes red (the route answers 409 instead of the
|
|
348
|
+
// created resource).
|
|
349
|
+
if (landingKey === NO_FREE_KEY && start !== 1) {
|
|
350
|
+
landingKey = firstFreeKey(storeMap, toStoreKey, toCandidate, 1);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (landingKey === NO_FREE_KEY) {
|
|
354
|
+
// Reachable only with a NON-INJECTIVE id transform — see `firstFreeKey`.
|
|
355
|
+
// `createHandler` matches this message and answers 409 rather than letting it
|
|
356
|
+
// reach express's default handler, which serialises a stack trace with
|
|
357
|
+
// absolute install paths outside NODE_ENV=production (the hazard
|
|
358
|
+
// orm-request.ts:553-558 exists to name). Pinned by AC8.
|
|
359
|
+
throw new Error(`${NO_FREE_ID_ERROR} for model "${modelName}"`);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
rawData.id = landingKey;
|
|
220
363
|
}
|
|
221
364
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
365
|
+
// Returned instead of a key so that "no key" cannot be confused with a transform
|
|
366
|
+
// that legitimately produced `undefined` or `null`.
|
|
367
|
+
const NO_FREE_KEY = Symbol('no free store key');
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* The first store key at or above `start` that no record occupies, walking
|
|
371
|
+
* candidate ids upward, or `NO_FREE_KEY` if the walk cannot reach one.
|
|
372
|
+
*/
|
|
373
|
+
function firstFreeKey(
|
|
374
|
+
storeMap: Map<number | string, unknown>,
|
|
375
|
+
toStoreKey: (value: number | string) => number | string,
|
|
376
|
+
toCandidate: (value: number) => number | string,
|
|
377
|
+
start: number
|
|
378
|
+
): number | string | typeof NO_FREE_KEY {
|
|
379
|
+
let candidate = start;
|
|
380
|
+
let landingKey = toStoreKey(toCandidate(candidate));
|
|
381
|
+
let attempts = 0;
|
|
382
|
+
|
|
383
|
+
while (storeMap.has(landingKey)) {
|
|
384
|
+
// THE BOUND IS EXACTLY TIGHT, not conservative: this walk tries
|
|
385
|
+
// `storeMap.size + 1` DISTINCT candidates against at most `storeMap.size`
|
|
386
|
+
// occupied keys, so under an injective `toStoreKey` it provably cannot fire.
|
|
387
|
+
// Under a non-injective one it provably terminates — and that is a reachable
|
|
388
|
+
// consumer state rather than a hypothesis: `transforms.boolean`
|
|
389
|
+
// (transforms.ts:4) collapses every candidate onto `true`/`false`, and
|
|
390
|
+
// `Orm.instance.transforms` (main.ts:70) is public and MUTABLE, so a consumer
|
|
391
|
+
// can register an arbitrary non-injective transform and name it as an id
|
|
392
|
+
// type. Without this, a no-id create spins forever inside a synchronous store
|
|
393
|
+
// walk and pins a worker, which is worse than either collision policy. Its
|
|
394
|
+
// EXISTENCE and its THRESHOLD are both pinned by AC8: deleting it makes AC8
|
|
395
|
+
// HANG rather than fail, and weakening it to fire on the first collision
|
|
396
|
+
// makes AC8.1 red.
|
|
397
|
+
if (++attempts > storeMap.size) return NO_FREE_KEY;
|
|
398
|
+
|
|
399
|
+
// NOTE FOR ANYONE ADDING A SECOND EXIT HERE. A `candidate + 1 === candidate`
|
|
400
|
+
// float-saturation check was written, measured, and REMOVED: with the
|
|
401
|
+
// restart-from-1 above in place, deleting the saturation check leaves the
|
|
402
|
+
// whole suite green, because the budget reaches the same `NO_FREE_KEY` one
|
|
403
|
+
// pass later and the restart still answers. An unkillable guard in a change
|
|
404
|
+
// whose deliverable is falsifiable coverage is exactly what this story exists
|
|
405
|
+
// to stop shipping. `+ 1` being a no-op at 2^53 costs `size` extra `Map.has`
|
|
406
|
+
// calls on that one path and changes no outcome.
|
|
407
|
+
candidate += 1;
|
|
408
|
+
landingKey = toStoreKey(toCandidate(candidate));
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return landingKey;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Returns the derivation that maps an id VALUE to the store KEY a record
|
|
416
|
+
* carrying it will actually be filed under — the model's declared id transform,
|
|
417
|
+
* the same one `serialize` runs at createRecord:68 before the `.set` at :69.
|
|
418
|
+
*/
|
|
419
|
+
function storeKeyDeriver(modelName: string): (value: number | string) => number | string {
|
|
420
|
+
const idType = getIdType(modelName);
|
|
421
|
+
const transform = idType ? Orm.instance?.transforms?.[idType] : undefined;
|
|
422
|
+
|
|
423
|
+
// SURVIVOR, RETAINED, with its reachability condition stated rather than left
|
|
424
|
+
// silent — `docs/project-structure.md` § "unkillable code reads as coverage and
|
|
425
|
+
// is not" is the standing rule and it applies outside orm-request.ts too.
|
|
426
|
+
//
|
|
427
|
+
// No mutation in this repo can kill this branch, and that is STRUCTURAL rather
|
|
428
|
+
// than an untested gap: `Model` declares `id = attr('number')` (model.ts:15) so
|
|
429
|
+
// every registered model has an id type; `ModelProperty` refuses a type with no
|
|
430
|
+
// registered transform (model-property.ts:4) so a declared type always
|
|
431
|
+
// resolves; and `getIdType` can therefore only return `undefined` when
|
|
432
|
+
// `getRecordClasses` yields no `modelClass` — in which case `createRecord`
|
|
433
|
+
// throws at :62 a few lines later regardless, so no record is ever filed
|
|
434
|
+
// through this branch.
|
|
435
|
+
//
|
|
436
|
+
// BECOMES REACHABLE if a model can be declared without an `id` property, if a
|
|
437
|
+
// store map can exist for a model with no registered class, or if
|
|
438
|
+
// `createRecord` stops constructing the model class. Kept rather than deleted
|
|
439
|
+
// because the alternative on that path is a `transform is not a function`
|
|
440
|
+
// TypeError, and because identity is exactly what `createRecord` would file
|
|
441
|
+
// under when no transform exists — the two agree, which is the property AC4 is
|
|
442
|
+
// about.
|
|
443
|
+
if (typeof transform !== 'function') return value => value;
|
|
444
|
+
|
|
445
|
+
return value => {
|
|
446
|
+
try {
|
|
447
|
+
return transform(value) as number | string;
|
|
448
|
+
} catch {
|
|
449
|
+
// `uppercase` and `trim` (transforms.ts:11-12) call a string method on the
|
|
450
|
+
// value directly, so a NUMERIC candidate throws `value?.toUpperCase is not
|
|
451
|
+
// a function`. On `dev` they never saw one — `lastRecord.id + 1` on a
|
|
452
|
+
// string id is a string — so feeding them a number here would regress a
|
|
453
|
+
// legal, registered id type into an uncaught 500. The retry feeds the
|
|
454
|
+
// string form, which is the shape an id actually arrives in off a JSON body
|
|
455
|
+
// or a URL param. A transform that throws on BOTH shapes still propagates.
|
|
456
|
+
// Pinned by AC9.
|
|
457
|
+
return transform(String(value)) as number | string;
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function getIdType(modelName: string): string | undefined {
|
|
463
|
+
const modelClass = Orm.instance?.getRecordClasses(modelName).modelClass as (new (name: string) => { [key: string]: unknown }) | undefined;
|
|
464
|
+
if (!modelClass) return undefined;
|
|
225
465
|
|
|
226
466
|
const model = new modelClass(modelName);
|
|
227
467
|
|
|
228
|
-
return (model.id as { type?: string } | undefined)?.type
|
|
468
|
+
return (model.id as { type?: string } | undefined)?.type;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function isStringIdModel(modelName: string): boolean {
|
|
472
|
+
return getIdType(modelName) === 'string';
|
|
229
473
|
}
|
package/src/orm-request.ts
CHANGED
|
@@ -98,8 +98,8 @@
|
|
|
98
98
|
*
|
|
99
99
|
* PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not make
|
|
100
100
|
* the answer model-correct on its own -- the resolved predicate has to READ it.
|
|
101
|
-
* Measured against
|
|
102
|
-
*
|
|
101
|
+
* Measured against an ARITY-1 predicate, on a request express dispatched to
|
|
102
|
+
* `GET /owners/angela`, asked about ANIMALS:
|
|
103
103
|
*
|
|
104
104
|
* getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
|
|
105
105
|
* -> record => record.id !== 'angela' && record.id !== 'restricted'
|
|
@@ -110,23 +110,30 @@
|
|
|
110
110
|
* `['read', 'create', 'update', 'delete']`, a full CRUD grant. Either way the
|
|
111
111
|
* context was supplied and the answer is not the animal answer, and it is wrong
|
|
112
112
|
* in the GRANTING direction, because that predicate is arity-1 and identifies
|
|
113
|
-
* its collection from the request. (
|
|
114
|
-
*
|
|
113
|
+
* its collection from the request. (Asserted on a live dispatch by AC9 in
|
|
114
|
+
* test/integration/orm-test.ts, against a deliberately arity-1 predicate.)
|
|
115
115
|
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
* the
|
|
116
|
+
* This repo's own sample access class has since been MIGRATED to read the
|
|
117
|
+
* context (abofs/stonyx-orm#222), so `getAccess('animal')` here now answers
|
|
118
|
+
* with the animal filter. That is not true of a consumer tree: an arity-1
|
|
119
|
+
* predicate keeps working -- the second argument is additive -- and the caller
|
|
120
|
+
* has no supported way to tell which kind it got. The boot-time arity warning
|
|
121
|
+
* that surfaces one is abofs/stonyx-orm#221.
|
|
119
122
|
* So: pass the context, and do not treat a resolved predicate's answer as
|
|
120
123
|
* model-specific until that predicate has been migrated to read the context.
|
|
121
124
|
*
|
|
122
125
|
* ---------------------------------------------------------------------------
|
|
123
126
|
* DO NOT RECONSTRUCT THE REQUEST PATH INSIDE `access()`.
|
|
124
127
|
* ---------------------------------------------------------------------------
|
|
125
|
-
* `auth()` below hands your
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
128
|
+
* You do not have to. `auth()` below hands your predicate the ACCESS CONTEXT as
|
|
129
|
+
* argument two, and `context.model` already names the collection -- see the
|
|
130
|
+
* contract section above. Argument ONE is still the raw transport artifact, and
|
|
131
|
+
* everything from here to the end of this banner is the record of what happened
|
|
132
|
+
* when predicates worked the collection out from it. IT IS HISTORY, NOT
|
|
133
|
+
* GUIDANCE: do not write any of it into a new predicate. Every attempt to
|
|
134
|
+
* identify the collection by parsing the request target has failed OPEN. Five
|
|
135
|
+
* distinct variants of the same three-line example have now been found, each
|
|
136
|
+
* after the previous was fixed, by five different people:
|
|
130
137
|
*
|
|
131
138
|
* 1. `request.url` is mount-relative under `RestServer.mountRoute`, so a
|
|
132
139
|
* prefix match against it is ALWAYS false.
|
|
@@ -145,22 +152,50 @@
|
|
|
145
152
|
* last, and the record comes back in full. It walks past a hard
|
|
146
153
|
* `return false` deny the same way.
|
|
147
154
|
*
|
|
148
|
-
* The fix is not a sixth rule. It is to
|
|
155
|
+
* The fix is not a sixth rule, and it is not a better string to match. It is to
|
|
156
|
+
* stop identifying the collection at all: read `context.model`. That is a claim
|
|
157
|
+
* about IDENTIFYING THE COLLECTION, not about the sample as a whole -- the
|
|
158
|
+
* `/archived` SUB-PATH rule is still a string match, and abofs/stonyx-orm#228 is
|
|
159
|
+
* a sixth spelling that gets past it.
|
|
149
160
|
*
|
|
150
|
-
* `request.baseUrl`
|
|
151
|
-
*
|
|
152
|
-
* unaffected by absolute-form,
|
|
153
|
-
* `ORM_REST_ROUTE` prefix
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
161
|
+
* An intermediate revision of the sample read `request.baseUrl` -- the mount
|
|
162
|
+
* Express ACTUALLY MATCHED. That closed all five variants (no query string,
|
|
163
|
+
* not mount-relative, unaffected by absolute-form, already carrying the
|
|
164
|
+
* configured `ORM_REST_ROUTE` prefix), but it was a transport artifact
|
|
165
|
+
* standing in for a structural fact and the sample no longer does it.
|
|
166
|
+
* `context.model` IS the structural fact, so variants 1, 2, 4 and 5 are
|
|
167
|
+
* unconstructible against a migrated predicate rather than handled.
|
|
168
|
+
*
|
|
169
|
+
* VARIANT 3 SURVIVES, and is deliberately not in that list. It is the general
|
|
170
|
+
* shape "a hand-written matcher normalises differently from the router", and a
|
|
171
|
+
* migrated predicate still runs one string comparison for any SUB-PATH rule --
|
|
172
|
+
* in the shipped sample, the `/archived` deny. That comparison folds case but
|
|
173
|
+
* does not decode, so `GET /owners/%61rchived` steps past it. See the
|
|
174
|
+
* normalisation paragraph below and abofs/stonyx-orm#228.
|
|
175
|
+
*
|
|
176
|
+
* ONE READ OF ARGUMENT ONE SURVIVES, AND IT MUST: `request.path`. It is
|
|
177
|
+
* mount-relative and query-free, and it is for rules that distinguish SUB-PATHS
|
|
178
|
+
* beneath the mount. The context names which model and which verb, NOT which
|
|
179
|
+
* route, so the sample's `/archived` deny cannot be expressed from the context
|
|
180
|
+
* alone and a context-ONLY rewrite would silently turn that deny into an allow.
|
|
181
|
+
*
|
|
182
|
+
* NORMALISE THE WAY THE ROUTER DOES, AND CASE-FOLDING ALONE IS NOT THAT. The
|
|
183
|
+
* sample lower-cases before comparing, because a matcher stricter than the
|
|
184
|
+
* case-insensitive router can be stepped around. That closes the case gap only.
|
|
185
|
+
* Express sets `request.path` from the RAW, UNDECODED pathname while the router
|
|
186
|
+
* DECODES `:id`, so `GET /owners/%61rchived` reaches a `path === '/archived'`
|
|
187
|
+
* comparison as `/%61rchived` and walks past the deny. That gap is live in the
|
|
188
|
+
* sample and is tracked as abofs/stonyx-orm#228; the `.toLowerCase()` is not a
|
|
189
|
+
* complete normalisation recipe. Compare record ids at their real case.
|
|
157
190
|
*
|
|
158
191
|
* `?? ''` is not a defence. It converts an absent request target into an empty
|
|
159
192
|
* string, which matches no collection, which falls through to the permission
|
|
160
|
-
* array -- a total grant. An input you cannot identify must DENY
|
|
193
|
+
* array -- a total grant. An input you cannot identify must DENY, and that
|
|
194
|
+
* applies to BOTH arguments: since #202 the guard and the read can sit on
|
|
195
|
+
* different objects, and a guard on argument two does not protect a read of
|
|
196
|
+
* argument one. The sample returns `false` for an absent `model` AND for an
|
|
197
|
+
* absent or non-string `request.path`, rather than falling through either way.
|
|
161
198
|
*
|
|
162
|
-
* THAT IS STILL A STOPGAP. `baseUrl` closes all five variants, but it is a
|
|
163
|
-
* transport artifact being asked to stand in for a structural fact.
|
|
164
199
|
* THE REAL FIX IS abofs/stonyx-orm#202: `access()` should receive the model,
|
|
165
200
|
* the operation and the record. Prefer the array shape (`['read']`) or `false`
|
|
166
201
|
* until #202 lands; the function shape is what requires any matching at all.
|
|
@@ -185,7 +220,7 @@ import type { HookContext } from './hooks.js';
|
|
|
185
220
|
import config from 'stonyx/config';
|
|
186
221
|
import log from 'stonyx/log';
|
|
187
222
|
import type { OrmRecord, AccessContext, AccessFunction, AccessMethod, AccessOperation } from './types/orm-types.js';
|
|
188
|
-
import { isOrmRecord } from './utils.js';
|
|
223
|
+
import { isOrmRecord, NO_FREE_ID_ERROR } from './utils.js';
|
|
189
224
|
|
|
190
225
|
interface OrmRequest$ extends Request {
|
|
191
226
|
protocol?: string;
|
|
@@ -762,7 +797,41 @@ export default class OrmRequest extends Request {
|
|
|
762
797
|
// only O(1) signal that distinguishes an insert from an overwrite.
|
|
763
798
|
const slotsBefore = store.get(model)?.size ?? 0;
|
|
764
799
|
|
|
765
|
-
|
|
800
|
+
// THE ONE `createRecord` FAILURE THIS ROUTE ANSWERS RATHER THAN
|
|
801
|
+
// PROPAGATES, and it is narrow on purpose.
|
|
802
|
+
//
|
|
803
|
+
// `assignRecordId` throws when it cannot derive a free store key for a
|
|
804
|
+
// server-assigned id. Unguarded that rejection is auto-forwarded -- there
|
|
805
|
+
// is no catch here, none in @stonyx/rest-server's dispatcher
|
|
806
|
+
// (dist/request.js:41-70), and express 5 hands it to its default error
|
|
807
|
+
// handler, which serialises the STACK, with absolute install paths and the
|
|
808
|
+
// internal module graph, to an unauthenticated caller outside
|
|
809
|
+
// NODE_ENV=production. That is the hazard :553-558 already names in this
|
|
810
|
+
// file, and every sibling refusal in this handler returns an integer
|
|
811
|
+
// status instead. So this one returns 409, matching the client-duplicate
|
|
812
|
+
// refusal at :713: the caller asked for a record and the collection has no
|
|
813
|
+
// id to give it.
|
|
814
|
+
//
|
|
815
|
+
// MATCHED ON THE SHARED PREFIX, not on a literal, and NOT by catching
|
|
816
|
+
// everything: `createRecord` also throws for "ORM is not ready", a
|
|
817
|
+
// read-only view and an unregistered model store, and turning any of those
|
|
818
|
+
// into a 409 would report a configuration fault as a conflict. Anything
|
|
819
|
+
// else is re-thrown unchanged.
|
|
820
|
+
let created;
|
|
821
|
+
|
|
822
|
+
try {
|
|
823
|
+
created = createRecord(model, recordAttributes as { [key: string]: unknown }, { serialize: false, _skipAutoPersist: true });
|
|
824
|
+
} catch (error) {
|
|
825
|
+
if (!(error instanceof Error) || !error.message.startsWith(NO_FREE_ID_ERROR)) throw error;
|
|
826
|
+
|
|
827
|
+
// Not silently. A collection that can no longer assign an id is a
|
|
828
|
+
// configuration fault (a non-injective id transform), and a bare 409
|
|
829
|
+
// with no diagnostic is indistinguishable from an ordinary duplicate.
|
|
830
|
+
log.error?.(`[@stonyx/orm] ${error.message}`);
|
|
831
|
+
|
|
832
|
+
return 409; // Conflict
|
|
833
|
+
}
|
|
834
|
+
|
|
766
835
|
const record = isOrmRecord(created) ? created : null;
|
|
767
836
|
if (!record) return 500;
|
|
768
837
|
|
|
@@ -787,11 +856,32 @@ export default class OrmRequest extends Request {
|
|
|
787
856
|
//
|
|
788
857
|
// Both conditions are required and neither implies the other:
|
|
789
858
|
// createdNewSlot -- the store grew, so this request inserted rather
|
|
790
|
-
// than overwrote.
|
|
791
|
-
//
|
|
792
|
-
// last-INSERTED + 1,
|
|
793
|
-
//
|
|
794
|
-
//
|
|
859
|
+
// than overwrote. SURVIVOR AS OF #203, AND THAT IS
|
|
860
|
+
// WHAT THIS NOTE IS FOR. It used to be killable:
|
|
861
|
+
// `assignRecordId` returned last-INSERTED + 1, so a
|
|
862
|
+
// server-assigned id could land on an occupied slot,
|
|
863
|
+
// `createRecord` updated in place, and removing this
|
|
864
|
+
// half turned access-filter-enforcement-test.ts
|
|
865
|
+
// assertion 31 red. #203 closed that: the
|
|
866
|
+
// server-assigned path now walks past occupied keys,
|
|
867
|
+
// so no create reaching here can overwrite. Measured
|
|
868
|
+
// -- delete `createdNewSlot &&` below: `dev` gives
|
|
869
|
+
// 55 pass / 1 fail with assertion 31 RED, this tree
|
|
870
|
+
// gives 56 pass / 0 fail, GREEN.
|
|
871
|
+
// KEPT ANYWAY, AND NOT FOR THE OLD REASON. Without
|
|
872
|
+
// it a denied create becomes `store.remove` on a key
|
|
873
|
+
// the caller may have influenced, which :815-820
|
|
874
|
+
// records as having been an unauthenticated deletion
|
|
875
|
+
// primitive across the whole id space. BECOMES
|
|
876
|
+
// KILLABLE AGAIN the moment any caller-supplied id
|
|
877
|
+
// can reach `createRecord` from this handler --
|
|
878
|
+
// which is exactly what has-many.ts:65 and
|
|
879
|
+
// belongs-to.ts:45 already do for ANOTHER model's
|
|
880
|
+
// store (abofs/stonyx-orm#207), and what a third
|
|
881
|
+
// un-stripped id channel would do for this one
|
|
882
|
+
// (#204). Do not delete it on the strength of #203
|
|
883
|
+
// being closed; that is the reasoning :862-867 warns
|
|
884
|
+
// about, one level up.
|
|
795
885
|
// identity -- the slot still holds the object we just created,
|
|
796
886
|
// so nothing between createRecord and here replaced
|
|
797
887
|
// it. Deleting this half SURVIVES the suite, and it
|