@d0paminedriven/fs 11.0.0 → 12.0.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/docs/index.d.ts +42 -10
- package/dist/docs/index.js +177 -41
- package/dist/extract/client.d.ts +13 -11
- package/dist/extract/client.js +2 -4
- package/dist/extract/index.d.ts +7 -10
- package/dist/extract/index.js +6 -8
- package/dist/fs/index.d.ts +7 -11
- package/dist/fs/index.js +15 -14
- package/dist/fs-atomic/index.d.ts +3 -9
- package/dist/fs-atomic/index.js +17 -11
- package/dist/fs-base/index.d.ts +8 -10
- package/dist/fs-base/index.js +36 -27
- package/dist/fs-core/index.d.ts +5 -9
- package/dist/fs-core/index.js +5 -6
- package/dist/fs-fetch/index.d.ts +6 -9
- package/dist/fs-fetch/index.js +64 -27
- package/dist/fs-size/index.d.ts +2 -6
- package/dist/fs-size/index.js +8 -9
- package/dist/fs-tmp/index.d.ts +7 -11
- package/dist/fs-tmp/index.js +7 -8
- package/dist/images/index.d.ts +44 -48
- package/dist/images/index.js +9 -15
- package/dist/images/workup.d.ts +5 -9
- package/dist/images/workup.js +4 -5
- package/dist/index.d.ts +42 -14
- package/dist/index.js +14 -15
- package/dist/ld/index.d.ts +1 -5
- package/dist/ld/index.js +2 -4
- package/dist/mime/index.d.ts +20 -20
- package/dist/mime/index.js +4 -4
- package/dist/mixins/index.d.ts +9 -13
- package/dist/mixins/index.js +6 -6
- package/dist/types/index.d.ts +105 -90
- package/dist/types/index.js +2 -3
- package/dist/types/stream.d.ts +5 -9
- package/dist/types/stream.js +1 -1
- package/dist/url/index.d.ts +6 -10
- package/dist/url/index.js +2 -4
- package/dist/utils/index.d.ts +5 -9
- package/dist/utils/index.js +2 -4
- package/package.json +18 -18
package/dist/docs/index.d.ts
CHANGED
|
@@ -1,9 +1,41 @@
|
|
|
1
|
-
import { DocSpecs, ZipEntry } from "../types/index.js";
|
|
2
1
|
import { Readable } from "node:stream";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
declare class DocMetadataExtractor {
|
|
2
|
+
import { DocSpecs, PdfImageAnalysisMetadata, ZipEntry } from "../types/index.js";
|
|
3
|
+
export declare class DocMetadataExtractor {
|
|
6
4
|
protected toSafeString(buf: Uint8Array, encoding?: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* Detect XObject image references in the PDF
|
|
7
|
+
* Images are declared as XObjects with /Subtype /Image
|
|
8
|
+
*/
|
|
9
|
+
private detectXObjectImages;
|
|
10
|
+
/**
|
|
11
|
+
* Detect inline images (BI ... ID ... EI blocks)
|
|
12
|
+
* These are embedded directly in content streams, common in older PDFs
|
|
13
|
+
*/
|
|
14
|
+
private detectInlineImages;
|
|
15
|
+
/**
|
|
16
|
+
* Check for vector graphics (Form XObjects, paths, etc.)
|
|
17
|
+
* These don't need multimodal but indicate visual complexity
|
|
18
|
+
*/
|
|
19
|
+
private detectVectorGraphics;
|
|
20
|
+
private hasFaxEncoding;
|
|
21
|
+
/**
|
|
22
|
+
* Estimate if PDF is scanned (image-only with no real text layer)
|
|
23
|
+
*/
|
|
24
|
+
private detectScannedPdf;
|
|
25
|
+
/**
|
|
26
|
+
* Analyze images in compressed streams (for Acrobat-optimized PDFs)
|
|
27
|
+
* Reuses your existing decompression infrastructure
|
|
28
|
+
*/
|
|
29
|
+
private detectImagesInCompressedStreams;
|
|
30
|
+
analyzePdfImages(buffer: Buffer, pageCount: number | null): {
|
|
31
|
+
hasImages: boolean;
|
|
32
|
+
imageCount: number;
|
|
33
|
+
estimatedImageCoverage: "none" | "moderate" | "minimal" | "heavy";
|
|
34
|
+
imageTypes: Set<"jpeg" | "jpeg2000" | "png-like" | "ccitt-fax" | "jbig2" | "inline">;
|
|
35
|
+
isLikelyScanned: boolean;
|
|
36
|
+
hasVectorGraphics: boolean;
|
|
37
|
+
recommendation: "multimodal" | "text-only";
|
|
38
|
+
};
|
|
7
39
|
protected detectTextEncodingPrefix(buffer: Uint8Array): {
|
|
8
40
|
encoding: string;
|
|
9
41
|
offset: number;
|
|
@@ -99,7 +131,8 @@ declare class DocMetadataExtractor {
|
|
|
99
131
|
* Used as fallback when traditional PDF Info dictionary lacks dates
|
|
100
132
|
*/
|
|
101
133
|
private extractXmpDates;
|
|
102
|
-
|
|
134
|
+
private cleanPdfImgAnalysis;
|
|
135
|
+
parsePdf(buffer: Buffer, mime: "application/pdf"): DocSpecs<PdfImageAnalysisMetadata>;
|
|
103
136
|
parseRtf(buffer: Buffer, mime: string): DocSpecs;
|
|
104
137
|
parsePlainText(buffer: Buffer, mime: string, filename?: string): DocSpecs;
|
|
105
138
|
/** ---- Minimal ZIP reader utilities (central directory based) ---- */
|
|
@@ -110,10 +143,9 @@ declare class DocMetadataExtractor {
|
|
|
110
143
|
readLocalFileData(buffer: Buffer, entry: ZipEntry): Uint8Array | null;
|
|
111
144
|
parseOpenXml(buffer: Buffer, mime: string, kind: "docx" | "pptx" | "xlsx"): DocSpecs;
|
|
112
145
|
protected readBytes(stream: Readable): Promise<Buffer<ArrayBufferLike>>;
|
|
113
|
-
extractDocViaStream(stream: Readable, mime: string): Promise<DocSpecs
|
|
114
|
-
extractDocViaPath(filePath: string, mime: string): Promise<DocSpecs
|
|
115
|
-
getDocumentSpecsWorkup(
|
|
146
|
+
extractDocViaStream(stream: Readable, mime: string): Promise<DocSpecs<unknown>>;
|
|
147
|
+
extractDocViaPath(filePath: string, mime: string): Promise<DocSpecs<unknown>>;
|
|
148
|
+
getDocumentSpecsWorkup(buffer: Buffer<ArrayBufferLike>, mime: "application/pdf", fileName?: string): DocSpecs<PdfImageAnalysisMetadata>;
|
|
149
|
+
getDocumentSpecsWorkup(buffer: Buffer<ArrayBufferLike>, mime: string, fileName?: string): DocSpecs<unknown>;
|
|
116
150
|
}
|
|
117
|
-
//#endregion
|
|
118
|
-
export { DocMetadataExtractor };
|
|
119
151
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/docs/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { createReadStream } from "node:fs";
|
|
2
2
|
import { relative } from "node:path";
|
|
3
3
|
import { inflateSync } from "fflate";
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
import { mimeToExt } from "../mime/index.js";
|
|
5
|
+
// src/docs/index.ts
|
|
6
6
|
var DocMetadataExtractor = class {
|
|
7
7
|
toSafeString(buf, encoding = "utf-8") {
|
|
8
8
|
try {
|
|
@@ -11,6 +11,132 @@ var DocMetadataExtractor = class {
|
|
|
11
11
|
return Array.from(buf).map((b) => String.fromCharCode(b)).join("");
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Detect XObject image references in the PDF
|
|
16
|
+
* Images are declared as XObjects with /Subtype /Image
|
|
17
|
+
*/
|
|
18
|
+
detectXObjectImages(text) {
|
|
19
|
+
const types = Array.of();
|
|
20
|
+
const imageMatches = text.match(/\d+\s+\d+\s+obj[\s\S]*?\/Subtype\s*\/Image[\s\S]*?endobj/g) ?? [];
|
|
21
|
+
const reverseMatches = text.match(/\d+\s+\d+\s+obj[\s\S]*?\/Type\s*\/XObject[\s\S]*?\/Subtype\s*\/Image[\s\S]*?endobj/g) ?? [];
|
|
22
|
+
const allImageBlocks = [.../* @__PURE__ */ new Set([...imageMatches, ...reverseMatches])];
|
|
23
|
+
for (const block of allImageBlocks) {
|
|
24
|
+
if (/\/Filter\s*\/DCTDecode/.test(block) || /\/Filter\s*\[.*?\/DCTDecode/.test(block)) types.push("jpeg");
|
|
25
|
+
if (/\/Filter\s*\/JPXDecode/.test(block) || /\/Filter\s*\[.*?\/JPXDecode/.test(block)) types.push("jpeg2000");
|
|
26
|
+
if (/\/Filter\s*\/CCITTFaxDecode/.test(block) || /\/Filter\s*\[.*?\/CCITTFaxDecode/.test(block)) types.push("ccitt-fax");
|
|
27
|
+
if (/\/Filter\s*\/JBIG2Decode/.test(block) || /\/Filter\s*\[.*?\/JBIG2Decode/.test(block)) types.push("jbig2");
|
|
28
|
+
if (/\/Filter\s*\/FlateDecode/.test(block) && /\/ColorSpace/.test(block)) types.push("png-like");
|
|
29
|
+
}
|
|
30
|
+
const objNumbers = /* @__PURE__ */ new Set();
|
|
31
|
+
const objNumPattern = /(\d+)\s+\d+\s+obj[\s\S]*?\/Subtype\s*\/Image/g;
|
|
32
|
+
let match;
|
|
33
|
+
while ((match = objNumPattern.exec(text)) !== null) if (match?.[1]) objNumbers.add(match[1]);
|
|
34
|
+
return {
|
|
35
|
+
count: objNumbers.size,
|
|
36
|
+
types: Array.from(types)
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Detect inline images (BI ... ID ... EI blocks)
|
|
41
|
+
* These are embedded directly in content streams, common in older PDFs
|
|
42
|
+
*/
|
|
43
|
+
detectInlineImages(text) {
|
|
44
|
+
return text.match(/\sBI\s[\s\S]*?\sID[\s\S]*?\sEI\s/g)?.length ?? 0;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Check for vector graphics (Form XObjects, paths, etc.)
|
|
48
|
+
* These don't need multimodal but indicate visual complexity
|
|
49
|
+
*/
|
|
50
|
+
detectVectorGraphics(text) {
|
|
51
|
+
const hasFormXObjects = /\/Subtype\s*\/Form/.test(text);
|
|
52
|
+
const hasComplexPaths = /\d+\.?\d*\s+\d+\.?\d*\s+\d+\.?\d*\s+\d+\.?\d*\s+\d+\.?\d*\s+\d+\.?\d*\s+c/.test(text);
|
|
53
|
+
const hasShading = /\/ShadingType\s+\d/.test(text) || /\/Pattern/.test(text);
|
|
54
|
+
return hasFormXObjects || hasComplexPaths || hasShading;
|
|
55
|
+
}
|
|
56
|
+
hasFaxEncoding(types) {
|
|
57
|
+
for (const type of types) {
|
|
58
|
+
if (type === "ccitt-fax") return true;
|
|
59
|
+
if (type === "jbig2") return true;
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Estimate if PDF is scanned (image-only with no real text layer)
|
|
65
|
+
*/
|
|
66
|
+
detectScannedPdf(text, imageAnalysis, pageCount) {
|
|
67
|
+
const hasFaxEncoding = this.hasFaxEncoding(imageAnalysis.types);
|
|
68
|
+
const hasSubstantialText = (text.match(/BT[\s\S]*?ET/g) ?? []).length > 5;
|
|
69
|
+
const hasToUnicode = /\/ToUnicode/.test(text);
|
|
70
|
+
const pages = pageCount ?? 1;
|
|
71
|
+
const imagesPerPage = imageAnalysis.count / pages;
|
|
72
|
+
if (hasFaxEncoding) return true;
|
|
73
|
+
if (imagesPerPage >= .8 && !hasSubstantialText && !hasToUnicode) return true;
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Analyze images in compressed streams (for Acrobat-optimized PDFs)
|
|
78
|
+
* Reuses your existing decompression infrastructure
|
|
79
|
+
*/
|
|
80
|
+
detectImagesInCompressedStreams(buffer) {
|
|
81
|
+
const types = /* @__PURE__ */ new Set();
|
|
82
|
+
let additionalImages = 0;
|
|
83
|
+
try {
|
|
84
|
+
const text = buffer.toString("latin1");
|
|
85
|
+
const objStreams = this.parseObjectStreams(buffer, text);
|
|
86
|
+
const flateStreams = this.decompressFlateStreams(buffer);
|
|
87
|
+
for (const content of [...objStreams, ...flateStreams]) {
|
|
88
|
+
const imageRefs = content.match(/\/Subtype\s*\/Image/g);
|
|
89
|
+
if (imageRefs) {
|
|
90
|
+
additionalImages += imageRefs.length;
|
|
91
|
+
if (/\/DCTDecode/.test(content)) types.add("jpeg");
|
|
92
|
+
if (/\/JPXDecode/.test(content)) types.add("jpeg2000");
|
|
93
|
+
if (/\/CCITTFaxDecode/.test(content)) types.add("ccitt-fax");
|
|
94
|
+
if (/\/JBIG2Decode/.test(content)) types.add("jbig2");
|
|
95
|
+
if (/\/FlateDecode[\s\S]{0,200}\/ColorSpace/.test(content)) types.add("png-like");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
} catch {}
|
|
99
|
+
return {
|
|
100
|
+
additionalImages,
|
|
101
|
+
types
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
analyzePdfImages(buffer, pageCount) {
|
|
105
|
+
const text = buffer.toString("latin1");
|
|
106
|
+
const xobjectAnalysis = this.detectXObjectImages(text);
|
|
107
|
+
const inlineCount = this.detectInlineImages(text);
|
|
108
|
+
const compressedAnalysis = this.detectImagesInCompressedStreams(buffer);
|
|
109
|
+
const xobjSet = /* @__PURE__ */ new Set([...xobjectAnalysis.types]);
|
|
110
|
+
const allTypes = compressedAnalysis.types.union(xobjSet);
|
|
111
|
+
if (inlineCount > 0) allTypes.add("inline");
|
|
112
|
+
const totalImages = xobjectAnalysis.count + inlineCount + compressedAnalysis.additionalImages;
|
|
113
|
+
const hasImages = totalImages > 0;
|
|
114
|
+
const hasVectorGraphics = this.detectVectorGraphics(text);
|
|
115
|
+
const isLikelyScanned = this.detectScannedPdf(text, {
|
|
116
|
+
count: totalImages,
|
|
117
|
+
types: Array.from(allTypes)
|
|
118
|
+
}, pageCount);
|
|
119
|
+
const imagesPerPage = totalImages / (pageCount ?? 1);
|
|
120
|
+
let estimatedImageCoverage = null;
|
|
121
|
+
if (totalImages === 0) estimatedImageCoverage = "none";
|
|
122
|
+
else if (imagesPerPage < .5) estimatedImageCoverage = "minimal";
|
|
123
|
+
else if (imagesPerPage < 2) estimatedImageCoverage = "moderate";
|
|
124
|
+
else estimatedImageCoverage = "heavy";
|
|
125
|
+
let recommendation = null;
|
|
126
|
+
if (isLikelyScanned) recommendation = "multimodal";
|
|
127
|
+
else if (estimatedImageCoverage === "none" && !hasVectorGraphics) recommendation = "text-only";
|
|
128
|
+
else if (estimatedImageCoverage === "minimal" && !isLikelyScanned) recommendation = "text-only";
|
|
129
|
+
else recommendation = "multimodal";
|
|
130
|
+
return {
|
|
131
|
+
hasImages,
|
|
132
|
+
imageCount: totalImages,
|
|
133
|
+
estimatedImageCoverage,
|
|
134
|
+
imageTypes: allTypes,
|
|
135
|
+
isLikelyScanned,
|
|
136
|
+
hasVectorGraphics,
|
|
137
|
+
recommendation
|
|
138
|
+
};
|
|
139
|
+
}
|
|
14
140
|
detectTextEncodingPrefix(buffer) {
|
|
15
141
|
if (buffer.length >= 3 && buffer[0] === 239 && buffer[1] === 187 && buffer[2] === 191) return {
|
|
16
142
|
encoding: "utf-8",
|
|
@@ -502,18 +628,20 @@ var DocMetadataExtractor = class {
|
|
|
502
628
|
const mi = Mi ?? "00";
|
|
503
629
|
const ss = S ?? "00";
|
|
504
630
|
let tz = "";
|
|
505
|
-
if (tzIndicator)
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
631
|
+
if (tzIndicator) {
|
|
632
|
+
if (tzIndicator === "Z") tz = "Z";
|
|
633
|
+
else {
|
|
634
|
+
const tzH = tzHours ?? "00";
|
|
635
|
+
const tzM = tzMinutes ?? "00";
|
|
636
|
+
tz = `${tzIndicator}${tzH.padStart(2, "0")}:${tzM.padStart(2, "0")}`;
|
|
637
|
+
}
|
|
510
638
|
}
|
|
511
639
|
return `${yyyy}-${mm.padStart(2, "0")}-${dd.padStart(2, "0")}T${hh.padStart(2, "0")}:${mi.padStart(2, "0")}:${ss.padStart(2, "0")}${tz}`;
|
|
512
640
|
}
|
|
513
641
|
getPdfInfoString(text, key) {
|
|
514
|
-
let m = text.match(
|
|
642
|
+
let m = text.match(new RegExp(`\/${key}\\s*\(([^)]*)\)`));
|
|
515
643
|
if (m?.[1]) return this.decodePdfString(m[1]);
|
|
516
|
-
m = text.match(
|
|
644
|
+
m = text.match(new RegExp(`\/${key}\\s*<([0-9A-Fa-f]+)>`));
|
|
517
645
|
if (m?.[1]) return this.decodeHexString(m[1]);
|
|
518
646
|
return null;
|
|
519
647
|
}
|
|
@@ -522,17 +650,17 @@ var DocMetadataExtractor = class {
|
|
|
522
650
|
* This is what you need to call from your main PDF parser
|
|
523
651
|
*/
|
|
524
652
|
extractPdfDate(text, fieldName) {
|
|
525
|
-
let match = text.match(
|
|
653
|
+
let match = text.match(new RegExp(`\/${fieldName}\\s*\\(([^)]+)\\)`));
|
|
526
654
|
if (match?.[1]) {
|
|
527
655
|
const decoded = this.decodePdfString(match[1]);
|
|
528
656
|
return this.parsePdfDate(decoded);
|
|
529
657
|
}
|
|
530
|
-
match = text.match(
|
|
658
|
+
match = text.match(new RegExp(`\/${fieldName}\\s*<([0-9A-Fa-f]+)>`));
|
|
531
659
|
if (match?.[1]) {
|
|
532
660
|
const decoded = this.decodeHexString(match[1]);
|
|
533
661
|
return this.parsePdfDate(decoded);
|
|
534
662
|
}
|
|
535
|
-
match = text.match(
|
|
663
|
+
match = text.match(new RegExp(`\/${fieldName}\\s+(D:[^\\s/>]+)`));
|
|
536
664
|
if (match?.[1]) return this.parsePdfDate(match[1]);
|
|
537
665
|
return null;
|
|
538
666
|
}
|
|
@@ -574,6 +702,13 @@ var DocMetadataExtractor = class {
|
|
|
574
702
|
modifiedDate
|
|
575
703
|
};
|
|
576
704
|
}
|
|
705
|
+
cleanPdfImgAnalysis(buffer, pageCount) {
|
|
706
|
+
const { imageTypes, ...rest } = this.analyzePdfImages(buffer, pageCount);
|
|
707
|
+
return {
|
|
708
|
+
imageTypes: Array.from(imageTypes),
|
|
709
|
+
...rest
|
|
710
|
+
};
|
|
711
|
+
}
|
|
577
712
|
parsePdf(buffer, mime) {
|
|
578
713
|
const text = buffer.toString("latin1");
|
|
579
714
|
const pdfVersion = text.match(/^%PDF-([0-9.]+)/)?.[1] ?? null;
|
|
@@ -618,7 +753,8 @@ var DocMetadataExtractor = class {
|
|
|
618
753
|
isLinearized,
|
|
619
754
|
textPreview,
|
|
620
755
|
createdDate,
|
|
621
|
-
modifiedDate
|
|
756
|
+
modifiedDate,
|
|
757
|
+
metadata: this.cleanPdfImgAnalysis(buffer, pageCount)
|
|
622
758
|
};
|
|
623
759
|
}
|
|
624
760
|
parseRtf(buffer, mime) {
|
|
@@ -654,7 +790,7 @@ var DocMetadataExtractor = class {
|
|
|
654
790
|
const lines = this.countLines(text);
|
|
655
791
|
return {
|
|
656
792
|
type: "DOCUMENT",
|
|
657
|
-
format: ext === "md" ? "
|
|
793
|
+
format: ext === "md" ? "md" : ext === "csv" ? "csv" : ext === "json" ? "json" : ext === "html" ? "html" : "txt",
|
|
658
794
|
mimeType: mime,
|
|
659
795
|
pageCount: null,
|
|
660
796
|
wordCount: words,
|
|
@@ -675,7 +811,7 @@ var DocMetadataExtractor = class {
|
|
|
675
811
|
}
|
|
676
812
|
/** ---- Minimal ZIP reader utilities (central directory based) ---- */
|
|
677
813
|
findEOCD(buffer) {
|
|
678
|
-
const sig =
|
|
814
|
+
const sig = 0x6054b50;
|
|
679
815
|
const maxSearch = Math.min(buffer.length, 65557);
|
|
680
816
|
for (let i = buffer.length - 22; i >= buffer.length - maxSearch; i--) {
|
|
681
817
|
if (i < 0) break;
|
|
@@ -687,7 +823,7 @@ var DocMetadataExtractor = class {
|
|
|
687
823
|
try {
|
|
688
824
|
const eocd = this.findEOCD(buffer);
|
|
689
825
|
if (eocd < 0 || eocd + 22 > buffer.length) return [];
|
|
690
|
-
if (buffer.readUInt32LE(eocd) !==
|
|
826
|
+
if (buffer.readUInt32LE(eocd) !== 0x6054b50) return [];
|
|
691
827
|
const diskNumber = buffer.readUInt16LE(eocd + 4);
|
|
692
828
|
const diskWithCD = buffer.readUInt16LE(eocd + 6);
|
|
693
829
|
if (diskNumber !== 0 || diskWithCD !== 0) return [];
|
|
@@ -794,7 +930,7 @@ var DocMetadataExtractor = class {
|
|
|
794
930
|
let modifiedDate = null;
|
|
795
931
|
if (coreXml) {
|
|
796
932
|
const s = this.toSafeString(coreXml);
|
|
797
|
-
const get = (tag) => s.match(
|
|
933
|
+
const get = (tag) => s.match(new RegExp(`<${tag}[^>]*>([\\s\\S]*?)<\\/${tag}>`));
|
|
798
934
|
author = get("dc:creator")?.[1]?.trim() ?? null;
|
|
799
935
|
subject = get("dc:subject")?.[1]?.trim() ?? null;
|
|
800
936
|
const kw = get("cp:keywords")?.[1]?.trim() ?? null;
|
|
@@ -807,7 +943,7 @@ var DocMetadataExtractor = class {
|
|
|
807
943
|
if (appXml) {
|
|
808
944
|
const s = this.toSafeString(appXml);
|
|
809
945
|
const getNum = (tag) => {
|
|
810
|
-
const m = s.match(
|
|
946
|
+
const m = s.match(new RegExp(`<${tag}[^>]*>([0-9]+)<\\/${tag}>`));
|
|
811
947
|
return m?.[1] ? Number.parseInt(m[1], 10) : null;
|
|
812
948
|
};
|
|
813
949
|
if (kind === "docx") {
|
|
@@ -934,7 +1070,7 @@ var DocMetadataExtractor = class {
|
|
|
934
1070
|
};
|
|
935
1071
|
}
|
|
936
1072
|
async readBytes(stream) {
|
|
937
|
-
return new Promise((resolve
|
|
1073
|
+
return new Promise((resolve, reject) => {
|
|
938
1074
|
const chunks = Array.of();
|
|
939
1075
|
let totalBytes = 0;
|
|
940
1076
|
const cleanup = () => {
|
|
@@ -956,12 +1092,12 @@ var DocMetadataExtractor = class {
|
|
|
956
1092
|
}
|
|
957
1093
|
if (totalBytes >= chunk.length) {
|
|
958
1094
|
cleanup();
|
|
959
|
-
resolve
|
|
1095
|
+
resolve(Buffer.concat(chunks));
|
|
960
1096
|
}
|
|
961
1097
|
});
|
|
962
1098
|
stream.on("end", () => {
|
|
963
1099
|
cleanup();
|
|
964
|
-
resolve
|
|
1100
|
+
resolve(Buffer.concat(chunks));
|
|
965
1101
|
});
|
|
966
1102
|
});
|
|
967
1103
|
}
|
|
@@ -975,30 +1111,30 @@ var DocMetadataExtractor = class {
|
|
|
975
1111
|
}
|
|
976
1112
|
getDocumentSpecsWorkup(rawbuffer, mime, filename) {
|
|
977
1113
|
const buffer = rawbuffer;
|
|
978
|
-
|
|
979
|
-
if (mime === "application/pdf"
|
|
980
|
-
|
|
1114
|
+
let ext = this.getExtFromFilename(filename);
|
|
1115
|
+
if (mime === "application/pdf") {
|
|
1116
|
+
ext = "pdf";
|
|
1117
|
+
return this.parsePdf(buffer, mime);
|
|
1118
|
+
} else if (buffer?.length >= 5 && buffer.toString("latin1", 0, 5) === "%PDF-") return this.parsePdf(buffer, "application/pdf");
|
|
1119
|
+
if (mime === "application/rtf" || buffer?.length >= 5 && buffer.toString("latin1", 0, 5) === "{\\rtf") {
|
|
1120
|
+
ext = "rtf";
|
|
1121
|
+
return this.parseRtf(buffer, mime);
|
|
1122
|
+
}
|
|
981
1123
|
if (buffer?.length >= 4 && buffer.readUInt32LE(0) === 67324752) {
|
|
982
1124
|
const kind = {
|
|
983
1125
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
984
1126
|
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "pptx",
|
|
985
1127
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx"
|
|
986
1128
|
}[mime] ?? (ext === "docx" ? "docx" : ext === "pptx" ? "pptx" : ext === "xlsx" ? "xlsx" : void 0);
|
|
987
|
-
if (kind)
|
|
1129
|
+
if (kind) {
|
|
1130
|
+
ext = kind;
|
|
1131
|
+
return this.parseOpenXml(buffer, mime, kind);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
if (mime in mimeToExt) {
|
|
1135
|
+
ext = mimeToExt[mime][0];
|
|
1136
|
+
return this.parsePlainText(buffer, mime, filename);
|
|
988
1137
|
}
|
|
989
|
-
if (mime.startsWith("text/") || [
|
|
990
|
-
"application/json",
|
|
991
|
-
"application/xml",
|
|
992
|
-
"application/javascript"
|
|
993
|
-
].includes(mime) || ext && [
|
|
994
|
-
"txt",
|
|
995
|
-
"md",
|
|
996
|
-
"markdown",
|
|
997
|
-
"csv",
|
|
998
|
-
"json",
|
|
999
|
-
"html",
|
|
1000
|
-
"xml"
|
|
1001
|
-
].includes(ext)) return this.parsePlainText(buffer, mime, filename);
|
|
1002
1138
|
return {
|
|
1003
1139
|
type: "DOCUMENT",
|
|
1004
1140
|
format: ext === "doc" ? "doc" : ext === "xls" ? "xls" : ext === "ppt" ? "ppt" : "bin",
|
|
@@ -1017,11 +1153,11 @@ var DocMetadataExtractor = class {
|
|
|
1017
1153
|
isLinearized: null,
|
|
1018
1154
|
textPreview: null,
|
|
1019
1155
|
createdDate: null,
|
|
1020
|
-
modifiedDate: null
|
|
1156
|
+
modifiedDate: null,
|
|
1157
|
+
metadata: {}
|
|
1021
1158
|
};
|
|
1022
1159
|
}
|
|
1023
1160
|
};
|
|
1024
|
-
|
|
1025
|
-
//#endregion
|
|
1026
1161
|
export { DocMetadataExtractor };
|
|
1162
|
+
|
|
1027
1163
|
//# sourceMappingURL=index.js.map
|
package/dist/extract/client.d.ts
CHANGED
|
@@ -1,29 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side synchronous metadata extractor
|
|
3
|
+
* Works only with buffers - no network operations
|
|
4
|
+
* Provides the same rich metadata extraction as Extract class
|
|
5
|
+
* Ideal for browser environments where you handle fetching separately
|
|
6
|
+
*/
|
|
1
7
|
import { DocMetadataExtractor } from "../docs/index.js";
|
|
2
8
|
import { ImgMetadataExtractor } from "../images/index.js";
|
|
3
|
-
import { ExpandedDocSpecs, ExpandedImgSpecs, ExtractorOptions } from "../types/index.js";
|
|
4
9
|
import "../index.js";
|
|
5
|
-
|
|
6
|
-
//#region src/extract/client.d.ts
|
|
7
|
-
|
|
10
|
+
import { DocSpecs, ExpandedDocSpecs, ExpandedImgSpecs, ExtractorOptions } from "../types/index.js";
|
|
8
11
|
declare class Base {
|
|
9
12
|
constructor(_opts?: ExtractorOptions);
|
|
10
13
|
}
|
|
11
14
|
declare const Unified: {
|
|
12
15
|
new (...args: any[]): {
|
|
13
16
|
img: ImgMetadataExtractor;
|
|
14
|
-
getImageSpecsWorkup(buffer: Buffer
|
|
17
|
+
getImageSpecsWorkup(buffer: Buffer, size?: number): ExpandedImgSpecs;
|
|
15
18
|
};
|
|
16
|
-
sharedImg?: ImgMetadataExtractor
|
|
19
|
+
sharedImg?: ImgMetadataExtractor;
|
|
17
20
|
setSharedImg(ex: ImgMetadataExtractor): void;
|
|
18
21
|
} & {
|
|
19
22
|
new (...args: any[]): {
|
|
20
23
|
docs: DocMetadataExtractor;
|
|
21
|
-
getDocumentSpecsWorkup(buffer: Buffer
|
|
24
|
+
getDocumentSpecsWorkup(buffer: Buffer, contentType?: string): DocSpecs<unknown>;
|
|
22
25
|
};
|
|
23
|
-
sharedDocs?: DocMetadataExtractor
|
|
26
|
+
sharedDocs?: DocMetadataExtractor;
|
|
24
27
|
setSharedDocs(ex: DocMetadataExtractor): void;
|
|
25
28
|
} & typeof Base;
|
|
26
|
-
declare class ExtractClient extends Unified {
|
|
29
|
+
export declare class ExtractClient extends Unified {
|
|
27
30
|
constructor(opts?: ExtractorOptions);
|
|
28
31
|
/**
|
|
29
32
|
* Comprehensive MIME type detection from buffer magic bytes
|
|
@@ -49,6 +52,5 @@ declare class ExtractClient extends Unified {
|
|
|
49
52
|
*/
|
|
50
53
|
extractFromBufferWithType(buffer: Buffer, mimeType: string): ExpandedDocSpecs | ExpandedImgSpecs;
|
|
51
54
|
}
|
|
52
|
-
|
|
53
|
-
export { ExtractClient };
|
|
55
|
+
export {};
|
|
54
56
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/extract/client.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { DocMixin, ImgMixin } from "../mixins/index.js";
|
|
2
|
-
|
|
3
|
-
//#region src/extract/client.ts
|
|
2
|
+
// src/extract/client.ts
|
|
4
3
|
var Base = class {
|
|
5
4
|
constructor(_opts) {}
|
|
6
5
|
};
|
|
@@ -112,7 +111,6 @@ var ExtractClient = class extends Unified {
|
|
|
112
111
|
}
|
|
113
112
|
}
|
|
114
113
|
};
|
|
115
|
-
|
|
116
|
-
//#endregion
|
|
117
114
|
export { ExtractClient };
|
|
115
|
+
|
|
118
116
|
//# sourceMappingURL=client.js.map
|
package/dist/extract/index.d.ts
CHANGED
|
@@ -1,28 +1,26 @@
|
|
|
1
1
|
import { DocMetadataExtractor } from "../docs/index.js";
|
|
2
2
|
import { ImgMetadataExtractor } from "../images/index.js";
|
|
3
|
-
import { ExpandedDocSpecs, ExpandedImgSpecs, ExtractorHardenedOptions, ExtractorOptions } from "../types/index.js";
|
|
4
3
|
import "../index.js";
|
|
5
|
-
|
|
6
|
-
//#region src/extract/index.d.ts
|
|
4
|
+
import { DocSpecs, ExpandedDocSpecs, ExpandedImgSpecs, ExtractorHardenedOptions, ExtractorOptions } from "../types/index.js";
|
|
7
5
|
declare class Base {
|
|
8
6
|
constructor(_opts?: ExtractorOptions);
|
|
9
7
|
}
|
|
10
8
|
declare const Unified: {
|
|
11
9
|
new (...args: any[]): {
|
|
12
10
|
img: ImgMetadataExtractor;
|
|
13
|
-
getImageSpecsWorkup(buffer: Buffer
|
|
11
|
+
getImageSpecsWorkup(buffer: Buffer, size?: number): ExpandedImgSpecs;
|
|
14
12
|
};
|
|
15
|
-
sharedImg?: ImgMetadataExtractor
|
|
13
|
+
sharedImg?: ImgMetadataExtractor;
|
|
16
14
|
setSharedImg(ex: ImgMetadataExtractor): void;
|
|
17
15
|
} & {
|
|
18
16
|
new (...args: any[]): {
|
|
19
17
|
docs: DocMetadataExtractor;
|
|
20
|
-
getDocumentSpecsWorkup(buffer: Buffer
|
|
18
|
+
getDocumentSpecsWorkup(buffer: Buffer, contentType?: string): DocSpecs<unknown>;
|
|
21
19
|
};
|
|
22
|
-
sharedDocs?: DocMetadataExtractor
|
|
20
|
+
sharedDocs?: DocMetadataExtractor;
|
|
23
21
|
setSharedDocs(ex: DocMetadataExtractor): void;
|
|
24
22
|
} & typeof Base;
|
|
25
|
-
declare class Extract extends Unified {
|
|
23
|
+
export declare class Extract extends Unified {
|
|
26
24
|
private readonly badUrls;
|
|
27
25
|
private readonly originFallback?;
|
|
28
26
|
private readonly invalidateCF?;
|
|
@@ -64,6 +62,5 @@ declare class Extract extends Unified {
|
|
|
64
62
|
private sniffJpegDims;
|
|
65
63
|
private sniffWebpDims;
|
|
66
64
|
}
|
|
67
|
-
|
|
68
|
-
export { Extract };
|
|
65
|
+
export {};
|
|
69
66
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/extract/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { DocMixin, ImgMixin } from "../mixins/index.js";
|
|
2
|
-
|
|
3
|
-
//#region src/extract/index.ts
|
|
2
|
+
// src/extract/index.ts
|
|
4
3
|
var Base = class {
|
|
5
4
|
constructor(_opts) {}
|
|
6
5
|
};
|
|
@@ -16,7 +15,7 @@ var Extract = class extends Unified {
|
|
|
16
15
|
super(opts);
|
|
17
16
|
this.originFallback = opts?.originFallback;
|
|
18
17
|
this.invalidateCF = opts?.invalidateCloudFrontKey;
|
|
19
|
-
this.quarantineTtl = opts?.quarantineTtlMs ??
|
|
18
|
+
this.quarantineTtl = opts?.quarantineTtlMs ?? 216e5;
|
|
20
19
|
this.ua = opts?.userAgent ?? "slipstream-extractor/1.0";
|
|
21
20
|
this.debug = !!opts?.debug;
|
|
22
21
|
}
|
|
@@ -279,7 +278,7 @@ var Extract = class extends Unified {
|
|
|
279
278
|
if (!supportsRange) try {
|
|
280
279
|
const head = await this.head(url, Math.min(timeout, 2500));
|
|
281
280
|
const ce = head.headers.get("content-encoding") ?? "";
|
|
282
|
-
if (/gzip|br|deflate/i.test(ce)) this.markQuarantined(url,
|
|
281
|
+
if (/gzip|br|deflate/i.test(ce)) this.markQuarantined(url, 18e5);
|
|
283
282
|
contentLength = parseInt(head.headers.get("content-length") ?? "0");
|
|
284
283
|
this.dlog("fetchMinimalBuffer:head", {
|
|
285
284
|
url,
|
|
@@ -341,7 +340,7 @@ var Extract = class extends Unified {
|
|
|
341
340
|
};
|
|
342
341
|
}
|
|
343
342
|
}
|
|
344
|
-
if (contentLength && contentLength <
|
|
343
|
+
if (contentLength && contentLength < 1048576) {
|
|
345
344
|
const ab = await (await this.fetchWithDeadline(url, { headers: {
|
|
346
345
|
"Accept-Encoding": "identity",
|
|
347
346
|
"User-Agent": this.ua
|
|
@@ -459,7 +458,7 @@ var Extract = class extends Unified {
|
|
|
459
458
|
format = "heic";
|
|
460
459
|
break;
|
|
461
460
|
case "image/tiff": {
|
|
462
|
-
const truncated = buffer.subarray(0, Math.min(buffer.length,
|
|
461
|
+
const truncated = buffer.subarray(0, Math.min(buffer.length, 24576));
|
|
463
462
|
dims = this.sniffTiff(buffer, truncated) ?? null;
|
|
464
463
|
format = "tiff";
|
|
465
464
|
break;
|
|
@@ -616,7 +615,6 @@ var Extract = class extends Unified {
|
|
|
616
615
|
return null;
|
|
617
616
|
}
|
|
618
617
|
};
|
|
619
|
-
|
|
620
|
-
//#endregion
|
|
621
618
|
export { Extract };
|
|
619
|
+
|
|
622
620
|
//# sourceMappingURL=index.js.map
|
package/dist/fs/index.d.ts
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
import { ExpandedDocSpecs, ExpandedImgSpecs } from "../types/index.js";
|
|
2
|
-
import { FsFetch } from "../fs-fetch/index.js";
|
|
3
2
|
import "../index.js";
|
|
4
3
|
import { Readable } from "node:stream";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
declare class Fs extends FsFetch {
|
|
4
|
+
import { FsFetch } from "../fs-fetch/index.js";
|
|
5
|
+
export default class Fs extends FsFetch {
|
|
8
6
|
cwd: string;
|
|
9
7
|
constructor(cwd: string);
|
|
10
|
-
getSpecs(filePath: string, size?: number): Promise<ExpandedDocSpecs | ExpandedImgSpecs>;
|
|
11
|
-
getSpecsFlexi(target: Buffer | string, size?: number): Promise<ExpandedDocSpecs | ExpandedImgSpecs>;
|
|
12
|
-
getSpecsTmp(filename: string, size?: number): Promise<ExpandedDocSpecs | ExpandedImgSpecs>;
|
|
8
|
+
getSpecs(filePath: string, size?: number): Promise<ExpandedDocSpecs<unknown> | ExpandedImgSpecs>;
|
|
9
|
+
getSpecsFlexi(target: Buffer | string, size?: number): Promise<ExpandedDocSpecs<unknown> | ExpandedImgSpecs>;
|
|
10
|
+
getSpecsTmp(filename: string, size?: number): Promise<ExpandedDocSpecs<unknown> | ExpandedImgSpecs>;
|
|
13
11
|
/**
|
|
14
12
|
* Extract image metadata using streaming (only reads ~4KB)
|
|
15
13
|
* Much more memory efficient for large images
|
|
16
14
|
*/
|
|
17
|
-
getSpecsStream(filePath: string, size?: number): Promise<ExpandedDocSpecs | ExpandedImgSpecs>;
|
|
18
|
-
getSpecsStreamTmp(filename: string): Promise<ExpandedDocSpecs | ExpandedImgSpecs>;
|
|
15
|
+
getSpecsStream(filePath: string, size?: number): Promise<ExpandedDocSpecs<unknown> | ExpandedImgSpecs>;
|
|
16
|
+
getSpecsStreamTmp(filename: string): Promise<ExpandedDocSpecs<unknown> | ExpandedImgSpecs>;
|
|
19
17
|
protected readBytes(stream: Readable, size?: number): Promise<Buffer>;
|
|
20
18
|
}
|
|
21
|
-
//#endregion
|
|
22
|
-
export { Fs as default };
|
|
23
19
|
//# sourceMappingURL=index.d.ts.map
|