@molecule/api-resource-status-page 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 +79 -0
- package/dist/authorizers/index.d.ts.map +1 -0
- package/dist/authorizers/index.js +158 -0
- package/dist/authorizers/index.js.map +1 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +19 -0
- package/dist/browser-guard.js.map +1 -0
- package/dist/handlers/createIncident.d.ts +44 -0
- package/dist/handlers/createIncident.d.ts.map +1 -0
- package/dist/handlers/createIncident.js +73 -0
- package/dist/handlers/createIncident.js.map +1 -0
- package/dist/handlers/createService.d.ts +44 -0
- package/dist/handlers/createService.d.ts.map +1 -0
- package/dist/handlers/createService.js +70 -0
- package/dist/handlers/createService.js.map +1 -0
- package/dist/handlers/deleteService.d.ts +34 -0
- package/dist/handlers/deleteService.d.ts.map +1 -0
- package/dist/handlers/deleteService.js +64 -0
- package/dist/handlers/deleteService.js.map +1 -0
- package/dist/handlers/getService.d.ts +52 -0
- package/dist/handlers/getService.d.ts.map +1 -0
- package/dist/handlers/getService.js +58 -0
- package/dist/handlers/getService.js.map +1 -0
- package/dist/handlers/getStatus.d.ts +48 -0
- package/dist/handlers/getStatus.d.ts.map +1 -0
- package/dist/handlers/getStatus.js +76 -0
- package/dist/handlers/getStatus.js.map +1 -0
- package/dist/handlers/getUptime.d.ts +35 -0
- package/dist/handlers/getUptime.d.ts.map +1 -0
- package/dist/handlers/getUptime.js +47 -0
- package/dist/handlers/getUptime.js.map +1 -0
- package/dist/handlers/index.d.ts +11 -0
- package/dist/handlers/index.d.ts.map +1 -0
- package/dist/handlers/index.js +11 -0
- package/dist/handlers/index.js.map +1 -0
- package/dist/handlers/listIncidents.d.ts +39 -0
- package/dist/handlers/listIncidents.d.ts.map +1 -0
- package/dist/handlers/listIncidents.js +51 -0
- package/dist/handlers/listIncidents.js.map +1 -0
- package/dist/handlers/listServices.d.ts +47 -0
- package/dist/handlers/listServices.d.ts.map +1 -0
- package/dist/handlers/listServices.js +51 -0
- package/dist/handlers/listServices.js.map +1 -0
- package/dist/handlers/updateIncident.d.ts +44 -0
- package/dist/handlers/updateIncident.d.ts.map +1 -0
- package/dist/handlers/updateIncident.js +87 -0
- package/dist/handlers/updateIncident.js.map +1 -0
- package/dist/handlers/updateService.d.ts +44 -0
- package/dist/handlers/updateService.d.ts.map +1 -0
- package/dist/handlers/updateService.js +82 -0
- package/dist/handlers/updateService.js.map +1 -0
- package/dist/i18n.d.ts +14 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +22 -0
- package/dist/i18n.js.map +1 -0
- package/dist/index.d.ts +95 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +95 -0
- package/dist/index.js.map +1 -0
- package/dist/requestHandlerMap.d.ts +25 -0
- package/dist/requestHandlerMap.d.ts.map +1 -0
- package/dist/requestHandlerMap.js +37 -0
- package/dist/requestHandlerMap.js.map +1 -0
- package/dist/resource.d.ts +17 -0
- package/dist/resource.d.ts.map +1 -0
- package/dist/resource.js +21 -0
- package/dist/resource.js.map +1 -0
- package/dist/routes.d.ts +39 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/routes.js +67 -0
- package/dist/routes.js.map +1 -0
- package/dist/schema.d.ts +199 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +140 -0
- package/dist/schema.js.map +1 -0
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +76 -0
- package/setup/checks.sql +12 -0
- package/setup/incidents.sql +16 -0
- package/setup/services.sql +15 -0
- package/setup/uptimeWindows.sql +11 -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,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Status page authorizers.
|
|
3
|
+
*
|
|
4
|
+
* The status page is a **public** surface — anyone may read `/status`,
|
|
5
|
+
* `/status/services`, `/status/incidents`, and `/status/uptime`. But the
|
|
6
|
+
* mutating routes (`POST`/`PATCH`/`DELETE /status/services`, `POST`/`PATCH
|
|
7
|
+
* /status/incidents`) must be locked down: an open mutation surface lets any
|
|
8
|
+
* caller deface the public status page (fabricate outages, delete services,
|
|
9
|
+
* post fake incidents). There is no per-row owner here — a service/incident is
|
|
10
|
+
* platform-operations data — so the secure-by-default model is a **role gate**:
|
|
11
|
+
* mutating the status page is restricted to a status-management authority
|
|
12
|
+
* (operator / admin). The gate is enforced in two independent layers so it can
|
|
13
|
+
* never silently regress:
|
|
14
|
+
*
|
|
15
|
+
* 1. {@link requireAdmin} — a route middleware referenced from `routes.ts`. It is
|
|
16
|
+
* a key in the request handler map, so the mlcl injector's route scanner
|
|
17
|
+
* preserves it (a bare `'auth'` middleware string is silently dropped because
|
|
18
|
+
* it isn't a handler-map key — which is exactly why the previous gate was
|
|
19
|
+
* inert and the mutating routes shipped PUBLIC).
|
|
20
|
+
* 2. {@link isStatusAdmin} — called at the top of every mutation handler, so the
|
|
21
|
+
* gate holds even when the resource is wired without the middleware
|
|
22
|
+
* (`requestHandlerMap[route.handler]` only). Fail-closed.
|
|
23
|
+
*
|
|
24
|
+
* Apps grant operators the `manage status` permission via
|
|
25
|
+
* `@molecule/api-permissions` (or set an admin session claim) — that is the
|
|
26
|
+
* extension point for a richer operator-role model.
|
|
27
|
+
*
|
|
28
|
+
* "Admin" is resolved fail-closed via {@link isStatusAdmin}: an explicit admin
|
|
29
|
+
* claim on the session, **or** an `@molecule/api-permissions` grant. With neither,
|
|
30
|
+
* access is denied — the app developer must define who may administer the status
|
|
31
|
+
* page.
|
|
32
|
+
*
|
|
33
|
+
* @module
|
|
34
|
+
*/
|
|
35
|
+
import type { MoleculeRequestHandler, MoleculeResponse } from '@molecule/api-resource';
|
|
36
|
+
/**
|
|
37
|
+
* Permission action checked against `@molecule/api-permissions` for status page
|
|
38
|
+
* administration.
|
|
39
|
+
*/
|
|
40
|
+
export declare const STATUS_PERMISSION_ACTION = "manage";
|
|
41
|
+
/**
|
|
42
|
+
* Permission resource checked against `@molecule/api-permissions` for status page
|
|
43
|
+
* administration.
|
|
44
|
+
*/
|
|
45
|
+
export declare const STATUS_PERMISSION_RESOURCE = "status";
|
|
46
|
+
/**
|
|
47
|
+
* Session-claim permission string (`'status:manage'`) that, when present in a
|
|
48
|
+
* session's `permissions` array, grants status page administration without a
|
|
49
|
+
* bonded permissions provider.
|
|
50
|
+
*/
|
|
51
|
+
export declare const STATUS_ADMIN_PERMISSION = "status:manage";
|
|
52
|
+
/**
|
|
53
|
+
* Resolves whether the current request's session belongs to an actor authorized
|
|
54
|
+
* to administer the status page (create/update/delete services + incidents).
|
|
55
|
+
* Fail-closed: returns `false` when there is no authenticated session, and
|
|
56
|
+
* otherwise only `true` when the session carries an admin claim or a bonded
|
|
57
|
+
* permissions provider grants the `manage status` permission.
|
|
58
|
+
*
|
|
59
|
+
* Use this for in-handler defense-in-depth (it does not depend on the route
|
|
60
|
+
* middleware being preserved by the injector).
|
|
61
|
+
*
|
|
62
|
+
* @param res - The response whose `locals.session` is inspected.
|
|
63
|
+
* @returns `true` when the session is an authorized status page admin.
|
|
64
|
+
*/
|
|
65
|
+
export declare function isStatusAdmin(res: MoleculeResponse): Promise<boolean>;
|
|
66
|
+
/**
|
|
67
|
+
* Route middleware that gates the admin-only status page mutation routes. Calls
|
|
68
|
+
* `next()` only for an authenticated status page admin; otherwise forwards an
|
|
69
|
+
* error to the framework error handler — `Unauthorized` when no session is
|
|
70
|
+
* present, `Forbidden` when the session is authenticated but not authorized to
|
|
71
|
+
* manage the status page.
|
|
72
|
+
*
|
|
73
|
+
* Exposed as a request-handler-map key so the injector's route scanner keeps it
|
|
74
|
+
* (unlike the inert global `'auth'` string, which is dropped).
|
|
75
|
+
*
|
|
76
|
+
* @returns An Express-compatible middleware function.
|
|
77
|
+
*/
|
|
78
|
+
export declare const requireAdmin: () => MoleculeRequestHandler;
|
|
79
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/authorizers/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAIH,OAAO,KAAK,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAItF;;;GAGG;AACH,eAAO,MAAM,wBAAwB,WAAW,CAAA;AAEhD;;;GAGG;AACH,eAAO,MAAM,0BAA0B,WAAW,CAAA;AAElD;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,kBAA8D,CAAA;AAoFlG;;;;;;;;;;;;GAYG;AACH,wBAAsB,aAAa,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC,CAU3E;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,YAAY,QACnB,sBAcH,CAAA"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Status page authorizers.
|
|
3
|
+
*
|
|
4
|
+
* The status page is a **public** surface — anyone may read `/status`,
|
|
5
|
+
* `/status/services`, `/status/incidents`, and `/status/uptime`. But the
|
|
6
|
+
* mutating routes (`POST`/`PATCH`/`DELETE /status/services`, `POST`/`PATCH
|
|
7
|
+
* /status/incidents`) must be locked down: an open mutation surface lets any
|
|
8
|
+
* caller deface the public status page (fabricate outages, delete services,
|
|
9
|
+
* post fake incidents). There is no per-row owner here — a service/incident is
|
|
10
|
+
* platform-operations data — so the secure-by-default model is a **role gate**:
|
|
11
|
+
* mutating the status page is restricted to a status-management authority
|
|
12
|
+
* (operator / admin). The gate is enforced in two independent layers so it can
|
|
13
|
+
* never silently regress:
|
|
14
|
+
*
|
|
15
|
+
* 1. {@link requireAdmin} — a route middleware referenced from `routes.ts`. It is
|
|
16
|
+
* a key in the request handler map, so the mlcl injector's route scanner
|
|
17
|
+
* preserves it (a bare `'auth'` middleware string is silently dropped because
|
|
18
|
+
* it isn't a handler-map key — which is exactly why the previous gate was
|
|
19
|
+
* inert and the mutating routes shipped PUBLIC).
|
|
20
|
+
* 2. {@link isStatusAdmin} — called at the top of every mutation handler, so the
|
|
21
|
+
* gate holds even when the resource is wired without the middleware
|
|
22
|
+
* (`requestHandlerMap[route.handler]` only). Fail-closed.
|
|
23
|
+
*
|
|
24
|
+
* Apps grant operators the `manage status` permission via
|
|
25
|
+
* `@molecule/api-permissions` (or set an admin session claim) — that is the
|
|
26
|
+
* extension point for a richer operator-role model.
|
|
27
|
+
*
|
|
28
|
+
* "Admin" is resolved fail-closed via {@link isStatusAdmin}: an explicit admin
|
|
29
|
+
* claim on the session, **or** an `@molecule/api-permissions` grant. With neither,
|
|
30
|
+
* access is denied — the app developer must define who may administer the status
|
|
31
|
+
* page.
|
|
32
|
+
*
|
|
33
|
+
* @module
|
|
34
|
+
*/
|
|
35
|
+
import { getLogger } from '@molecule/api-bond';
|
|
36
|
+
import { t } from '@molecule/api-i18n';
|
|
37
|
+
const logger = getLogger();
|
|
38
|
+
/**
|
|
39
|
+
* Permission action checked against `@molecule/api-permissions` for status page
|
|
40
|
+
* administration.
|
|
41
|
+
*/
|
|
42
|
+
export const STATUS_PERMISSION_ACTION = 'manage';
|
|
43
|
+
/**
|
|
44
|
+
* Permission resource checked against `@molecule/api-permissions` for status page
|
|
45
|
+
* administration.
|
|
46
|
+
*/
|
|
47
|
+
export const STATUS_PERMISSION_RESOURCE = 'status';
|
|
48
|
+
/**
|
|
49
|
+
* Session-claim permission string (`'status:manage'`) that, when present in a
|
|
50
|
+
* session's `permissions` array, grants status page administration without a
|
|
51
|
+
* bonded permissions provider.
|
|
52
|
+
*/
|
|
53
|
+
export const STATUS_ADMIN_PERMISSION = `${STATUS_PERMISSION_RESOURCE}:${STATUS_PERMISSION_ACTION}`;
|
|
54
|
+
/**
|
|
55
|
+
* Whether an admin signal is present directly on the session claims (no provider
|
|
56
|
+
* lookup). Honors `isAdmin === true`, `role === 'admin'`, `roles` containing
|
|
57
|
+
* `'admin'`, or `permissions` containing `'admin'` / `'status:manage'`.
|
|
58
|
+
*
|
|
59
|
+
* @param session - The structural session, or `undefined`.
|
|
60
|
+
* @returns `true` when the session carries an admin claim.
|
|
61
|
+
*/
|
|
62
|
+
function sessionClaimIsAdmin(session) {
|
|
63
|
+
if (!session) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
if (session.isAdmin === true) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
if (session.role === 'admin') {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
if (Array.isArray(session.roles) && session.roles.includes('admin')) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
if (Array.isArray(session.permissions) &&
|
|
76
|
+
(session.permissions.includes('admin') || session.permissions.includes(STATUS_ADMIN_PERMISSION))) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Whether a bonded `@molecule/api-permissions` provider grants the current user
|
|
83
|
+
* the `manage status` permission. Fail-closed: returns `false` when the
|
|
84
|
+
* permissions package is not installed, no provider is bonded, or the check
|
|
85
|
+
* throws — so a missing/misconfigured permissions layer denies rather than opens
|
|
86
|
+
* access.
|
|
87
|
+
*
|
|
88
|
+
* @param userId - The authenticated user id.
|
|
89
|
+
* @returns `true` only when a bonded provider affirmatively allows the action.
|
|
90
|
+
*/
|
|
91
|
+
async function permissionAllows(userId) {
|
|
92
|
+
try {
|
|
93
|
+
const permissions = await import('@molecule/api-permissions');
|
|
94
|
+
if (!permissions.hasProvider()) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return await permissions.can(`user:${userId}`, STATUS_PERMISSION_ACTION, STATUS_PERMISSION_RESOURCE);
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
// Best-effort: api-permissions is an optional peer dep. If it isn't installed,
|
|
101
|
+
// has no bonded provider, or the check throws, deny (fail-closed) rather than
|
|
102
|
+
// letting any caller deface the public status page.
|
|
103
|
+
logger.debug('Status permissions check unavailable; denying access (fail-closed)', {
|
|
104
|
+
error,
|
|
105
|
+
userId,
|
|
106
|
+
});
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Resolves whether the current request's session belongs to an actor authorized
|
|
112
|
+
* to administer the status page (create/update/delete services + incidents).
|
|
113
|
+
* Fail-closed: returns `false` when there is no authenticated session, and
|
|
114
|
+
* otherwise only `true` when the session carries an admin claim or a bonded
|
|
115
|
+
* permissions provider grants the `manage status` permission.
|
|
116
|
+
*
|
|
117
|
+
* Use this for in-handler defense-in-depth (it does not depend on the route
|
|
118
|
+
* middleware being preserved by the injector).
|
|
119
|
+
*
|
|
120
|
+
* @param res - The response whose `locals.session` is inspected.
|
|
121
|
+
* @returns `true` when the session is an authorized status page admin.
|
|
122
|
+
*/
|
|
123
|
+
export async function isStatusAdmin(res) {
|
|
124
|
+
const session = res.locals.session;
|
|
125
|
+
const userId = session?.userId;
|
|
126
|
+
if (!userId) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
if (sessionClaimIsAdmin(session)) {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
return permissionAllows(userId);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Route middleware that gates the admin-only status page mutation routes. Calls
|
|
136
|
+
* `next()` only for an authenticated status page admin; otherwise forwards an
|
|
137
|
+
* error to the framework error handler — `Unauthorized` when no session is
|
|
138
|
+
* present, `Forbidden` when the session is authenticated but not authorized to
|
|
139
|
+
* manage the status page.
|
|
140
|
+
*
|
|
141
|
+
* Exposed as a request-handler-map key so the injector's route scanner keeps it
|
|
142
|
+
* (unlike the inert global `'auth'` string, which is dropped).
|
|
143
|
+
*
|
|
144
|
+
* @returns An Express-compatible middleware function.
|
|
145
|
+
*/
|
|
146
|
+
export const requireAdmin = () => async (_req, res, next) => {
|
|
147
|
+
const session = res.locals.session;
|
|
148
|
+
if (!session?.userId) {
|
|
149
|
+
return next(t('resource.error.unauthorized', undefined, { defaultValue: 'Unauthorized' }));
|
|
150
|
+
}
|
|
151
|
+
if (await isStatusAdmin(res)) {
|
|
152
|
+
return next();
|
|
153
|
+
}
|
|
154
|
+
return next(t('status.error.forbidden', undefined, {
|
|
155
|
+
defaultValue: 'Permission required to manage the status page',
|
|
156
|
+
}));
|
|
157
|
+
};
|
|
158
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/authorizers/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC9C,OAAO,EAAE,CAAC,EAAE,MAAM,oBAAoB,CAAA;AAGtC,MAAM,MAAM,GAAG,SAAS,EAAE,CAAA;AAE1B;;;GAGG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,QAAQ,CAAA;AAEhD;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,QAAQ,CAAA;AAElD;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,GAAG,0BAA0B,IAAI,wBAAwB,EAAE,CAAA;AAqBlG;;;;;;;GAOG;AACH,SAAS,mBAAmB,CAAC,OAAkC;IAC7D,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,KAAK,CAAA;IACd,CAAC;IACD,IAAI,OAAO,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;QAC7B,OAAO,IAAI,CAAA;IACb,CAAC;IACD,IAAI,OAAO,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;QAC7B,OAAO,IAAI,CAAA;IACb,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QACpE,OAAO,IAAI,CAAA;IACb,CAAC;IACD,IACE,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC;QAClC,CAAC,OAAO,CAAC,WAAW,CAAC,QAAQ,CAAC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ACvC,MAAM,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,OAAoC,CAAA;IAC/D,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;QACrB,OAAO,IAAI,CAAC,CAAC,CAAC,6BAA6B,EAAE,SAAS,EAAE,EAAE,YAAY,EAAE,cAAc,EAAE,CAAC,CAAC,CAAA;IAC5F,CAAC;IACD,IAAI,MAAM,aAAa,CAAC,GAAG,CAAC,EAAE,CAAC;QAC7B,OAAO,IAAI,EAAE,CAAA;IACf,CAAC;IACD,OAAO,IAAI,CACT,CAAC,CAAC,wBAAwB,EAAE,SAAS,EAAE;QACrC,YAAY,EAAE,+CAA+C;KAC9D,CAAC,CACH,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -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,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-resource-status-page` 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-status-page 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 {};
|
|
19
|
+
//# sourceMappingURL=browser-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.js","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,CAAC,GAAG,UAIT,CAAA;AACD,IAAI,CAAC,CAAC,MAAM,KAAK,SAAS,IAAI,CAAC,CAAC,QAAQ,KAAK,SAAS,IAAI,CAAC,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;IACrF,MAAM,IAAI,KAAK,CACb,6GAA6G;QAC3G,sFAAsF;QACtF,iFAAiF;QACjF,0CAA0C,CAC7C,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a new incident (admin).
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new incident record. Validates the request body against
|
|
9
|
+
* the create incident schema before inserting.
|
|
10
|
+
*
|
|
11
|
+
* @param _resource - The service resource configuration (unused).
|
|
12
|
+
* @param _resource.tableName - The database table name for services.
|
|
13
|
+
* @returns A request handler that creates an incident and responds with the created record.
|
|
14
|
+
*/
|
|
15
|
+
export declare const createIncident: ({ tableName: _tableName }: {
|
|
16
|
+
tableName: string;
|
|
17
|
+
}) => (req: MoleculeRequest, res: MoleculeResponse) => Promise<{
|
|
18
|
+
statusCode: number;
|
|
19
|
+
body: {
|
|
20
|
+
error: string;
|
|
21
|
+
errorKey: string;
|
|
22
|
+
props?: undefined;
|
|
23
|
+
};
|
|
24
|
+
} | {
|
|
25
|
+
statusCode: number;
|
|
26
|
+
body: {
|
|
27
|
+
props: {
|
|
28
|
+
id: string;
|
|
29
|
+
createdAt: string;
|
|
30
|
+
updatedAt: string;
|
|
31
|
+
serviceId: string;
|
|
32
|
+
title: string;
|
|
33
|
+
severity: "minor" | "major" | "critical";
|
|
34
|
+
status: "investigating" | "identified" | "monitoring" | "resolved";
|
|
35
|
+
autoDetected: boolean;
|
|
36
|
+
startedAt: string;
|
|
37
|
+
description?: string | undefined;
|
|
38
|
+
resolvedAt?: string | undefined;
|
|
39
|
+
} | null;
|
|
40
|
+
error?: undefined;
|
|
41
|
+
errorKey?: undefined;
|
|
42
|
+
};
|
|
43
|
+
}>;
|
|
44
|
+
//# sourceMappingURL=createIncident.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createIncident.d.ts","sourceRoot":"","sources":["../../src/handlers/createIncident.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAS/E;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,GAAI,2BAA2B;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,MAC/D,KAAK,eAAe,EAAE,KAAK,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;EAqD1D,CAAA"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a new incident (admin).
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { getAnalytics, getLogger } from '@molecule/api-bond';
|
|
7
|
+
import { create } from '@molecule/api-database';
|
|
8
|
+
import { t } from '@molecule/api-i18n';
|
|
9
|
+
import { isStatusAdmin } from '../authorizers/index.js';
|
|
10
|
+
import { createIncidentPropsSchema } from '../schema.js';
|
|
11
|
+
const logger = getLogger();
|
|
12
|
+
const analytics = getAnalytics();
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new incident record. Validates the request body against
|
|
15
|
+
* the create incident schema before inserting.
|
|
16
|
+
*
|
|
17
|
+
* @param _resource - The service resource configuration (unused).
|
|
18
|
+
* @param _resource.tableName - The database table name for services.
|
|
19
|
+
* @returns A request handler that creates an incident and responds with the created record.
|
|
20
|
+
*/
|
|
21
|
+
export const createIncident = ({ tableName: _tableName }) => {
|
|
22
|
+
return async (req, res) => {
|
|
23
|
+
// Defense-in-depth: deny anyone who isn't a status page admin, even if the
|
|
24
|
+
// `requireAdmin` route middleware was stripped/omitted by the injector. An open
|
|
25
|
+
// create route lets anyone post fake incidents to the public status page.
|
|
26
|
+
if (!(await isStatusAdmin(res))) {
|
|
27
|
+
return {
|
|
28
|
+
statusCode: 403,
|
|
29
|
+
body: {
|
|
30
|
+
error: t('status.error.forbidden', undefined, {
|
|
31
|
+
defaultValue: 'Permission required to manage the status page',
|
|
32
|
+
}),
|
|
33
|
+
errorKey: 'status.error.forbidden',
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const parsed = createIncidentPropsSchema.safeParse(req.body);
|
|
39
|
+
if (!parsed.success) {
|
|
40
|
+
const errors = parsed.error.issues
|
|
41
|
+
.map((e) => `${e.path.join('.')}: ${e.message}`)
|
|
42
|
+
.join(', ');
|
|
43
|
+
return {
|
|
44
|
+
statusCode: 400,
|
|
45
|
+
body: {
|
|
46
|
+
error: t('status.error.validationFailed', { errors }),
|
|
47
|
+
errorKey: 'status.error.validationFailed',
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const result = await create('incidents', parsed.data);
|
|
52
|
+
logger.debug('Incident created', { incidentId: result.data?.id });
|
|
53
|
+
analytics
|
|
54
|
+
.track({
|
|
55
|
+
name: 'incident.created',
|
|
56
|
+
properties: { incidentId: result.data?.id, serviceId: parsed.data.serviceId },
|
|
57
|
+
})
|
|
58
|
+
.catch(() => { });
|
|
59
|
+
return { statusCode: 201, body: { props: result.data } };
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
logger.error(error);
|
|
63
|
+
return {
|
|
64
|
+
statusCode: 500,
|
|
65
|
+
body: {
|
|
66
|
+
error: t('status.error.createIncidentFailed'),
|
|
67
|
+
errorKey: 'status.error.createIncidentFailed',
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
//# sourceMappingURL=createIncident.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createIncident.js","sourceRoot":"","sources":["../../src/handlers/createIncident.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC5D,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAC/C,OAAO,EAAE,CAAC,EAAE,MAAM,oBAAoB,CAAA;AAGtC,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AACvD,OAAO,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAA;AAGxD,MAAM,MAAM,GAAG,SAAS,EAAE,CAAA;AAC1B,MAAM,SAAS,GAAG,YAAY,EAAE,CAAA;AAEhC;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,EAAE,SAAS,EAAE,UAAU,EAAyB,EAAE,EAAE;IACjF,OAAO,KAAK,EAAE,GAAoB,EAAE,GAAqB,EAAE,EAAE;QAC3D,2EAA2E;QAC3E,gFAAgF;QAChF,0EAA0E;QAC1E,IAAI,CAAC,CAAC,MAAM,aAAa,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;YAChC,OAAO;gBACL,UAAU,EAAE,GAAG;gBACf,IAAI,EAAE;oBACJ,KAAK,EAAE,CAAC,CAAC,wBAAwB,EAAE,SAAS,EAAE;wBAC5C,YAAY,EAAE,+CAA+C;qBAC9D,CAAC;oBACF,QAAQ,EAAE,wBAAwB;iBACnC;aACF,CAAA;QACH,CAAC;QAED,IAAI,CAAC;YACH,MAAM,MAAM,GA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AK,EAAE,CAAC,CAAC,mCAAmC,CAAC;oBAC7C,QAAQ,EAAE,mCAAmC;iBAC9C;aACF,CAAA;QACH,CAAC;IACH,CAAC,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a new monitored service (admin).
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { MoleculeRequest, MoleculeResponse } from '@molecule/api-resource';
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new monitored service. Validates the request body against
|
|
9
|
+
* the create service schema before inserting into the database.
|
|
10
|
+
*
|
|
11
|
+
* @param resource - The service resource configuration.
|
|
12
|
+
* @param resource.tableName - The database table name for services.
|
|
13
|
+
* @returns A request handler that creates a service and responds with the created record.
|
|
14
|
+
*/
|
|
15
|
+
export declare const createService: ({ tableName }: {
|
|
16
|
+
tableName: string;
|
|
17
|
+
}) => (req: MoleculeRequest, res: MoleculeResponse) => Promise<{
|
|
18
|
+
statusCode: number;
|
|
19
|
+
body: {
|
|
20
|
+
error: string;
|
|
21
|
+
errorKey: string;
|
|
22
|
+
props?: undefined;
|
|
23
|
+
};
|
|
24
|
+
} | {
|
|
25
|
+
statusCode: number;
|
|
26
|
+
body: {
|
|
27
|
+
props: {
|
|
28
|
+
id: string;
|
|
29
|
+
createdAt: string;
|
|
30
|
+
updatedAt: string;
|
|
31
|
+
name: string;
|
|
32
|
+
url: string;
|
|
33
|
+
method: "GET" | "HEAD" | "POST";
|
|
34
|
+
expectedStatus: number;
|
|
35
|
+
timeoutMs: number;
|
|
36
|
+
intervalMs: number;
|
|
37
|
+
enabled: boolean;
|
|
38
|
+
groupName?: string | undefined;
|
|
39
|
+
} | null;
|
|
40
|
+
error?: undefined;
|
|
41
|
+
errorKey?: undefined;
|
|
42
|
+
};
|
|
43
|
+
}>;
|
|
44
|
+
//# sourceMappingURL=createService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createService.d.ts","sourceRoot":"","sources":["../../src/handlers/createService.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAS/E;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,GAAI,eAAe;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,MAClD,KAAK,eAAe,EAAE,KAAK,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;EAkD1D,CAAA"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a new monitored service (admin).
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { getAnalytics, getLogger } from '@molecule/api-bond';
|
|
7
|
+
import { create } from '@molecule/api-database';
|
|
8
|
+
import { t } from '@molecule/api-i18n';
|
|
9
|
+
import { isStatusAdmin } from '../authorizers/index.js';
|
|
10
|
+
import { createServicePropsSchema } from '../schema.js';
|
|
11
|
+
const logger = getLogger();
|
|
12
|
+
const analytics = getAnalytics();
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new monitored service. Validates the request body against
|
|
15
|
+
* the create service schema before inserting into the database.
|
|
16
|
+
*
|
|
17
|
+
* @param resource - The service resource configuration.
|
|
18
|
+
* @param resource.tableName - The database table name for services.
|
|
19
|
+
* @returns A request handler that creates a service and responds with the created record.
|
|
20
|
+
*/
|
|
21
|
+
export const createService = ({ tableName }) => {
|
|
22
|
+
return async (req, res) => {
|
|
23
|
+
// Defense-in-depth: deny anyone who isn't a status page admin, even if the
|
|
24
|
+
// `requireAdmin` route middleware was stripped/omitted by the injector. The
|
|
25
|
+
// status page is a public surface; an open create route lets anyone deface it.
|
|
26
|
+
if (!(await isStatusAdmin(res))) {
|
|
27
|
+
return {
|
|
28
|
+
statusCode: 403,
|
|
29
|
+
body: {
|
|
30
|
+
error: t('status.error.forbidden', undefined, {
|
|
31
|
+
defaultValue: 'Permission required to manage the status page',
|
|
32
|
+
}),
|
|
33
|
+
errorKey: 'status.error.forbidden',
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const parsed = createServicePropsSchema.safeParse(req.body);
|
|
39
|
+
if (!parsed.success) {
|
|
40
|
+
const errors = parsed.error.issues
|
|
41
|
+
.map((e) => `${e.path.join('.')}: ${e.message}`)
|
|
42
|
+
.join(', ');
|
|
43
|
+
return {
|
|
44
|
+
statusCode: 400,
|
|
45
|
+
body: {
|
|
46
|
+
error: t('status.error.validationFailed', { errors }),
|
|
47
|
+
errorKey: 'status.error.validationFailed',
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const result = await create(tableName, parsed.data);
|
|
52
|
+
logger.debug('Service created', { serviceId: result.data?.id });
|
|
53
|
+
analytics
|
|
54
|
+
.track({ name: 'service.created', properties: { serviceId: result.data?.id } })
|
|
55
|
+
.catch(() => { });
|
|
56
|
+
return { statusCode: 201, body: { props: result.data } };
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
logger.error(error);
|
|
60
|
+
return {
|
|
61
|
+
statusCode: 500,
|
|
62
|
+
body: {
|
|
63
|
+
error: t('status.error.createServiceFailed'),
|
|
64
|
+
errorKey: 'status.error.createServiceFailed',
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
//# sourceMappingURL=createService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createService.js","sourceRoot":"","sources":["../../src/handlers/createService.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC5D,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAC/C,OAAO,EAAE,CAAC,EAAE,MAAM,oBAAoB,CAAA;AAGtC,OA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