@hilvana/office 0.1.0 → 0.2.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/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/parse.d.ts +8 -2
- package/dist/parse.js +57 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { wordToPdf, type WordToPdfOptions, type WordToPdfResult, type WordWarning } from './word.js';
|
|
2
2
|
export { convertWord, type WordEngine } from './convert.js';
|
|
3
3
|
export { findLibreOffice, libreOfficeToPdf } from './libreoffice.js';
|
|
4
|
-
export { DocxError, isDocx, parseDocx } from './parse.js';
|
|
4
|
+
export { DOCX_LIMITS, DocxError, isDocx, parseDocx } from './parse.js';
|
|
5
5
|
export { fallbackTextFonts, familyFor, nodeFontLoader, type Family, type FontLoader } from './fonts.js';
|
|
6
6
|
export type * from './model.js';
|
|
7
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { wordToPdf } from './word.js';
|
|
2
2
|
export { convertWord } from './convert.js';
|
|
3
3
|
export { findLibreOffice, libreOfficeToPdf } from './libreoffice.js';
|
|
4
|
-
export { DocxError, isDocx, parseDocx } from './parse.js';
|
|
4
|
+
export { DOCX_LIMITS, DocxError, isDocx, parseDocx } from './parse.js';
|
|
5
5
|
export { fallbackTextFonts, familyFor, nodeFontLoader } from './fonts.js';
|
|
6
6
|
//# sourceMappingURL=index.js.map
|
package/dist/parse.d.ts
CHANGED
|
@@ -5,9 +5,15 @@ export interface ParsedDocx extends DocModel {
|
|
|
5
5
|
headerDistance: number;
|
|
6
6
|
footerDistance: number;
|
|
7
7
|
}
|
|
8
|
+
/** Ceilings for what a .docx may expand to in memory. Real documents stay far below them. */
|
|
9
|
+
export declare const DOCX_LIMITS: {
|
|
10
|
+
partBytes: number;
|
|
11
|
+
totalBytes: number;
|
|
12
|
+
entries: number;
|
|
13
|
+
};
|
|
8
14
|
export declare class DocxError extends Error {
|
|
9
|
-
readonly code: 'NOT_DOCX' | 'ENCRYPTED_DOCX' | 'CORRUPT_DOCX';
|
|
10
|
-
constructor(code: 'NOT_DOCX' | 'ENCRYPTED_DOCX' | 'CORRUPT_DOCX', message?: string);
|
|
15
|
+
readonly code: 'NOT_DOCX' | 'ENCRYPTED_DOCX' | 'CORRUPT_DOCX' | 'DOCX_TOO_LARGE';
|
|
16
|
+
constructor(code: 'NOT_DOCX' | 'ENCRYPTED_DOCX' | 'CORRUPT_DOCX' | 'DOCX_TOO_LARGE', message?: string);
|
|
11
17
|
}
|
|
12
18
|
/** True when the bytes look like a Word .docx package. */
|
|
13
19
|
export declare function isDocx(bytes: Uint8Array): boolean;
|
package/dist/parse.js
CHANGED
|
@@ -786,6 +786,8 @@ function readPage(sect) {
|
|
|
786
786
|
footer: twip(attr(pgMar, 'footer')) ?? 36,
|
|
787
787
|
};
|
|
788
788
|
}
|
|
789
|
+
/** Ceilings for what a .docx may expand to in memory. Real documents stay far below them. */
|
|
790
|
+
export const DOCX_LIMITS = { partBytes: 64 * 1024 * 1024, totalBytes: 256 * 1024 * 1024, entries: 5000 };
|
|
789
791
|
export class DocxError extends Error {
|
|
790
792
|
code;
|
|
791
793
|
constructor(code, message) {
|
|
@@ -813,14 +815,68 @@ export function parseDocx(bytes) {
|
|
|
813
815
|
if (bytes[0] === 0xd0 && bytes[1] === 0xcf && bytes[2] === 0x11 && bytes[3] === 0xe0)
|
|
814
816
|
throw new DocxError('ENCRYPTED_DOCX');
|
|
815
817
|
let files;
|
|
818
|
+
// Guard against zip bombs: a few MB can declare gigabytes of content. Only the parts the
|
|
819
|
+
// converter reads are extracted, and their declared sizes are capped before inflating.
|
|
820
|
+
let total = 0;
|
|
821
|
+
let count = 0;
|
|
822
|
+
let tooLarge;
|
|
823
|
+
const take = (f) => {
|
|
824
|
+
// Deflate never grows data by more than a few bytes per block: a compressed part much larger
|
|
825
|
+
// than its declared size is lying about it, so the cap above would not hold.
|
|
826
|
+
if (f.size > f.originalSize * 1.01 + 1024)
|
|
827
|
+
tooLarge ??= `${f.name} hides its real size`;
|
|
828
|
+
total += f.originalSize;
|
|
829
|
+
if (f.originalSize > DOCX_LIMITS.partBytes)
|
|
830
|
+
tooLarge ??= `${f.name} declares ${f.originalSize} bytes`;
|
|
831
|
+
if (total > DOCX_LIMITS.totalBytes)
|
|
832
|
+
tooLarge ??= `content declares ${total} bytes`;
|
|
833
|
+
return !tooLarge;
|
|
834
|
+
};
|
|
816
835
|
try {
|
|
817
|
-
|
|
836
|
+
// First the XML parts and relationships, then only the media those relationships point to.
|
|
837
|
+
files = unzipSync(bytes, {
|
|
838
|
+
filter: (f) => {
|
|
839
|
+
if (++count > DOCX_LIMITS.entries)
|
|
840
|
+
tooLarge ??= 'too many parts';
|
|
841
|
+
if (!/^word\/.*\.(xml|rels)$/.test(f.name))
|
|
842
|
+
return false;
|
|
843
|
+
return take(f);
|
|
844
|
+
},
|
|
845
|
+
});
|
|
846
|
+
const referenced = new Set();
|
|
847
|
+
for (const [name, data] of Object.entries(files)) {
|
|
848
|
+
if (!name.endsWith('.rels'))
|
|
849
|
+
continue;
|
|
850
|
+
const dir = name.replace(/_rels\/[^/]+\.rels$/, '');
|
|
851
|
+
for (const m of strFromU8(data).matchAll(/Target="([^"]+)"/g)) {
|
|
852
|
+
const target = m[1];
|
|
853
|
+
if (/^[a-z]+:/i.test(target))
|
|
854
|
+
continue; // external links are never fetched
|
|
855
|
+
referenced.add(normalize(target.startsWith('/') ? target.slice(1) : dir + target));
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
if (!tooLarge && referenced.size) {
|
|
859
|
+
Object.assign(files, unzipSync(bytes, { filter: (f) => !(f.name in files) && referenced.has(f.name) && take(f) }));
|
|
860
|
+
}
|
|
818
861
|
}
|
|
819
862
|
catch (err) {
|
|
820
863
|
throw new DocxError('CORRUPT_DOCX', String(err.message));
|
|
821
864
|
}
|
|
865
|
+
if (tooLarge)
|
|
866
|
+
throw new DocxError('DOCX_TOO_LARGE', tooLarge);
|
|
822
867
|
if (!files['word/document.xml'])
|
|
823
868
|
throw new DocxError('NOT_DOCX');
|
|
869
|
+
// Anything that breaks while reading the parts means a damaged document, with a code callers can explain (D21).
|
|
870
|
+
try {
|
|
871
|
+
return readDocx(files);
|
|
872
|
+
}
|
|
873
|
+
catch (err) {
|
|
874
|
+
if (err instanceof DocxError)
|
|
875
|
+
throw err;
|
|
876
|
+
throw new DocxError('CORRUPT_DOCX', String(err?.message ?? err).slice(0, 200));
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
function readDocx(files) {
|
|
824
880
|
const ctx = new Context(files);
|
|
825
881
|
loadStyles(ctx);
|
|
826
882
|
const docPart = { path: 'word/document.xml', rels: loadRels(ctx, 'word/document.xml') };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hilvana/office",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Hilvana office conversions that run locally: Word (.docx) to PDF with real, selectable text.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"docx",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "https://github.com/gethilvana/hilvana",
|
|
21
|
+
"url": "git+https://github.com/gethilvana/hilvana.git",
|
|
22
22
|
"directory": "packages/office"
|
|
23
23
|
},
|
|
24
24
|
"type": "module",
|