@stll/docx-utils 0.1.2 → 0.1.4
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/dist/index.d.ts +43 -3
- package/dist/index.js +362 -9
- package/package.json +8 -4
package/dist/index.d.ts
CHANGED
|
@@ -27,6 +27,48 @@ export declare const OOXML_NS: {
|
|
|
27
27
|
};
|
|
28
28
|
type OoxmlPrefix = keyof typeof OOXML_NS;
|
|
29
29
|
//#endregion
|
|
30
|
+
//#region src/archive.d.ts
|
|
31
|
+
/** Maximum bytes any single archive entry may decompress to. */
|
|
32
|
+
export declare const DOCX_MAX_ENTRY_BYTES: number;
|
|
33
|
+
/** Maximum cumulative uncompressed bytes the archive may yield. */
|
|
34
|
+
export declare const DOCX_MAX_TOTAL_BYTES: number;
|
|
35
|
+
/**
|
|
36
|
+
* Maximum number of entries an archive may declare. Real DOCX/XLSX
|
|
37
|
+
* documents have well under 200; orders of magnitude above that
|
|
38
|
+
* indicate a hostile archive.
|
|
39
|
+
*/
|
|
40
|
+
export declare const DOCX_MAX_ENTRIES = 4096;
|
|
41
|
+
declare const DocxArchiveError_base: import("better-result").TaggedErrorClass<"DocxArchiveError">;
|
|
42
|
+
export declare class DocxArchiveError extends DocxArchiveError_base<{
|
|
43
|
+
message: string;
|
|
44
|
+
reason: "load-failed" | "too-many-entries" | "entry-too-large" | "total-too-large" | "invalid-entry-name";
|
|
45
|
+
cause?: unknown;
|
|
46
|
+
}> {}
|
|
47
|
+
type ArchiveOptions = {
|
|
48
|
+
maxEntryBytes?: number;
|
|
49
|
+
maxTotalBytes?: number;
|
|
50
|
+
maxEntries?: number;
|
|
51
|
+
};
|
|
52
|
+
type DocxArchive = {
|
|
53
|
+
/**
|
|
54
|
+
* The underlying JSZip instance. Safe for write operations
|
|
55
|
+
* (`zip.file(path, value)`, `zip.generateAsync(...)`); for reads,
|
|
56
|
+
* prefer the bounded helpers on this object.
|
|
57
|
+
*/
|
|
58
|
+
zip: JSZip;
|
|
59
|
+
/**
|
|
60
|
+
* Read an entry as UTF-8 text. Returns null if the entry is not in
|
|
61
|
+
* the archive. Throws `DocxArchiveError` if the read would cross
|
|
62
|
+
* the per-entry or cumulative archive cap.
|
|
63
|
+
*/
|
|
64
|
+
readEntryString: (path: string) => Promise<string | null>;
|
|
65
|
+
/** As {@link readEntryString} but returns the raw bytes. */
|
|
66
|
+
readEntryUint8: (path: string) => Promise<Uint8Array | null>;
|
|
67
|
+
};
|
|
68
|
+
/** Load and validate an archive, preserving the promise-based ZIP API. */
|
|
69
|
+
export declare const loadDocx: (buffer: ArrayBuffer | Uint8Array | Buffer, options?: ArchiveOptions) => Promise<JSZip>;
|
|
70
|
+
export declare const loadDocxArchive: (buffer: ArrayBuffer | Uint8Array | Buffer, options?: ArchiveOptions) => Promise<DocxArchive>;
|
|
71
|
+
//#endregion
|
|
30
72
|
//#region src/zip.d.ts
|
|
31
73
|
/** Standard DOCX compression options */
|
|
32
74
|
export declare const DOCX_COMPRESSION: {
|
|
@@ -36,8 +78,6 @@ export declare const DOCX_COMPRESSION: {
|
|
|
36
78
|
level: number;
|
|
37
79
|
};
|
|
38
80
|
};
|
|
39
|
-
/** Load a DOCX file (ArrayBuffer) into a JSZip instance */
|
|
40
|
-
export declare const loadDocx: (buffer: ArrayBuffer) => Promise<JSZip>;
|
|
41
81
|
/** Extract a text file from a ZIP */
|
|
42
82
|
export declare const extractText: (zip: JSZip, path: string) => Promise<string | null>;
|
|
43
83
|
/** Extract a binary file from a ZIP */
|
|
@@ -92,4 +132,4 @@ export declare const sanitizeAttachedTemplateRelationships: (xml: string, relati
|
|
|
92
132
|
/** Remove WordprocessingML references to the relationships removed above. */
|
|
93
133
|
export declare const sanitizeAttachedTemplateSource: (xml: string, removedRelationshipIds: readonly string[]) => SanitizedAttachedTemplateSource;
|
|
94
134
|
//#endregion
|
|
95
|
-
export type { AttachedTemplateRelationshipFinding, AttachedTemplateTargetKind, OoxmlPrefix };
|
|
135
|
+
export type { ArchiveOptions, AttachedTemplateRelationshipFinding, AttachedTemplateTargetKind, DocxArchive, OoxmlPrefix };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Result, TaggedError } from "better-result";
|
|
1
2
|
import JSZip from "jszip";
|
|
2
3
|
import * as slimdom from "slimdom";
|
|
3
4
|
//#region src/namespaces.ts
|
|
@@ -27,6 +28,351 @@ const OOXML_NS = {
|
|
|
27
28
|
pr: "http://schemas.openxmlformats.org/package/2006/relationships"
|
|
28
29
|
};
|
|
29
30
|
//#endregion
|
|
31
|
+
//#region src/archive.ts
|
|
32
|
+
/** Bounded metadata validation and sequential reads over OOXML archives. */
|
|
33
|
+
/** Maximum bytes any single archive entry may decompress to. */
|
|
34
|
+
const DOCX_MAX_ENTRY_BYTES = 134217728;
|
|
35
|
+
/** Maximum cumulative uncompressed bytes the archive may yield. */
|
|
36
|
+
const DOCX_MAX_TOTAL_BYTES = 268435456;
|
|
37
|
+
/**
|
|
38
|
+
* Maximum number of entries an archive may declare. Real DOCX/XLSX
|
|
39
|
+
* documents have well under 200; orders of magnitude above that
|
|
40
|
+
* indicate a hostile archive.
|
|
41
|
+
*/
|
|
42
|
+
const DOCX_MAX_ENTRIES = 4096;
|
|
43
|
+
var DocxArchiveError = class extends TaggedError("DocxArchiveError") {};
|
|
44
|
+
const isStreamingEntry = (entry) => "internalStream" in entry && typeof entry.internalStream === "function";
|
|
45
|
+
const readEntryBounded = async (entry, { maxEntryBytes, remainingBytes, onChunk }) => await new Promise((resolve, reject) => {
|
|
46
|
+
if (!isStreamingEntry(entry)) {
|
|
47
|
+
reject(new DocxArchiveError({
|
|
48
|
+
message: "Failed to read DOCX entry",
|
|
49
|
+
reason: "load-failed"
|
|
50
|
+
}));
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const stream = entry.internalStream("uint8array");
|
|
54
|
+
let bytes = 0;
|
|
55
|
+
let settled = false;
|
|
56
|
+
stream.on("data", (chunk) => {
|
|
57
|
+
if (settled) return;
|
|
58
|
+
bytes += chunk.byteLength;
|
|
59
|
+
if (bytes > maxEntryBytes || bytes > remainingBytes) {
|
|
60
|
+
settled = true;
|
|
61
|
+
stream.pause();
|
|
62
|
+
reject(new DocxArchiveError({
|
|
63
|
+
message: "DOCX entry exceeds its decompression budget",
|
|
64
|
+
reason: bytes > maxEntryBytes ? "entry-too-large" : "total-too-large"
|
|
65
|
+
}));
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
onChunk?.(chunk);
|
|
69
|
+
});
|
|
70
|
+
stream.on("error", (cause) => {
|
|
71
|
+
if (!settled) {
|
|
72
|
+
settled = true;
|
|
73
|
+
reject(new DocxArchiveError({
|
|
74
|
+
message: "Failed to read DOCX entry",
|
|
75
|
+
reason: "load-failed",
|
|
76
|
+
cause
|
|
77
|
+
}));
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
stream.on("end", () => {
|
|
81
|
+
if (!settled) {
|
|
82
|
+
settled = true;
|
|
83
|
+
resolve(bytes);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
stream.resume();
|
|
87
|
+
});
|
|
88
|
+
const invalid = () => Result.err(new DocxArchiveError({
|
|
89
|
+
message: "DOCX archive contains invalid entry metadata",
|
|
90
|
+
reason: "load-failed"
|
|
91
|
+
}));
|
|
92
|
+
const limitError = (reason) => Result.err(new DocxArchiveError({
|
|
93
|
+
message: "DOCX archive exceeds its budget",
|
|
94
|
+
reason
|
|
95
|
+
}));
|
|
96
|
+
const readZip64Directory = (view, locator) => {
|
|
97
|
+
if (view.getUint32(locator, true) !== 117853008 || view.getUint32(locator + 4, true) !== 0 || view.getUint32(locator + 16, true) !== 1) return invalid();
|
|
98
|
+
const recordOffset = view.getBigUint64(locator + 8, true);
|
|
99
|
+
if (recordOffset > BigInt(locator - 56)) return invalid();
|
|
100
|
+
const record = Number(recordOffset);
|
|
101
|
+
if (record < 0 || record + 56 > view.byteLength || view.getUint32(record, true) !== 101075792 || view.getBigUint64(record + 4, true) !== 44n || record + 56 !== locator) return invalid();
|
|
102
|
+
if (view.getUint32(record + 16, true) !== 0 || view.getUint32(record + 20, true) !== 0) return invalid();
|
|
103
|
+
const zipCount = view.getBigUint64(record + 32, true);
|
|
104
|
+
const zipDiskCount = view.getBigUint64(record + 24, true);
|
|
105
|
+
const zipSize = view.getBigUint64(record + 40, true);
|
|
106
|
+
const zipStart = view.getBigUint64(record + 48, true);
|
|
107
|
+
return Result.ok({
|
|
108
|
+
count: zipCount,
|
|
109
|
+
diskCount: zipDiskCount,
|
|
110
|
+
size: zipSize,
|
|
111
|
+
start: zipStart,
|
|
112
|
+
directoryEnd: record
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
const readDirectory = (view) => {
|
|
116
|
+
const bytes = Buffer.from(view.buffer, view.byteOffset, view.byteLength);
|
|
117
|
+
const end = bytes.lastIndexOf(new Uint8Array([
|
|
118
|
+
80,
|
|
119
|
+
75,
|
|
120
|
+
5,
|
|
121
|
+
6
|
|
122
|
+
]));
|
|
123
|
+
if (end < Math.max(0, view.byteLength - 65535 - 22) || end > view.byteLength - 22 || end + 22 + view.getUint16(end + 20, true) !== view.byteLength) return invalid();
|
|
124
|
+
if (view.getUint16(end + 4, true) !== 0 || view.getUint16(end + 6, true) !== 0) return invalid();
|
|
125
|
+
let count = BigInt(view.getUint16(end + 10, true));
|
|
126
|
+
let diskCount = BigInt(view.getUint16(end + 8, true));
|
|
127
|
+
let size = BigInt(view.getUint32(end + 12, true));
|
|
128
|
+
let start = BigInt(view.getUint32(end + 16, true));
|
|
129
|
+
let directoryEnd = end;
|
|
130
|
+
const locator = bytes.lastIndexOf(new Uint8Array([
|
|
131
|
+
80,
|
|
132
|
+
75,
|
|
133
|
+
6,
|
|
134
|
+
7
|
|
135
|
+
]));
|
|
136
|
+
const needsZip64 = count === 65535n || diskCount === 65535n || size === 4294967295n || start === 4294967295n;
|
|
137
|
+
if (!needsZip64 && locator !== -1) return invalid();
|
|
138
|
+
if (needsZip64) {
|
|
139
|
+
if (locator === -1 || locator !== end - 20) return invalid();
|
|
140
|
+
const zip64 = readZip64Directory(view, locator);
|
|
141
|
+
if (Result.isError(zip64)) return zip64;
|
|
142
|
+
const actual = zip64.value;
|
|
143
|
+
if ([
|
|
144
|
+
[
|
|
145
|
+
count,
|
|
146
|
+
actual.count,
|
|
147
|
+
65535n
|
|
148
|
+
],
|
|
149
|
+
[
|
|
150
|
+
diskCount,
|
|
151
|
+
actual.diskCount,
|
|
152
|
+
65535n
|
|
153
|
+
],
|
|
154
|
+
[
|
|
155
|
+
size,
|
|
156
|
+
actual.size,
|
|
157
|
+
4294967295n
|
|
158
|
+
],
|
|
159
|
+
[
|
|
160
|
+
start,
|
|
161
|
+
actual.start,
|
|
162
|
+
4294967295n
|
|
163
|
+
]
|
|
164
|
+
].some(([declared, value, sentinel]) => declared !== sentinel && declared !== value)) return invalid();
|
|
165
|
+
({count, diskCount, size, start, directoryEnd} = actual);
|
|
166
|
+
}
|
|
167
|
+
if (count !== diskCount || start + size !== BigInt(directoryEnd) || start > BigInt(directoryEnd)) return invalid();
|
|
168
|
+
return Result.ok({
|
|
169
|
+
count,
|
|
170
|
+
diskCount,
|
|
171
|
+
size,
|
|
172
|
+
start,
|
|
173
|
+
directoryEnd
|
|
174
|
+
});
|
|
175
|
+
};
|
|
176
|
+
const readEntrySizes = ({ view, offset, extraStart, extraEndOffset }) => {
|
|
177
|
+
let declared = BigInt(view.getUint32(offset + 24, true));
|
|
178
|
+
let compressed = BigInt(view.getUint32(offset + 20, true));
|
|
179
|
+
let localOffset = BigInt(view.getUint32(offset + 42, true));
|
|
180
|
+
let extra = extraStart;
|
|
181
|
+
let zip64Seen = false;
|
|
182
|
+
while (extra < extraEndOffset) {
|
|
183
|
+
if (extra + 4 > extraEndOffset) return invalid();
|
|
184
|
+
const tag = view.getUint16(extra, true);
|
|
185
|
+
const length = view.getUint16(extra + 2, true);
|
|
186
|
+
const payload = extra + 4;
|
|
187
|
+
if (payload + length > extraEndOffset) return invalid();
|
|
188
|
+
if (tag === 1) {
|
|
189
|
+
if (zip64Seen) return invalid();
|
|
190
|
+
zip64Seen = true;
|
|
191
|
+
if (length < (declared === 4294967295n ? 8 : 0) + (compressed === 4294967295n ? 8 : 0) + (localOffset === 4294967295n ? 8 : 0)) return invalid();
|
|
192
|
+
let field = payload;
|
|
193
|
+
if (declared === 4294967295n) {
|
|
194
|
+
declared = view.getBigUint64(field, true);
|
|
195
|
+
field += 8;
|
|
196
|
+
}
|
|
197
|
+
if (compressed === 4294967295n) {
|
|
198
|
+
compressed = view.getBigUint64(field, true);
|
|
199
|
+
field += 8;
|
|
200
|
+
}
|
|
201
|
+
if (localOffset === 4294967295n) localOffset = view.getBigUint64(field, true);
|
|
202
|
+
}
|
|
203
|
+
extra = payload + length;
|
|
204
|
+
}
|
|
205
|
+
if (!zip64Seen && (declared === 4294967295n || compressed === 4294967295n || localOffset === 4294967295n)) return invalid();
|
|
206
|
+
return Result.ok({
|
|
207
|
+
declared,
|
|
208
|
+
compressed,
|
|
209
|
+
localOffset
|
|
210
|
+
});
|
|
211
|
+
};
|
|
212
|
+
const readEntryMetadata = ({ bytes, view, offset, start, directoryEnd }) => {
|
|
213
|
+
if (offset + 46 > directoryEnd || view.getUint32(offset, true) !== 33639248) return invalid();
|
|
214
|
+
const nameLength = view.getUint16(offset + 28, true);
|
|
215
|
+
const extraLength = view.getUint16(offset + 30, true);
|
|
216
|
+
const commentLength = view.getUint16(offset + 32, true);
|
|
217
|
+
const next = offset + 46 + nameLength + extraLength + commentLength;
|
|
218
|
+
if (next > directoryEnd || nameLength === 0 || view.getUint16(offset + 34, true) !== 0) return invalid();
|
|
219
|
+
const name = Buffer.from(bytes.subarray(offset + 46, offset + 46 + nameLength)).toString("hex");
|
|
220
|
+
const sizes = readEntrySizes({
|
|
221
|
+
view,
|
|
222
|
+
offset,
|
|
223
|
+
extraStart: offset + 46 + nameLength,
|
|
224
|
+
extraEndOffset: offset + 46 + nameLength + extraLength
|
|
225
|
+
});
|
|
226
|
+
if (Result.isError(sizes)) return sizes;
|
|
227
|
+
const { declared, compressed, localOffset } = sizes.value;
|
|
228
|
+
if (compressed > start || localOffset + 30n > start) return invalid();
|
|
229
|
+
const local = Number(localOffset);
|
|
230
|
+
if (local < 0 || local + 30 > bytes.length || view.getUint32(local, true) !== 67324752) return invalid();
|
|
231
|
+
const localNameLength = view.getUint16(local + 26, true);
|
|
232
|
+
if (localOffset + BigInt(30 + localNameLength + view.getUint16(local + 28, true)) + compressed > start || localNameLength !== nameLength || Buffer.from(bytes.subarray(local + 30, local + 30 + localNameLength)).toString("hex") !== name) return invalid();
|
|
233
|
+
const directory = bytes[offset + 46 + nameLength - 1] === 47 || Math.floor(view.getUint32(offset + 38, true) / 16) % 2 === 1;
|
|
234
|
+
return Result.ok({
|
|
235
|
+
next,
|
|
236
|
+
name,
|
|
237
|
+
declared,
|
|
238
|
+
directory
|
|
239
|
+
});
|
|
240
|
+
};
|
|
241
|
+
const preflightArchive = (bytes, { maxEntries, maxEntryBytes, maxTotalBytes }) => {
|
|
242
|
+
if ([
|
|
243
|
+
maxEntries,
|
|
244
|
+
maxEntryBytes,
|
|
245
|
+
maxTotalBytes
|
|
246
|
+
].some((limit) => !Number.isSafeInteger(limit) || limit < 0)) return invalid();
|
|
247
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
248
|
+
const metadata = readDirectory(view);
|
|
249
|
+
if (Result.isError(metadata)) return metadata;
|
|
250
|
+
const { count, start, directoryEnd } = metadata.value;
|
|
251
|
+
let offset = Number(start);
|
|
252
|
+
let records = 0;
|
|
253
|
+
let total = 0n;
|
|
254
|
+
const names = /* @__PURE__ */ new Set();
|
|
255
|
+
while (offset < directoryEnd) {
|
|
256
|
+
if (++records > maxEntries) return limitError("too-many-entries");
|
|
257
|
+
const entry = readEntryMetadata({
|
|
258
|
+
bytes,
|
|
259
|
+
view,
|
|
260
|
+
offset,
|
|
261
|
+
start,
|
|
262
|
+
directoryEnd
|
|
263
|
+
});
|
|
264
|
+
if (Result.isError(entry)) return entry;
|
|
265
|
+
const { name, declared, directory, next } = entry.value;
|
|
266
|
+
if (names.has(name)) return invalid();
|
|
267
|
+
names.add(name);
|
|
268
|
+
if (!directory) {
|
|
269
|
+
if (declared > BigInt(maxEntryBytes)) return limitError("entry-too-large");
|
|
270
|
+
total += declared;
|
|
271
|
+
if (total > BigInt(maxTotalBytes)) return limitError("total-too-large");
|
|
272
|
+
}
|
|
273
|
+
offset = next;
|
|
274
|
+
}
|
|
275
|
+
if (BigInt(records) !== count) return invalid();
|
|
276
|
+
return Result.ok();
|
|
277
|
+
};
|
|
278
|
+
const loadDocxResult = async (buffer, options = {}) => {
|
|
279
|
+
const maxEntryBytes = options.maxEntryBytes ?? 134217728;
|
|
280
|
+
const maxTotalBytes = options.maxTotalBytes ?? 268435456;
|
|
281
|
+
const maxEntries = options.maxEntries ?? 4096;
|
|
282
|
+
if (buffer.byteLength > 268435456) return Result.err(new DocxArchiveError({
|
|
283
|
+
message: "DOCX archive exceeds the input byte limit",
|
|
284
|
+
reason: "total-too-large"
|
|
285
|
+
}));
|
|
286
|
+
const bytes = buffer instanceof ArrayBuffer ? new Uint8Array(buffer) : buffer;
|
|
287
|
+
const preflight = preflightArchive(bytes, {
|
|
288
|
+
maxEntries,
|
|
289
|
+
maxEntryBytes,
|
|
290
|
+
maxTotalBytes
|
|
291
|
+
});
|
|
292
|
+
if (Result.isError(preflight)) return preflight;
|
|
293
|
+
const loaded = await Result.tryPromise({
|
|
294
|
+
try: async () => await JSZip.loadAsync(buffer),
|
|
295
|
+
catch: (cause) => new DocxArchiveError({
|
|
296
|
+
message: "Failed to parse DOCX archive",
|
|
297
|
+
reason: "load-failed",
|
|
298
|
+
cause
|
|
299
|
+
})
|
|
300
|
+
});
|
|
301
|
+
if (Result.isError(loaded)) return loaded;
|
|
302
|
+
const zip = loaded.value;
|
|
303
|
+
for (const entry of Object.values(zip.files)) {
|
|
304
|
+
if (entry.dir) continue;
|
|
305
|
+
const originalName = entry.unsafeOriginalName ?? entry.name;
|
|
306
|
+
if (originalName.startsWith("/") || /^[a-z]:/iu.test(originalName) || originalName.includes("\\") || originalName.split("/").includes("..")) return Result.err(new DocxArchiveError({
|
|
307
|
+
message: "DOCX archive contains an invalid entry name",
|
|
308
|
+
reason: "invalid-entry-name"
|
|
309
|
+
}));
|
|
310
|
+
}
|
|
311
|
+
let validatedBytes = 0;
|
|
312
|
+
for (const entry of Object.values(zip.files)) {
|
|
313
|
+
if (entry.dir) continue;
|
|
314
|
+
const remainingBytes = maxTotalBytes - validatedBytes;
|
|
315
|
+
const read = await Result.tryPromise({
|
|
316
|
+
try: async () => await readEntryBounded(entry, {
|
|
317
|
+
maxEntryBytes,
|
|
318
|
+
remainingBytes
|
|
319
|
+
}),
|
|
320
|
+
catch: (cause) => cause instanceof DocxArchiveError ? cause : new DocxArchiveError({
|
|
321
|
+
message: "Failed to read DOCX entry",
|
|
322
|
+
reason: "load-failed",
|
|
323
|
+
cause
|
|
324
|
+
})
|
|
325
|
+
});
|
|
326
|
+
if (Result.isError(read)) return read;
|
|
327
|
+
validatedBytes += read.value;
|
|
328
|
+
}
|
|
329
|
+
return Result.ok(zip);
|
|
330
|
+
};
|
|
331
|
+
/** Load and validate an archive, preserving the promise-based ZIP API. */
|
|
332
|
+
const loadDocx = async (buffer, options = {}) => {
|
|
333
|
+
const result = await loadDocxResult(buffer, options);
|
|
334
|
+
if (Result.isError(result)) return await Promise.reject(result.error);
|
|
335
|
+
return result.value;
|
|
336
|
+
};
|
|
337
|
+
const loadDocxArchive = async (buffer, options = {}) => {
|
|
338
|
+
const maxEntryBytes = options.maxEntryBytes ?? 134217728;
|
|
339
|
+
const maxTotalBytes = options.maxTotalBytes ?? 268435456;
|
|
340
|
+
const zip = await loadDocx(buffer, options);
|
|
341
|
+
let totalRead = 0;
|
|
342
|
+
let readChain = Promise.resolve();
|
|
343
|
+
const readEntry = async (path) => {
|
|
344
|
+
const work = async () => {
|
|
345
|
+
const entry = zip.file(path);
|
|
346
|
+
if (!entry) return null;
|
|
347
|
+
const remaining = maxTotalBytes - totalRead;
|
|
348
|
+
const chunks = [];
|
|
349
|
+
const size = await readEntryBounded(entry, {
|
|
350
|
+
maxEntryBytes,
|
|
351
|
+
remainingBytes: remaining,
|
|
352
|
+
onChunk: (chunk) => {
|
|
353
|
+
chunks.push(chunk);
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
totalRead += size;
|
|
357
|
+
return Buffer.concat(chunks, size);
|
|
358
|
+
};
|
|
359
|
+
const next = readChain.then(work, work);
|
|
360
|
+
readChain = next.then(() => void 0, () => void 0);
|
|
361
|
+
return await next;
|
|
362
|
+
};
|
|
363
|
+
return {
|
|
364
|
+
zip,
|
|
365
|
+
async readEntryString(path) {
|
|
366
|
+
const buf = await readEntry(path);
|
|
367
|
+
return buf === null ? null : buf.toString("utf-8");
|
|
368
|
+
},
|
|
369
|
+
async readEntryUint8(path) {
|
|
370
|
+
const buf = await readEntry(path);
|
|
371
|
+
return buf === null ? null : new Uint8Array(buf);
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
};
|
|
375
|
+
//#endregion
|
|
30
376
|
//#region src/zip.ts
|
|
31
377
|
/** Standard DOCX compression options */
|
|
32
378
|
const DOCX_COMPRESSION = {
|
|
@@ -34,8 +380,6 @@ const DOCX_COMPRESSION = {
|
|
|
34
380
|
compression: "DEFLATE",
|
|
35
381
|
compressionOptions: { level: 6 }
|
|
36
382
|
};
|
|
37
|
-
/** Load a DOCX file (ArrayBuffer) into a JSZip instance */
|
|
38
|
-
const loadDocx = async (buffer) => await JSZip.loadAsync(buffer);
|
|
39
383
|
/** Extract a text file from a ZIP */
|
|
40
384
|
const extractText = async (zip, path) => {
|
|
41
385
|
const file = zip.file(path);
|
|
@@ -58,15 +402,24 @@ const repackZip = async (zip) => await zip.generateAsync(DOCX_COMPRESSION);
|
|
|
58
402
|
* entities produced for the other characters.
|
|
59
403
|
*/
|
|
60
404
|
const escapeXmlAttribute = (value) => value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """);
|
|
405
|
+
const relationshipIds = (relsXml) => {
|
|
406
|
+
const document = slimdom.parseXmlDocument(relsXml);
|
|
407
|
+
const ids = /* @__PURE__ */ new Set();
|
|
408
|
+
for (const element of document.getElementsByTagNameNS("*", "Relationship")) {
|
|
409
|
+
const id = element.getAttribute("Id");
|
|
410
|
+
if (id !== null) ids.add(id);
|
|
411
|
+
}
|
|
412
|
+
return ids;
|
|
413
|
+
};
|
|
61
414
|
/** Find the next available rId in a relationships XML string */
|
|
62
415
|
const findNextRId = (relsXml) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const n =
|
|
416
|
+
let max = 0n;
|
|
417
|
+
for (const id of relationshipIds(relsXml)) {
|
|
418
|
+
if (!/^rId\d+$/u.test(id)) continue;
|
|
419
|
+
const n = BigInt(id.slice(3));
|
|
67
420
|
if (n > max) max = n;
|
|
68
421
|
}
|
|
69
|
-
return `rId${max +
|
|
422
|
+
return `rId${max + 1n}`;
|
|
70
423
|
};
|
|
71
424
|
/** Ensure a content type entry exists in [Content_Types].xml */
|
|
72
425
|
const ensureContentType = (contentTypesXml, partName, contentType) => {
|
|
@@ -78,7 +431,7 @@ const ensureContentType = (contentTypesXml, partName, contentType) => {
|
|
|
78
431
|
/** Ensure a relationship entry exists */
|
|
79
432
|
const ensureRelationship = (relsXml, rId, type, target) => {
|
|
80
433
|
const escapedRId = escapeXmlAttribute(rId);
|
|
81
|
-
if (relsXml.
|
|
434
|
+
if (relationshipIds(relsXml).has(rId)) return relsXml;
|
|
82
435
|
const rel = `<Relationship Id="${escapedRId}" Type="${escapeXmlAttribute(type)}" Target="${escapeXmlAttribute(target)}"/>`;
|
|
83
436
|
return relsXml.replace("</Relationships>", () => `${rel}\n</Relationships>`);
|
|
84
437
|
};
|
|
@@ -206,4 +559,4 @@ const sanitizeAttachedTemplateSource = (xml, removedRelationshipIds) => {
|
|
|
206
559
|
};
|
|
207
560
|
};
|
|
208
561
|
//#endregion
|
|
209
|
-
export { ATTACHED_TEMPLATE_SECURITY_RULE, ATTACHED_TEMPLATE_TARGET_KIND, DOCX_COMPRESSION, OOXML_NS, classifyAttachedTemplateTarget, ensureContentType, ensureRelationship, extractBinary, extractText, findNextRId, isOpcRelationshipPartPath, loadDocx, relationshipSourcePartPath, repackZip, sanitizeAttachedTemplateRelationships, sanitizeAttachedTemplateSource };
|
|
562
|
+
export { ATTACHED_TEMPLATE_SECURITY_RULE, ATTACHED_TEMPLATE_TARGET_KIND, DOCX_COMPRESSION, DOCX_MAX_ENTRIES, DOCX_MAX_ENTRY_BYTES, DOCX_MAX_TOTAL_BYTES, DocxArchiveError, OOXML_NS, classifyAttachedTemplateTarget, ensureContentType, ensureRelationship, extractBinary, extractText, findNextRId, isOpcRelationshipPartPath, loadDocx, loadDocxArchive, relationshipSourcePartPath, repackZip, sanitizeAttachedTemplateRelationships, sanitizeAttachedTemplateSource };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stll/docx-utils",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Low-level helpers for reading and writing DOCX/OOXML zip packages: text and binary extraction, relationship and content-type management, and namespace constants.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"docx",
|
|
@@ -41,18 +41,22 @@
|
|
|
41
41
|
"pack:dry-run": "bun pm pack --dry-run",
|
|
42
42
|
"test": "bun test src",
|
|
43
43
|
"typecheck": "bun ../../packages/scripts/src/tsc-native.ts --noEmit",
|
|
44
|
-
"lint": "cd ../.. && bun --bun oxlint -c oxlint.config.ts --report-unused-disable-directives-severity=error --
|
|
44
|
+
"lint": "cd ../.. && bun --bun oxlint -c oxlint.config.ts --report-unused-disable-directives-severity=error --type-aware --type-check packages/docx-utils",
|
|
45
45
|
"lint:fix": "cd ../.. && bun --bun oxlint -c oxlint.config.ts --type-aware --fix packages/docx-utils",
|
|
46
46
|
"format": "bun ../../scripts/run-oxfmt.ts .",
|
|
47
|
-
"prepack": "bun run build"
|
|
47
|
+
"prepack": "bun run build",
|
|
48
|
+
"test:property": "bun test --preload @stll/property-testing/preload $(grep -rlF 'fc.assert' src --include='*.test.ts')"
|
|
48
49
|
},
|
|
49
50
|
"dependencies": {
|
|
50
|
-
"
|
|
51
|
+
"better-result": "3.0.1",
|
|
52
|
+
"jszip": "3.10.2",
|
|
51
53
|
"slimdom": "^4.3.5"
|
|
52
54
|
},
|
|
53
55
|
"devDependencies": {
|
|
56
|
+
"@stll/property-testing": "0.0.0",
|
|
54
57
|
"@stll/typescript-config": "0.0.0",
|
|
55
58
|
"bun-types": "1.4.2",
|
|
59
|
+
"fast-check": "^4.10.1",
|
|
56
60
|
"tsdown": "0.23.0"
|
|
57
61
|
},
|
|
58
62
|
"main": "./dist/index.js",
|