@rangefoundry/importers 0.0.0 → 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 +661 -0
- package/README.md +20 -1
- package/dist/cells.d.ts +31 -0
- package/dist/cells.js +22 -0
- package/dist/cfb.d.ts +2 -0
- package/dist/cfb.js +196 -0
- package/dist/csrload.d.ts +12 -0
- package/dist/csrload.js +153 -0
- package/dist/diagnostics.d.ts +42 -0
- package/dist/diagnostics.js +29 -0
- package/dist/generic.d.ts +44 -0
- package/dist/generic.js +272 -0
- package/dist/grtload.d.ts +32 -0
- package/dist/grtload.js +264 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.js +126 -0
- package/dist/limits.d.ts +7 -0
- package/dist/limits.js +12 -0
- package/dist/pressure-history.d.ts +61 -0
- package/dist/pressure-history.js +158 -0
- package/dist/rangefoundry-json.d.ts +17 -0
- package/dist/rangefoundry-json.js +176 -0
- package/dist/record-fields.d.ts +35 -0
- package/dist/record-fields.js +190 -0
- package/dist/record-ids.d.ts +2 -0
- package/dist/record-ids.js +28 -0
- package/dist/records.d.ts +91 -0
- package/dist/records.js +433 -0
- package/dist/result.d.ts +65 -0
- package/dist/result.js +90 -0
- package/dist/shot-rows.d.ts +11 -0
- package/dist/shot-rows.js +39 -0
- package/dist/shotview.d.ts +16 -0
- package/dist/shotview.js +131 -0
- package/dist/statistics.d.ts +12 -0
- package/dist/statistics.js +48 -0
- package/dist/tables.d.ts +41 -0
- package/dist/tables.js +277 -0
- package/dist/template.d.ts +13 -0
- package/dist/template.js +37 -0
- package/dist/text.d.ts +28 -0
- package/dist/text.js +150 -0
- package/dist/units.d.ts +12 -0
- package/dist/units.js +62 -0
- package/dist/workbook.d.ts +12 -0
- package/dist/workbook.js +264 -0
- package/dist/worksheet-strings.d.ts +36 -0
- package/dist/worksheet-strings.js +50 -0
- package/dist/xls.d.ts +3 -0
- package/dist/xls.js +486 -0
- package/dist/xml.d.ts +26 -0
- package/dist/xml.js +261 -0
- package/dist/zip.d.ts +27 -0
- package/dist/zip.js +183 -0
- package/package.json +58 -4
package/dist/xml.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// Minimal XML 1.0 reader for workbook parts (https://www.w3.org/TR/xml/, section numbers refer to it): one left-to-right pass and no document type declarations, so no entity can be declared, expanded or fetched
|
|
2
|
+
import { refuseFile } from './diagnostics.js';
|
|
3
|
+
// assumed bound, not a cited one: worksheet XML nests 5 deep
|
|
4
|
+
const maxDepth = 32;
|
|
5
|
+
// assumed bound, not a cited one: workbook elements carry fewer than 20 attributes
|
|
6
|
+
const maxAttributes = 256;
|
|
7
|
+
const predefined = { lt: '<', gt: '>', amp: '&', quot: '"', apos: "'" };
|
|
8
|
+
/** Section 2.2 `Char`. */
|
|
9
|
+
const isXmlChar = (c) => c === 0x9 ||
|
|
10
|
+
c === 0xa ||
|
|
11
|
+
c === 0xd ||
|
|
12
|
+
(c >= 0x20 && c <= 0xd7ff) ||
|
|
13
|
+
(c >= 0xe000 && c <= 0xfffd) ||
|
|
14
|
+
(c >= 0x10000 && c <= 0x10ffff);
|
|
15
|
+
/** The text of the entity or character reference `&name;`. */
|
|
16
|
+
function replacementOf(name) {
|
|
17
|
+
const known = Object.hasOwn(predefined, name) ? predefined[name] : undefined;
|
|
18
|
+
if (known !== undefined)
|
|
19
|
+
return known;
|
|
20
|
+
const m = /^#(?:x([0-9a-fA-F]+)|([0-9]+))$/.exec(name);
|
|
21
|
+
if (!m)
|
|
22
|
+
refuseFile('unknownEntity', { name });
|
|
23
|
+
const code = m[1] ? Number.parseInt(m[1], 16) : Number(m[2]);
|
|
24
|
+
if (!isXmlChar(code))
|
|
25
|
+
refuseFile('notXmlChar', { name });
|
|
26
|
+
return String.fromCodePoint(code);
|
|
27
|
+
}
|
|
28
|
+
/** The five predefined entities (4.6) and character references (4.1) replaced; pieces are joined in batches, so memory stays near the size of the text for any number of references. */
|
|
29
|
+
function references(text) {
|
|
30
|
+
let amp = text.indexOf('&');
|
|
31
|
+
if (amp < 0)
|
|
32
|
+
return text;
|
|
33
|
+
let out = '';
|
|
34
|
+
const pieces = [];
|
|
35
|
+
let from = 0;
|
|
36
|
+
while (amp >= 0) {
|
|
37
|
+
pieces.push(text.slice(from, amp));
|
|
38
|
+
const semi = text.indexOf(';', amp + 1);
|
|
39
|
+
const next = text.indexOf('&', amp + 1);
|
|
40
|
+
if (semi <= amp + 1 || (next >= 0 && next < semi))
|
|
41
|
+
refuseFile('bareAmpersand');
|
|
42
|
+
const name = text.slice(amp + 1, semi);
|
|
43
|
+
pieces.push(replacementOf(name));
|
|
44
|
+
if (pieces.length >= 8192) {
|
|
45
|
+
out += pieces.join('');
|
|
46
|
+
pieces.length = 0;
|
|
47
|
+
}
|
|
48
|
+
from = semi + 1;
|
|
49
|
+
amp = next;
|
|
50
|
+
}
|
|
51
|
+
pieces.push(text.slice(from));
|
|
52
|
+
return out + pieces.join('');
|
|
53
|
+
}
|
|
54
|
+
const local = (name) => name.slice(name.indexOf(':') + 1);
|
|
55
|
+
const nameAt = /[^\s/>=<"']+/y;
|
|
56
|
+
function match(re, text, at) {
|
|
57
|
+
re.lastIndex = at;
|
|
58
|
+
return re.exec(text);
|
|
59
|
+
}
|
|
60
|
+
const space = /\s/;
|
|
61
|
+
/** The position of the first character from `at` that is not white space. */
|
|
62
|
+
function skipSpace(text, at) {
|
|
63
|
+
let i = at;
|
|
64
|
+
while (i < text.length && space.test(text.charAt(i)))
|
|
65
|
+
i++;
|
|
66
|
+
return i;
|
|
67
|
+
}
|
|
68
|
+
/** One attribute (white space, name, `=`, a quoted value without `<`) at `at`, else null. */
|
|
69
|
+
function attributeAt(text, at) {
|
|
70
|
+
const start = skipSpace(text, at);
|
|
71
|
+
const n = start > at ? match(nameAt, text, start) : null;
|
|
72
|
+
if (!n)
|
|
73
|
+
return null;
|
|
74
|
+
const eq = skipSpace(text, start + n[0].length);
|
|
75
|
+
if (text.charAt(eq) !== '=')
|
|
76
|
+
return null;
|
|
77
|
+
const open = skipSpace(text, eq + 1);
|
|
78
|
+
const quote = text.charAt(open);
|
|
79
|
+
if (quote !== '"' && quote !== "'")
|
|
80
|
+
return null;
|
|
81
|
+
const close = text.indexOf(quote, open + 1);
|
|
82
|
+
const value = close < 0 ? '' : text.slice(open + 1, close);
|
|
83
|
+
if (close < 0 || value.includes('<'))
|
|
84
|
+
return null;
|
|
85
|
+
return { name: n[0], value, length: close + 1 - at };
|
|
86
|
+
}
|
|
87
|
+
/** The end of a start tag at `at` (`>` or `/>` after white space), else null. */
|
|
88
|
+
function tagEndAt(text, at) {
|
|
89
|
+
const i = skipSpace(text, at);
|
|
90
|
+
const empty = text.charAt(i) === '/';
|
|
91
|
+
return text.charAt(i + (empty ? 1 : 0)) === '>'
|
|
92
|
+
? { empty, length: i + (empty ? 2 : 1) - at }
|
|
93
|
+
: null;
|
|
94
|
+
}
|
|
95
|
+
/** The name and length of an end tag body (`name`, white space, `>`) at `at`, else null. */
|
|
96
|
+
function endTagBodyAt(text, at) {
|
|
97
|
+
const n = match(nameAt, text, at);
|
|
98
|
+
if (!n)
|
|
99
|
+
return null;
|
|
100
|
+
const i = skipSpace(text, at + n[0].length);
|
|
101
|
+
return text.charAt(i) === '>' ? { name: n[0], length: i + 1 - at } : null;
|
|
102
|
+
}
|
|
103
|
+
/** Counts `n` down from `remaining[key]`; throws once the budget is used up. */
|
|
104
|
+
function spend(remaining, key, n) {
|
|
105
|
+
if (!remaining)
|
|
106
|
+
return;
|
|
107
|
+
remaining[key] -= n;
|
|
108
|
+
if (remaining[key] < 0)
|
|
109
|
+
refuseFile('xmlBudgetSpent', { key });
|
|
110
|
+
}
|
|
111
|
+
/** Position of `marker` from `from`; refuses with `unclosed` when it is absent. */
|
|
112
|
+
function closing(text, marker, from, unclosed) {
|
|
113
|
+
const e = text.indexOf(marker, from);
|
|
114
|
+
return e < 0 ? refuseFile(unclosed) : e;
|
|
115
|
+
}
|
|
116
|
+
/** Attributes of the start tag `qname` from `at`, and the position after the last one. */
|
|
117
|
+
function readAttributes(text, at, qname, remaining) {
|
|
118
|
+
const attributes = {};
|
|
119
|
+
const seen = new Set();
|
|
120
|
+
let end = at;
|
|
121
|
+
for (let a = attributeAt(text, end); a; a = attributeAt(text, end)) {
|
|
122
|
+
const name = a.name;
|
|
123
|
+
if (seen.has(name))
|
|
124
|
+
refuseFile('duplicateAttribute', { name, tag: qname });
|
|
125
|
+
seen.add(name);
|
|
126
|
+
if (seen.size > maxAttributes)
|
|
127
|
+
refuseFile('tooManyAttributes', { max: maxAttributes, tag: qname });
|
|
128
|
+
spend(remaining, 'characters', a.length);
|
|
129
|
+
if (name !== 'xmlns' && !name.startsWith('xmlns:')) {
|
|
130
|
+
attributes[local(name)] = references(a.value);
|
|
131
|
+
}
|
|
132
|
+
end += a.length;
|
|
133
|
+
}
|
|
134
|
+
return { attributes, end };
|
|
135
|
+
}
|
|
136
|
+
/** The start tag at `lt`: its qualified name, attributes, whether it is empty, and its end. */
|
|
137
|
+
function readStartTag(text, lt, remaining) {
|
|
138
|
+
const n = match(nameAt, text, lt + 1);
|
|
139
|
+
if (!n)
|
|
140
|
+
refuseFile('malformedTag');
|
|
141
|
+
const qname = n[0];
|
|
142
|
+
spend(remaining, 'elements', 1);
|
|
143
|
+
const { attributes, end } = readAttributes(text, lt + 1 + qname.length, qname, remaining);
|
|
144
|
+
const close = tagEndAt(text, end);
|
|
145
|
+
if (!close)
|
|
146
|
+
refuseFile('malformedTagNamed', { tag: qname });
|
|
147
|
+
return { qname, attributes, empty: close.empty, end: end + close.length };
|
|
148
|
+
}
|
|
149
|
+
function addText(st, t) {
|
|
150
|
+
if (st.path.length > 0)
|
|
151
|
+
st.events.text(t, st.path);
|
|
152
|
+
else if (t.trim())
|
|
153
|
+
refuseFile('textOutsideRoot');
|
|
154
|
+
}
|
|
155
|
+
/** Refuses an XML declaration that names an encoding other than UTF-8. */
|
|
156
|
+
function checkEncoding(pi) {
|
|
157
|
+
const encoding = /^xml\s[\s\S]*?encoding\s*=\s*["']([^"']*)["']/.exec(pi)?.[1];
|
|
158
|
+
if (encoding !== undefined && encoding.toLowerCase() !== 'utf-8')
|
|
159
|
+
refuseFile('encodingNotRead', { encoding });
|
|
160
|
+
}
|
|
161
|
+
/** Closes the element named by the end tag at `lt`; returns the position after the tag. */
|
|
162
|
+
function readEndTag(text, lt, st) {
|
|
163
|
+
const m = endTagBodyAt(text, lt + 2);
|
|
164
|
+
const top = st.qnames.pop();
|
|
165
|
+
if (!m || top === undefined || m.name !== top)
|
|
166
|
+
refuseFile('endTagMismatch', { end: m?.name ?? '', start: top ?? '' });
|
|
167
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the path grows with qnames, so it holds the element closed here
|
|
168
|
+
const name = st.path.pop();
|
|
169
|
+
st.events.close(name, st.path);
|
|
170
|
+
return lt + 2 + m.length;
|
|
171
|
+
}
|
|
172
|
+
/** Opens the element of the start tag at `lt`; returns the position after the tag. */
|
|
173
|
+
function readElement(text, lt, st) {
|
|
174
|
+
const tag = readStartTag(text, lt, st.remaining);
|
|
175
|
+
if (st.qnames.length >= maxDepth)
|
|
176
|
+
refuseFile('nestTooDeep', { max: maxDepth });
|
|
177
|
+
if (st.qnames.length === 0) {
|
|
178
|
+
if (st.rooted)
|
|
179
|
+
refuseFile('secondRoot');
|
|
180
|
+
st.rooted = true;
|
|
181
|
+
}
|
|
182
|
+
const name = local(tag.qname);
|
|
183
|
+
st.events.open(name, tag.attributes, st.path);
|
|
184
|
+
if (tag.empty)
|
|
185
|
+
st.events.close(name, st.path);
|
|
186
|
+
else {
|
|
187
|
+
st.qnames.push(tag.qname);
|
|
188
|
+
st.path.push(name);
|
|
189
|
+
}
|
|
190
|
+
return tag.end;
|
|
191
|
+
}
|
|
192
|
+
/** Reads the markup at `lt` (a `<`); returns the position after it. */
|
|
193
|
+
function readMarkup(text, lt, st) {
|
|
194
|
+
if (text.startsWith('<?', lt)) {
|
|
195
|
+
const e = closing(text, '?>', lt + 2, 'unclosedProcessingInstruction');
|
|
196
|
+
checkEncoding(text.slice(lt + 2, e));
|
|
197
|
+
return e + 2;
|
|
198
|
+
}
|
|
199
|
+
if (text.startsWith('<!--', lt))
|
|
200
|
+
return closing(text, '-->', lt + 4, 'unclosedComment') + 3;
|
|
201
|
+
if (text.startsWith('<![CDATA[', lt)) {
|
|
202
|
+
const e = closing(text, ']]>', lt + 9, 'unclosedCdata');
|
|
203
|
+
spend(st.remaining, 'characters', e - lt - 9);
|
|
204
|
+
addText(st, text.slice(lt + 9, e));
|
|
205
|
+
return e + 3;
|
|
206
|
+
}
|
|
207
|
+
if (text.startsWith('<!DOCTYPE', lt))
|
|
208
|
+
refuseFile('doctype');
|
|
209
|
+
if (text[lt + 1] === '!')
|
|
210
|
+
refuseFile('markupDeclaration');
|
|
211
|
+
if (text[lt + 1] === '/')
|
|
212
|
+
return readEndTag(text, lt, st);
|
|
213
|
+
return readElement(text, lt, st);
|
|
214
|
+
}
|
|
215
|
+
/** Throws on markup this reader does not accept; `remaining` is counted down before each element or text is reported, so several parts can share one budget; errors thrown by `events` are not caught. */
|
|
216
|
+
export function scanXml(text, events, remaining) {
|
|
217
|
+
const st = { events, remaining, qnames: [], path: [], rooted: false };
|
|
218
|
+
let i = 0;
|
|
219
|
+
while (i < text.length) {
|
|
220
|
+
const lt = text.indexOf('<', i);
|
|
221
|
+
if (lt < 0) {
|
|
222
|
+
spend(remaining, 'characters', text.length - i);
|
|
223
|
+
addText(st, references(text.slice(i)));
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
if (lt > i) {
|
|
227
|
+
spend(remaining, 'characters', lt - i);
|
|
228
|
+
addText(st, references(text.slice(i, lt)));
|
|
229
|
+
}
|
|
230
|
+
i = readMarkup(text, lt, st);
|
|
231
|
+
}
|
|
232
|
+
if (st.qnames.length > 0)
|
|
233
|
+
refuseFile('unclosedElement', { tag: st.qnames.at(-1) ?? '' });
|
|
234
|
+
if (!st.rooted)
|
|
235
|
+
refuseFile('noRoot');
|
|
236
|
+
}
|
|
237
|
+
/** The root element of `text` as a tree, read with `scanXml` (same checks and budget). */
|
|
238
|
+
export function parseXml(text, remaining) {
|
|
239
|
+
const stack = [];
|
|
240
|
+
let root;
|
|
241
|
+
scanXml(text, {
|
|
242
|
+
open(name, attributes) {
|
|
243
|
+
const element = { name, attributes, children: [] };
|
|
244
|
+
const top = stack.at(-1);
|
|
245
|
+
if (top)
|
|
246
|
+
top.children.push(element);
|
|
247
|
+
else
|
|
248
|
+
root = element;
|
|
249
|
+
stack.push(element);
|
|
250
|
+
},
|
|
251
|
+
text(t) {
|
|
252
|
+
stack.at(-1)?.children.push(t);
|
|
253
|
+
},
|
|
254
|
+
close() {
|
|
255
|
+
stack.pop();
|
|
256
|
+
},
|
|
257
|
+
}, remaining);
|
|
258
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- scanXml refuses a text without a root element, so `open` set it
|
|
259
|
+
return root;
|
|
260
|
+
}
|
|
261
|
+
//# sourceMappingURL=xml.js.map
|
package/dist/zip.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Raw DEFLATE data (RFC 1951) to bytes. It may stop once the output exceeds `size`; the reader
|
|
3
|
+
* refuses any output whose length is not `size`, and treats a rejection as corrupt data.
|
|
4
|
+
*/
|
|
5
|
+
export type Inflate = (data: Uint8Array<ArrayBuffer>, size: number) => Promise<Uint8Array<ArrayBuffer>>;
|
|
6
|
+
export interface ZipEntry {
|
|
7
|
+
name: string;
|
|
8
|
+
flags: number;
|
|
9
|
+
method: number;
|
|
10
|
+
crc: number;
|
|
11
|
+
compressedSize: number;
|
|
12
|
+
size: number;
|
|
13
|
+
offset: number;
|
|
14
|
+
}
|
|
15
|
+
/** Inflates with the platform's `DecompressionStream('deflate-raw')`, stopping past `size`. */
|
|
16
|
+
export declare const inflateRaw: Inflate;
|
|
17
|
+
/** From the central directory (4.3.12), keyed by lower-case name, directories left out; split, ZIP64 and malformed archives and repeated names, also in another case, are refused. */
|
|
18
|
+
export declare function zipEntries(bytes: Uint8Array): Map<string, ZipEntry>;
|
|
19
|
+
/** The bytes of an archive and the function that inflates its deflated entries. */
|
|
20
|
+
export interface Archive {
|
|
21
|
+
bytes: Uint8Array;
|
|
22
|
+
inflate: Inflate;
|
|
23
|
+
}
|
|
24
|
+
/** Stored (method 0) or deflated (method 8), checked against the declared size and CRC-32; `budget.left` is the declared size the workbook may still read, `limit` the largest declared size of this part. */
|
|
25
|
+
export declare function readEntry({ bytes, inflate }: Archive, entry: ZipEntry, budget: {
|
|
26
|
+
left: number;
|
|
27
|
+
}, limit?: number): Promise<Uint8Array<ArrayBuffer>>;
|
package/dist/zip.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// ZIP container reading for XLSX workbooks: PKWARE APPNOTE.TXT 6.3.10, https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT (section numbers refer to it).
|
|
2
|
+
import { refuseFile } from './diagnostics.js';
|
|
3
|
+
import { maxPartSize, maxTotalSize } from './limits.js';
|
|
4
|
+
const crcTable = Array.from({ length: 256 }, (_, n) => {
|
|
5
|
+
let c = n;
|
|
6
|
+
for (let k = 0; k < 8; k++)
|
|
7
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
8
|
+
return c >>> 0;
|
|
9
|
+
});
|
|
10
|
+
/** CRC-32 of APPNOTE 4.4.7 (reflected polynomial 0xEDB88320, RFC 1952 section 8). */
|
|
11
|
+
function crc32(bytes) {
|
|
12
|
+
let crc = 0xffffffff;
|
|
13
|
+
for (const b of bytes) {
|
|
14
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the index is a byte of a 256-entry table
|
|
15
|
+
crc = crcTable[(crc ^ b) & 0xff] ^ (crc >>> 8);
|
|
16
|
+
}
|
|
17
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
18
|
+
}
|
|
19
|
+
/** Inflates with the platform's `DecompressionStream('deflate-raw')`, stopping past `size`. */
|
|
20
|
+
export const inflateRaw = async (data, size) => {
|
|
21
|
+
const reader = new Blob([data])
|
|
22
|
+
.stream()
|
|
23
|
+
.pipeThrough(new DecompressionStream('deflate-raw'))
|
|
24
|
+
.getReader();
|
|
25
|
+
const chunks = [];
|
|
26
|
+
let length = 0;
|
|
27
|
+
for (;;) {
|
|
28
|
+
const { done, value } = await reader.read();
|
|
29
|
+
if (done)
|
|
30
|
+
break;
|
|
31
|
+
chunks.push(value);
|
|
32
|
+
length += value.length;
|
|
33
|
+
if (length > size) {
|
|
34
|
+
await reader.cancel();
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const out = new Uint8Array(length);
|
|
39
|
+
let at = 0;
|
|
40
|
+
for (const c of chunks) {
|
|
41
|
+
out.set(c, at);
|
|
42
|
+
at += c.length;
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
};
|
|
46
|
+
/** The position of the end of central directory record (4.3.16), or -1. */
|
|
47
|
+
function findEnd(bytes, view) {
|
|
48
|
+
// 22 bytes plus a comment of up to 65535 bytes
|
|
49
|
+
for (let i = bytes.length - 22; i >= Math.max(0, bytes.length - 22 - 0xffff); i--) {
|
|
50
|
+
if (view.getUint32(i, true) === 0x06054b50 &&
|
|
51
|
+
i + 22 + view.getUint16(i + 20, true) === bytes.length) {
|
|
52
|
+
return i;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return -1;
|
|
56
|
+
}
|
|
57
|
+
/** The entry of the central directory header at `p` and the position of the next header. */
|
|
58
|
+
function readHeader(bytes, view, p, end) {
|
|
59
|
+
if (p + 46 > end || view.getUint32(p, true) !== 0x02014b50)
|
|
60
|
+
refuseFile('zipDirectoryMalformed');
|
|
61
|
+
const nameLength = view.getUint16(p + 28, true);
|
|
62
|
+
const next = p + 46 + nameLength + view.getUint16(p + 30, true) + view.getUint16(p + 32, true);
|
|
63
|
+
if (next > end)
|
|
64
|
+
refuseFile('zipDirectoryMalformed');
|
|
65
|
+
// shortcut: names read as Latin-1, exact for the ASCII part names of XLSX; upgrade if a needed part name is not ASCII
|
|
66
|
+
const name = String.fromCharCode(...bytes.subarray(p + 46, p + 46 + nameLength));
|
|
67
|
+
const entry = {
|
|
68
|
+
name,
|
|
69
|
+
flags: view.getUint16(p + 8, true),
|
|
70
|
+
method: view.getUint16(p + 10, true),
|
|
71
|
+
crc: view.getUint32(p + 16, true),
|
|
72
|
+
compressedSize: view.getUint32(p + 20, true),
|
|
73
|
+
size: view.getUint32(p + 24, true),
|
|
74
|
+
offset: view.getUint32(p + 42, true),
|
|
75
|
+
};
|
|
76
|
+
return { entry, next };
|
|
77
|
+
}
|
|
78
|
+
/** Adds a file entry under its lower-case name; ZIP64 sizes and repeated names are refused. */
|
|
79
|
+
function addEntry(entries, entry) {
|
|
80
|
+
if (entry.compressedSize === 0xffffffff ||
|
|
81
|
+
entry.size === 0xffffffff ||
|
|
82
|
+
entry.offset === 0xffffffff) {
|
|
83
|
+
refuseFile('zip64');
|
|
84
|
+
}
|
|
85
|
+
const key = entry.name.toLowerCase();
|
|
86
|
+
const other = entries.get(key);
|
|
87
|
+
if (other?.name === entry.name)
|
|
88
|
+
refuseFile('partTwice', { name: entry.name });
|
|
89
|
+
if (other)
|
|
90
|
+
refuseFile('partsDifferInCase', { name: entry.name, other: other.name });
|
|
91
|
+
entries.set(key, entry);
|
|
92
|
+
}
|
|
93
|
+
/** From the central directory (4.3.12), keyed by lower-case name, directories left out; split, ZIP64 and malformed archives and repeated names, also in another case, are refused. */
|
|
94
|
+
export function zipEntries(bytes) {
|
|
95
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
96
|
+
const end = findEnd(bytes, view);
|
|
97
|
+
if (end < 0)
|
|
98
|
+
refuseFile('zipNoEnd');
|
|
99
|
+
const { total, directoryOffset } = centralDirectory(view, end);
|
|
100
|
+
const entries = new Map();
|
|
101
|
+
let p = directoryOffset;
|
|
102
|
+
for (let k = 0; k < total; k++) {
|
|
103
|
+
const { entry, next } = readHeader(bytes, view, p, end);
|
|
104
|
+
p = next;
|
|
105
|
+
if (!entry.name.endsWith('/'))
|
|
106
|
+
addEntry(entries, entry);
|
|
107
|
+
}
|
|
108
|
+
return entries;
|
|
109
|
+
}
|
|
110
|
+
/** From the end record at `end` (4.3.16); ZIP64 and split archives and a directory past the end record are refused. */
|
|
111
|
+
function centralDirectory(view, end) {
|
|
112
|
+
const disk = view.getUint16(end + 4, true);
|
|
113
|
+
const directoryDisk = view.getUint16(end + 6, true);
|
|
114
|
+
const onDisk = view.getUint16(end + 8, true);
|
|
115
|
+
const total = view.getUint16(end + 10, true);
|
|
116
|
+
const directorySize = view.getUint32(end + 12, true);
|
|
117
|
+
const directoryOffset = view.getUint32(end + 16, true);
|
|
118
|
+
if (total === 0xffff || directorySize === 0xffffffff || directoryOffset === 0xffffffff) {
|
|
119
|
+
refuseFile('zip64');
|
|
120
|
+
}
|
|
121
|
+
if (disk !== 0 || directoryDisk !== 0 || onDisk !== total)
|
|
122
|
+
refuseFile('zipSplit');
|
|
123
|
+
if (directoryOffset + directorySize > end)
|
|
124
|
+
refuseFile('zipDirectoryOutside');
|
|
125
|
+
return { total, directoryOffset };
|
|
126
|
+
}
|
|
127
|
+
/** Checks the declared sizes of an entry against `limit` and the shared budget, and spends them. */
|
|
128
|
+
function spendDeclared(entry, budget, limit, part) {
|
|
129
|
+
if (entry.flags & 1)
|
|
130
|
+
part('partEncrypted');
|
|
131
|
+
if (entry.method !== 0 && entry.method !== 8)
|
|
132
|
+
part('partMethod', { method: entry.method });
|
|
133
|
+
if (entry.size > limit)
|
|
134
|
+
part('partTooLarge', { size: entry.size, limit });
|
|
135
|
+
if (entry.size > budget.left)
|
|
136
|
+
refuseFile('partsTooLarge', { max: maxTotalSize });
|
|
137
|
+
budget.left -= entry.size;
|
|
138
|
+
// assumed bound, not a cited one: literal-only fixed Huffman blocks code a byte in at most 9 bits (RFC 1951 3.2.6); the margin keeps a too-small declared size refused as an overrun below
|
|
139
|
+
const maxCompressed = entry.size + (entry.size >>> 3) + 65536;
|
|
140
|
+
if (entry.method === 8 && entry.compressedSize > maxCompressed) {
|
|
141
|
+
part('partCompressedTooLarge', { compressed: entry.compressedSize, size: entry.size });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/** The compressed data of an entry, located through its local header. */
|
|
145
|
+
function entryData(bytes, entry, part) {
|
|
146
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
147
|
+
// local header (4.3.7): only its name and extra field lengths locate the data; sizes come from the central directory, since a data descriptor (4.3.9) leaves them zero here
|
|
148
|
+
const at = entry.offset;
|
|
149
|
+
if (at + 30 > bytes.length || view.getUint32(at, true) !== 0x04034b50)
|
|
150
|
+
part('partNoLocalHeader');
|
|
151
|
+
const start = at + 30 + view.getUint16(at + 26, true) + view.getUint16(at + 28, true);
|
|
152
|
+
if (start + entry.compressedSize > bytes.length)
|
|
153
|
+
part('partDataOutside');
|
|
154
|
+
return bytes.slice(start, start + entry.compressedSize);
|
|
155
|
+
}
|
|
156
|
+
/** Stored (method 0) or deflated (method 8), checked against the declared size and CRC-32; `budget.left` is the declared size the workbook may still read, `limit` the largest declared size of this part. */
|
|
157
|
+
export async function readEntry({ bytes, inflate }, entry, budget, limit = maxPartSize) {
|
|
158
|
+
const part = (code, params = {}) => refuseFile(code, { ...params, part: entry.name });
|
|
159
|
+
spendDeclared(entry, budget, limit, part);
|
|
160
|
+
const data = entryData(bytes, entry, part);
|
|
161
|
+
let out;
|
|
162
|
+
if (entry.method === 0) {
|
|
163
|
+
if (entry.compressedSize !== entry.size)
|
|
164
|
+
part('partStoredSize', { stored: entry.compressedSize, size: entry.size });
|
|
165
|
+
out = data;
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
try {
|
|
169
|
+
out = await inflate(data, entry.size);
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
return part('partCorrupt');
|
|
173
|
+
}
|
|
174
|
+
if (out.length > entry.size)
|
|
175
|
+
part('partInflatesMore', { size: entry.size });
|
|
176
|
+
if (out.length < entry.size)
|
|
177
|
+
part('partInflatesLess', { length: out.length, size: entry.size });
|
|
178
|
+
}
|
|
179
|
+
if (crc32(out) !== entry.crc)
|
|
180
|
+
part('partCrc');
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
//# sourceMappingURL=zip.js.map
|
package/package.json
CHANGED
|
@@ -1,11 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rangefoundry/importers",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Parsers of chronograph exports, pressure histories and reloading record tables.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"reloading",
|
|
7
|
+
"ballistics",
|
|
8
|
+
"chronograph",
|
|
9
|
+
"csv",
|
|
10
|
+
"importer"
|
|
11
|
+
],
|
|
12
|
+
"license": "AGPL-3.0-or-later",
|
|
13
|
+
"homepage": "https://rangefoundry.github.io/workbench/docs/libraries/importers/",
|
|
5
14
|
"repository": {
|
|
6
15
|
"type": "git",
|
|
7
16
|
"url": "git+https://github.com/RangeFoundry/workbench.git",
|
|
8
17
|
"directory": "packages/importers"
|
|
9
18
|
},
|
|
10
|
-
"
|
|
11
|
-
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=26"
|
|
25
|
+
},
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"main": "dist/index.js",
|
|
28
|
+
"types": "dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"!dist/**/*.map",
|
|
38
|
+
"!dist/*.tsbuildinfo",
|
|
39
|
+
"!dist/api-extractor",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@rangefoundry/schema": "0.1.0",
|
|
44
|
+
"@rangefoundry/statistics": "0.1.0",
|
|
45
|
+
"@rangefoundry/units": "0.1.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@arethetypeswrong/cli": "0.18.5",
|
|
49
|
+
"@microsoft/api-extractor": "7.60.1",
|
|
50
|
+
"@types/node": "26.6.6",
|
|
51
|
+
"@vitest/coverage-v8": "5.1.1",
|
|
52
|
+
"fast-check": "4.11.0",
|
|
53
|
+
"oxlint": "1.94.0",
|
|
54
|
+
"oxlint-tsgolint": "7.0.2008",
|
|
55
|
+
"publint": "0.3.25",
|
|
56
|
+
"typescript": "7.0.2",
|
|
57
|
+
"vitest": "5.1.1"
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsc -b tsconfig.build.json && api-extractor run",
|
|
61
|
+
"typecheck": "tsc --noEmit && tsc -p test",
|
|
62
|
+
"lint": "oxlint --type-aware --deny-warnings --report-unused-disable-directives",
|
|
63
|
+
"test": "vitest run --coverage && publint --strict && attw --pack . --profile esm-only"
|
|
64
|
+
}
|
|
65
|
+
}
|