@cloudhal/core-contracts 0.0.0-stage → 0.1.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 +202 -0
- package/README.md +18 -2
- package/dist/scripts/bundle.d.ts +11 -0
- package/dist/scripts/bundle.js +175 -0
- package/dist/scripts/gen-rs.d.ts +1 -0
- package/dist/scripts/gen-rs.js +64 -0
- package/dist/scripts/gen-ts.d.ts +1 -0
- package/dist/scripts/gen-ts.js +61 -0
- package/dist/scripts/gen-validators.d.ts +1 -0
- package/dist/scripts/gen-validators.js +111 -0
- package/dist/scripts/schema-list.d.ts +7 -0
- package/dist/scripts/schema-list.js +30 -0
- package/dist/src/generated/analytics-beacon.d.ts +54 -0
- package/dist/src/generated/analytics-beacon.js +58 -0
- package/dist/src/generated/analytics-event.d.ts +201 -0
- package/dist/src/generated/analytics-event.js +207 -0
- package/dist/src/generated/asset.d.ts +283 -0
- package/dist/src/generated/asset.js +287 -0
- package/dist/src/generated/automation.d.ts +173 -0
- package/dist/src/generated/automation.js +185 -0
- package/dist/src/generated/comment-submission.d.ts +81 -0
- package/dist/src/generated/comment-submission.js +85 -0
- package/dist/src/generated/comment.d.ts +219 -0
- package/dist/src/generated/comment.js +244 -0
- package/dist/src/generated/content-entry.d.ts +300 -0
- package/dist/src/generated/content-entry.js +304 -0
- package/dist/src/generated/glossary.d.ts +97 -0
- package/dist/src/generated/glossary.js +103 -0
- package/dist/src/generated/index.d.ts +23 -0
- package/dist/src/generated/index.js +26 -0
- package/dist/src/generated/menu.d.ts +197 -0
- package/dist/src/generated/menu.js +202 -0
- package/dist/src/generated/page.d.ts +383 -0
- package/dist/src/generated/page.js +393 -0
- package/dist/src/generated/project.d.ts +63 -0
- package/dist/src/generated/project.js +67 -0
- package/dist/src/generated/redirect.d.ts +93 -0
- package/dist/src/generated/redirect.js +97 -0
- package/dist/src/generated/release.d.ts +300 -0
- package/dist/src/generated/release.js +326 -0
- package/dist/src/generated/schedule-run.d.ts +151 -0
- package/dist/src/generated/schedule-run.js +164 -0
- package/dist/src/generated/schemas.d.ts +11616 -0
- package/dist/src/generated/schemas.js +7244 -0
- package/dist/src/generated/section.d.ts +181 -0
- package/dist/src/generated/section.js +191 -0
- package/dist/src/generated/site.d.ts +520 -0
- package/dist/src/generated/site.js +567 -0
- package/dist/src/generated/skill-manifest.d.ts +30 -0
- package/dist/src/generated/skill-manifest.js +33 -0
- package/dist/src/generated/snapshot.d.ts +99 -0
- package/dist/src/generated/snapshot.js +112 -0
- package/dist/src/generated/taxonomy.d.ts +135 -0
- package/dist/src/generated/taxonomy.js +139 -0
- package/dist/src/generated/term.d.ts +194 -0
- package/dist/src/generated/term.js +209 -0
- package/dist/src/generated/validators.d.ts +35 -0
- package/dist/src/generated/validators.js +36614 -0
- package/dist/src/index.d.ts +13 -0
- package/dist/src/index.js +15 -0
- package/dist/src/validate.d.ts +38 -0
- package/dist/src/validate.js +109 -0
- package/package.json +55 -3
- package/schemas/.bundled/analytics-beacon.schema.json +132 -0
- package/schemas/.bundled/analytics-event.schema.json +411 -0
- package/schemas/.bundled/asset.schema.json +519 -0
- package/schemas/.bundled/automation.schema.json +334 -0
- package/schemas/.bundled/comment-submission.schema.json +172 -0
- package/schemas/.bundled/comment.schema.json +425 -0
- package/schemas/.bundled/content-entry.schema.json +514 -0
- package/schemas/.bundled/glossary.schema.json +139 -0
- package/schemas/.bundled/menu.schema.json +371 -0
- package/schemas/.bundled/page.schema.json +738 -0
- package/schemas/.bundled/project.schema.json +97 -0
- package/schemas/.bundled/redirect.schema.json +152 -0
- package/schemas/.bundled/release.schema.json +639 -0
- package/schemas/.bundled/schedule-run.schema.json +310 -0
- package/schemas/.bundled/section.schema.json +434 -0
- package/schemas/.bundled/site.schema.json +1050 -0
- package/schemas/.bundled/skill-manifest.schema.json +51 -0
- package/schemas/.bundled/snapshot.schema.json +192 -0
- package/schemas/.bundled/taxonomy.schema.json +200 -0
- package/schemas/.bundled/term.schema.json +315 -0
- package/schemas/analytics-beacon.schema.json +74 -0
- package/schemas/analytics-event.schema.json +239 -0
- package/schemas/asset.schema.json +293 -0
- package/schemas/automation.schema.json +183 -0
- package/schemas/comment-submission.schema.json +109 -0
- package/schemas/comment.schema.json +302 -0
- package/schemas/common.schema.json +281 -0
- package/schemas/content-entry.schema.json +129 -0
- package/schemas/glossary.schema.json +66 -0
- package/schemas/menu.schema.json +195 -0
- package/schemas/page.schema.json +59 -0
- package/schemas/project.schema.json +35 -0
- package/schemas/redirect.schema.json +74 -0
- package/schemas/release.schema.json +331 -0
- package/schemas/schedule-run.schema.json +181 -0
- package/schemas/section.schema.json +112 -0
- package/schemas/site.schema.json +427 -0
- package/schemas/skill-manifest.schema.json +27 -0
- package/schemas/snapshot.schema.json +82 -0
- package/schemas/taxonomy.schema.json +104 -0
- package/schemas/term.schema.json +116 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @cloudhal/core-contracts
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The domain contracts of Cloudhal. `schemas/*.schema.json` is the only hand-edited source; everything else is generated by `pnpm gen` and drift-checked in CI (ADR-0001):
|
|
4
|
+
|
|
5
|
+
| Generated | From | Used by |
|
|
6
|
+
|---|---|---|
|
|
7
|
+
| `src/generated/*.ts` | json-schema-to-typescript 16 | every TypeScript package |
|
|
8
|
+
| `src/generated/schemas.ts` | bundler | tooling, docs, MCP `outputSchema` |
|
|
9
|
+
| `src/generated/validators.js` | ajv standalone (pure ESM, no `eval`) | runtime validation on Node **and** Workers (ADR-0008) |
|
|
10
|
+
| `crates/cloudhal-schema/src/generated/*.rs` | cargo-typify 0.8 | Rust |
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { assertValid, isValid, type Page } from "@cloudhal/core-contracts";
|
|
14
|
+
|
|
15
|
+
if (isValid("page", data)) data.sections; // narrowed to Page
|
|
16
|
+
assertValid("snapshot", snapshot); // throws ContractValidationError
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Fixtures under `fixtures/{valid,invalid}/<contract>/` are validated in both languages and round-tripped through the Rust types. To change a contract follow `.agents/skills/contract-change`.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
type Json = null | boolean | number | string | Json[] | {
|
|
2
|
+
[k: string]: Json;
|
|
3
|
+
};
|
|
4
|
+
type SchemaObject = {
|
|
5
|
+
[k: string]: Json;
|
|
6
|
+
};
|
|
7
|
+
export declare const SCHEMAS_DIR: string;
|
|
8
|
+
export declare const BUNDLED_DIR: string;
|
|
9
|
+
export declare function bundleAll(): Promise<Map<string, SchemaObject>>;
|
|
10
|
+
export declare function writeBundled(): Promise<Map<string, SchemaObject>>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bundles each root schema into a self-contained file under schemas/.bundled/.
|
|
3
|
+
*
|
|
4
|
+
* Why: typify (Rust codegen) cannot resolve cross-file $refs, and we want one input for
|
|
5
|
+
* both generators and both validators. Authoring stays DRY (shared $defs in
|
|
6
|
+
* common.schema.json, Section referenced from Page); bundling is mechanical.
|
|
7
|
+
*
|
|
8
|
+
* Rules handled:
|
|
9
|
+
* "common.schema.json#/$defs/X" -> "#/$defs/X" (def hoisted, transitively)
|
|
10
|
+
* "section.schema.json" -> "#/$defs/Section" (root of another schema hoisted)
|
|
11
|
+
* "#/$defs/X" -> unchanged
|
|
12
|
+
*/
|
|
13
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
16
|
+
import { COMMON_SCHEMA, ROOT_SCHEMAS } from "./schema-list.js";
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
22
|
+
export const SCHEMAS_DIR = path.resolve(here, "../schemas");
|
|
23
|
+
export const BUNDLED_DIR = path.join(SCHEMAS_DIR, ".bundled");
|
|
24
|
+
|
|
25
|
+
async function loadSchema(name ) {
|
|
26
|
+
const raw = await readFile(path.join(SCHEMAS_DIR, `${name}.schema.json`), "utf8");
|
|
27
|
+
return JSON.parse(raw) ;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function isObject(v ) {
|
|
31
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function pascal(name ) {
|
|
35
|
+
return name
|
|
36
|
+
.split("-")
|
|
37
|
+
.map((p) => p.charAt(0).toUpperCase() + p.slice(1))
|
|
38
|
+
.join("");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function bundleAll() {
|
|
42
|
+
const sources = new Map ();
|
|
43
|
+
sources.set(COMMON_SCHEMA, await loadSchema(COMMON_SCHEMA));
|
|
44
|
+
for (const name of ROOT_SCHEMAS) sources.set(name, await loadSchema(name));
|
|
45
|
+
|
|
46
|
+
const out = new Map ();
|
|
47
|
+
for (const name of ROOT_SCHEMAS) {
|
|
48
|
+
out.set(name, bundleOne(name, sources));
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function bundleOne(rootName , sources ) {
|
|
54
|
+
const root = structuredClone(sources.get(rootName) );
|
|
55
|
+
const defs = isObject(root.$defs) ? root.$defs : {};
|
|
56
|
+
const pending = [];
|
|
57
|
+
const seen = new Set ();
|
|
58
|
+
|
|
59
|
+
const rewrite = (node , ownerFile ) => {
|
|
60
|
+
if (Array.isArray(node)) return node.map((n) => rewrite(n, ownerFile));
|
|
61
|
+
if (!isObject(node)) return node;
|
|
62
|
+
const copy = {};
|
|
63
|
+
for (const [k, v] of Object.entries(node)) {
|
|
64
|
+
if (k === "$ref" && typeof v === "string") {
|
|
65
|
+
copy[k] = rewriteRef(v, ownerFile);
|
|
66
|
+
} else if (k === "$id" || k === "$schema") {
|
|
67
|
+
// keep only on the root; nested copies would change ref resolution
|
|
68
|
+
if (node === root) copy[k] = v;
|
|
69
|
+
} else {
|
|
70
|
+
copy[k] = rewrite(v, ownerFile);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return copy;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const rewriteRef = (ref , ownerFile ) => {
|
|
77
|
+
if (ref.startsWith("#/$defs/")) {
|
|
78
|
+
// local ref inside a hoisted file must be re-targeted to the bundled $defs
|
|
79
|
+
if (ownerFile !== rootName) {
|
|
80
|
+
const def = ref.slice("#/$defs/".length);
|
|
81
|
+
const key = `${ownerFile}#${def}`;
|
|
82
|
+
if (!seen.has(key)) {
|
|
83
|
+
seen.add(key);
|
|
84
|
+
pending.push({ file: ownerFile, def, targetName: def });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return ref;
|
|
88
|
+
}
|
|
89
|
+
const hashIdx = ref.indexOf("#");
|
|
90
|
+
const file = (hashIdx === -1 ? ref : ref.slice(0, hashIdx)).replace(/\.schema\.json$/, "");
|
|
91
|
+
const frag = hashIdx === -1 ? "" : ref.slice(hashIdx + 1);
|
|
92
|
+
if (!sources.has(file)) throw new Error(`${rootName}: unknown schema file in $ref "${ref}"`);
|
|
93
|
+
if (frag === "" || frag === "/") {
|
|
94
|
+
const targetName = pascal(file);
|
|
95
|
+
const key = `${file}#`;
|
|
96
|
+
if (!seen.has(key)) {
|
|
97
|
+
seen.add(key);
|
|
98
|
+
pending.push({ file, def: null, targetName });
|
|
99
|
+
}
|
|
100
|
+
return `#/$defs/${targetName}`;
|
|
101
|
+
}
|
|
102
|
+
if (!frag.startsWith("/$defs/")) throw new Error(`${rootName}: unsupported $ref "${ref}"`);
|
|
103
|
+
const def = frag.slice("/$defs/".length);
|
|
104
|
+
const key = `${file}#${def}`;
|
|
105
|
+
if (!seen.has(key)) {
|
|
106
|
+
seen.add(key);
|
|
107
|
+
pending.push({ file, def, targetName: def });
|
|
108
|
+
}
|
|
109
|
+
return `#/$defs/${def}`;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// Rewrite the root body (excluding $defs, handled below so local defs are also scanned).
|
|
113
|
+
const rewrittenRoot = rewrite(root, rootName) ;
|
|
114
|
+
const rewrittenDefs = {};
|
|
115
|
+
for (const [k, v] of Object.entries(defs)) rewrittenDefs[k] = rewrite(v, rootName);
|
|
116
|
+
|
|
117
|
+
while (pending.length > 0) {
|
|
118
|
+
const item = pending.shift() ;
|
|
119
|
+
const src = sources.get(item.file) ;
|
|
120
|
+
let def ;
|
|
121
|
+
if (item.def === null) {
|
|
122
|
+
const clone = structuredClone(src);
|
|
123
|
+
delete clone.$id;
|
|
124
|
+
delete clone.$schema;
|
|
125
|
+
delete clone.$defs;
|
|
126
|
+
def = clone;
|
|
127
|
+
// hoist that file's own $defs too
|
|
128
|
+
if (isObject(src.$defs)) {
|
|
129
|
+
for (const k of Object.keys(src.$defs)) {
|
|
130
|
+
const key = `${item.file}#${k}`;
|
|
131
|
+
if (!seen.has(key)) {
|
|
132
|
+
seen.add(key);
|
|
133
|
+
pending.push({ file: item.file, def: k, targetName: k });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
const srcDefs = isObject(src.$defs) ? src.$defs : {};
|
|
139
|
+
if (!(item.def in srcDefs)) throw new Error(`${item.file}: missing $defs/${item.def}`);
|
|
140
|
+
def = srcDefs[item.def] ;
|
|
141
|
+
}
|
|
142
|
+
if (item.targetName in rewrittenDefs) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`${rootName}: $defs name collision on "${item.targetName}" from ${item.file}`,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
rewrittenDefs[item.targetName] = rewrite(def, item.file);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const { $defs: _ignored, ...body } = rewrittenRoot;
|
|
151
|
+
const bundled = { ...body };
|
|
152
|
+
if (Object.keys(rewrittenDefs).length > 0) bundled.$defs = sortKeys(rewrittenDefs);
|
|
153
|
+
return bundled;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function sortKeys(o ) {
|
|
157
|
+
return Object.fromEntries(Object.entries(o).sort(([a], [b]) => a.localeCompare(b)));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export async function writeBundled() {
|
|
161
|
+
const bundled = await bundleAll();
|
|
162
|
+
await mkdir(BUNDLED_DIR, { recursive: true });
|
|
163
|
+
for (const [name, schema] of bundled) {
|
|
164
|
+
await writeFile(
|
|
165
|
+
path.join(BUNDLED_DIR, `${name}.schema.json`),
|
|
166
|
+
`${JSON.stringify(schema, null, 2)}\n`,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
return bundled;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
173
|
+
const bundled = await writeBundled();
|
|
174
|
+
console.log(`bundled ${bundled.size} schemas → ${path.relative(process.cwd(), BUNDLED_DIR)}`);
|
|
175
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Schema → Rust via cargo-typify (pinned 0.8.0), from the bundled schemas.
|
|
3
|
+
* Output is committed under crates/cloudhal-schema/src/generated/ and drift-checked in CI.
|
|
4
|
+
*
|
|
5
|
+
* Requires: `cargo install cargo-typify --version 0.8.0 --locked` and rustfmt.
|
|
6
|
+
*/
|
|
7
|
+
import { spawnSync } from "node:child_process";
|
|
8
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
import { BUNDLED_DIR, writeBundled } from "./bundle.js";
|
|
12
|
+
import { ROOT_SCHEMAS } from "./schema-list.js";
|
|
13
|
+
|
|
14
|
+
const TYPIFY_VERSION = "0.8.0";
|
|
15
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
const OUT_DIR = path.resolve(here, "../../../crates/cloudhal-schema/src/generated");
|
|
17
|
+
|
|
18
|
+
function run(cmd , args ) {
|
|
19
|
+
const r = spawnSync(cmd, args, { encoding: "utf8" });
|
|
20
|
+
if (r.error) throw r.error;
|
|
21
|
+
if (r.status !== 0) throw new Error(`${cmd} ${args.join(" ")} failed:\n${r.stderr}`);
|
|
22
|
+
return r.stdout;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const version = run("cargo", ["typify", "--version"]).trim();
|
|
26
|
+
if (!version.includes(TYPIFY_VERSION)) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`cargo-typify ${TYPIFY_VERSION} required, found "${version}". Run: cargo install cargo-typify --version ${TYPIFY_VERSION} --locked`,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
await writeBundled();
|
|
33
|
+
await mkdir(OUT_DIR, { recursive: true });
|
|
34
|
+
|
|
35
|
+
const HEADER = `//! GENERATED FILE — DO NOT EDIT.
|
|
36
|
+
//! Source: packages/core-contracts/schemas/*.schema.json (run \`pnpm gen\`)
|
|
37
|
+
#![allow(clippy::all, rustdoc::all, dead_code, unused_imports)]
|
|
38
|
+
|
|
39
|
+
`;
|
|
40
|
+
|
|
41
|
+
const mods = [];
|
|
42
|
+
for (const name of ROOT_SCHEMAS) {
|
|
43
|
+
const modName = name.replace(/-/g, "_");
|
|
44
|
+
const input = path.join(BUNDLED_DIR, `${name}.schema.json`);
|
|
45
|
+
const output = path.join(OUT_DIR, `${modName}.rs`);
|
|
46
|
+
run("cargo", ["typify", input, "--no-builder", "--additional-derive", "PartialEq", "-o", output]);
|
|
47
|
+
const body = await readFile(output, "utf8");
|
|
48
|
+
await writeFile(
|
|
49
|
+
output,
|
|
50
|
+
HEADER + body.replace(/^#!\[allow\(clippy::redundant_closure_call\)\]\n/m, ""),
|
|
51
|
+
);
|
|
52
|
+
mods.push(modName);
|
|
53
|
+
}
|
|
54
|
+
await writeFile(
|
|
55
|
+
path.join(OUT_DIR, "mod.rs"),
|
|
56
|
+
`//! GENERATED FILE — DO NOT EDIT. One module per root contract.\n\n${mods.map((m) => `pub mod ${m};`).join("\n")}\n`,
|
|
57
|
+
);
|
|
58
|
+
run("rustfmt", [
|
|
59
|
+
"--edition",
|
|
60
|
+
"2024",
|
|
61
|
+
...mods.map((m) => path.join(OUT_DIR, `${m}.rs`)),
|
|
62
|
+
path.join(OUT_DIR, "mod.rs"),
|
|
63
|
+
]);
|
|
64
|
+
console.log(`generated ${mods.length} Rust modules → ${path.relative(process.cwd(), OUT_DIR)}`);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Schema → TypeScript via json-schema-to-typescript, from the bundled schemas.
|
|
3
|
+
* Output is committed under src/generated/ and checked for drift in CI.
|
|
4
|
+
*/
|
|
5
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { compile } from "json-schema-to-typescript";
|
|
9
|
+
import { writeBundled } from "./bundle.js";
|
|
10
|
+
import { ROOT_SCHEMAS } from "./schema-list.js";
|
|
11
|
+
|
|
12
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
const OUT_DIR = path.resolve(here, "../src/generated");
|
|
14
|
+
|
|
15
|
+
const BANNER = `/* eslint-disable */
|
|
16
|
+
/* biome-ignore-all lint: generated */
|
|
17
|
+
/**
|
|
18
|
+
* GENERATED FILE — DO NOT EDIT.
|
|
19
|
+
* Source: packages/core-contracts/schemas/*.schema.json (run \`pnpm gen\`)
|
|
20
|
+
*/
|
|
21
|
+
`;
|
|
22
|
+
|
|
23
|
+
const bundled = await writeBundled();
|
|
24
|
+
await mkdir(OUT_DIR, { recursive: true });
|
|
25
|
+
|
|
26
|
+
// Each generated module re-declares the shared $defs it uses (Id, Timestamp, ...). The index
|
|
27
|
+
// exports every name exactly once, from the first module (in ROOT_SCHEMAS order) declaring it.
|
|
28
|
+
const exportsByModule = new Map ();
|
|
29
|
+
const claimed = new Set ();
|
|
30
|
+
for (const name of ROOT_SCHEMAS) {
|
|
31
|
+
const schema = bundled.get(name);
|
|
32
|
+
if (!schema) throw new Error(`missing bundled schema ${name}`);
|
|
33
|
+
// biome-ignore lint/suspicious/noExplicitAny: json-schema-to-typescript's JSONSchema4 typing is looser than ours
|
|
34
|
+
const ts = await compile(schema , name, {
|
|
35
|
+
bannerComment: BANNER,
|
|
36
|
+
additionalProperties: false,
|
|
37
|
+
strictIndexSignatures: true,
|
|
38
|
+
declareExternallyReferenced: true,
|
|
39
|
+
unreachableDefinitions: true,
|
|
40
|
+
style: { semi: true, singleQuote: false, trailingComma: "all", printWidth: 100 },
|
|
41
|
+
});
|
|
42
|
+
await writeFile(path.join(OUT_DIR, `${name}.ts`), ts);
|
|
43
|
+
const names = [];
|
|
44
|
+
for (const m of ts.matchAll(/^export (?:interface|type) (\w+)/gm)) {
|
|
45
|
+
const id = m[1] ;
|
|
46
|
+
if (!claimed.has(id)) {
|
|
47
|
+
claimed.add(id);
|
|
48
|
+
names.push(id);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exportsByModule.set(name, names);
|
|
52
|
+
}
|
|
53
|
+
const indexLines = [BANNER];
|
|
54
|
+
for (const [name, names] of exportsByModule) {
|
|
55
|
+
if (names.length === 0) continue;
|
|
56
|
+
indexLines.push(`export type { ${names.join(", ")} } from "./${name}.ts";`);
|
|
57
|
+
}
|
|
58
|
+
await writeFile(path.join(OUT_DIR, "index.ts"), `${indexLines.join("\n")}\n`);
|
|
59
|
+
console.log(
|
|
60
|
+
`generated ${ROOT_SCHEMAS.length} TypeScript modules → ${path.relative(process.cwd(), OUT_DIR)}`,
|
|
61
|
+
);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Precompiles ajv validators (ajv standalone) for every root contract so runtime validation
|
|
3
|
+
* works on Cloudflare Workers, where `new Function` is unavailable (ADR-0008).
|
|
4
|
+
*
|
|
5
|
+
* Outputs (committed, drift-checked):
|
|
6
|
+
* src/generated/schemas.ts — bundled schemas as constants
|
|
7
|
+
* src/generated/validators.js — standalone validator functions (ESM)
|
|
8
|
+
* src/generated/validators.d.ts
|
|
9
|
+
*/
|
|
10
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
14
|
+
import standaloneModule from "ajv/dist/standalone/index.js";
|
|
15
|
+
import addFormatsModule from "ajv-formats";
|
|
16
|
+
import { writeBundled } from "./bundle.js";
|
|
17
|
+
import { ROOT_SCHEMAS } from "./schema-list.js";
|
|
18
|
+
|
|
19
|
+
const addFormats = addFormatsModule.default;
|
|
20
|
+
const standaloneCode = standaloneModule.default;
|
|
21
|
+
|
|
22
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
23
|
+
const OUT_DIR = path.resolve(here, "../src/generated");
|
|
24
|
+
|
|
25
|
+
const BANNER = `/* eslint-disable */
|
|
26
|
+
/* biome-ignore-all lint: generated */
|
|
27
|
+
/**
|
|
28
|
+
* GENERATED FILE — DO NOT EDIT.
|
|
29
|
+
* Source: packages/core-contracts/schemas/*.schema.json (run \`pnpm gen\`)
|
|
30
|
+
*/
|
|
31
|
+
`;
|
|
32
|
+
|
|
33
|
+
const pascal = (name ) =>
|
|
34
|
+
name
|
|
35
|
+
.split("-")
|
|
36
|
+
.map((p) => p.charAt(0).toUpperCase() + p.slice(1))
|
|
37
|
+
.join("");
|
|
38
|
+
|
|
39
|
+
const bundled = await writeBundled();
|
|
40
|
+
await mkdir(OUT_DIR, { recursive: true });
|
|
41
|
+
|
|
42
|
+
// 1. schemas.ts — constants, deterministic order.
|
|
43
|
+
const schemaLines = [BANNER];
|
|
44
|
+
for (const name of ROOT_SCHEMAS) {
|
|
45
|
+
schemaLines.push(
|
|
46
|
+
`export const ${pascal(name)}Schema = ${JSON.stringify(bundled.get(name), null, 2)} as const;`,
|
|
47
|
+
"",
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
schemaLines.push("export const schemas = {");
|
|
51
|
+
for (const name of ROOT_SCHEMAS) schemaLines.push(` "${name}": ${pascal(name)}Schema,`);
|
|
52
|
+
schemaLines.push("} as const;", "");
|
|
53
|
+
await writeFile(path.join(OUT_DIR, "schemas.ts"), `${schemaLines.join("\n")}`);
|
|
54
|
+
|
|
55
|
+
// 2. validators.js — ajv standalone. `code.source` is required for standaloneCode.
|
|
56
|
+
const ajv = new Ajv2020({
|
|
57
|
+
strict: true,
|
|
58
|
+
allErrors: true,
|
|
59
|
+
code: { source: true, esm: true, lines: true },
|
|
60
|
+
});
|
|
61
|
+
addFormats(ajv);
|
|
62
|
+
const exportsMap = {};
|
|
63
|
+
for (const name of ROOT_SCHEMAS) {
|
|
64
|
+
const schema = bundled.get(name) ;
|
|
65
|
+
ajv.addSchema(schema, name);
|
|
66
|
+
exportsMap[`validate${pascal(name)}`] = schema.$id;
|
|
67
|
+
}
|
|
68
|
+
const code = toEsm(standaloneCode(ajv, exportsMap));
|
|
69
|
+
await writeFile(path.join(OUT_DIR, "validators.js"), `${BANNER}${code}\n`);
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* ajv's standalone output still uses `require()` for its runtime helpers even in ESM mode.
|
|
73
|
+
* Rewrite those into static imports (with explicit .js extensions for Node ESM resolution) so
|
|
74
|
+
* the module runs under plain Node ESM and on Workers without a CommonJS shim.
|
|
75
|
+
*/
|
|
76
|
+
function toEsm(src ) {
|
|
77
|
+
const imports = [];
|
|
78
|
+
let n = 0;
|
|
79
|
+
const body = src.replace(/require\("([^"]+)"\)/g, (_m, spec ) => {
|
|
80
|
+
const id = `__req${n++}`;
|
|
81
|
+
const withExt = /\.[cm]?js$/.test(spec) ? spec : `${spec}.js`;
|
|
82
|
+
imports.push(`import ${id} from "${withExt}";`);
|
|
83
|
+
return id;
|
|
84
|
+
});
|
|
85
|
+
if (/\brequire\(/.test(body))
|
|
86
|
+
throw new Error("gen-validators: unhandled require() in ajv standalone output");
|
|
87
|
+
// CommonJS interop differs between Node ESM (default import = module.exports) and bundlers
|
|
88
|
+
// (default import may already be `exports.default`); accept both.
|
|
89
|
+
const interop = body.replace(/\b(__req\d+)\.default\b/g, "($1.default ?? $1)");
|
|
90
|
+
return `${imports.join("\n")}\n${interop}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 3. validators.d.ts — typed against the generated interfaces.
|
|
94
|
+
const dts = [BANNER];
|
|
95
|
+
dts.push(`import type { ErrorObject } from "ajv";`);
|
|
96
|
+
dts.push(
|
|
97
|
+
`import type { ${ROOT_SCHEMAS.map(pascal).join(", ")} } from "./index.ts";`,
|
|
98
|
+
"",
|
|
99
|
+
"export interface StandaloneValidator<T> {",
|
|
100
|
+
" (data: unknown): data is T;",
|
|
101
|
+
" errors?: ErrorObject[] | null;",
|
|
102
|
+
"}",
|
|
103
|
+
"",
|
|
104
|
+
);
|
|
105
|
+
for (const name of ROOT_SCHEMAS) {
|
|
106
|
+
dts.push(`export declare const validate${pascal(name)}: StandaloneValidator<${pascal(name)}>;`);
|
|
107
|
+
}
|
|
108
|
+
await writeFile(path.join(OUT_DIR, "validators.d.ts"), `${dts.join("\n")}\n`);
|
|
109
|
+
console.log(
|
|
110
|
+
`generated schemas.ts + validators for ${ROOT_SCHEMAS.length} contracts → ${path.relative(process.cwd(), OUT_DIR)}`,
|
|
111
|
+
);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Single list of root contracts. Order is the order generators emit in; keep it stable
|
|
3
|
+
* so generated files and `tools/list`-style outputs are deterministic.
|
|
4
|
+
*/
|
|
5
|
+
export declare const ROOT_SCHEMAS: readonly ["project", "site", "page", "section", "content-entry", "snapshot", "skill-manifest", "taxonomy", "term", "redirect", "asset", "menu", "analytics-event", "analytics-beacon", "comment", "comment-submission", "release", "automation", "schedule-run", "glossary"];
|
|
6
|
+
export type RootSchemaName = (typeof ROOT_SCHEMAS)[number];
|
|
7
|
+
export declare const COMMON_SCHEMA = "common";
|