@airalogy/aira-core 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 +201 -0
- package/README.md +20 -0
- package/dist/index.js +291 -0
- package/package.json +50 -0
- package/src/index.ts +552 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# `@airalogy/aira-core`
|
|
2
|
+
|
|
3
|
+
Core TypeScript parser and validator for Airalogy `.aira` archives.
|
|
4
|
+
|
|
5
|
+
It opens `.aira` files in browser-compatible JavaScript, reads `_airalogy_archive/manifest.json`, lists archive members, loads JSON/text payloads, and validates manifest references, Record hashes, Protocol file hashes, and offline blob hashes.
|
|
6
|
+
|
|
7
|
+
Supported archive kinds are `protocol`, `protocols`, and `records`.
|
|
8
|
+
|
|
9
|
+
Example archives covering these kinds are available in `examples/aira/`.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { openAiraArchive } from '@airalogy/aira-core'
|
|
13
|
+
|
|
14
|
+
const archive = await openAiraArchive(file)
|
|
15
|
+
const summary = archive.summary()
|
|
16
|
+
const validation = await archive.validate()
|
|
17
|
+
const manifest = archive.manifest
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`.aira` archives are standard ZIP containers. The core package keeps reading independent from the full Airalogy platform, database, or execution engine.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
const b = "_airalogy_archive/manifest.json", g = "airalogy.archive", A = new TextDecoder("utf-8"), E = new TextEncoder(), R = /^[0-9a-f]{64}$/;
|
|
2
|
+
function m(i, t) {
|
|
3
|
+
return i.getUint16(t, !0);
|
|
4
|
+
}
|
|
5
|
+
function p(i, t) {
|
|
6
|
+
return i.getUint32(t, !0);
|
|
7
|
+
}
|
|
8
|
+
function U(i) {
|
|
9
|
+
return A.decode(i);
|
|
10
|
+
}
|
|
11
|
+
function v(i) {
|
|
12
|
+
return !i || i.startsWith("/") ? `Archive member '${i}' uses an absolute or empty path.` : i.split("/").some((t) => t === "..") ? `Archive member '${i}' escapes the archive root.` : null;
|
|
13
|
+
}
|
|
14
|
+
function k(i) {
|
|
15
|
+
const t = Math.max(0, i.length - 65535 - 22);
|
|
16
|
+
for (let o = i.length - 22; o >= t; o -= 1)
|
|
17
|
+
if (i[o] === 80 && i[o + 1] === 75 && i[o + 2] === 5 && i[o + 3] === 6)
|
|
18
|
+
return o;
|
|
19
|
+
throw new Error("Archive is not a valid zip file: EOCD not found.");
|
|
20
|
+
}
|
|
21
|
+
function D(i) {
|
|
22
|
+
const t = new DataView(i.buffer, i.byteOffset, i.byteLength), o = k(i), n = m(t, o + 10), s = p(t, o + 16), c = [];
|
|
23
|
+
let e = s;
|
|
24
|
+
for (let r = 0; r < n; r += 1) {
|
|
25
|
+
if (p(t, e) !== 33639248)
|
|
26
|
+
throw new Error(`Archive central directory is invalid at offset ${e}.`);
|
|
27
|
+
const a = m(t, e + 10), h = p(t, e + 20), l = p(t, e + 24), f = m(t, e + 28), d = m(t, e + 30), $ = m(t, e + 32), u = p(t, e + 42), w = e + 46, S = U(i.slice(w, w + f));
|
|
28
|
+
if (p(t, u) !== 67324752)
|
|
29
|
+
throw new Error(`Archive local header is invalid for '${S}'.`);
|
|
30
|
+
const B = m(t, u + 26), P = m(t, u + 28), x = u + 30 + B + P;
|
|
31
|
+
c.push({
|
|
32
|
+
name: S,
|
|
33
|
+
compressionMethod: a,
|
|
34
|
+
compressedSize: h,
|
|
35
|
+
uncompressedSize: l,
|
|
36
|
+
localHeaderOffset: u,
|
|
37
|
+
compressedDataStart: x
|
|
38
|
+
}), e = w + f + d + $;
|
|
39
|
+
}
|
|
40
|
+
return c.filter((r) => !r.name.endsWith("/"));
|
|
41
|
+
}
|
|
42
|
+
async function j(i) {
|
|
43
|
+
const t = globalThis.DecompressionStream;
|
|
44
|
+
if (!t)
|
|
45
|
+
throw new Error("This browser does not support DecompressionStream.");
|
|
46
|
+
const o = new Blob([i]).stream().pipeThrough(new t("deflate-raw")), n = await new Response(o).arrayBuffer();
|
|
47
|
+
return new Uint8Array(n);
|
|
48
|
+
}
|
|
49
|
+
async function _(i) {
|
|
50
|
+
const t = await crypto.subtle.digest("SHA-256", i);
|
|
51
|
+
return Array.from(new Uint8Array(t)).map((o) => o.toString(16).padStart(2, "0")).join("");
|
|
52
|
+
}
|
|
53
|
+
class y {
|
|
54
|
+
bytes;
|
|
55
|
+
entries;
|
|
56
|
+
manifest;
|
|
57
|
+
entryMap;
|
|
58
|
+
constructor(t, o, n) {
|
|
59
|
+
this.bytes = t, this.entries = o.map(({ compressedDataStart: s, ...c }) => c), this.entryMap = new Map(o.map((s) => [s.name, s])), this.manifest = n;
|
|
60
|
+
}
|
|
61
|
+
static async open(t) {
|
|
62
|
+
const o = t instanceof Uint8Array ? t : t instanceof Blob ? new Uint8Array(await t.arrayBuffer()) : new Uint8Array(t), n = D(o);
|
|
63
|
+
if (!n.find((r) => r.name === b))
|
|
64
|
+
throw new Error(`Archive does not contain '${b}'.`);
|
|
65
|
+
const e = await new y(o, n, {
|
|
66
|
+
format: g,
|
|
67
|
+
version: 0,
|
|
68
|
+
kind: "records"
|
|
69
|
+
}).readJson(b);
|
|
70
|
+
if (e.format !== g)
|
|
71
|
+
throw new Error(`Unsupported archive format '${String(e.format)}'.`);
|
|
72
|
+
if (e.kind !== "protocol" && e.kind !== "protocols" && e.kind !== "records")
|
|
73
|
+
throw new Error(`Unsupported archive kind '${String(e.kind)}'.`);
|
|
74
|
+
return new y(o, n, e);
|
|
75
|
+
}
|
|
76
|
+
has(t) {
|
|
77
|
+
return this.entryMap.has(t);
|
|
78
|
+
}
|
|
79
|
+
summary() {
|
|
80
|
+
const t = Array.isArray(this.manifest.records) ? this.manifest.records.length : 0, o = this.manifest.kind === "protocol" ? 1 : Array.isArray(this.manifest.protocols) ? this.manifest.protocols.length : 0, n = Array.isArray(this.manifest.blobs) ? this.manifest.blobs.length : 0, s = Array.isArray(this.manifest.files) ? this.manifest.files.length : 0;
|
|
81
|
+
return {
|
|
82
|
+
format: this.manifest.format,
|
|
83
|
+
version: this.manifest.version,
|
|
84
|
+
kind: this.manifest.kind,
|
|
85
|
+
createdAt: this.manifest.created_at,
|
|
86
|
+
memberCount: this.entries.length,
|
|
87
|
+
recordCount: t,
|
|
88
|
+
protocolCount: o,
|
|
89
|
+
blobCount: n,
|
|
90
|
+
fileCount: s
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
async readBytes(t) {
|
|
94
|
+
const o = this.entryMap.get(t);
|
|
95
|
+
if (!o)
|
|
96
|
+
throw new Error(`Archive member '${t}' not found.`);
|
|
97
|
+
const n = this.bytes.slice(
|
|
98
|
+
o.compressedDataStart,
|
|
99
|
+
o.compressedDataStart + o.compressedSize
|
|
100
|
+
);
|
|
101
|
+
if (o.compressionMethod === 0)
|
|
102
|
+
return n;
|
|
103
|
+
if (o.compressionMethod === 8)
|
|
104
|
+
return j(n);
|
|
105
|
+
throw new Error(`Archive member '${t}' uses unsupported compression method ${o.compressionMethod}.`);
|
|
106
|
+
}
|
|
107
|
+
async readText(t) {
|
|
108
|
+
return A.decode(await this.readBytes(t));
|
|
109
|
+
}
|
|
110
|
+
async readJson(t) {
|
|
111
|
+
return JSON.parse(await this.readText(t));
|
|
112
|
+
}
|
|
113
|
+
async validate() {
|
|
114
|
+
const t = [], o = new Set(this.entries.map((n) => n.name));
|
|
115
|
+
for (const n of this.entries) {
|
|
116
|
+
const s = v(n.name);
|
|
117
|
+
s && t.push(s);
|
|
118
|
+
}
|
|
119
|
+
return o.has(b) || t.push(`Archive is missing '${b}'.`), this.manifest.format !== g && t.push(`Unsupported archive format '${String(this.manifest.format)}'.`), this.manifest.version !== 1 && t.push(`Unsupported archive version '${String(this.manifest.version)}'.`), this.manifest.kind === "protocol" ? await this.validateProtocol(this.manifest.protocol, "", t) : this.manifest.kind === "protocols" ? await this.validateProtocolList(this.manifest.protocols, t, !0) : this.manifest.kind === "records" ? await this.validateRecords(t) : t.push(`Unsupported archive kind '${String(this.manifest.kind)}'.`), { ok: t.length === 0, issues: t };
|
|
120
|
+
}
|
|
121
|
+
async validateProtocol(t, o, n) {
|
|
122
|
+
if (!t || typeof t != "object") {
|
|
123
|
+
n.push("Protocol manifest entry must be an object.");
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const s = t.entrypoint || "protocol.aimd", c = `${o}${s}`;
|
|
127
|
+
this.has(c) || n.push(`Protocol entrypoint '${c}' is missing.`);
|
|
128
|
+
const e = Array.isArray(t.files) ? t.files : [], r = t.file_hashes && typeof t.file_hashes == "object" ? t.file_hashes : {};
|
|
129
|
+
for (const a of e) {
|
|
130
|
+
const h = `${o}${a}`, l = v(h);
|
|
131
|
+
if (l) {
|
|
132
|
+
n.push(l);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (!this.has(h)) {
|
|
136
|
+
n.push(`Protocol file '${h}' is missing.`);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
const f = r[a];
|
|
140
|
+
if (f) {
|
|
141
|
+
const d = await _(await this.readBytes(h));
|
|
142
|
+
d !== f && n.push(`Protocol file '${h}' sha256 mismatch: expected ${f}, got ${d}.`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async validateProtocolList(t, o, n = !1) {
|
|
147
|
+
const s = /* @__PURE__ */ new Set();
|
|
148
|
+
if (!Array.isArray(t))
|
|
149
|
+
return o.push("Protocols manifest field must be a list."), s;
|
|
150
|
+
n && t.length === 0 && o.push("Protocols manifest field must include at least one protocol.");
|
|
151
|
+
for (const [c, e] of t.entries()) {
|
|
152
|
+
if (!e || typeof e != "object") {
|
|
153
|
+
o.push(`Protocol manifest entry #${c + 1} must be an object.`);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (!e.archive_root) {
|
|
157
|
+
o.push(`Protocol manifest entry #${c + 1} is missing archive_root.`);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (s.has(e.archive_root)) {
|
|
161
|
+
o.push(`Protocol manifest entry #${c + 1} uses duplicate archive_root '${e.archive_root}'.`);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
s.add(e.archive_root), await this.validateProtocol(e, `${e.archive_root.replace(/\/+$/, "")}/`, o);
|
|
165
|
+
}
|
|
166
|
+
return s;
|
|
167
|
+
}
|
|
168
|
+
async validateRecords(t) {
|
|
169
|
+
const o = Array.isArray(this.manifest.records) ? this.manifest.records : [], n = await this.validateProtocolList(this.manifest.protocols, t), s = /* @__PURE__ */ new Set();
|
|
170
|
+
for (const [e, r] of o.entries()) {
|
|
171
|
+
if (!r || typeof r != "object") {
|
|
172
|
+
t.push(`Record manifest entry #${e + 1} must be an object.`);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const a = r.path;
|
|
176
|
+
if (!a) {
|
|
177
|
+
t.push(`Record manifest entry #${e + 1} is missing a path.`);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
const h = v(a);
|
|
181
|
+
if (h) {
|
|
182
|
+
t.push(h);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (!this.has(a)) {
|
|
186
|
+
t.push(`Record file '${a}' is missing.`);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
s.add(a);
|
|
190
|
+
let l;
|
|
191
|
+
try {
|
|
192
|
+
l = await this.readBytes(a), JSON.parse(A.decode(l));
|
|
193
|
+
} catch {
|
|
194
|
+
t.push(`Record file '${a}' is not valid UTF-8 JSON.`);
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (r.sha256) {
|
|
198
|
+
const f = await _(l);
|
|
199
|
+
f !== r.sha256 && t.push(`Record file '${a}' sha256 mismatch: expected ${r.sha256}, got ${f}.`);
|
|
200
|
+
}
|
|
201
|
+
r.embedded_protocol_root && !n.has(r.embedded_protocol_root) && t.push(`Record file '${a}' references missing embedded protocol root '${r.embedded_protocol_root}'.`);
|
|
202
|
+
}
|
|
203
|
+
const c = await this.validateBlobs(t);
|
|
204
|
+
this.validateFileReferences(t, c, s);
|
|
205
|
+
}
|
|
206
|
+
async validateBlobs(t) {
|
|
207
|
+
const o = /* @__PURE__ */ new Set(), n = this.manifest.blobs;
|
|
208
|
+
if (n === void 0)
|
|
209
|
+
return o;
|
|
210
|
+
if (!Array.isArray(n))
|
|
211
|
+
return t.push("Blobs manifest field must be a list."), o;
|
|
212
|
+
const s = /* @__PURE__ */ new Set();
|
|
213
|
+
for (const [c, e] of n.entries()) {
|
|
214
|
+
if (!e || typeof e != "object") {
|
|
215
|
+
t.push(`Blob manifest entry #${c + 1} must be an object.`);
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
const r = e.blob_id, a = e.archive_path, h = e.sha256;
|
|
219
|
+
if (!r) {
|
|
220
|
+
t.push(`Blob manifest entry #${c + 1} is missing blob_id.`);
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (o.has(r)) {
|
|
224
|
+
t.push(`Blob manifest entry #${c + 1} uses duplicate blob_id '${r}'.`);
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (o.add(r), !h || !R.test(h)) {
|
|
228
|
+
t.push(`Blob '${r}' must include a valid sha256 hash.`);
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
const l = `sha256:${h}`;
|
|
232
|
+
if (r !== l && t.push(`Blob '${r}' does not match sha256-derived id '${l}'.`), !a) {
|
|
233
|
+
t.push(`Blob '${r}' is missing archive_path.`);
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
const f = v(a);
|
|
237
|
+
if (f) {
|
|
238
|
+
t.push(f);
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (a.startsWith("blobs/sha256/") || t.push(`Blob '${r}' archive_path must be under 'blobs/sha256/'.`), s.has(a)) {
|
|
242
|
+
t.push(`Blob '${r}' uses duplicate archive_path '${a}'.`);
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (s.add(a), !this.has(a)) {
|
|
246
|
+
t.push(`Blob file '${a}' is missing.`);
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
const d = await this.readBytes(a), $ = await _(d);
|
|
250
|
+
$ !== h && t.push(`Blob file '${a}' sha256 mismatch: expected ${h}, got ${$}.`), typeof e.size == "number" && e.size !== d.byteLength ? t.push(`Blob file '${a}' size mismatch: expected ${e.size}, got ${d.byteLength}.`) : e.size !== void 0 && typeof e.size != "number" && t.push(`Blob '${r}' size must be a number when present.`);
|
|
251
|
+
}
|
|
252
|
+
return o;
|
|
253
|
+
}
|
|
254
|
+
validateFileReferences(t, o, n) {
|
|
255
|
+
const s = this.manifest.files;
|
|
256
|
+
if (s !== void 0) {
|
|
257
|
+
if (!Array.isArray(s)) {
|
|
258
|
+
t.push("Files manifest field must be a list.");
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
for (const [c, e] of s.entries()) {
|
|
262
|
+
if (!e || typeof e != "object") {
|
|
263
|
+
t.push(`File manifest entry #${c + 1} must be an object.`);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
!e.file_id && !e.source_uri && !e.blob_id && t.push(`File manifest entry #${c + 1} must include file_id, source_uri, or blob_id.`), e.blob_id && !o.has(e.blob_id) && t.push(`File manifest entry #${c + 1} references missing blob_id '${e.blob_id}'.`), e.record_path && !n.has(e.record_path) && t.push(`File manifest entry #${c + 1} references missing record_path '${e.record_path}'.`), e.field_path !== void 0 && typeof e.field_path != "string" && t.push(`File manifest entry #${c + 1} field_path must be a string.`);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
async function M(i) {
|
|
272
|
+
return y.open(i);
|
|
273
|
+
}
|
|
274
|
+
async function O(i) {
|
|
275
|
+
return (await y.open(i)).summary();
|
|
276
|
+
}
|
|
277
|
+
function z(i) {
|
|
278
|
+
return JSON.stringify(i, null, 2);
|
|
279
|
+
}
|
|
280
|
+
function F(i) {
|
|
281
|
+
return E.encode(i);
|
|
282
|
+
}
|
|
283
|
+
export {
|
|
284
|
+
g as AIRA_ARCHIVE_FORMAT,
|
|
285
|
+
b as AIRA_MANIFEST_PATH,
|
|
286
|
+
y as AiraArchive,
|
|
287
|
+
F as encodeUtf8,
|
|
288
|
+
M as openAiraArchive,
|
|
289
|
+
z as prettyPrintJson,
|
|
290
|
+
O as readAiraArchiveSummary
|
|
291
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@airalogy/aira-core",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "Core parser and validator for Airalogy .aira archives",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/airalogy/airalogy.git",
|
|
10
|
+
"directory": "packages/npm/aira-core"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/airalogy/airalogy/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/airalogy/airalogy/tree/main/packages/npm/aira-core#readme",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"airalogy",
|
|
18
|
+
"aira",
|
|
19
|
+
"archive",
|
|
20
|
+
"record",
|
|
21
|
+
"protocol"
|
|
22
|
+
],
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./src/index.ts",
|
|
26
|
+
"import": "./src/index.ts"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"main": "./src/index.ts",
|
|
30
|
+
"types": "./src/index.ts",
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"src"
|
|
37
|
+
],
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^24.3.0",
|
|
40
|
+
"typescript": "5.8.3",
|
|
41
|
+
"vite": "^7.1.3"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"type-check": "tsc --noEmit",
|
|
45
|
+
"build:types": "tsc --emitDeclarationOnly --declaration --outDir dist",
|
|
46
|
+
"build": "vite build && pnpm run build:types",
|
|
47
|
+
"dev": "vite build --watch",
|
|
48
|
+
"test": "pnpm run build && node --test ./tests/*.test.mjs"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
export const AIRA_MANIFEST_PATH = '_airalogy_archive/manifest.json'
|
|
2
|
+
export const AIRA_ARCHIVE_FORMAT = 'airalogy.archive'
|
|
3
|
+
|
|
4
|
+
export type AiraArchiveKind = 'protocol' | 'protocols' | 'records'
|
|
5
|
+
|
|
6
|
+
export interface AiraEntry {
|
|
7
|
+
name: string
|
|
8
|
+
compressedSize: number
|
|
9
|
+
uncompressedSize: number
|
|
10
|
+
compressionMethod: number
|
|
11
|
+
localHeaderOffset: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface AiraProtocolManifest {
|
|
15
|
+
protocol_id?: string | null
|
|
16
|
+
protocol_version?: string | null
|
|
17
|
+
protocol_name?: string | null
|
|
18
|
+
entrypoint?: string
|
|
19
|
+
archive_root?: string
|
|
20
|
+
files?: string[]
|
|
21
|
+
file_hashes?: Record<string, string>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface AiraRecordManifest {
|
|
25
|
+
path: string
|
|
26
|
+
record_id?: string | null
|
|
27
|
+
record_version?: string | number | null
|
|
28
|
+
protocol_id?: string | null
|
|
29
|
+
protocol_version?: string | null
|
|
30
|
+
sha1?: string | null
|
|
31
|
+
sha256?: string | null
|
|
32
|
+
source_path?: string
|
|
33
|
+
source_index?: number
|
|
34
|
+
embedded_protocol_root?: string | null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface AiraBlobManifest {
|
|
38
|
+
blob_id: string
|
|
39
|
+
archive_path: string
|
|
40
|
+
sha256: string
|
|
41
|
+
size?: number
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface AiraFileManifest {
|
|
45
|
+
file_id?: string | null
|
|
46
|
+
source_uri?: string | null
|
|
47
|
+
blob_id?: string | null
|
|
48
|
+
filename?: string | null
|
|
49
|
+
mime_type?: string | null
|
|
50
|
+
size?: number | null
|
|
51
|
+
record_path?: string | null
|
|
52
|
+
field_path?: string | null
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface AiraManifest {
|
|
56
|
+
format: string
|
|
57
|
+
version: number
|
|
58
|
+
kind: AiraArchiveKind
|
|
59
|
+
created_at?: string
|
|
60
|
+
protocol?: AiraProtocolManifest
|
|
61
|
+
records?: AiraRecordManifest[]
|
|
62
|
+
protocols?: AiraProtocolManifest[]
|
|
63
|
+
blobs?: AiraBlobManifest[]
|
|
64
|
+
files?: AiraFileManifest[]
|
|
65
|
+
[key: string]: unknown
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface AiraSummary {
|
|
69
|
+
format: string
|
|
70
|
+
version: number
|
|
71
|
+
kind: AiraArchiveKind
|
|
72
|
+
createdAt?: string
|
|
73
|
+
memberCount: number
|
|
74
|
+
recordCount: number
|
|
75
|
+
protocolCount: number
|
|
76
|
+
blobCount: number
|
|
77
|
+
fileCount: number
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface AiraValidationResult {
|
|
81
|
+
ok: boolean
|
|
82
|
+
issues: string[]
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
type ZipEntryInternal = AiraEntry & {
|
|
86
|
+
compressedDataStart: number
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const textDecoder = new TextDecoder('utf-8')
|
|
90
|
+
const textEncoder = new TextEncoder()
|
|
91
|
+
const sha256Pattern = /^[0-9a-f]{64}$/
|
|
92
|
+
|
|
93
|
+
function getUint16(view: DataView, offset: number): number {
|
|
94
|
+
return view.getUint16(offset, true)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function getUint32(view: DataView, offset: number): number {
|
|
98
|
+
return view.getUint32(offset, true)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function decodeName(bytes: Uint8Array): string {
|
|
102
|
+
return textDecoder.decode(bytes)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function validateMemberPath(name: string): string | null {
|
|
106
|
+
if (!name || name.startsWith('/')) {
|
|
107
|
+
return `Archive member '${name}' uses an absolute or empty path.`
|
|
108
|
+
}
|
|
109
|
+
if (name.split('/').some(part => part === '..')) {
|
|
110
|
+
return `Archive member '${name}' escapes the archive root.`
|
|
111
|
+
}
|
|
112
|
+
return null
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function findEndOfCentralDirectory(bytes: Uint8Array): number {
|
|
116
|
+
const minOffset = Math.max(0, bytes.length - 0xffff - 22)
|
|
117
|
+
for (let offset = bytes.length - 22; offset >= minOffset; offset -= 1) {
|
|
118
|
+
if (
|
|
119
|
+
bytes[offset] === 0x50
|
|
120
|
+
&& bytes[offset + 1] === 0x4b
|
|
121
|
+
&& bytes[offset + 2] === 0x05
|
|
122
|
+
&& bytes[offset + 3] === 0x06
|
|
123
|
+
) {
|
|
124
|
+
return offset
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
throw new Error('Archive is not a valid zip file: EOCD not found.')
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function parseZipEntries(bytes: Uint8Array): ZipEntryInternal[] {
|
|
131
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
132
|
+
const eocdOffset = findEndOfCentralDirectory(bytes)
|
|
133
|
+
const entryCount = getUint16(view, eocdOffset + 10)
|
|
134
|
+
const centralDirectoryOffset = getUint32(view, eocdOffset + 16)
|
|
135
|
+
const entries: ZipEntryInternal[] = []
|
|
136
|
+
let offset = centralDirectoryOffset
|
|
137
|
+
|
|
138
|
+
for (let index = 0; index < entryCount; index += 1) {
|
|
139
|
+
if (getUint32(view, offset) !== 0x02014b50) {
|
|
140
|
+
throw new Error(`Archive central directory is invalid at offset ${offset}.`)
|
|
141
|
+
}
|
|
142
|
+
const compressionMethod = getUint16(view, offset + 10)
|
|
143
|
+
const compressedSize = getUint32(view, offset + 20)
|
|
144
|
+
const uncompressedSize = getUint32(view, offset + 24)
|
|
145
|
+
const fileNameLength = getUint16(view, offset + 28)
|
|
146
|
+
const extraLength = getUint16(view, offset + 30)
|
|
147
|
+
const commentLength = getUint16(view, offset + 32)
|
|
148
|
+
const localHeaderOffset = getUint32(view, offset + 42)
|
|
149
|
+
const fileNameStart = offset + 46
|
|
150
|
+
const name = decodeName(bytes.slice(fileNameStart, fileNameStart + fileNameLength))
|
|
151
|
+
|
|
152
|
+
if (getUint32(view, localHeaderOffset) !== 0x04034b50) {
|
|
153
|
+
throw new Error(`Archive local header is invalid for '${name}'.`)
|
|
154
|
+
}
|
|
155
|
+
const localNameLength = getUint16(view, localHeaderOffset + 26)
|
|
156
|
+
const localExtraLength = getUint16(view, localHeaderOffset + 28)
|
|
157
|
+
const compressedDataStart = localHeaderOffset + 30 + localNameLength + localExtraLength
|
|
158
|
+
|
|
159
|
+
entries.push({
|
|
160
|
+
name,
|
|
161
|
+
compressionMethod,
|
|
162
|
+
compressedSize,
|
|
163
|
+
uncompressedSize,
|
|
164
|
+
localHeaderOffset,
|
|
165
|
+
compressedDataStart,
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
offset = fileNameStart + fileNameLength + extraLength + commentLength
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return entries.filter(entry => !entry.name.endsWith('/'))
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async function inflateRaw(data: Uint8Array): Promise<Uint8Array> {
|
|
175
|
+
const DecompressionStreamClass = globalThis.DecompressionStream
|
|
176
|
+
if (!DecompressionStreamClass) {
|
|
177
|
+
throw new Error('This browser does not support DecompressionStream.')
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const stream = new Blob([data]).stream().pipeThrough(new DecompressionStreamClass('deflate-raw'))
|
|
181
|
+
const buffer = await new Response(stream).arrayBuffer()
|
|
182
|
+
return new Uint8Array(buffer)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function sha256Hex(bytes: Uint8Array): Promise<string> {
|
|
186
|
+
const digest = await crypto.subtle.digest('SHA-256', bytes)
|
|
187
|
+
return Array.from(new Uint8Array(digest))
|
|
188
|
+
.map(value => value.toString(16).padStart(2, '0'))
|
|
189
|
+
.join('')
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export class AiraArchive {
|
|
193
|
+
readonly bytes: Uint8Array
|
|
194
|
+
readonly entries: AiraEntry[]
|
|
195
|
+
readonly manifest: AiraManifest
|
|
196
|
+
|
|
197
|
+
private readonly entryMap: Map<string, ZipEntryInternal>
|
|
198
|
+
|
|
199
|
+
private constructor(bytes: Uint8Array, entries: ZipEntryInternal[], manifest: AiraManifest) {
|
|
200
|
+
this.bytes = bytes
|
|
201
|
+
this.entries = entries.map(({ compressedDataStart: _compressedDataStart, ...entry }) => entry)
|
|
202
|
+
this.entryMap = new Map(entries.map(entry => [entry.name, entry]))
|
|
203
|
+
this.manifest = manifest
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
static async open(input: ArrayBuffer | Blob | Uint8Array): Promise<AiraArchive> {
|
|
207
|
+
const bytes = input instanceof Uint8Array
|
|
208
|
+
? input
|
|
209
|
+
: input instanceof Blob
|
|
210
|
+
? new Uint8Array(await input.arrayBuffer())
|
|
211
|
+
: new Uint8Array(input)
|
|
212
|
+
const entries = parseZipEntries(bytes)
|
|
213
|
+
const manifestEntry = entries.find(entry => entry.name === AIRA_MANIFEST_PATH)
|
|
214
|
+
if (!manifestEntry) {
|
|
215
|
+
throw new Error(`Archive does not contain '${AIRA_MANIFEST_PATH}'.`)
|
|
216
|
+
}
|
|
217
|
+
const archive = new AiraArchive(bytes, entries, {
|
|
218
|
+
format: AIRA_ARCHIVE_FORMAT,
|
|
219
|
+
version: 0,
|
|
220
|
+
kind: 'records',
|
|
221
|
+
})
|
|
222
|
+
const manifest = await archive.readJson<AiraManifest>(AIRA_MANIFEST_PATH)
|
|
223
|
+
if (manifest.format !== AIRA_ARCHIVE_FORMAT) {
|
|
224
|
+
throw new Error(`Unsupported archive format '${String(manifest.format)}'.`)
|
|
225
|
+
}
|
|
226
|
+
if (manifest.kind !== 'protocol' && manifest.kind !== 'protocols' && manifest.kind !== 'records') {
|
|
227
|
+
throw new Error(`Unsupported archive kind '${String(manifest.kind)}'.`)
|
|
228
|
+
}
|
|
229
|
+
return new AiraArchive(bytes, entries, manifest)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
has(path: string): boolean {
|
|
233
|
+
return this.entryMap.has(path)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
summary(): AiraSummary {
|
|
237
|
+
const records = Array.isArray(this.manifest.records) ? this.manifest.records.length : 0
|
|
238
|
+
const protocols = this.manifest.kind === 'protocol'
|
|
239
|
+
? 1
|
|
240
|
+
: Array.isArray(this.manifest.protocols) ? this.manifest.protocols.length : 0
|
|
241
|
+
const blobs = Array.isArray(this.manifest.blobs) ? this.manifest.blobs.length : 0
|
|
242
|
+
const files = Array.isArray(this.manifest.files) ? this.manifest.files.length : 0
|
|
243
|
+
return {
|
|
244
|
+
format: this.manifest.format,
|
|
245
|
+
version: this.manifest.version,
|
|
246
|
+
kind: this.manifest.kind,
|
|
247
|
+
createdAt: this.manifest.created_at,
|
|
248
|
+
memberCount: this.entries.length,
|
|
249
|
+
recordCount: records,
|
|
250
|
+
protocolCount: protocols,
|
|
251
|
+
blobCount: blobs,
|
|
252
|
+
fileCount: files,
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
async readBytes(path: string): Promise<Uint8Array> {
|
|
257
|
+
const entry = this.entryMap.get(path)
|
|
258
|
+
if (!entry) {
|
|
259
|
+
throw new Error(`Archive member '${path}' not found.`)
|
|
260
|
+
}
|
|
261
|
+
const compressed = this.bytes.slice(
|
|
262
|
+
entry.compressedDataStart,
|
|
263
|
+
entry.compressedDataStart + entry.compressedSize,
|
|
264
|
+
)
|
|
265
|
+
if (entry.compressionMethod === 0) {
|
|
266
|
+
return compressed
|
|
267
|
+
}
|
|
268
|
+
if (entry.compressionMethod === 8) {
|
|
269
|
+
return inflateRaw(compressed)
|
|
270
|
+
}
|
|
271
|
+
throw new Error(`Archive member '${path}' uses unsupported compression method ${entry.compressionMethod}.`)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async readText(path: string): Promise<string> {
|
|
275
|
+
return textDecoder.decode(await this.readBytes(path))
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async readJson<T = unknown>(path: string): Promise<T> {
|
|
279
|
+
return JSON.parse(await this.readText(path)) as T
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async validate(): Promise<AiraValidationResult> {
|
|
283
|
+
const issues: string[] = []
|
|
284
|
+
const entryNames = new Set(this.entries.map(entry => entry.name))
|
|
285
|
+
for (const entry of this.entries) {
|
|
286
|
+
const issue = validateMemberPath(entry.name)
|
|
287
|
+
if (issue) {
|
|
288
|
+
issues.push(issue)
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (!entryNames.has(AIRA_MANIFEST_PATH)) {
|
|
292
|
+
issues.push(`Archive is missing '${AIRA_MANIFEST_PATH}'.`)
|
|
293
|
+
}
|
|
294
|
+
if (this.manifest.format !== AIRA_ARCHIVE_FORMAT) {
|
|
295
|
+
issues.push(`Unsupported archive format '${String(this.manifest.format)}'.`)
|
|
296
|
+
}
|
|
297
|
+
if (this.manifest.version !== 1) {
|
|
298
|
+
issues.push(`Unsupported archive version '${String(this.manifest.version)}'.`)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (this.manifest.kind === 'protocol') {
|
|
302
|
+
await this.validateProtocol(this.manifest.protocol, '', issues)
|
|
303
|
+
}
|
|
304
|
+
else if (this.manifest.kind === 'protocols') {
|
|
305
|
+
await this.validateProtocolList(this.manifest.protocols, issues, true)
|
|
306
|
+
}
|
|
307
|
+
else if (this.manifest.kind === 'records') {
|
|
308
|
+
await this.validateRecords(issues)
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
issues.push(`Unsupported archive kind '${String(this.manifest.kind)}'.`)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return { ok: issues.length === 0, issues }
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
private async validateProtocol(protocol: AiraProtocolManifest | undefined, prefix: string, issues: string[]): Promise<void> {
|
|
318
|
+
if (!protocol || typeof protocol !== 'object') {
|
|
319
|
+
issues.push('Protocol manifest entry must be an object.')
|
|
320
|
+
return
|
|
321
|
+
}
|
|
322
|
+
const entrypoint = protocol.entrypoint || 'protocol.aimd'
|
|
323
|
+
const entrypointPath = `${prefix}${entrypoint}`
|
|
324
|
+
if (!this.has(entrypointPath)) {
|
|
325
|
+
issues.push(`Protocol entrypoint '${entrypointPath}' is missing.`)
|
|
326
|
+
}
|
|
327
|
+
const files = Array.isArray(protocol.files) ? protocol.files : []
|
|
328
|
+
const fileHashes = protocol.file_hashes && typeof protocol.file_hashes === 'object'
|
|
329
|
+
? protocol.file_hashes
|
|
330
|
+
: {}
|
|
331
|
+
for (const file of files) {
|
|
332
|
+
const path = `${prefix}${file}`
|
|
333
|
+
const pathIssue = validateMemberPath(path)
|
|
334
|
+
if (pathIssue) {
|
|
335
|
+
issues.push(pathIssue)
|
|
336
|
+
continue
|
|
337
|
+
}
|
|
338
|
+
if (!this.has(path)) {
|
|
339
|
+
issues.push(`Protocol file '${path}' is missing.`)
|
|
340
|
+
continue
|
|
341
|
+
}
|
|
342
|
+
const expectedHash = fileHashes[file]
|
|
343
|
+
if (expectedHash) {
|
|
344
|
+
const actualHash = await sha256Hex(await this.readBytes(path))
|
|
345
|
+
if (actualHash !== expectedHash) {
|
|
346
|
+
issues.push(`Protocol file '${path}' sha256 mismatch: expected ${expectedHash}, got ${actualHash}.`)
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
private async validateProtocolList(
|
|
353
|
+
protocols: AiraProtocolManifest[] | undefined,
|
|
354
|
+
issues: string[],
|
|
355
|
+
requireNonEmpty = false,
|
|
356
|
+
): Promise<Set<string>> {
|
|
357
|
+
const protocolRoots = new Set<string>()
|
|
358
|
+
if (!Array.isArray(protocols)) {
|
|
359
|
+
issues.push('Protocols manifest field must be a list.')
|
|
360
|
+
return protocolRoots
|
|
361
|
+
}
|
|
362
|
+
if (requireNonEmpty && protocols.length === 0) {
|
|
363
|
+
issues.push('Protocols manifest field must include at least one protocol.')
|
|
364
|
+
}
|
|
365
|
+
for (const [index, protocol] of protocols.entries()) {
|
|
366
|
+
if (!protocol || typeof protocol !== 'object') {
|
|
367
|
+
issues.push(`Protocol manifest entry #${index + 1} must be an object.`)
|
|
368
|
+
continue
|
|
369
|
+
}
|
|
370
|
+
if (!protocol.archive_root) {
|
|
371
|
+
issues.push(`Protocol manifest entry #${index + 1} is missing archive_root.`)
|
|
372
|
+
continue
|
|
373
|
+
}
|
|
374
|
+
if (protocolRoots.has(protocol.archive_root)) {
|
|
375
|
+
issues.push(`Protocol manifest entry #${index + 1} uses duplicate archive_root '${protocol.archive_root}'.`)
|
|
376
|
+
continue
|
|
377
|
+
}
|
|
378
|
+
protocolRoots.add(protocol.archive_root)
|
|
379
|
+
await this.validateProtocol(protocol, `${protocol.archive_root.replace(/\/+$/, '')}/`, issues)
|
|
380
|
+
}
|
|
381
|
+
return protocolRoots
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
private async validateRecords(issues: string[]): Promise<void> {
|
|
385
|
+
const records = Array.isArray(this.manifest.records) ? this.manifest.records : []
|
|
386
|
+
const protocolRoots = await this.validateProtocolList(this.manifest.protocols, issues)
|
|
387
|
+
const recordPaths = new Set<string>()
|
|
388
|
+
|
|
389
|
+
for (const [index, record] of records.entries()) {
|
|
390
|
+
if (!record || typeof record !== 'object') {
|
|
391
|
+
issues.push(`Record manifest entry #${index + 1} must be an object.`)
|
|
392
|
+
continue
|
|
393
|
+
}
|
|
394
|
+
const path = record.path
|
|
395
|
+
if (!path) {
|
|
396
|
+
issues.push(`Record manifest entry #${index + 1} is missing a path.`)
|
|
397
|
+
continue
|
|
398
|
+
}
|
|
399
|
+
const pathIssue = validateMemberPath(path)
|
|
400
|
+
if (pathIssue) {
|
|
401
|
+
issues.push(pathIssue)
|
|
402
|
+
continue
|
|
403
|
+
}
|
|
404
|
+
if (!this.has(path)) {
|
|
405
|
+
issues.push(`Record file '${path}' is missing.`)
|
|
406
|
+
continue
|
|
407
|
+
}
|
|
408
|
+
recordPaths.add(path)
|
|
409
|
+
let raw: Uint8Array
|
|
410
|
+
try {
|
|
411
|
+
raw = await this.readBytes(path)
|
|
412
|
+
JSON.parse(textDecoder.decode(raw))
|
|
413
|
+
}
|
|
414
|
+
catch {
|
|
415
|
+
issues.push(`Record file '${path}' is not valid UTF-8 JSON.`)
|
|
416
|
+
continue
|
|
417
|
+
}
|
|
418
|
+
if (record.sha256) {
|
|
419
|
+
const actualHash = await sha256Hex(raw)
|
|
420
|
+
if (actualHash !== record.sha256) {
|
|
421
|
+
issues.push(`Record file '${path}' sha256 mismatch: expected ${record.sha256}, got ${actualHash}.`)
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
if (record.embedded_protocol_root && !protocolRoots.has(record.embedded_protocol_root)) {
|
|
425
|
+
issues.push(`Record file '${path}' references missing embedded protocol root '${record.embedded_protocol_root}'.`)
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
const blobIds = await this.validateBlobs(issues)
|
|
429
|
+
this.validateFileReferences(issues, blobIds, recordPaths)
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
private async validateBlobs(issues: string[]): Promise<Set<string>> {
|
|
433
|
+
const blobIds = new Set<string>()
|
|
434
|
+
const blobs = this.manifest.blobs
|
|
435
|
+
if (blobs === undefined) {
|
|
436
|
+
return blobIds
|
|
437
|
+
}
|
|
438
|
+
if (!Array.isArray(blobs)) {
|
|
439
|
+
issues.push('Blobs manifest field must be a list.')
|
|
440
|
+
return blobIds
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const archivePaths = new Set<string>()
|
|
444
|
+
for (const [index, blob] of blobs.entries()) {
|
|
445
|
+
if (!blob || typeof blob !== 'object') {
|
|
446
|
+
issues.push(`Blob manifest entry #${index + 1} must be an object.`)
|
|
447
|
+
continue
|
|
448
|
+
}
|
|
449
|
+
const blobId = blob.blob_id
|
|
450
|
+
const archivePath = blob.archive_path
|
|
451
|
+
const expectedHash = blob.sha256
|
|
452
|
+
|
|
453
|
+
if (!blobId) {
|
|
454
|
+
issues.push(`Blob manifest entry #${index + 1} is missing blob_id.`)
|
|
455
|
+
continue
|
|
456
|
+
}
|
|
457
|
+
if (blobIds.has(blobId)) {
|
|
458
|
+
issues.push(`Blob manifest entry #${index + 1} uses duplicate blob_id '${blobId}'.`)
|
|
459
|
+
continue
|
|
460
|
+
}
|
|
461
|
+
blobIds.add(blobId)
|
|
462
|
+
|
|
463
|
+
if (!expectedHash || !sha256Pattern.test(expectedHash)) {
|
|
464
|
+
issues.push(`Blob '${blobId}' must include a valid sha256 hash.`)
|
|
465
|
+
continue
|
|
466
|
+
}
|
|
467
|
+
const expectedBlobId = `sha256:${expectedHash}`
|
|
468
|
+
if (blobId !== expectedBlobId) {
|
|
469
|
+
issues.push(`Blob '${blobId}' does not match sha256-derived id '${expectedBlobId}'.`)
|
|
470
|
+
}
|
|
471
|
+
if (!archivePath) {
|
|
472
|
+
issues.push(`Blob '${blobId}' is missing archive_path.`)
|
|
473
|
+
continue
|
|
474
|
+
}
|
|
475
|
+
const pathIssue = validateMemberPath(archivePath)
|
|
476
|
+
if (pathIssue) {
|
|
477
|
+
issues.push(pathIssue)
|
|
478
|
+
continue
|
|
479
|
+
}
|
|
480
|
+
if (!archivePath.startsWith('blobs/sha256/')) {
|
|
481
|
+
issues.push(`Blob '${blobId}' archive_path must be under 'blobs/sha256/'.`)
|
|
482
|
+
}
|
|
483
|
+
if (archivePaths.has(archivePath)) {
|
|
484
|
+
issues.push(`Blob '${blobId}' uses duplicate archive_path '${archivePath}'.`)
|
|
485
|
+
continue
|
|
486
|
+
}
|
|
487
|
+
archivePaths.add(archivePath)
|
|
488
|
+
if (!this.has(archivePath)) {
|
|
489
|
+
issues.push(`Blob file '${archivePath}' is missing.`)
|
|
490
|
+
continue
|
|
491
|
+
}
|
|
492
|
+
const raw = await this.readBytes(archivePath)
|
|
493
|
+
const actualHash = await sha256Hex(raw)
|
|
494
|
+
if (actualHash !== expectedHash) {
|
|
495
|
+
issues.push(`Blob file '${archivePath}' sha256 mismatch: expected ${expectedHash}, got ${actualHash}.`)
|
|
496
|
+
}
|
|
497
|
+
if (typeof blob.size === 'number' && blob.size !== raw.byteLength) {
|
|
498
|
+
issues.push(`Blob file '${archivePath}' size mismatch: expected ${blob.size}, got ${raw.byteLength}.`)
|
|
499
|
+
}
|
|
500
|
+
else if (blob.size !== undefined && typeof blob.size !== 'number') {
|
|
501
|
+
issues.push(`Blob '${blobId}' size must be a number when present.`)
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
return blobIds
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
private validateFileReferences(issues: string[], blobIds: Set<string>, recordPaths: Set<string>): void {
|
|
508
|
+
const files = this.manifest.files
|
|
509
|
+
if (files === undefined) {
|
|
510
|
+
return
|
|
511
|
+
}
|
|
512
|
+
if (!Array.isArray(files)) {
|
|
513
|
+
issues.push('Files manifest field must be a list.')
|
|
514
|
+
return
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
for (const [index, fileRef] of files.entries()) {
|
|
518
|
+
if (!fileRef || typeof fileRef !== 'object') {
|
|
519
|
+
issues.push(`File manifest entry #${index + 1} must be an object.`)
|
|
520
|
+
continue
|
|
521
|
+
}
|
|
522
|
+
if (!fileRef.file_id && !fileRef.source_uri && !fileRef.blob_id) {
|
|
523
|
+
issues.push(`File manifest entry #${index + 1} must include file_id, source_uri, or blob_id.`)
|
|
524
|
+
}
|
|
525
|
+
if (fileRef.blob_id && !blobIds.has(fileRef.blob_id)) {
|
|
526
|
+
issues.push(`File manifest entry #${index + 1} references missing blob_id '${fileRef.blob_id}'.`)
|
|
527
|
+
}
|
|
528
|
+
if (fileRef.record_path && !recordPaths.has(fileRef.record_path)) {
|
|
529
|
+
issues.push(`File manifest entry #${index + 1} references missing record_path '${fileRef.record_path}'.`)
|
|
530
|
+
}
|
|
531
|
+
if (fileRef.field_path !== undefined && typeof fileRef.field_path !== 'string') {
|
|
532
|
+
issues.push(`File manifest entry #${index + 1} field_path must be a string.`)
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
export async function openAiraArchive(input: ArrayBuffer | Blob | Uint8Array): Promise<AiraArchive> {
|
|
539
|
+
return AiraArchive.open(input)
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
export async function readAiraArchiveSummary(input: ArrayBuffer | Blob | Uint8Array): Promise<AiraSummary> {
|
|
543
|
+
return (await AiraArchive.open(input)).summary()
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
export function prettyPrintJson(value: unknown): string {
|
|
547
|
+
return JSON.stringify(value, null, 2)
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
export function encodeUtf8(value: string): Uint8Array {
|
|
551
|
+
return textEncoder.encode(value)
|
|
552
|
+
}
|