@stigmer/zip-structure 3.13.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 +190 -0
- package/index.d.ts +2 -0
- package/index.d.ts.map +1 -0
- package/index.js +2 -0
- package/index.js.map +1 -0
- package/package.json +36 -0
- package/src/__tests__/zip-structure.test.ts +124 -0
- package/src/index.ts +6 -0
- package/src/testing.ts +205 -0
- package/src/zip-structure.ts +248 -0
- package/testing.d.ts +65 -0
- package/testing.d.ts.map +1 -0
- package/testing.js +132 -0
- package/testing.js.map +1 -0
- package/zip-structure.d.ts +99 -0
- package/zip-structure.d.ts.map +1 -0
- package/zip-structure.js +169 -0
- package/zip-structure.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
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
|
+
Copyright 2026 Stigmer Contributors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/index.d.ts
ADDED
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,KAAK,EACL,iBAAiB,EACjB,KAAK,kBAAkB,GACxB,MAAM,oBAAoB,CAAC"}
|
package/index.js
ADDED
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,KAAK,EACL,iBAAiB,GAElB,MAAM,oBAAoB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stigmer/zip-structure",
|
|
3
|
+
"version": "3.13.0",
|
|
4
|
+
"description": "Policy-free structural ZIP parsing shared by Stigmer's TypeScript processes — central-directory-based entry enumeration (never local-header walks) plus the byte-level archive builder its consumers' tests craft fixtures with",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/stigmer/stigmer.git",
|
|
11
|
+
"directory": "backend/libs/ts/zip-structure"
|
|
12
|
+
},
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": "^22.13.0 || >=23.4.0"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"stigmer",
|
|
18
|
+
"zip",
|
|
19
|
+
"central-directory",
|
|
20
|
+
"structural-parsing"
|
|
21
|
+
],
|
|
22
|
+
"main": "./index.js",
|
|
23
|
+
"types": "./index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./index.d.ts",
|
|
27
|
+
"import": "./index.js",
|
|
28
|
+
"default": "./index.js"
|
|
29
|
+
},
|
|
30
|
+
"./testing": {
|
|
31
|
+
"types": "./testing.d.ts",
|
|
32
|
+
"import": "./testing.js",
|
|
33
|
+
"default": "./testing.js"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The structural layer's own suite. Until the lib extraction this module
|
|
3
|
+
* was tested only through its consumers (the runner's zip-extract and
|
|
4
|
+
* attachment-injector suites); per ci.ts-libs' contract a lib change must
|
|
5
|
+
* fail attributably here even when no consumer file moved.
|
|
6
|
+
*/
|
|
7
|
+
import { inflateRawSync } from "node:zlib";
|
|
8
|
+
import { describe, expect, it } from "vitest";
|
|
9
|
+
|
|
10
|
+
import { buildZip } from "../testing.js";
|
|
11
|
+
import { parseZipStructure } from "../zip-structure.js";
|
|
12
|
+
|
|
13
|
+
function text(bytes: Uint8Array): string {
|
|
14
|
+
return new TextDecoder().decode(bytes);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe("parseZipStructure", () => {
|
|
18
|
+
it("enumerates stored entries with names, sizes, and payload views", () => {
|
|
19
|
+
const zip = buildZip([
|
|
20
|
+
{ name: "SKILL.md", content: "# Skill" },
|
|
21
|
+
{ name: "references/schema.md", content: "tables" },
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
const entries = parseZipStructure(zip);
|
|
25
|
+
expect(entries).toHaveLength(2);
|
|
26
|
+
expect(entries[0]!.name).toBe("SKILL.md");
|
|
27
|
+
expect(entries[0]!.compressionMethod).toBe(0);
|
|
28
|
+
expect(entries[0]!.uncompressedSize).toBe("# Skill".length);
|
|
29
|
+
expect(entries[0]!.compressedSize).toBe("# Skill".length);
|
|
30
|
+
expect(text(entries[0]!.compressedData)).toBe("# Skill");
|
|
31
|
+
expect(entries[1]!.name).toBe("references/schema.md");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("inflates a deflated entry's payload back to the original bytes", () => {
|
|
35
|
+
const content = "compressed with deflate, long enough to actually shrink ".repeat(8);
|
|
36
|
+
const zip = buildZip([{ name: "notes.txt", content, method: "deflated" }]);
|
|
37
|
+
|
|
38
|
+
const [entry] = parseZipStructure(zip);
|
|
39
|
+
expect(entry!.compressionMethod).toBe(8);
|
|
40
|
+
expect(text(new Uint8Array(inflateRawSync(entry!.compressedData)))).toBe(content);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("reads sizes from the central directory for streaming entries (zeroed local headers, #450)", () => {
|
|
44
|
+
const content = "streamed by Go's archive/zip writer";
|
|
45
|
+
const zip = buildZip([{ name: "streamed.md", content, streaming: true }]);
|
|
46
|
+
|
|
47
|
+
const [entry] = parseZipStructure(zip);
|
|
48
|
+
expect(entry!.uncompressedSize).toBe(content.length);
|
|
49
|
+
expect(text(entry!.compressedData)).toBe(content);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("marks directory entries", () => {
|
|
53
|
+
const zip = buildZip([
|
|
54
|
+
{ name: "references/", content: "" },
|
|
55
|
+
{ name: "references/data.md", content: "data" },
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
const entries = parseZipStructure(zip);
|
|
59
|
+
expect(entries[0]!.isDirectory).toBe(true);
|
|
60
|
+
expect(entries[1]!.isDirectory).toBe(false);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("locates the EOCD behind a trailing archive comment", () => {
|
|
64
|
+
const zip = buildZip([{ name: "a.md", content: "a" }], { comment: "trailing comment" });
|
|
65
|
+
expect(parseZipStructure(zip)).toHaveLength(1);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("is not fooled by EOCD signature bytes inside the comment", () => {
|
|
69
|
+
// The four signature bytes ("PK\x05\x06") occur INSIDE the comment; a
|
|
70
|
+
// signature-only scan would lock onto them and misparse. The parser
|
|
71
|
+
// requires the candidate record to point at a real central directory.
|
|
72
|
+
const zip = buildZip([{ name: "a.md", content: "a" }], {
|
|
73
|
+
comment: "x PK\x05\x06 y padding padding padding",
|
|
74
|
+
});
|
|
75
|
+
const entries = parseZipStructure(zip);
|
|
76
|
+
expect(entries).toHaveLength(1);
|
|
77
|
+
expect(entries[0]!.name).toBe("a.md");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("parses a genuinely empty archive to zero entries", () => {
|
|
81
|
+
const zip = buildZip([]);
|
|
82
|
+
expect(parseZipStructure(zip)).toEqual([]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("throws when the central directory and EOCD are missing (truncated download)", () => {
|
|
86
|
+
const zip = buildZip([{ name: "a.md", content: "a" }], { omitCentralDirectory: true });
|
|
87
|
+
expect(() => parseZipStructure(zip)).toThrow("no end-of-central-directory record found");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("throws on a corrupted central directory record", () => {
|
|
91
|
+
const zip = buildZip([{ name: "a.md", content: "a" }]);
|
|
92
|
+
// The EOCD's central-directory-offset field (u32 at EOCD+16) locates the
|
|
93
|
+
// record; corrupt the record's signature byte behind it. The EOCD scan
|
|
94
|
+
// then rejects the record (directoryLooksReal fails) and no other valid
|
|
95
|
+
// EOCD exists.
|
|
96
|
+
const view = new DataView(zip.buffer, zip.byteOffset, zip.byteLength);
|
|
97
|
+
const eocdPos = zip.length - 22; // no comment in this fixture
|
|
98
|
+
const cdOffset = view.getUint32(eocdPos + 16, true);
|
|
99
|
+
zip[cdOffset] = 0x00;
|
|
100
|
+
expect(() => parseZipStructure(zip)).toThrow("no end-of-central-directory record found");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("exposes versionMadeBy and externalAttributes verbatim for policy layers", () => {
|
|
104
|
+
// A Unix-creator entry carrying a POSIX mode in the attribute high bits
|
|
105
|
+
// (0o120777 = a symlink) — the raw facts the server's safearchive-parity
|
|
106
|
+
// pre-filter decodes. The structural layer must not interpret them.
|
|
107
|
+
const symlinkMode = (0o120777 << 16) >>> 0;
|
|
108
|
+
const zip = buildZip([
|
|
109
|
+
{
|
|
110
|
+
name: "link",
|
|
111
|
+
content: "target",
|
|
112
|
+
versionMadeBy: 0x0314, // high byte 3 = Unix, low byte 20
|
|
113
|
+
externalAttributes: symlinkMode,
|
|
114
|
+
},
|
|
115
|
+
{ name: "plain.md", content: "plain" },
|
|
116
|
+
]);
|
|
117
|
+
|
|
118
|
+
const entries = parseZipStructure(zip);
|
|
119
|
+
expect(entries[0]!.versionMadeBy).toBe(0x0314);
|
|
120
|
+
expect(entries[0]!.externalAttributes).toBe(symlinkMode);
|
|
121
|
+
expect(entries[1]!.versionMadeBy).toBe(20);
|
|
122
|
+
expect(entries[1]!.externalAttributes).toBe(0);
|
|
123
|
+
});
|
|
124
|
+
});
|
package/src/index.ts
ADDED
package/src/testing.ts
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Byte-level ZIP fixture builder for the structural parser's consumers'
|
|
3
|
+
* tests (exported as `@stigmer/zip-structure/testing`). Moved here from the
|
|
4
|
+
* runner's src/__test-utils__/zip-fixtures.ts when the lib was extracted —
|
|
5
|
+
* the lib's own tests, the runner's, and the TS server's skill-gate tests
|
|
6
|
+
* all craft archives with it, and one builder keeps their fixture shapes
|
|
7
|
+
* from drifting apart.
|
|
8
|
+
*
|
|
9
|
+
* Emits complete, real-shaped archives — local file headers (optionally
|
|
10
|
+
* streaming-style, the Go stdlib writer's default), payloads, central
|
|
11
|
+
* directory, and EOCD — because that is the only shape that can reach the
|
|
12
|
+
* runner: both editions' skill push gates validate artifacts with
|
|
13
|
+
* central-directory-based readers (see zip-structure.ts's module doc).
|
|
14
|
+
* Earlier fixtures emitted local headers only, a shape no real ZIP writer
|
|
15
|
+
* produces, and were coupled to the old parser's front-to-back walk.
|
|
16
|
+
*
|
|
17
|
+
* Mirrors the conformance suite's `zipFilesStreaming()`
|
|
18
|
+
* (test/conformance/src/support/skills.ts) so unit fixtures and
|
|
19
|
+
* cross-edition fixtures share one shape.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { deflateRawSync } from "node:zlib";
|
|
23
|
+
|
|
24
|
+
import { crc32 } from "./zip-structure.js";
|
|
25
|
+
|
|
26
|
+
export interface ZipFixtureFile {
|
|
27
|
+
name: string;
|
|
28
|
+
/** Text content is UTF-8 encoded; pass bytes directly for binary payloads. */
|
|
29
|
+
content: string | Uint8Array;
|
|
30
|
+
/** Compression method for the entry. Defaults to stored. */
|
|
31
|
+
method?: "stored" | "deflated";
|
|
32
|
+
/**
|
|
33
|
+
* Emit the entry the way Go's archive/zip does by default: general-purpose
|
|
34
|
+
* flag bit 3 set, zeroed sizes in the local header, and a trailing data
|
|
35
|
+
* descriptor (with the conventional signature) carrying the real values.
|
|
36
|
+
*/
|
|
37
|
+
streaming?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Lie about the entry's uncompressed size everywhere the writer would
|
|
40
|
+
* declare it (local header, data descriptor, central directory). Models a
|
|
41
|
+
* crafted archive whose declarations disagree with its actual payload —
|
|
42
|
+
* the input class the attachment injector's declared-size enforcement
|
|
43
|
+
* exists to reject (issue #567).
|
|
44
|
+
*/
|
|
45
|
+
declaredUncompressedSize?: number;
|
|
46
|
+
/**
|
|
47
|
+
* The central directory's "version made by" field. Defaults to 20
|
|
48
|
+
* (MS-DOS creator, the historical fixture value). Set the high byte to
|
|
49
|
+
* 3 (`0x0300 | 20`) to model a Unix creator, which makes readers
|
|
50
|
+
* interpret `externalAttributes`' high 16 bits as a POSIX mode — how
|
|
51
|
+
* symlink and device entries are represented (the server's
|
|
52
|
+
* safearchive-parity pre-filter fixtures need this).
|
|
53
|
+
*/
|
|
54
|
+
versionMadeBy?: number;
|
|
55
|
+
/** The central directory's external file attributes field. Defaults to 0. */
|
|
56
|
+
externalAttributes?: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface ZipFixtureOptions {
|
|
60
|
+
/** Trailing archive comment appended after the EOCD record. */
|
|
61
|
+
comment?: string;
|
|
62
|
+
/**
|
|
63
|
+
* Drop the central directory and EOCD, leaving only local headers and
|
|
64
|
+
* payloads. No real ZIP writer produces this shape — it models a download
|
|
65
|
+
* truncated before the archive's index.
|
|
66
|
+
*/
|
|
67
|
+
omitCentralDirectory?: boolean;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Build a ZIP archive from path + content pairs. */
|
|
71
|
+
export function buildZip(files: ZipFixtureFile[], options?: ZipFixtureOptions): Uint8Array {
|
|
72
|
+
const out = new ByteWriter();
|
|
73
|
+
|
|
74
|
+
interface WrittenEntry {
|
|
75
|
+
nameBytes: Uint8Array;
|
|
76
|
+
payload: Uint8Array;
|
|
77
|
+
declaredUncompressed: number;
|
|
78
|
+
crc: number;
|
|
79
|
+
flags: number;
|
|
80
|
+
method: number;
|
|
81
|
+
versionMadeBy: number;
|
|
82
|
+
externalAttributes: number;
|
|
83
|
+
localHeaderOffset: number;
|
|
84
|
+
}
|
|
85
|
+
const written: WrittenEntry[] = [];
|
|
86
|
+
|
|
87
|
+
for (const file of files) {
|
|
88
|
+
const contentBytes =
|
|
89
|
+
typeof file.content === "string" ? new TextEncoder().encode(file.content) : file.content;
|
|
90
|
+
const deflated = file.method === "deflated";
|
|
91
|
+
const payload = deflated ? new Uint8Array(deflateRawSync(contentBytes)) : contentBytes;
|
|
92
|
+
const entry: WrittenEntry = {
|
|
93
|
+
nameBytes: new TextEncoder().encode(file.name),
|
|
94
|
+
payload,
|
|
95
|
+
declaredUncompressed: file.declaredUncompressedSize ?? contentBytes.length,
|
|
96
|
+
crc: crc32(contentBytes),
|
|
97
|
+
flags: file.streaming ? 0x0008 : 0,
|
|
98
|
+
method: deflated ? 8 : 0,
|
|
99
|
+
versionMadeBy: file.versionMadeBy ?? 20,
|
|
100
|
+
externalAttributes: file.externalAttributes ?? 0,
|
|
101
|
+
localHeaderOffset: out.length,
|
|
102
|
+
};
|
|
103
|
+
written.push(entry);
|
|
104
|
+
|
|
105
|
+
out.u32(0x04034b50); // local file header signature
|
|
106
|
+
out.u16(20); // version needed to extract
|
|
107
|
+
out.u16(entry.flags);
|
|
108
|
+
out.u16(entry.method);
|
|
109
|
+
out.u16(0); // mod time
|
|
110
|
+
out.u16(0); // mod date
|
|
111
|
+
out.u32(file.streaming ? 0 : entry.crc);
|
|
112
|
+
out.u32(file.streaming ? 0 : payload.length);
|
|
113
|
+
out.u32(file.streaming ? 0 : entry.declaredUncompressed);
|
|
114
|
+
out.u16(entry.nameBytes.length);
|
|
115
|
+
out.u16(0); // extra field length
|
|
116
|
+
out.raw(entry.nameBytes);
|
|
117
|
+
out.raw(payload);
|
|
118
|
+
|
|
119
|
+
if (file.streaming) {
|
|
120
|
+
out.u32(0x08074b50); // data descriptor signature (Go writes it)
|
|
121
|
+
out.u32(entry.crc);
|
|
122
|
+
out.u32(payload.length);
|
|
123
|
+
out.u32(entry.declaredUncompressed);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (options?.omitCentralDirectory) {
|
|
128
|
+
return out.toUint8Array();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const centralDirectoryOffset = out.length;
|
|
132
|
+
for (const entry of written) {
|
|
133
|
+
out.u32(0x02014b50); // central directory record signature
|
|
134
|
+
out.u16(entry.versionMadeBy);
|
|
135
|
+
out.u16(20); // version needed to extract
|
|
136
|
+
out.u16(entry.flags);
|
|
137
|
+
out.u16(entry.method);
|
|
138
|
+
out.u16(0); // mod time
|
|
139
|
+
out.u16(0); // mod date
|
|
140
|
+
out.u32(entry.crc);
|
|
141
|
+
out.u32(entry.payload.length);
|
|
142
|
+
out.u32(entry.declaredUncompressed);
|
|
143
|
+
out.u16(entry.nameBytes.length);
|
|
144
|
+
out.u16(0); // extra field length
|
|
145
|
+
out.u16(0); // comment length
|
|
146
|
+
out.u16(0); // disk number start
|
|
147
|
+
out.u16(0); // internal attributes
|
|
148
|
+
out.u32(entry.externalAttributes);
|
|
149
|
+
out.u32(entry.localHeaderOffset);
|
|
150
|
+
out.raw(entry.nameBytes);
|
|
151
|
+
}
|
|
152
|
+
const centralDirectorySize = out.length - centralDirectoryOffset;
|
|
153
|
+
|
|
154
|
+
const commentBytes = new TextEncoder().encode(options?.comment ?? "");
|
|
155
|
+
out.u32(0x06054b50); // EOCD signature
|
|
156
|
+
out.u16(0); // disk number
|
|
157
|
+
out.u16(0); // disk with central directory
|
|
158
|
+
out.u16(written.length); // entries on this disk
|
|
159
|
+
out.u16(written.length); // total entries
|
|
160
|
+
out.u32(centralDirectorySize);
|
|
161
|
+
out.u32(centralDirectoryOffset);
|
|
162
|
+
out.u16(commentBytes.length);
|
|
163
|
+
out.raw(commentBytes);
|
|
164
|
+
|
|
165
|
+
return out.toUint8Array();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Chunked assembly instead of a number[]-per-byte accumulator: fixtures at
|
|
169
|
+
// the injector's 100 MB zip-bomb limit are built from payload-sized chunks,
|
|
170
|
+
// which a per-byte spread-push cannot survive (argument-count overflow).
|
|
171
|
+
class ByteWriter {
|
|
172
|
+
private readonly chunks: Uint8Array[] = [];
|
|
173
|
+
private size = 0;
|
|
174
|
+
|
|
175
|
+
get length(): number {
|
|
176
|
+
return this.size;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
u16(v: number): void {
|
|
180
|
+
this.raw(new Uint8Array([v & 0xff, (v >>> 8) & 0xff]));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
u32(v: number): void {
|
|
184
|
+
this.raw(new Uint8Array([v & 0xff, (v >>> 8) & 0xff, (v >>> 16) & 0xff, (v >>> 24) & 0xff]));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
raw(b: Uint8Array): void {
|
|
188
|
+
this.chunks.push(b);
|
|
189
|
+
this.size += b.length;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
toUint8Array(): Uint8Array {
|
|
193
|
+
const result = new Uint8Array(this.size);
|
|
194
|
+
let offset = 0;
|
|
195
|
+
for (const chunk of this.chunks) {
|
|
196
|
+
result.set(chunk, offset);
|
|
197
|
+
offset += chunk.length;
|
|
198
|
+
}
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// CRC-32 comes from the structural module (one implementation for the
|
|
204
|
+
// parser's consumers and this builder; semantically identical to the
|
|
205
|
+
// conformance suite's inline bit-loop helper).
|