@stonyx/orm 0.3.2-alpha.56 → 0.3.2-alpha.58
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 +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/main.d.ts +26 -5
- package/dist/main.js +28 -7
- package/dist/setup-rest-server.js +6 -6
- package/dist/types/orm-types.d.ts +44 -10
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/main.ts +28 -7
- package/src/orm-request.ts +2 -2
- package/src/setup-rest-server.ts +6 -6
- package/src/types/orm-types.ts +45 -10
- package/src/types/stonyx-rest-server.d.ts +14 -1
package/README.md
CHANGED
|
@@ -463,7 +463,7 @@ const verdict = predicate?.(request, { model: 'animal', operation: 'read' });
|
|
|
463
463
|
```
|
|
464
464
|
|
|
465
465
|
`Orm.instance.getAccess(modelName)` returns the predicate, or `undefined` when
|
|
466
|
-
that model has no access class. The raw map is `Orm.instance.
|
|
466
|
+
that model has no access class. The raw map is `Orm.instance.accessFunctions`, keyed
|
|
467
467
|
by model name; prefer `getAccess()`.
|
|
468
468
|
|
|
469
469
|
Passing the context explicitly is what makes the answer **model-correct**. A
|
package/dist/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { count, avg, sum, min, max } from './aggregates.js';
|
|
|
9
9
|
export { default } from './main.js';
|
|
10
10
|
export { store, relationships } from './main.js';
|
|
11
11
|
export type { PersistErrorDetail } from './main.js';
|
|
12
|
-
export type { AccessContext, AccessFunction, AccessMethod } from './types/orm-types.js';
|
|
12
|
+
export type { AccessContext, AccessFunction, AccessMethod, AccessOperation } from './types/orm-types.js';
|
|
13
13
|
export { Model, View, Serializer };
|
|
14
14
|
export { attr, belongsTo, hasMany, createRecord, updateRecord };
|
|
15
15
|
export { count, avg, sum, min, max };
|
package/dist/main.d.ts
CHANGED
|
@@ -34,7 +34,25 @@ export default class Orm {
|
|
|
34
34
|
transforms: Record<string, (value: unknown) => unknown>;
|
|
35
35
|
warnings: Set<string>;
|
|
36
36
|
/**
|
|
37
|
-
* Model name ->
|
|
37
|
+
* Model name -> the `access` predicate of the access class that CLAIMS that
|
|
38
|
+
* model (abofs/stonyx-orm#202).
|
|
39
|
+
*
|
|
40
|
+
* Not "that model's own predicate". One access class may claim many models
|
|
41
|
+
* -- `GlobalAccess` in this repo's fixtures declares five, and `models = '*'`
|
|
42
|
+
* claims every model in the store -- and it declares ONE `access` method, so
|
|
43
|
+
* the same function object is registered under every one of those keys.
|
|
44
|
+
* `getAccess('owner') === getAccess('animal')` is `true` there. The
|
|
45
|
+
* one-to-one guarantee below is key -> function, never function -> model,
|
|
46
|
+
* and a caller must not read a resolved predicate as being animal-specific.
|
|
47
|
+
* What makes the ANSWER model-specific is the context the caller passes and
|
|
48
|
+
* the predicate actually reading it -- see {@link Orm#getAccess}.
|
|
49
|
+
*
|
|
50
|
+
* NAMED FOR WHAT IT HOLDS. It was `accessFiles` through review, inherited
|
|
51
|
+
* from the function-local in `setup-rest-server.ts` where the values came
|
|
52
|
+
* straight out of `forEachFileImport` and "files" was defensible. The values
|
|
53
|
+
* are `AccessFunction`s, and the sibling public registries on this class
|
|
54
|
+
* (`models`, `serializers`, `views`, `transforms`) are all plural nouns of
|
|
55
|
+
* the thing held. Renamed here because #202 is the last moment it is free.
|
|
38
56
|
*
|
|
39
57
|
* Populated by `setup-rest-server.ts` at boot, from the access classes under
|
|
40
58
|
* `config.orm.paths.access`, BEFORE any route is mounted -- so it is complete
|
|
@@ -53,10 +71,13 @@ export default class Orm {
|
|
|
53
71
|
* request routed to model Y -- inexpressible, which is the capability
|
|
54
72
|
* abofs/stonyx-orm#196 and abofs/stonyx-orm#207 are built on.
|
|
55
73
|
*
|
|
56
|
-
* Empty when the REST server is disabled,
|
|
57
|
-
*
|
|
74
|
+
* Empty when the REST server is disabled, and PARTIAL when one access file
|
|
75
|
+
* failed to load (`setup-rest-server.ts` catches, warns and assigns whatever
|
|
76
|
+
* it had). So a missing key does NOT mean the model has no access class.
|
|
77
|
+
* Prefer {@link Orm#getAccess} over indexing this directly -- it is guarded
|
|
78
|
+
* against the prototype chain and this is not.
|
|
58
79
|
*/
|
|
59
|
-
|
|
80
|
+
accessFunctions: Record<string, AccessFunction>;
|
|
60
81
|
options: OrmOptions;
|
|
61
82
|
sqlDb?: SqlDb;
|
|
62
83
|
db?: OrmDB | SqlDb;
|
|
@@ -81,7 +102,7 @@ export default class Orm {
|
|
|
81
102
|
* collection the request is ADDRESSED TO -- owners -- while being asked about
|
|
82
103
|
* animals, and per #202's thesis it answers wrong in the granting direction.
|
|
83
104
|
*
|
|
84
|
-
* OWN PROPERTIES ONLY. A bare `this.
|
|
105
|
+
* OWN PROPERTIES ONLY. A bare `this.accessFunctions[modelName]` walks the
|
|
85
106
|
* prototype chain, so `getAccess('constructor')` resolved `Object` and
|
|
86
107
|
* `getAccess('toString')` resolved `Object.prototype.toString` -- both
|
|
87
108
|
* callable, and the documented `predicate?.(request, ctx)` pattern then
|
package/dist/main.js
CHANGED
|
@@ -39,7 +39,25 @@ export default class Orm {
|
|
|
39
39
|
transforms = { ...baseTransforms };
|
|
40
40
|
warnings = new Set();
|
|
41
41
|
/**
|
|
42
|
-
* Model name ->
|
|
42
|
+
* Model name -> the `access` predicate of the access class that CLAIMS that
|
|
43
|
+
* model (abofs/stonyx-orm#202).
|
|
44
|
+
*
|
|
45
|
+
* Not "that model's own predicate". One access class may claim many models
|
|
46
|
+
* -- `GlobalAccess` in this repo's fixtures declares five, and `models = '*'`
|
|
47
|
+
* claims every model in the store -- and it declares ONE `access` method, so
|
|
48
|
+
* the same function object is registered under every one of those keys.
|
|
49
|
+
* `getAccess('owner') === getAccess('animal')` is `true` there. The
|
|
50
|
+
* one-to-one guarantee below is key -> function, never function -> model,
|
|
51
|
+
* and a caller must not read a resolved predicate as being animal-specific.
|
|
52
|
+
* What makes the ANSWER model-specific is the context the caller passes and
|
|
53
|
+
* the predicate actually reading it -- see {@link Orm#getAccess}.
|
|
54
|
+
*
|
|
55
|
+
* NAMED FOR WHAT IT HOLDS. It was `accessFiles` through review, inherited
|
|
56
|
+
* from the function-local in `setup-rest-server.ts` where the values came
|
|
57
|
+
* straight out of `forEachFileImport` and "files" was defensible. The values
|
|
58
|
+
* are `AccessFunction`s, and the sibling public registries on this class
|
|
59
|
+
* (`models`, `serializers`, `views`, `transforms`) are all plural nouns of
|
|
60
|
+
* the thing held. Renamed here because #202 is the last moment it is free.
|
|
43
61
|
*
|
|
44
62
|
* Populated by `setup-rest-server.ts` at boot, from the access classes under
|
|
45
63
|
* `config.orm.paths.access`, BEFORE any route is mounted -- so it is complete
|
|
@@ -58,10 +76,13 @@ export default class Orm {
|
|
|
58
76
|
* request routed to model Y -- inexpressible, which is the capability
|
|
59
77
|
* abofs/stonyx-orm#196 and abofs/stonyx-orm#207 are built on.
|
|
60
78
|
*
|
|
61
|
-
* Empty when the REST server is disabled,
|
|
62
|
-
*
|
|
79
|
+
* Empty when the REST server is disabled, and PARTIAL when one access file
|
|
80
|
+
* failed to load (`setup-rest-server.ts` catches, warns and assigns whatever
|
|
81
|
+
* it had). So a missing key does NOT mean the model has no access class.
|
|
82
|
+
* Prefer {@link Orm#getAccess} over indexing this directly -- it is guarded
|
|
83
|
+
* against the prototype chain and this is not.
|
|
63
84
|
*/
|
|
64
|
-
|
|
85
|
+
accessFunctions = {};
|
|
65
86
|
options;
|
|
66
87
|
sqlDb;
|
|
67
88
|
db;
|
|
@@ -187,7 +208,7 @@ export default class Orm {
|
|
|
187
208
|
* collection the request is ADDRESSED TO -- owners -- while being asked about
|
|
188
209
|
* animals, and per #202's thesis it answers wrong in the granting direction.
|
|
189
210
|
*
|
|
190
|
-
* OWN PROPERTIES ONLY. A bare `this.
|
|
211
|
+
* OWN PROPERTIES ONLY. A bare `this.accessFunctions[modelName]` walks the
|
|
191
212
|
* prototype chain, so `getAccess('constructor')` resolved `Object` and
|
|
192
213
|
* `getAccess('toString')` resolved `Object.prototype.toString` -- both
|
|
193
214
|
* callable, and the documented `predicate?.(request, ctx)` pattern then
|
|
@@ -206,9 +227,9 @@ export default class Orm {
|
|
|
206
227
|
* note above. Treat it as deny.
|
|
207
228
|
*/
|
|
208
229
|
getAccess(modelName) {
|
|
209
|
-
if (!Object.hasOwn(this.
|
|
230
|
+
if (!Object.hasOwn(this.accessFunctions, modelName))
|
|
210
231
|
return undefined;
|
|
211
|
-
return this.
|
|
232
|
+
return this.accessFunctions[modelName];
|
|
212
233
|
}
|
|
213
234
|
async startup() {
|
|
214
235
|
if (this.sqlDb)
|
|
@@ -8,7 +8,7 @@ import { dbKey } from './db.js';
|
|
|
8
8
|
import { getPluralName } from './plural-registry.js';
|
|
9
9
|
import log from 'stonyx/log';
|
|
10
10
|
export default async function (route, accessPath, metaRoute) {
|
|
11
|
-
const
|
|
11
|
+
const accessFunctions = {};
|
|
12
12
|
try {
|
|
13
13
|
await forEachFileImport(accessPath, (accessClass) => {
|
|
14
14
|
const accessInstance = new accessClass();
|
|
@@ -25,9 +25,9 @@ export default async function (route, accessPath, metaRoute) {
|
|
|
25
25
|
continue;
|
|
26
26
|
if (!store.data.has(model))
|
|
27
27
|
throw new Error(`Unable to define access for Invalid Model "${model}". Model does not exist`);
|
|
28
|
-
if (
|
|
28
|
+
if (accessFunctions[model])
|
|
29
29
|
throw new Error(`Access for model "${model}" has already been defined by another access class.`);
|
|
30
|
-
|
|
30
|
+
accessFunctions[model] = accessInstance.access;
|
|
31
31
|
}
|
|
32
32
|
});
|
|
33
33
|
}
|
|
@@ -38,7 +38,7 @@ export default async function (route, accessPath, metaRoute) {
|
|
|
38
38
|
// -------------------------------------------------------------------------
|
|
39
39
|
// #202 -- the registry has to survive this function.
|
|
40
40
|
//
|
|
41
|
-
// `
|
|
41
|
+
// `accessFunctions` used to be a function-local that was discarded at the return
|
|
42
42
|
// below, so the only thing that ever saw it was the mount loop. Each mounted
|
|
43
43
|
// OrmRequest then held its OWN model's predicate and nothing held the map, so
|
|
44
44
|
// at request time there was no route from a model NAME to that model's
|
|
@@ -56,12 +56,12 @@ export default async function (route, accessPath, metaRoute) {
|
|
|
56
56
|
// set of predicates that is actually enforcing. A guard here that skipped the
|
|
57
57
|
// assignment would let the registry go silently missing, which is precisely
|
|
58
58
|
// the failure #202's AC8 exists to catch.
|
|
59
|
-
Orm.instance.
|
|
59
|
+
Orm.instance.accessFunctions = accessFunctions;
|
|
60
60
|
await waitForModule('rest-server');
|
|
61
61
|
// Remove "/" prefix and name mount point accordingly
|
|
62
62
|
const name = route === '/' ? 'index' : (route[0] === '/' ? route.slice(1) : route);
|
|
63
63
|
// Configure endpoints for models and views with access configuration
|
|
64
|
-
for (const [model, access] of Object.entries(
|
|
64
|
+
for (const [model, access] of Object.entries(accessFunctions)) {
|
|
65
65
|
const pluralizedModel = getPluralName(model);
|
|
66
66
|
const modelName = name === 'index' ? pluralizedModel : `${name}/${pluralizedModel}`;
|
|
67
67
|
RestServer.instance.mountRoute(OrmRequest, { name: modelName, options: { model, access } });
|
|
@@ -176,6 +176,20 @@ export interface SnapshotEntry {
|
|
|
176
176
|
* Anything else fails CLOSED. See `src/orm-request.ts` `auth()`.
|
|
177
177
|
*/
|
|
178
178
|
export type AccessMethod = string | boolean | string[] | ((record: unknown) => boolean);
|
|
179
|
+
/**
|
|
180
|
+
* The closed vocabulary `AccessContext.operation` is drawn from
|
|
181
|
+
* (abofs/stonyx-orm#202).
|
|
182
|
+
*
|
|
183
|
+
* A literal union rather than `string`, so the guarantee the prose makes is the
|
|
184
|
+
* one the compiler enforces: a consumer who writes `operation === 'GET'` or
|
|
185
|
+
* `operation === 'get'` -- the hook vocabulary, see below -- gets a compile
|
|
186
|
+
* error instead of a comparison that never matches. A predicate that stops
|
|
187
|
+
* matching falls through to the permission array, so the misreading is
|
|
188
|
+
* fail-open shaped.
|
|
189
|
+
*
|
|
190
|
+
* In-repo precedent: `PersistErrorDetail.operation` in `src/main.ts`.
|
|
191
|
+
*/
|
|
192
|
+
export type AccessOperation = 'read' | 'create' | 'update' | 'delete';
|
|
179
193
|
/**
|
|
180
194
|
* The structural facts about the request being authorised, handed to a consumer
|
|
181
195
|
* `access()` predicate as its SECOND argument (abofs/stonyx-orm#202).
|
|
@@ -205,27 +219,47 @@ export interface AccessContext {
|
|
|
205
219
|
*/
|
|
206
220
|
model: string;
|
|
207
221
|
/**
|
|
208
|
-
* The operation being authorised
|
|
209
|
-
*
|
|
222
|
+
* The operation being authorised. Exactly one of the four {@link
|
|
223
|
+
* AccessOperation} verbs, or `undefined`. These are exactly the values of
|
|
210
224
|
* `methodAccessMap` in `src/orm-request.ts`, which is also what the
|
|
211
225
|
* permission-array return shape is matched against -- so the two forms cannot
|
|
212
226
|
* disagree.
|
|
213
227
|
*
|
|
228
|
+
* NOT the hook vocabulary. `HookContext.operation` (`src/hooks.ts`) carries
|
|
229
|
+
* `'list' | 'get' | 'create' | 'update' | 'delete'` on an identically-named
|
|
230
|
+
* key of an identically-shaped context object, and the access vocabulary
|
|
231
|
+
* collapses `list` and `get` into `'read'`. For one `GET /animals/1` a hook
|
|
232
|
+
* sees `'get'` and `access()` sees `'read'`. "No second vocabulary" is a
|
|
233
|
+
* statement about the ACCESS path only.
|
|
234
|
+
*
|
|
214
235
|
* `undefined` when the dispatched method has no entry in that map. Express
|
|
215
236
|
* delivers `HEAD` to the `GET` handler, so this is reachable. It is left
|
|
216
237
|
* undefined rather than defaulted on purpose: a fabricated `'read'` would
|
|
217
238
|
* turn an unclassified request into an authorised one.
|
|
239
|
+
*
|
|
240
|
+
* The KEY is required even though the value may be undefined: `auth()` always
|
|
241
|
+
* sets it, and a context that simply omitted it would be indistinguishable
|
|
242
|
+
* from one that classified the request and found nothing.
|
|
218
243
|
*/
|
|
219
|
-
operation
|
|
244
|
+
operation: AccessOperation | undefined;
|
|
220
245
|
}
|
|
221
246
|
/**
|
|
222
247
|
* A consumer `access()` predicate.
|
|
223
248
|
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
249
|
+
* The second argument is ADDITIVE: JavaScript ignores extra arguments, so every
|
|
250
|
+
* pre-#202 single-argument predicate keeps working untouched. Changing the
|
|
251
|
+
* FIRST argument instead would have been the breaking form, and a predicate
|
|
252
|
+
* that can no longer identify its collection falls through to a full CRUD
|
|
253
|
+
* grant -- so the "safer" breaking change would have converted every unmigrated
|
|
254
|
+
* predicate into a fail-open.
|
|
255
|
+
*
|
|
256
|
+
* `context` is nonetheless REQUIRED in the type, and that costs back-compat
|
|
257
|
+
* nothing. TypeScript already lets a fewer-parameter implementation satisfy a
|
|
258
|
+
* more-parameter signature, so an arity-1 predicate assigns to this type
|
|
259
|
+
* cleanly -- measured under `--strict`. What the `?` bought was the opposite of
|
|
260
|
+
* safety: it silently permitted `getAccess('animal')?.(request)` at the CALL
|
|
261
|
+
* site, i.e. exactly the omission {@link AccessContext} exists to prevent, and
|
|
262
|
+
* that call gets the model-wrong answer. Required, a caller that drops the
|
|
263
|
+
* context gets `TS2554: Expected 2 arguments, but got 1`.
|
|
230
264
|
*/
|
|
231
|
-
export type AccessFunction = (request: unknown, context
|
|
265
|
+
export type AccessFunction = (request: unknown, context: AccessContext) => AccessMethod;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -27,7 +27,7 @@ import { count, avg, sum, min, max } from './aggregates.js';
|
|
|
27
27
|
export { default } from './main.js';
|
|
28
28
|
export { store, relationships } from './main.js';
|
|
29
29
|
export type { PersistErrorDetail } from './main.js';
|
|
30
|
-
export type { AccessContext, AccessFunction, AccessMethod } from './types/orm-types.js'; // access() contract (#202)
|
|
30
|
+
export type { AccessContext, AccessFunction, AccessMethod, AccessOperation } from './types/orm-types.js'; // access() contract (#202)
|
|
31
31
|
export { Model, View, Serializer }; // base classes
|
|
32
32
|
export { attr, belongsTo, hasMany, createRecord, updateRecord }; // helpers
|
|
33
33
|
export { count, avg, sum, min, max }; // aggregate helpers
|
package/src/main.ts
CHANGED
|
@@ -71,7 +71,25 @@ export default class Orm {
|
|
|
71
71
|
warnings: Set<string> = new Set();
|
|
72
72
|
|
|
73
73
|
/**
|
|
74
|
-
* Model name ->
|
|
74
|
+
* Model name -> the `access` predicate of the access class that CLAIMS that
|
|
75
|
+
* model (abofs/stonyx-orm#202).
|
|
76
|
+
*
|
|
77
|
+
* Not "that model's own predicate". One access class may claim many models
|
|
78
|
+
* -- `GlobalAccess` in this repo's fixtures declares five, and `models = '*'`
|
|
79
|
+
* claims every model in the store -- and it declares ONE `access` method, so
|
|
80
|
+
* the same function object is registered under every one of those keys.
|
|
81
|
+
* `getAccess('owner') === getAccess('animal')` is `true` there. The
|
|
82
|
+
* one-to-one guarantee below is key -> function, never function -> model,
|
|
83
|
+
* and a caller must not read a resolved predicate as being animal-specific.
|
|
84
|
+
* What makes the ANSWER model-specific is the context the caller passes and
|
|
85
|
+
* the predicate actually reading it -- see {@link Orm#getAccess}.
|
|
86
|
+
*
|
|
87
|
+
* NAMED FOR WHAT IT HOLDS. It was `accessFiles` through review, inherited
|
|
88
|
+
* from the function-local in `setup-rest-server.ts` where the values came
|
|
89
|
+
* straight out of `forEachFileImport` and "files" was defensible. The values
|
|
90
|
+
* are `AccessFunction`s, and the sibling public registries on this class
|
|
91
|
+
* (`models`, `serializers`, `views`, `transforms`) are all plural nouns of
|
|
92
|
+
* the thing held. Renamed here because #202 is the last moment it is free.
|
|
75
93
|
*
|
|
76
94
|
* Populated by `setup-rest-server.ts` at boot, from the access classes under
|
|
77
95
|
* `config.orm.paths.access`, BEFORE any route is mounted -- so it is complete
|
|
@@ -90,10 +108,13 @@ export default class Orm {
|
|
|
90
108
|
* request routed to model Y -- inexpressible, which is the capability
|
|
91
109
|
* abofs/stonyx-orm#196 and abofs/stonyx-orm#207 are built on.
|
|
92
110
|
*
|
|
93
|
-
* Empty when the REST server is disabled,
|
|
94
|
-
*
|
|
111
|
+
* Empty when the REST server is disabled, and PARTIAL when one access file
|
|
112
|
+
* failed to load (`setup-rest-server.ts` catches, warns and assigns whatever
|
|
113
|
+
* it had). So a missing key does NOT mean the model has no access class.
|
|
114
|
+
* Prefer {@link Orm#getAccess} over indexing this directly -- it is guarded
|
|
115
|
+
* against the prototype chain and this is not.
|
|
95
116
|
*/
|
|
96
|
-
|
|
117
|
+
accessFunctions: Record<string, AccessFunction> = {};
|
|
97
118
|
|
|
98
119
|
options!: OrmOptions;
|
|
99
120
|
sqlDb?: SqlDb;
|
|
@@ -240,7 +261,7 @@ export default class Orm {
|
|
|
240
261
|
* collection the request is ADDRESSED TO -- owners -- while being asked about
|
|
241
262
|
* animals, and per #202's thesis it answers wrong in the granting direction.
|
|
242
263
|
*
|
|
243
|
-
* OWN PROPERTIES ONLY. A bare `this.
|
|
264
|
+
* OWN PROPERTIES ONLY. A bare `this.accessFunctions[modelName]` walks the
|
|
244
265
|
* prototype chain, so `getAccess('constructor')` resolved `Object` and
|
|
245
266
|
* `getAccess('toString')` resolved `Object.prototype.toString` -- both
|
|
246
267
|
* callable, and the documented `predicate?.(request, ctx)` pattern then
|
|
@@ -259,9 +280,9 @@ export default class Orm {
|
|
|
259
280
|
* note above. Treat it as deny.
|
|
260
281
|
*/
|
|
261
282
|
getAccess(modelName: string): AccessFunction | undefined {
|
|
262
|
-
if (!Object.hasOwn(this.
|
|
283
|
+
if (!Object.hasOwn(this.accessFunctions, modelName)) return undefined;
|
|
263
284
|
|
|
264
|
-
return this.
|
|
285
|
+
return this.accessFunctions[modelName];
|
|
265
286
|
}
|
|
266
287
|
|
|
267
288
|
async startup(): Promise<void> {
|
package/src/orm-request.ts
CHANGED
|
@@ -122,7 +122,7 @@ import { getBeforeHooks, getAfterHooks } from './hooks.js';
|
|
|
122
122
|
import type { HookContext } from './hooks.js';
|
|
123
123
|
import config from 'stonyx/config';
|
|
124
124
|
import log from 'stonyx/log';
|
|
125
|
-
import type { OrmRecord, AccessContext, AccessFunction, AccessMethod } from './types/orm-types.js';
|
|
125
|
+
import type { OrmRecord, AccessContext, AccessFunction, AccessMethod, AccessOperation } from './types/orm-types.js';
|
|
126
126
|
import { isOrmRecord } from './utils.js';
|
|
127
127
|
|
|
128
128
|
interface OrmRequest$ extends Request {
|
|
@@ -152,7 +152,7 @@ interface JsonApiResponse {
|
|
|
152
152
|
|
|
153
153
|
type HandlerFn = (request: OrmRequest$, state: { [key: string]: unknown }) => unknown | Promise<unknown>;
|
|
154
154
|
|
|
155
|
-
const methodAccessMap: { [key: string]:
|
|
155
|
+
const methodAccessMap: { [key: string]: AccessOperation } = {
|
|
156
156
|
GET: 'read',
|
|
157
157
|
POST: 'create',
|
|
158
158
|
DELETE: 'delete',
|
package/src/setup-rest-server.ts
CHANGED
|
@@ -20,7 +20,7 @@ interface AccessInstance {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export default async function(route: string, accessPath: string, metaRoute: boolean): Promise<void> {
|
|
23
|
-
const
|
|
23
|
+
const accessFunctions: Record<string, AccessFunction> = {};
|
|
24
24
|
|
|
25
25
|
try {
|
|
26
26
|
await forEachFileImport(accessPath, (accessClass: unknown) => {
|
|
@@ -37,9 +37,9 @@ export default async function(route: string, accessPath: string, metaRoute: bool
|
|
|
37
37
|
for (const model of models === '*' ? availableModels : models) {
|
|
38
38
|
if (model === dbKey) continue;
|
|
39
39
|
if (!store.data.has(model)) throw new Error(`Unable to define access for Invalid Model "${model}". Model does not exist`);
|
|
40
|
-
if (
|
|
40
|
+
if (accessFunctions![model]) throw new Error(`Access for model "${model}" has already been defined by another access class.`);
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
accessFunctions![model] = accessInstance.access;
|
|
43
43
|
}
|
|
44
44
|
});
|
|
45
45
|
} catch (error) {
|
|
@@ -50,7 +50,7 @@ export default async function(route: string, accessPath: string, metaRoute: bool
|
|
|
50
50
|
// -------------------------------------------------------------------------
|
|
51
51
|
// #202 -- the registry has to survive this function.
|
|
52
52
|
//
|
|
53
|
-
// `
|
|
53
|
+
// `accessFunctions` used to be a function-local that was discarded at the return
|
|
54
54
|
// below, so the only thing that ever saw it was the mount loop. Each mounted
|
|
55
55
|
// OrmRequest then held its OWN model's predicate and nothing held the map, so
|
|
56
56
|
// at request time there was no route from a model NAME to that model's
|
|
@@ -68,7 +68,7 @@ export default async function(route: string, accessPath: string, metaRoute: bool
|
|
|
68
68
|
// set of predicates that is actually enforcing. A guard here that skipped the
|
|
69
69
|
// assignment would let the registry go silently missing, which is precisely
|
|
70
70
|
// the failure #202's AC8 exists to catch.
|
|
71
|
-
Orm.instance.
|
|
71
|
+
Orm.instance.accessFunctions = accessFunctions;
|
|
72
72
|
|
|
73
73
|
await waitForModule('rest-server');
|
|
74
74
|
|
|
@@ -76,7 +76,7 @@ export default async function(route: string, accessPath: string, metaRoute: bool
|
|
|
76
76
|
const name = route === '/' ? 'index' : (route[0] === '/' ? route.slice(1) : route);
|
|
77
77
|
|
|
78
78
|
// Configure endpoints for models and views with access configuration
|
|
79
|
-
for (const [model, access] of Object.entries(
|
|
79
|
+
for (const [model, access] of Object.entries(accessFunctions!)) {
|
|
80
80
|
const pluralizedModel = getPluralName(model);
|
|
81
81
|
const modelName = name === 'index' ? pluralizedModel : `${name}/${pluralizedModel}`;
|
|
82
82
|
RestServer.instance.mountRoute(OrmRequest, { name: modelName, options: { model, access } });
|
package/src/types/orm-types.ts
CHANGED
|
@@ -184,6 +184,21 @@ export interface SnapshotEntry {
|
|
|
184
184
|
*/
|
|
185
185
|
export type AccessMethod = string | boolean | string[] | ((record: unknown) => boolean);
|
|
186
186
|
|
|
187
|
+
/**
|
|
188
|
+
* The closed vocabulary `AccessContext.operation` is drawn from
|
|
189
|
+
* (abofs/stonyx-orm#202).
|
|
190
|
+
*
|
|
191
|
+
* A literal union rather than `string`, so the guarantee the prose makes is the
|
|
192
|
+
* one the compiler enforces: a consumer who writes `operation === 'GET'` or
|
|
193
|
+
* `operation === 'get'` -- the hook vocabulary, see below -- gets a compile
|
|
194
|
+
* error instead of a comparison that never matches. A predicate that stops
|
|
195
|
+
* matching falls through to the permission array, so the misreading is
|
|
196
|
+
* fail-open shaped.
|
|
197
|
+
*
|
|
198
|
+
* In-repo precedent: `PersistErrorDetail.operation` in `src/main.ts`.
|
|
199
|
+
*/
|
|
200
|
+
export type AccessOperation = 'read' | 'create' | 'update' | 'delete';
|
|
201
|
+
|
|
187
202
|
/**
|
|
188
203
|
* The structural facts about the request being authorised, handed to a consumer
|
|
189
204
|
* `access()` predicate as its SECOND argument (abofs/stonyx-orm#202).
|
|
@@ -214,28 +229,48 @@ export interface AccessContext {
|
|
|
214
229
|
model: string;
|
|
215
230
|
|
|
216
231
|
/**
|
|
217
|
-
* The operation being authorised
|
|
218
|
-
*
|
|
232
|
+
* The operation being authorised. Exactly one of the four {@link
|
|
233
|
+
* AccessOperation} verbs, or `undefined`. These are exactly the values of
|
|
219
234
|
* `methodAccessMap` in `src/orm-request.ts`, which is also what the
|
|
220
235
|
* permission-array return shape is matched against -- so the two forms cannot
|
|
221
236
|
* disagree.
|
|
222
237
|
*
|
|
238
|
+
* NOT the hook vocabulary. `HookContext.operation` (`src/hooks.ts`) carries
|
|
239
|
+
* `'list' | 'get' | 'create' | 'update' | 'delete'` on an identically-named
|
|
240
|
+
* key of an identically-shaped context object, and the access vocabulary
|
|
241
|
+
* collapses `list` and `get` into `'read'`. For one `GET /animals/1` a hook
|
|
242
|
+
* sees `'get'` and `access()` sees `'read'`. "No second vocabulary" is a
|
|
243
|
+
* statement about the ACCESS path only.
|
|
244
|
+
*
|
|
223
245
|
* `undefined` when the dispatched method has no entry in that map. Express
|
|
224
246
|
* delivers `HEAD` to the `GET` handler, so this is reachable. It is left
|
|
225
247
|
* undefined rather than defaulted on purpose: a fabricated `'read'` would
|
|
226
248
|
* turn an unclassified request into an authorised one.
|
|
249
|
+
*
|
|
250
|
+
* The KEY is required even though the value may be undefined: `auth()` always
|
|
251
|
+
* sets it, and a context that simply omitted it would be indistinguishable
|
|
252
|
+
* from one that classified the request and found nothing.
|
|
227
253
|
*/
|
|
228
|
-
operation
|
|
254
|
+
operation: AccessOperation | undefined;
|
|
229
255
|
}
|
|
230
256
|
|
|
231
257
|
/**
|
|
232
258
|
* A consumer `access()` predicate.
|
|
233
259
|
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
260
|
+
* The second argument is ADDITIVE: JavaScript ignores extra arguments, so every
|
|
261
|
+
* pre-#202 single-argument predicate keeps working untouched. Changing the
|
|
262
|
+
* FIRST argument instead would have been the breaking form, and a predicate
|
|
263
|
+
* that can no longer identify its collection falls through to a full CRUD
|
|
264
|
+
* grant -- so the "safer" breaking change would have converted every unmigrated
|
|
265
|
+
* predicate into a fail-open.
|
|
266
|
+
*
|
|
267
|
+
* `context` is nonetheless REQUIRED in the type, and that costs back-compat
|
|
268
|
+
* nothing. TypeScript already lets a fewer-parameter implementation satisfy a
|
|
269
|
+
* more-parameter signature, so an arity-1 predicate assigns to this type
|
|
270
|
+
* cleanly -- measured under `--strict`. What the `?` bought was the opposite of
|
|
271
|
+
* safety: it silently permitted `getAccess('animal')?.(request)` at the CALL
|
|
272
|
+
* site, i.e. exactly the omission {@link AccessContext} exists to prevent, and
|
|
273
|
+
* that call gets the model-wrong answer. Required, a caller that drops the
|
|
274
|
+
* context gets `TS2554: Expected 2 arguments, but got 1`.
|
|
240
275
|
*/
|
|
241
|
-
export type AccessFunction = (request: unknown, context
|
|
276
|
+
export type AccessFunction = (request: unknown, context: AccessContext) => AccessMethod;
|
|
@@ -5,7 +5,20 @@ declare module '@stonyx/rest-server' {
|
|
|
5
5
|
|
|
6
6
|
interface RouteOptions {
|
|
7
7
|
name: string;
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* `access` is the two-argument post-#202 shape. This is the THIRD place the
|
|
10
|
+
* contract is declared (`AccessInstance.access` in
|
|
11
|
+
* `src/setup-rest-server.ts` and `OrmRequest.access` in
|
|
12
|
+
* `src/orm-request.ts` are the other two) and it is the one `mountRoute` is
|
|
13
|
+
* actually called through, at `src/setup-rest-server.ts`. It kept the
|
|
14
|
+
* pre-#202 single-argument signature after the other two migrated; the
|
|
15
|
+
* union with `Record<string, unknown>` meant nothing broke, which is
|
|
16
|
+
* exactly why it would have drifted silently.
|
|
17
|
+
*
|
|
18
|
+
* Spelled structurally rather than as `AccessFunction`: an ambient
|
|
19
|
+
* `declare module` block cannot carry an `import type`.
|
|
20
|
+
*/
|
|
21
|
+
options?: { model: string; access: (request: unknown, context: { model: string; operation: string | undefined }) => unknown } | Record<string, unknown>;
|
|
9
22
|
}
|
|
10
23
|
|
|
11
24
|
export default class RestServer {
|