@molecule/api-resource-version-history 1.0.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/LICENSE +115 -0
- package/dist/authorizers/index.d.ts +24 -0
- package/dist/authorizers/index.d.ts.map +1 -0
- package/dist/authorizers/index.js +23 -0
- package/dist/authorizers/ownership.d.ts +77 -0
- package/dist/authorizers/ownership.d.ts.map +1 -0
- package/dist/authorizers/ownership.js +112 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +18 -0
- package/dist/diff.d.ts +33 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +110 -0
- package/dist/handlers/count.d.ts +17 -0
- package/dist/handlers/count.d.ts.map +1 -0
- package/dist/handlers/count.js +58 -0
- package/dist/handlers/create.d.ts +19 -0
- package/dist/handlers/create.d.ts.map +1 -0
- package/dist/handlers/create.js +73 -0
- package/dist/handlers/diff.d.ts +19 -0
- package/dist/handlers/diff.d.ts.map +1 -0
- package/dist/handlers/diff.js +70 -0
- package/dist/handlers/index.d.ts +8 -0
- package/dist/handlers/index.d.ts.map +1 -0
- package/dist/handlers/index.js +7 -0
- package/dist/handlers/list.d.ts +18 -0
- package/dist/handlers/list.d.ts.map +1 -0
- package/dist/handlers/list.js +61 -0
- package/dist/handlers/read.d.ts +20 -0
- package/dist/handlers/read.d.ts.map +1 -0
- package/dist/handlers/read.js +68 -0
- package/dist/handlers/readByNumber.d.ts +17 -0
- package/dist/handlers/readByNumber.d.ts.map +1 -0
- package/dist/handlers/readByNumber.js +80 -0
- package/dist/handlers/restore.d.ts +20 -0
- package/dist/handlers/restore.d.ts.map +1 -0
- package/dist/handlers/restore.js +84 -0
- package/dist/i18n.d.ts +10 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +17 -0
- package/dist/index.d.ts +130 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +129 -0
- package/dist/registry.d.ts +79 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +65 -0
- package/dist/requestHandlerMap.d.ts +25 -0
- package/dist/requestHandlerMap.d.ts.map +1 -0
- package/dist/requestHandlerMap.js +24 -0
- package/dist/routes.d.ts +67 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/routes.js +69 -0
- package/dist/service.d.ts +107 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +193 -0
- package/dist/types.d.ts +106 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +11 -0
- package/dist/validation.d.ts +21 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +34 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version-history authorizers.
|
|
3
|
+
*
|
|
4
|
+
* The version-history store is polymorphic — it captures snapshots of any
|
|
5
|
+
* resource type and owns no permission model of its own — so access control is
|
|
6
|
+
* **pluggable and fail-closed**, not open. Every route requires an
|
|
7
|
+
* authenticated session (see `routes.ts`) AND its handler calls
|
|
8
|
+
* {@link isVersionAuthorized}, which re-derives the caller from
|
|
9
|
+
* `res.locals.session.userId` and dispatches to the app-registered
|
|
10
|
+
* {@link VersionOwnershipResolver} for the parent `resourceType`
|
|
11
|
+
* (`registerOwnershipResolver`). With no resolver registered the caller is
|
|
12
|
+
* denied (404, no existence leak), so mounting the raw routes never exposes
|
|
13
|
+
* another tenant's snapshots.
|
|
14
|
+
*
|
|
15
|
+
* Cross-tenant inspection/restore (an admin/compliance console) is **opt-in**:
|
|
16
|
+
* compose {@link versionHistoryAdmin} onto a route to widen an
|
|
17
|
+
* authenticated *admin* to all users' versions. Apps with a finer-grained
|
|
18
|
+
* access model may instead supply their own widening middleware that sets
|
|
19
|
+
* `res.locals.versionHistoryAdmin` after its own check.
|
|
20
|
+
*
|
|
21
|
+
* @module
|
|
22
|
+
*/
|
|
23
|
+
export * from './ownership.js';
|
|
24
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/authorizers/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,cAAc,gBAAgB,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version-history authorizers.
|
|
3
|
+
*
|
|
4
|
+
* The version-history store is polymorphic — it captures snapshots of any
|
|
5
|
+
* resource type and owns no permission model of its own — so access control is
|
|
6
|
+
* **pluggable and fail-closed**, not open. Every route requires an
|
|
7
|
+
* authenticated session (see `routes.ts`) AND its handler calls
|
|
8
|
+
* {@link isVersionAuthorized}, which re-derives the caller from
|
|
9
|
+
* `res.locals.session.userId` and dispatches to the app-registered
|
|
10
|
+
* {@link VersionOwnershipResolver} for the parent `resourceType`
|
|
11
|
+
* (`registerOwnershipResolver`). With no resolver registered the caller is
|
|
12
|
+
* denied (404, no existence leak), so mounting the raw routes never exposes
|
|
13
|
+
* another tenant's snapshots.
|
|
14
|
+
*
|
|
15
|
+
* Cross-tenant inspection/restore (an admin/compliance console) is **opt-in**:
|
|
16
|
+
* compose {@link versionHistoryAdmin} onto a route to widen an
|
|
17
|
+
* authenticated *admin* to all users' versions. Apps with a finer-grained
|
|
18
|
+
* access model may instead supply their own widening middleware that sets
|
|
19
|
+
* `res.locals.versionHistoryAdmin` after its own check.
|
|
20
|
+
*
|
|
21
|
+
* @module
|
|
22
|
+
*/
|
|
23
|
+
export * from './ownership.js';
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parent-resource ownership authorizer for the version-history routes.
|
|
3
|
+
*
|
|
4
|
+
* The version-history store is polymorphic (snapshots of *any* resource type)
|
|
5
|
+
* and append-only, so it owns no permission model itself. {@link isVersionAuthorized}
|
|
6
|
+
* is the in-handler gate every route calls after re-deriving the caller from
|
|
7
|
+
* `res.locals.session.userId`: it dispatches to the app-registered
|
|
8
|
+
* {@link VersionOwnershipResolver} for the parent `resourceType` and is
|
|
9
|
+
* **fail-closed** — with no registered resolver it denies, so mounting the raw
|
|
10
|
+
* routes never silently exposes another tenant's snapshots.
|
|
11
|
+
*
|
|
12
|
+
* Cross-tenant access (an admin/compliance console that may read or restore any
|
|
13
|
+
* user's versions) is **opt-in**: compose {@link versionHistoryAdmin} onto a
|
|
14
|
+
* route to widen an authenticated *admin* by setting `res.locals.versionHistoryAdmin`,
|
|
15
|
+
* which bypasses the resolver. It never blocks — a non-admin (or anonymous)
|
|
16
|
+
* caller passes through unchanged and stays subject to the resolver, so
|
|
17
|
+
* composing it can only widen for admins, never open the endpoint.
|
|
18
|
+
*
|
|
19
|
+
* @module
|
|
20
|
+
*/
|
|
21
|
+
import type { MoleculeRequestHandler, MoleculeResponse } from '@molecule/api-resource';
|
|
22
|
+
import { type VersionOwnershipContext } from '../registry.js';
|
|
23
|
+
/**
|
|
24
|
+
* Permission action describing version-history administration, e.g. for an
|
|
25
|
+
* app's own `@molecule/api-permissions` wiring.
|
|
26
|
+
*/
|
|
27
|
+
export declare const VERSION_HISTORY_PERMISSION_ACTION = "manage";
|
|
28
|
+
/**
|
|
29
|
+
* Permission resource describing version-history administration, e.g. for an
|
|
30
|
+
* app's own `@molecule/api-permissions` wiring.
|
|
31
|
+
*/
|
|
32
|
+
export declare const VERSION_HISTORY_PERMISSION_RESOURCE = "versionHistory";
|
|
33
|
+
/**
|
|
34
|
+
* Session-claim permission string (`'versionHistory:manage'`) that, when
|
|
35
|
+
* present in a session's `permissions` array, marks the caller as a
|
|
36
|
+
* version-history admin.
|
|
37
|
+
*/
|
|
38
|
+
export declare const VERSION_HISTORY_ADMIN_PERMISSION = "versionHistory:manage";
|
|
39
|
+
/**
|
|
40
|
+
* Authorizes whether the authenticated caller may access versions of a parent
|
|
41
|
+
* resource. Admins widened by {@link versionHistoryAdmin} are always allowed;
|
|
42
|
+
* otherwise the app-registered {@link VersionOwnershipResolver} for the parent
|
|
43
|
+
* `resourceType` decides. **Fail-closed:** when no resolver is registered the
|
|
44
|
+
* caller is denied, so the polymorphic version store never leaks a snapshot it
|
|
45
|
+
* cannot prove the caller owns.
|
|
46
|
+
*
|
|
47
|
+
* @param res - The response whose `locals.session`/`locals.versionHistoryAdmin`
|
|
48
|
+
* is inspected.
|
|
49
|
+
* @param context - The {@link VersionOwnershipContext} (parent resource + caller).
|
|
50
|
+
* @returns `true` when the caller may access the parent resource's versions.
|
|
51
|
+
*/
|
|
52
|
+
export declare function isVersionAuthorized(res: MoleculeResponse, context: VersionOwnershipContext): Promise<boolean>;
|
|
53
|
+
/**
|
|
54
|
+
* Resolves whether the current request's session belongs to an actor
|
|
55
|
+
* authorized to administer version history (read/restore any user's versions).
|
|
56
|
+
* Fail-closed: returns `false` when there is no authenticated session, and
|
|
57
|
+
* otherwise only `true` when the session carries an admin claim — `isAdmin ===
|
|
58
|
+
* true`, `role === 'admin'`, `roles` containing `'admin'`, or `permissions`
|
|
59
|
+
* containing `'admin'` / `'versionHistory:manage'`.
|
|
60
|
+
*
|
|
61
|
+
* @param res - The response whose `locals.session` is inspected.
|
|
62
|
+
* @returns `true` when the session is an authorized version-history admin.
|
|
63
|
+
*/
|
|
64
|
+
export declare function isVersionHistoryAdmin(res: MoleculeResponse): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Opt-in route middleware that *widens* an authenticated admin to cross-tenant
|
|
67
|
+
* version access by setting `res.locals.versionHistoryAdmin = true`. It never
|
|
68
|
+
* blocks: a non-admin (or anonymous) caller passes through unchanged and
|
|
69
|
+
* remains subject to the ownership resolver in the handlers, so composing this
|
|
70
|
+
* onto a route can only widen for admins, never open the endpoint. Wire it onto
|
|
71
|
+
* a dedicated admin route when a support/compliance console needs to read or
|
|
72
|
+
* restore every user's versions.
|
|
73
|
+
*
|
|
74
|
+
* @returns An Express-compatible middleware function.
|
|
75
|
+
*/
|
|
76
|
+
export declare const versionHistoryAdmin: () => MoleculeRequestHandler;
|
|
77
|
+
//# sourceMappingURL=ownership.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ownership.d.ts","sourceRoot":"","sources":["../../src/authorizers/ownership.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAEtF,OAAO,EAAwB,KAAK,uBAAuB,EAAE,MAAM,gBAAgB,CAAA;AAEnF;;;GAGG;AACH,eAAO,MAAM,iCAAiC,WAAW,CAAA;AAEzD;;;GAGG;AACH,eAAO,MAAM,mCAAmC,mBAAmB,CAAA;AAEnE;;;;GAIG;AACH,eAAO,MAAM,gCAAgC,0BAAgF,CAAA;AAqB7H;;;;;;;;;;;;GAYG;AACH,wBAAsB,mBAAmB,CACvC,GAAG,EAAE,gBAAgB,EACrB,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,OAAO,CAAC,CAYlB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAsBpE;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mBAAmB,QAC1B,sBAMH,CAAA"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parent-resource ownership authorizer for the version-history routes.
|
|
3
|
+
*
|
|
4
|
+
* The version-history store is polymorphic (snapshots of *any* resource type)
|
|
5
|
+
* and append-only, so it owns no permission model itself. {@link isVersionAuthorized}
|
|
6
|
+
* is the in-handler gate every route calls after re-deriving the caller from
|
|
7
|
+
* `res.locals.session.userId`: it dispatches to the app-registered
|
|
8
|
+
* {@link VersionOwnershipResolver} for the parent `resourceType` and is
|
|
9
|
+
* **fail-closed** — with no registered resolver it denies, so mounting the raw
|
|
10
|
+
* routes never silently exposes another tenant's snapshots.
|
|
11
|
+
*
|
|
12
|
+
* Cross-tenant access (an admin/compliance console that may read or restore any
|
|
13
|
+
* user's versions) is **opt-in**: compose {@link versionHistoryAdmin} onto a
|
|
14
|
+
* route to widen an authenticated *admin* by setting `res.locals.versionHistoryAdmin`,
|
|
15
|
+
* which bypasses the resolver. It never blocks — a non-admin (or anonymous)
|
|
16
|
+
* caller passes through unchanged and stays subject to the resolver, so
|
|
17
|
+
* composing it can only widen for admins, never open the endpoint.
|
|
18
|
+
*
|
|
19
|
+
* @module
|
|
20
|
+
*/
|
|
21
|
+
import { getOwnershipResolver } from '../registry.js';
|
|
22
|
+
/**
|
|
23
|
+
* Permission action describing version-history administration, e.g. for an
|
|
24
|
+
* app's own `@molecule/api-permissions` wiring.
|
|
25
|
+
*/
|
|
26
|
+
export const VERSION_HISTORY_PERMISSION_ACTION = 'manage';
|
|
27
|
+
/**
|
|
28
|
+
* Permission resource describing version-history administration, e.g. for an
|
|
29
|
+
* app's own `@molecule/api-permissions` wiring.
|
|
30
|
+
*/
|
|
31
|
+
export const VERSION_HISTORY_PERMISSION_RESOURCE = 'versionHistory';
|
|
32
|
+
/**
|
|
33
|
+
* Session-claim permission string (`'versionHistory:manage'`) that, when
|
|
34
|
+
* present in a session's `permissions` array, marks the caller as a
|
|
35
|
+
* version-history admin.
|
|
36
|
+
*/
|
|
37
|
+
export const VERSION_HISTORY_ADMIN_PERMISSION = `${VERSION_HISTORY_PERMISSION_RESOURCE}:${VERSION_HISTORY_PERMISSION_ACTION}`;
|
|
38
|
+
/**
|
|
39
|
+
* Authorizes whether the authenticated caller may access versions of a parent
|
|
40
|
+
* resource. Admins widened by {@link versionHistoryAdmin} are always allowed;
|
|
41
|
+
* otherwise the app-registered {@link VersionOwnershipResolver} for the parent
|
|
42
|
+
* `resourceType` decides. **Fail-closed:** when no resolver is registered the
|
|
43
|
+
* caller is denied, so the polymorphic version store never leaks a snapshot it
|
|
44
|
+
* cannot prove the caller owns.
|
|
45
|
+
*
|
|
46
|
+
* @param res - The response whose `locals.session`/`locals.versionHistoryAdmin`
|
|
47
|
+
* is inspected.
|
|
48
|
+
* @param context - The {@link VersionOwnershipContext} (parent resource + caller).
|
|
49
|
+
* @returns `true` when the caller may access the parent resource's versions.
|
|
50
|
+
*/
|
|
51
|
+
export async function isVersionAuthorized(res, context) {
|
|
52
|
+
if (res.locals.versionHistoryAdmin === true) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
const resolver = getOwnershipResolver(context.resourceType);
|
|
56
|
+
if (!resolver) {
|
|
57
|
+
// Fail-closed: with no registered ownership resolver for this resource
|
|
58
|
+
// type the polymorphic version store cannot prove the caller owns the
|
|
59
|
+
// parent resource, so it must deny rather than leak snapshots.
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return resolver(context);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Resolves whether the current request's session belongs to an actor
|
|
66
|
+
* authorized to administer version history (read/restore any user's versions).
|
|
67
|
+
* Fail-closed: returns `false` when there is no authenticated session, and
|
|
68
|
+
* otherwise only `true` when the session carries an admin claim — `isAdmin ===
|
|
69
|
+
* true`, `role === 'admin'`, `roles` containing `'admin'`, or `permissions`
|
|
70
|
+
* containing `'admin'` / `'versionHistory:manage'`.
|
|
71
|
+
*
|
|
72
|
+
* @param res - The response whose `locals.session` is inspected.
|
|
73
|
+
* @returns `true` when the session is an authorized version-history admin.
|
|
74
|
+
*/
|
|
75
|
+
export function isVersionHistoryAdmin(res) {
|
|
76
|
+
const session = res.locals.session;
|
|
77
|
+
if (!session?.userId) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
if (session.isAdmin === true) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
if (session.role === 'admin') {
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
if (Array.isArray(session.roles) && session.roles.includes('admin')) {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
if (Array.isArray(session.permissions) &&
|
|
90
|
+
(session.permissions.includes('admin') ||
|
|
91
|
+
session.permissions.includes(VERSION_HISTORY_ADMIN_PERMISSION))) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Opt-in route middleware that *widens* an authenticated admin to cross-tenant
|
|
98
|
+
* version access by setting `res.locals.versionHistoryAdmin = true`. It never
|
|
99
|
+
* blocks: a non-admin (or anonymous) caller passes through unchanged and
|
|
100
|
+
* remains subject to the ownership resolver in the handlers, so composing this
|
|
101
|
+
* onto a route can only widen for admins, never open the endpoint. Wire it onto
|
|
102
|
+
* a dedicated admin route when a support/compliance console needs to read or
|
|
103
|
+
* restore every user's versions.
|
|
104
|
+
*
|
|
105
|
+
* @returns An Express-compatible middleware function.
|
|
106
|
+
*/
|
|
107
|
+
export const versionHistoryAdmin = () => (_req, res, next) => {
|
|
108
|
+
if (isVersionHistoryAdmin(res)) {
|
|
109
|
+
res.locals.versionHistoryAdmin = true;
|
|
110
|
+
}
|
|
111
|
+
next();
|
|
112
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-resource-version-history` is SERVER-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
|
|
5
|
+
* Evaluating a server package in a browser bundle is always an import-graph mistake
|
|
6
|
+
* (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
|
|
7
|
+
* ("Buffer is not defined") far from the culprit. Throwing here names the package and
|
|
8
|
+
* the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
|
|
9
|
+
* unaffected: the throw requires browser globals AND the absence of a node runtime.
|
|
10
|
+
*/
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
|
|
13
|
+
throw new Error('@molecule/api-resource-version-history is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
|
|
14
|
+
'from server code (a server route/function or your API), or dynamic-import it inside ' +
|
|
15
|
+
'the server handler — never from components or shared client modules, and never ' +
|
|
16
|
+
'polyfill Buffer/process to silence this.');
|
|
17
|
+
}
|
|
18
|
+
export {};
|
package/dist/diff.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure shallow-diff helper for version snapshots.
|
|
3
|
+
*
|
|
4
|
+
* Compares two object-shaped snapshots key-by-key and emits a
|
|
5
|
+
* {@link VersionChanges} record describing only the keys whose values
|
|
6
|
+
* differ. Non-object snapshots (primitives, arrays at the top level) are
|
|
7
|
+
* supported via a single synthetic `'$value'` key.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import type { JSONValue, VersionChanges } from './types.js';
|
|
12
|
+
/**
|
|
13
|
+
* Returns `true` if `a` and `b` are deeply equal as JSON values.
|
|
14
|
+
*
|
|
15
|
+
* @param a - First value to compare.
|
|
16
|
+
* @param b - Second value to compare.
|
|
17
|
+
* @returns Whether the two values are structurally equal.
|
|
18
|
+
*/
|
|
19
|
+
export declare function jsonEqual(a: JSONValue | undefined, b: JSONValue | undefined): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Computes a shallow per-field diff between two JSON snapshots.
|
|
22
|
+
*
|
|
23
|
+
* For object snapshots, every key present in either snapshot is examined;
|
|
24
|
+
* keys whose values differ (by deep equality) are included in the result.
|
|
25
|
+
* For non-object snapshots, the entire value is reported under the
|
|
26
|
+
* synthetic key `$value`.
|
|
27
|
+
*
|
|
28
|
+
* @param before - The previous snapshot, or `null` if this is the first version.
|
|
29
|
+
* @param after - The new snapshot.
|
|
30
|
+
* @returns A {@link VersionChanges} record. Empty when the snapshots are equal.
|
|
31
|
+
*/
|
|
32
|
+
export declare function diffSnapshots(before: JSONValue | null, after: JSONValue): VersionChanges;
|
|
33
|
+
//# sourceMappingURL=diff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../src/diff.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAsB,MAAM,YAAY,CAAA;AAE/E;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,SAAS,GAAG,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,SAAS,GAAG,OAAO,CA0BrF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,EAAE,KAAK,EAAE,SAAS,GAAG,cAAc,CAqCxF"}
|
package/dist/diff.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure shallow-diff helper for version snapshots.
|
|
3
|
+
*
|
|
4
|
+
* Compares two object-shaped snapshots key-by-key and emits a
|
|
5
|
+
* {@link VersionChanges} record describing only the keys whose values
|
|
6
|
+
* differ. Non-object snapshots (primitives, arrays at the top level) are
|
|
7
|
+
* supported via a single synthetic `'$value'` key.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Returns `true` if `a` and `b` are deeply equal as JSON values.
|
|
13
|
+
*
|
|
14
|
+
* @param a - First value to compare.
|
|
15
|
+
* @param b - Second value to compare.
|
|
16
|
+
* @returns Whether the two values are structurally equal.
|
|
17
|
+
*/
|
|
18
|
+
export function jsonEqual(a, b) {
|
|
19
|
+
if (a === b)
|
|
20
|
+
return true;
|
|
21
|
+
if (a === undefined || b === undefined)
|
|
22
|
+
return false;
|
|
23
|
+
if (a === null || b === null)
|
|
24
|
+
return a === b;
|
|
25
|
+
if (typeof a !== typeof b)
|
|
26
|
+
return false;
|
|
27
|
+
if (typeof a !== 'object')
|
|
28
|
+
return false;
|
|
29
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
30
|
+
if (!Array.isArray(a) || !Array.isArray(b))
|
|
31
|
+
return false;
|
|
32
|
+
if (a.length !== b.length)
|
|
33
|
+
return false;
|
|
34
|
+
for (let i = 0; i < a.length; i++) {
|
|
35
|
+
if (!jsonEqual(a[i], b[i]))
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
const aRecord = a;
|
|
41
|
+
const bRecord = b;
|
|
42
|
+
const aKeys = Object.keys(aRecord);
|
|
43
|
+
const bKeys = Object.keys(bRecord);
|
|
44
|
+
if (aKeys.length !== bKeys.length)
|
|
45
|
+
return false;
|
|
46
|
+
for (const key of aKeys) {
|
|
47
|
+
if (!Object.prototype.hasOwnProperty.call(bRecord, key))
|
|
48
|
+
return false;
|
|
49
|
+
if (!jsonEqual(aRecord[key], bRecord[key]))
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Computes a shallow per-field diff between two JSON snapshots.
|
|
56
|
+
*
|
|
57
|
+
* For object snapshots, every key present in either snapshot is examined;
|
|
58
|
+
* keys whose values differ (by deep equality) are included in the result.
|
|
59
|
+
* For non-object snapshots, the entire value is reported under the
|
|
60
|
+
* synthetic key `$value`.
|
|
61
|
+
*
|
|
62
|
+
* @param before - The previous snapshot, or `null` if this is the first version.
|
|
63
|
+
* @param after - The new snapshot.
|
|
64
|
+
* @returns A {@link VersionChanges} record. Empty when the snapshots are equal.
|
|
65
|
+
*/
|
|
66
|
+
export function diffSnapshots(before, after) {
|
|
67
|
+
const changes = {};
|
|
68
|
+
if (before === null) {
|
|
69
|
+
if (isPlainObject(after)) {
|
|
70
|
+
for (const [key, value] of Object.entries(after)) {
|
|
71
|
+
changes[key] = { after: value };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
changes.$value = { after };
|
|
76
|
+
}
|
|
77
|
+
return changes;
|
|
78
|
+
}
|
|
79
|
+
const beforeIsObject = isPlainObject(before);
|
|
80
|
+
const afterIsObject = isPlainObject(after);
|
|
81
|
+
if (!beforeIsObject || !afterIsObject) {
|
|
82
|
+
if (!jsonEqual(before, after)) {
|
|
83
|
+
changes.$value = { before, after };
|
|
84
|
+
}
|
|
85
|
+
return changes;
|
|
86
|
+
}
|
|
87
|
+
const keys = new Set([...Object.keys(before), ...Object.keys(after)]);
|
|
88
|
+
for (const key of keys) {
|
|
89
|
+
const beforeValue = before[key];
|
|
90
|
+
const afterValue = after[key];
|
|
91
|
+
if (jsonEqual(beforeValue, afterValue))
|
|
92
|
+
continue;
|
|
93
|
+
const change = {};
|
|
94
|
+
if (beforeValue !== undefined)
|
|
95
|
+
change.before = beforeValue;
|
|
96
|
+
if (afterValue !== undefined)
|
|
97
|
+
change.after = afterValue;
|
|
98
|
+
changes[key] = change;
|
|
99
|
+
}
|
|
100
|
+
return changes;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Type-guard for a plain JSON object (record, not array, not null).
|
|
104
|
+
*
|
|
105
|
+
* @param value - The value to check.
|
|
106
|
+
* @returns Whether the value is a plain JSON object.
|
|
107
|
+
*/
|
|
108
|
+
function isPlainObject(value) {
|
|
109
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
110
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Count versions handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Returns the total number of versions for a resource.
|
|
9
|
+
*
|
|
10
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
11
|
+
* when the caller is not authorized for the parent resource.
|
|
12
|
+
*
|
|
13
|
+
* @param req - The request with `resourceType` and `resourceId` params.
|
|
14
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
15
|
+
*/
|
|
16
|
+
export declare function versionCount(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
17
|
+
//# sourceMappingURL=count.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"count.d.ts","sourceRoot":"","sources":["../../src/handlers/count.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAK/E;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAyC7F"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Count versions handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { t } from '@molecule/api-i18n';
|
|
7
|
+
import { logger } from '@molecule/api-logger';
|
|
8
|
+
import { isVersionAuthorized } from '../authorizers/ownership.js';
|
|
9
|
+
import { getVersionCount } from '../service.js';
|
|
10
|
+
/**
|
|
11
|
+
* Returns the total number of versions for a resource.
|
|
12
|
+
*
|
|
13
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
14
|
+
* when the caller is not authorized for the parent resource.
|
|
15
|
+
*
|
|
16
|
+
* @param req - The request with `resourceType` and `resourceId` params.
|
|
17
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
18
|
+
*/
|
|
19
|
+
export async function versionCount(req, res) {
|
|
20
|
+
const userId = res.locals.session?.userId;
|
|
21
|
+
if (!userId) {
|
|
22
|
+
res.status(401).json({
|
|
23
|
+
error: t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }),
|
|
24
|
+
errorKey: 'resource.error.unauthorized',
|
|
25
|
+
});
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const { resourceType, resourceId } = req.params;
|
|
29
|
+
if (!resourceType || !resourceId) {
|
|
30
|
+
res.status(400).json({
|
|
31
|
+
error: t('versionHistory.error.missingResource', undefined, {
|
|
32
|
+
defaultValue: 'Resource type and ID are required',
|
|
33
|
+
}),
|
|
34
|
+
errorKey: 'versionHistory.error.missingResource',
|
|
35
|
+
});
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (!(await isVersionAuthorized(res, { resourceType, resourceId, userId }))) {
|
|
39
|
+
res.status(404).json({
|
|
40
|
+
error: t('versionHistory.error.notFound', undefined, { defaultValue: 'Version not found' }),
|
|
41
|
+
errorKey: 'versionHistory.error.notFound',
|
|
42
|
+
});
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
const total = await getVersionCount(resourceType, resourceId);
|
|
47
|
+
res.json({ total });
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
logger.error('Failed to count versions', { resourceType, resourceId, error });
|
|
51
|
+
res.status(500).json({
|
|
52
|
+
error: t('versionHistory.error.countFailed', undefined, {
|
|
53
|
+
defaultValue: 'Failed to count versions',
|
|
54
|
+
}),
|
|
55
|
+
errorKey: 'versionHistory.error.countFailed',
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create version handler.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Captures a new version of a resource.
|
|
9
|
+
*
|
|
10
|
+
* Secure by default: returns 401 with no session, and 404 (no existence leak)
|
|
11
|
+
* when the caller is not authorized for the parent resource — a caller can only
|
|
12
|
+
* capture versions of a resource they own, never inject snapshots into another
|
|
13
|
+
* tenant's resource.
|
|
14
|
+
*
|
|
15
|
+
* @param req - The request with `resourceType` / `resourceId` params and a snapshot body.
|
|
16
|
+
* @param res - The response object (reads `locals.session`/`locals.versionHistoryAdmin`).
|
|
17
|
+
*/
|
|
18
|
+
export declare function create(req: MoleculeRequest, res: MoleculeResponse): Promise<void>;
|
|
19
|
+
//# sourceMappingURL=create.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../src/handlers/create.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAM/E;;;;;;;;;;GAUG;AACH,wBAAsB,MAAM,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAsDvF"}
|