@tangle-network/agent-app 0.45.20 → 0.45.22
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/DesignCanvas-ZG7DJS7C.js +10 -0
- package/dist/DesignCanvasEditor-SLFMHOII.js +13 -0
- package/dist/app-auth/index.js +2 -2
- package/dist/assistant/index.d.ts +1 -1
- package/dist/assistant/index.js +5 -5
- package/dist/{attachment-validation-CsmUzuI3.d.ts → attachment-validation-BfJDBU1g.d.ts} +41 -5
- package/dist/{auth-DJjJBtYw.d.ts → auth-BYtiazA4.d.ts} +1 -1
- package/dist/chat-react/index.js +2 -2
- package/dist/chat-routes/index.d.ts +8 -4
- package/dist/chat-routes/index.js +22 -5
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/{chunk-KPRBFNGT.js → chunk-3IHYJIDJ.js} +3 -3
- package/dist/{chunk-MYS7DHMM.js → chunk-4QVG4WJQ.js} +2 -2
- package/dist/{chunk-RMHZKMQ4.js → chunk-73CQJOIU.js} +5 -5
- package/dist/{chunk-PAB253MA.js → chunk-7V2I3JGZ.js} +19 -3
- package/dist/chunk-7V2I3JGZ.js.map +1 -0
- package/dist/chunk-CCVG2TL6.js +327 -0
- package/dist/chunk-CCVG2TL6.js.map +1 -0
- package/dist/{chunk-U4BXAZCN.js → chunk-DRB77FYL.js} +2 -2
- package/dist/{chunk-6MUJROBT.js → chunk-E4DHYENH.js} +2 -2
- package/dist/chunk-F7VH4G5C.js +283 -0
- package/dist/chunk-F7VH4G5C.js.map +1 -0
- package/dist/chunk-I24YIKHO.js +183 -0
- package/dist/chunk-I24YIKHO.js.map +1 -0
- package/dist/{chunk-AH3IOSWF.js → chunk-NZVBJM73.js} +1432 -187
- package/dist/chunk-NZVBJM73.js.map +1 -0
- package/dist/{chunk-OO4DROKM.js → chunk-OASVAPRL.js} +3 -3
- package/dist/{chunk-ZSGTG6PK.js → chunk-P5PIAQVS.js} +2 -2
- package/dist/chunk-PC2WYTK7.js +405 -0
- package/dist/chunk-PC2WYTK7.js.map +1 -0
- package/dist/{chunk-YEFFHORB.js → chunk-TX6S7XXU.js} +8 -2
- package/dist/chunk-TX6S7XXU.js.map +1 -0
- package/dist/chunk-UGWQLQDS.js +35 -0
- package/dist/chunk-UGWQLQDS.js.map +1 -0
- package/dist/{chunk-O22STZ3P.js → chunk-X5DQ5QOP.js} +2 -2
- package/dist/{chunk-PCJHEZA7.js → chunk-YABHE2QY.js} +2 -2
- package/dist/design-canvas/index.d.ts +3 -3
- package/dist/design-canvas/index.js +2 -2
- package/dist/design-canvas-react/engine.js +4 -4
- package/dist/design-canvas-react/index.js +7 -7
- package/dist/design-canvas-react/lazy.js +1 -1
- package/dist/documents/index.d.ts +233 -0
- package/dist/documents/index.js +495 -0
- package/dist/documents/index.js.map +1 -0
- package/dist/documents/pdf-inspector.d.ts +60 -0
- package/dist/documents/pdf-inspector.js +100 -0
- package/dist/documents/pdf-inspector.js.map +1 -0
- package/dist/eval/index.d.ts +1 -1
- package/dist/fold-DACU7gtf.d.ts +320 -0
- package/dist/{mcp-BiKqnsJV.d.ts → mcp-xxoZZfor.d.ts} +2 -2
- package/dist/openui/index.d.ts +208 -0
- package/dist/openui/index.js +350 -0
- package/dist/openui/index.js.map +1 -0
- package/dist/openui-react/index.d.ts +119 -0
- package/dist/openui-react/index.js +149 -0
- package/dist/openui-react/index.js.map +1 -0
- package/dist/platform/index.js +2 -2
- package/dist/preset-cloudflare/index.d.ts +2 -2
- package/dist/rate-limit-BOVmJRXZ.d.ts +35 -0
- package/dist/record/drizzle.d.ts +682 -0
- package/dist/record/drizzle.js +579 -0
- package/dist/record/drizzle.js.map +1 -0
- package/dist/record/index.d.ts +16 -0
- package/dist/record/index.js +35 -0
- package/dist/record/index.js.map +1 -0
- package/dist/runtime/index.d.ts +3 -3
- package/dist/sandbox/index.d.ts +13 -4
- package/dist/sandbox/index.js +6 -3
- package/dist/sequences/index.d.ts +3 -3
- package/dist/sequences/index.js +1 -1
- package/dist/session-shell/index.d.ts +173 -1
- package/dist/session-shell/index.js +13 -1
- package/dist/teams/index.js +9 -9
- package/dist/teams/invitations-api.js +3 -3
- package/dist/tools/index.d.ts +5 -5
- package/dist/tools/index.js +3 -2
- package/dist/{types-DbU-oO5h.d.ts → types-B25Mib1r.d.ts} +11 -0
- package/dist/types-DrBidf3O.d.ts +123 -0
- package/dist/values-C7RidJuI.d.ts +156 -0
- package/dist/web/index.d.ts +197 -31
- package/dist/web/index.js +15 -3
- package/dist/web-react/async/index.d.ts +309 -0
- package/dist/web-react/async/index.js +327 -0
- package/dist/web-react/async/index.js.map +1 -0
- package/dist/web-react/index.d.ts +338 -2
- package/dist/web-react/index.js +52 -6
- package/dist/work-product/index.d.ts +1 -1
- package/dist/work-product/index.js +2 -1
- package/dist/work-product/index.js.map +1 -1
- package/dist/workspace-react/index.js +1 -1
- package/package.json +43 -3
- package/dist/DesignCanvas-NEIW3AER.js +0 -10
- package/dist/DesignCanvasEditor-QD33OI2W.js +0 -13
- package/dist/chunk-AH3IOSWF.js.map +0 -1
- package/dist/chunk-GINKOZFD.js +0 -214
- package/dist/chunk-GINKOZFD.js.map +0 -1
- package/dist/chunk-J25RST7G.js +0 -126
- package/dist/chunk-J25RST7G.js.map +0 -1
- package/dist/chunk-PAB253MA.js.map +0 -1
- package/dist/chunk-WBHPN5DY.js +0 -158
- package/dist/chunk-WBHPN5DY.js.map +0 -1
- package/dist/chunk-YEFFHORB.js.map +0 -1
- /package/dist/{DesignCanvas-NEIW3AER.js.map → DesignCanvas-ZG7DJS7C.js.map} +0 -0
- /package/dist/{DesignCanvasEditor-QD33OI2W.js.map → DesignCanvasEditor-SLFMHOII.js.map} +0 -0
- /package/dist/{chunk-KPRBFNGT.js.map → chunk-3IHYJIDJ.js.map} +0 -0
- /package/dist/{chunk-MYS7DHMM.js.map → chunk-4QVG4WJQ.js.map} +0 -0
- /package/dist/{chunk-RMHZKMQ4.js.map → chunk-73CQJOIU.js.map} +0 -0
- /package/dist/{chunk-U4BXAZCN.js.map → chunk-DRB77FYL.js.map} +0 -0
- /package/dist/{chunk-6MUJROBT.js.map → chunk-E4DHYENH.js.map} +0 -0
- /package/dist/{chunk-OO4DROKM.js.map → chunk-OASVAPRL.js.map} +0 -0
- /package/dist/{chunk-ZSGTG6PK.js.map → chunk-P5PIAQVS.js.map} +0 -0
- /package/dist/{chunk-O22STZ3P.js.map → chunk-X5DQ5QOP.js.map} +0 -0
- /package/dist/{chunk-PCJHEZA7.js.map → chunk-YABHE2QY.js.map} +0 -0
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
// src/documents/zip.ts
|
|
2
|
+
var END_OF_CENTRAL_DIRECTORY = 101010256;
|
|
3
|
+
var CENTRAL_FILE_HEADER = 33639248;
|
|
4
|
+
var LOCAL_FILE_HEADER = 67324752;
|
|
5
|
+
var ZIP64_SENTINEL_32 = 4294967295;
|
|
6
|
+
var ZIP64_SENTINEL_16 = 65535;
|
|
7
|
+
var MAX_COMMENT_LENGTH = 65535;
|
|
8
|
+
var END_OF_CENTRAL_DIRECTORY_SIZE = 22;
|
|
9
|
+
var STORED = 0;
|
|
10
|
+
var DEFLATED = 8;
|
|
11
|
+
function view(bytes) {
|
|
12
|
+
return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
13
|
+
}
|
|
14
|
+
function findEndOfCentralDirectory(bytes) {
|
|
15
|
+
const dv = view(bytes);
|
|
16
|
+
const earliest = Math.max(0, bytes.byteLength - END_OF_CENTRAL_DIRECTORY_SIZE - MAX_COMMENT_LENGTH);
|
|
17
|
+
for (let offset = bytes.byteLength - END_OF_CENTRAL_DIRECTORY_SIZE; offset >= earliest; offset--) {
|
|
18
|
+
if (dv.getUint32(offset, true) !== END_OF_CENTRAL_DIRECTORY) continue;
|
|
19
|
+
if (offset + END_OF_CENTRAL_DIRECTORY_SIZE + dv.getUint16(offset + 20, true) === bytes.byteLength) return offset;
|
|
20
|
+
}
|
|
21
|
+
return -1;
|
|
22
|
+
}
|
|
23
|
+
function readZipDirectory(bytes) {
|
|
24
|
+
if (bytes.byteLength < END_OF_CENTRAL_DIRECTORY_SIZE) {
|
|
25
|
+
return { succeeded: false, error: `not a zip archive \u2014 ${bytes.byteLength} bytes is shorter than an empty archive` };
|
|
26
|
+
}
|
|
27
|
+
const eocd = findEndOfCentralDirectory(bytes);
|
|
28
|
+
if (eocd < 0) {
|
|
29
|
+
return { succeeded: false, error: "not a zip archive \u2014 no end-of-central-directory record found" };
|
|
30
|
+
}
|
|
31
|
+
const dv = view(bytes);
|
|
32
|
+
const entryCount = dv.getUint16(eocd + 10, true);
|
|
33
|
+
const directoryOffset = dv.getUint32(eocd + 16, true);
|
|
34
|
+
if (entryCount === ZIP64_SENTINEL_16 || directoryOffset === ZIP64_SENTINEL_32) {
|
|
35
|
+
return { succeeded: false, error: "zip64 archives are not supported" };
|
|
36
|
+
}
|
|
37
|
+
const decoder = new TextDecoder("utf-8", { fatal: false });
|
|
38
|
+
const entries = [];
|
|
39
|
+
let cursor = directoryOffset;
|
|
40
|
+
for (let i = 0; i < entryCount; i++) {
|
|
41
|
+
if (cursor + 46 > bytes.byteLength || dv.getUint32(cursor, true) !== CENTRAL_FILE_HEADER) {
|
|
42
|
+
return { succeeded: false, error: `central directory is truncated at entry ${i + 1} of ${entryCount}` };
|
|
43
|
+
}
|
|
44
|
+
const flags = dv.getUint16(cursor + 8, true);
|
|
45
|
+
const nameLength = dv.getUint16(cursor + 28, true);
|
|
46
|
+
const extraLength = dv.getUint16(cursor + 30, true);
|
|
47
|
+
const commentLength = dv.getUint16(cursor + 32, true);
|
|
48
|
+
entries.push({
|
|
49
|
+
name: decoder.decode(bytes.subarray(cursor + 46, cursor + 46 + nameLength)),
|
|
50
|
+
compressionMethod: dv.getUint16(cursor + 10, true),
|
|
51
|
+
compressedSize: dv.getUint32(cursor + 20, true),
|
|
52
|
+
uncompressedSize: dv.getUint32(cursor + 24, true),
|
|
53
|
+
localHeaderOffset: dv.getUint32(cursor + 42, true),
|
|
54
|
+
encrypted: (flags & 1) !== 0
|
|
55
|
+
});
|
|
56
|
+
cursor += 46 + nameLength + extraLength + commentLength;
|
|
57
|
+
}
|
|
58
|
+
return { succeeded: true, value: entries };
|
|
59
|
+
}
|
|
60
|
+
var DEFAULT_MAX_ZIP_ENTRY_BYTES = 32 * 1024 * 1024;
|
|
61
|
+
async function inflateRaw(payload, limit) {
|
|
62
|
+
const body = new Response(payload).body;
|
|
63
|
+
if (!body) throw new Error("payload produced no readable stream");
|
|
64
|
+
const reader = body.pipeThrough(new DecompressionStream("deflate-raw")).getReader();
|
|
65
|
+
const chunks = [];
|
|
66
|
+
let total = 0;
|
|
67
|
+
for (; ; ) {
|
|
68
|
+
const { done, value } = await reader.read();
|
|
69
|
+
if (done) break;
|
|
70
|
+
if (value === void 0) continue;
|
|
71
|
+
total += value.byteLength;
|
|
72
|
+
if (total > limit) {
|
|
73
|
+
await reader.cancel();
|
|
74
|
+
return { kind: "limit-exceeded", bytes: total };
|
|
75
|
+
}
|
|
76
|
+
chunks.push(value);
|
|
77
|
+
}
|
|
78
|
+
const out = new Uint8Array(total);
|
|
79
|
+
let offset = 0;
|
|
80
|
+
for (const chunk of chunks) {
|
|
81
|
+
out.set(chunk, offset);
|
|
82
|
+
offset += chunk.byteLength;
|
|
83
|
+
}
|
|
84
|
+
return { kind: "ok", value: out };
|
|
85
|
+
}
|
|
86
|
+
async function readZipEntry(bytes, entry, options = {}) {
|
|
87
|
+
if (entry.encrypted) {
|
|
88
|
+
return { succeeded: false, error: `'${entry.name}' is encrypted \u2014 password-protected archives are not supported` };
|
|
89
|
+
}
|
|
90
|
+
const limit = options.maxUncompressedBytes ?? DEFAULT_MAX_ZIP_ENTRY_BYTES;
|
|
91
|
+
if (entry.uncompressedSize > limit) {
|
|
92
|
+
return {
|
|
93
|
+
succeeded: false,
|
|
94
|
+
error: `'${entry.name}' declares ${entry.uncompressedSize} uncompressed bytes, over the ${limit}-byte per-entry limit`
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
const dv = view(bytes);
|
|
98
|
+
if (entry.localHeaderOffset + 30 > bytes.byteLength || dv.getUint32(entry.localHeaderOffset, true) !== LOCAL_FILE_HEADER) {
|
|
99
|
+
return { succeeded: false, error: `'${entry.name}' has no local file header at offset ${entry.localHeaderOffset}` };
|
|
100
|
+
}
|
|
101
|
+
const nameLength = dv.getUint16(entry.localHeaderOffset + 26, true);
|
|
102
|
+
const extraLength = dv.getUint16(entry.localHeaderOffset + 28, true);
|
|
103
|
+
const start = entry.localHeaderOffset + 30 + nameLength + extraLength;
|
|
104
|
+
const end = start + entry.compressedSize;
|
|
105
|
+
if (end > bytes.byteLength) {
|
|
106
|
+
return { succeeded: false, error: `'${entry.name}' payload runs past the end of the archive` };
|
|
107
|
+
}
|
|
108
|
+
const payload = bytes.subarray(start, end);
|
|
109
|
+
if (entry.compressionMethod === STORED) return { succeeded: true, value: payload };
|
|
110
|
+
if (entry.compressionMethod !== DEFLATED) {
|
|
111
|
+
return { succeeded: false, error: `'${entry.name}' uses unsupported compression method ${entry.compressionMethod}` };
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
const inflated = await inflateRaw(payload, limit);
|
|
115
|
+
if (inflated.kind === "limit-exceeded") {
|
|
116
|
+
return {
|
|
117
|
+
succeeded: false,
|
|
118
|
+
error: `'${entry.name}' expands past the ${limit}-byte per-entry limit (declared ${entry.uncompressedSize})`
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (inflated.value.byteLength !== entry.uncompressedSize) {
|
|
122
|
+
return {
|
|
123
|
+
succeeded: false,
|
|
124
|
+
error: `'${entry.name}' inflated to ${inflated.value.byteLength} bytes but the directory declares ${entry.uncompressedSize}`
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
return { succeeded: true, value: inflated.value };
|
|
128
|
+
} catch (error2) {
|
|
129
|
+
return { succeeded: false, error: `'${entry.name}' failed to inflate \u2014 ${error2 instanceof Error ? error2.message : String(error2)}` };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/documents/docx.ts
|
|
134
|
+
var RELATIONSHIPS_PART = "_rels/.rels";
|
|
135
|
+
var OFFICE_DOCUMENT_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
|
|
136
|
+
var NAMED_ENTITIES = {
|
|
137
|
+
amp: "&",
|
|
138
|
+
lt: "<",
|
|
139
|
+
gt: ">",
|
|
140
|
+
quot: '"',
|
|
141
|
+
apos: "'"
|
|
142
|
+
};
|
|
143
|
+
function decodeXmlEntities(input) {
|
|
144
|
+
if (!input.includes("&")) return input;
|
|
145
|
+
return input.replace(/&(#x[0-9a-fA-F]+|#\d+|[a-zA-Z]+);/g, (match, reference) => {
|
|
146
|
+
if (reference.startsWith("#x") || reference.startsWith("#X")) {
|
|
147
|
+
const code = Number.parseInt(reference.slice(2), 16);
|
|
148
|
+
return Number.isFinite(code) ? String.fromCodePoint(code) : match;
|
|
149
|
+
}
|
|
150
|
+
if (reference.startsWith("#")) {
|
|
151
|
+
const code = Number.parseInt(reference.slice(1), 10);
|
|
152
|
+
return Number.isFinite(code) ? String.fromCodePoint(code) : match;
|
|
153
|
+
}
|
|
154
|
+
return NAMED_ENTITIES[reference] ?? match;
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
function parseTag(body) {
|
|
158
|
+
const closing = body.startsWith("/");
|
|
159
|
+
const selfClosing = body.endsWith("/");
|
|
160
|
+
let inner = body.slice(closing ? 1 : 0, selfClosing ? body.length - 1 : body.length);
|
|
161
|
+
const space = inner.search(/[\s/]/);
|
|
162
|
+
if (space >= 0) inner = inner.slice(0, space);
|
|
163
|
+
const colon = inner.indexOf(":");
|
|
164
|
+
return { name: colon >= 0 ? inner.slice(colon + 1) : inner, closing, selfClosing };
|
|
165
|
+
}
|
|
166
|
+
var DROPPED_ELEMENTS = /* @__PURE__ */ new Set(["instrText", "delInstrText", "delText"]);
|
|
167
|
+
function wordprocessingXmlToText(xml) {
|
|
168
|
+
const out = [];
|
|
169
|
+
let paragraphCount = 0;
|
|
170
|
+
let textDepth = 0;
|
|
171
|
+
let dropDepth = 0;
|
|
172
|
+
let cursor = 0;
|
|
173
|
+
while (cursor < xml.length) {
|
|
174
|
+
const open = xml.indexOf("<", cursor);
|
|
175
|
+
if (open < 0) break;
|
|
176
|
+
if (open > cursor && textDepth > 0 && dropDepth === 0) {
|
|
177
|
+
out.push(decodeXmlEntities(xml.slice(cursor, open)));
|
|
178
|
+
}
|
|
179
|
+
if (xml.startsWith("<!--", open)) {
|
|
180
|
+
const close2 = xml.indexOf("-->", open + 4);
|
|
181
|
+
cursor = close2 < 0 ? xml.length : close2 + 3;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
if (xml.startsWith("<![CDATA[", open)) {
|
|
185
|
+
const close2 = xml.indexOf("]]>", open + 9);
|
|
186
|
+
const end = close2 < 0 ? xml.length : close2;
|
|
187
|
+
if (textDepth > 0 && dropDepth === 0) out.push(xml.slice(open + 9, end));
|
|
188
|
+
cursor = close2 < 0 ? xml.length : close2 + 3;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const close = xml.indexOf(">", open + 1);
|
|
192
|
+
if (close < 0) break;
|
|
193
|
+
const body = xml.slice(open + 1, close);
|
|
194
|
+
cursor = close + 1;
|
|
195
|
+
if (body.startsWith("?") || body.startsWith("!")) continue;
|
|
196
|
+
const tag = parseTag(body);
|
|
197
|
+
if (DROPPED_ELEMENTS.has(tag.name)) {
|
|
198
|
+
if (tag.selfClosing) continue;
|
|
199
|
+
dropDepth += tag.closing ? -1 : 1;
|
|
200
|
+
if (dropDepth < 0) dropDepth = 0;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (dropDepth > 0) continue;
|
|
204
|
+
if (tag.name === "t") {
|
|
205
|
+
if (tag.selfClosing) continue;
|
|
206
|
+
textDepth += tag.closing ? -1 : 1;
|
|
207
|
+
if (textDepth < 0) textDepth = 0;
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (tag.name === "tab" && !tag.closing) out.push(" ");
|
|
211
|
+
else if ((tag.name === "br" || tag.name === "cr") && !tag.closing) out.push("\n");
|
|
212
|
+
else if (tag.name === "p" && tag.closing) {
|
|
213
|
+
out.push("\n");
|
|
214
|
+
paragraphCount++;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return { text: out.join("").replace(/\r\n?/g, "\n"), paragraphCount };
|
|
218
|
+
}
|
|
219
|
+
function relationshipTarget(relsXml) {
|
|
220
|
+
for (const match of relsXml.matchAll(/<[^>]*Relationship\b[^>]*>/g)) {
|
|
221
|
+
const element = match[0];
|
|
222
|
+
const type = /\bType\s*=\s*"([^"]*)"/.exec(element)?.[1];
|
|
223
|
+
if (type !== OFFICE_DOCUMENT_RELATIONSHIP) continue;
|
|
224
|
+
const target = /\bTarget\s*=\s*"([^"]*)"/.exec(element)?.[1];
|
|
225
|
+
if (target) return decodeXmlEntities(target).replace(/^\/+/, "");
|
|
226
|
+
}
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
async function extractDocxText(bytes, mediaType, zip = {}) {
|
|
230
|
+
const fail = (code, message) => ({
|
|
231
|
+
succeeded: false,
|
|
232
|
+
error: { code, stage: "extract", message, mediaType }
|
|
233
|
+
});
|
|
234
|
+
const directory = readZipDirectory(bytes);
|
|
235
|
+
if (!directory.succeeded) return fail("malformed-archive", `Not a readable Office package \u2014 ${directory.error}.`);
|
|
236
|
+
const relsEntry = directory.value.find((entry) => entry.name === RELATIONSHIPS_PART);
|
|
237
|
+
if (!relsEntry) {
|
|
238
|
+
return fail(
|
|
239
|
+
"malformed-archive",
|
|
240
|
+
`Office package has no '${RELATIONSHIPS_PART}' part, so its main document cannot be located. Parts found: ${directory.value.map((e) => e.name).slice(0, 12).join(", ") || "none"}.`
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
const relsBytes = await readZipEntry(bytes, relsEntry, zip);
|
|
244
|
+
if (!relsBytes.succeeded) return fail("malformed-archive", `Could not read '${RELATIONSHIPS_PART}' \u2014 ${relsBytes.error}.`);
|
|
245
|
+
const decoder = new TextDecoder("utf-8", { fatal: false });
|
|
246
|
+
const partName = relationshipTarget(decoder.decode(relsBytes.value));
|
|
247
|
+
if (partName === null) {
|
|
248
|
+
return fail("malformed-archive", `Office package declares no officeDocument relationship in '${RELATIONSHIPS_PART}'.`);
|
|
249
|
+
}
|
|
250
|
+
const documentEntry = directory.value.find((entry) => entry.name === partName);
|
|
251
|
+
if (!documentEntry) {
|
|
252
|
+
return fail("malformed-archive", `Office package points at '${partName}' as its main document, but no such part exists in the archive.`);
|
|
253
|
+
}
|
|
254
|
+
const documentBytes = await readZipEntry(bytes, documentEntry, zip);
|
|
255
|
+
if (!documentBytes.succeeded) return fail("malformed-archive", `Could not read '${partName}' \u2014 ${documentBytes.error}.`);
|
|
256
|
+
const strict = new TextDecoder("utf-8", { fatal: true });
|
|
257
|
+
let xml;
|
|
258
|
+
try {
|
|
259
|
+
xml = strict.decode(documentBytes.value);
|
|
260
|
+
} catch (error2) {
|
|
261
|
+
return fail("extraction-failed", `'${partName}' is not valid UTF-8 \u2014 ${error2 instanceof Error ? error2.message : String(error2)}.`);
|
|
262
|
+
}
|
|
263
|
+
const walked = wordprocessingXmlToText(xml);
|
|
264
|
+
return {
|
|
265
|
+
succeeded: true,
|
|
266
|
+
value: { text: walked.text, detail: { part: partName, paragraphCount: walked.paragraphCount } }
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// src/documents/media-type.ts
|
|
271
|
+
var DOCX_MEDIA_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
272
|
+
var DOCM_MEDIA_TYPE = "application/vnd.ms-word.document.macroEnabled.12";
|
|
273
|
+
var PDF_MEDIA_TYPE = "application/pdf";
|
|
274
|
+
var LEGACY_DOC_MEDIA_TYPES = /* @__PURE__ */ new Set(["application/msword", "application/vnd.ms-word"]);
|
|
275
|
+
var DOCX_MEDIA_TYPES = /* @__PURE__ */ new Map([
|
|
276
|
+
[DOCX_MEDIA_TYPE.toLowerCase(), DOCX_MEDIA_TYPE],
|
|
277
|
+
[DOCM_MEDIA_TYPE.toLowerCase(), DOCM_MEDIA_TYPE]
|
|
278
|
+
]);
|
|
279
|
+
var EXTENSION_MEDIA_TYPES = {
|
|
280
|
+
pdf: PDF_MEDIA_TYPE,
|
|
281
|
+
docx: DOCX_MEDIA_TYPE,
|
|
282
|
+
docm: DOCM_MEDIA_TYPE,
|
|
283
|
+
doc: "application/msword",
|
|
284
|
+
txt: "text/plain",
|
|
285
|
+
text: "text/plain",
|
|
286
|
+
md: "text/markdown",
|
|
287
|
+
markdown: "text/markdown",
|
|
288
|
+
csv: "text/csv"
|
|
289
|
+
};
|
|
290
|
+
var UNDECLARED = /* @__PURE__ */ new Set(["", "application/octet-stream", "binary/octet-stream"]);
|
|
291
|
+
function normalizeMediaType(mediaType) {
|
|
292
|
+
const base = mediaType.split(";", 1)[0] ?? "";
|
|
293
|
+
return base.trim().toLowerCase();
|
|
294
|
+
}
|
|
295
|
+
function extensionOf(filename) {
|
|
296
|
+
const dot = filename.lastIndexOf(".");
|
|
297
|
+
if (dot < 0 || dot === filename.length - 1) return "";
|
|
298
|
+
return filename.slice(dot + 1).toLowerCase();
|
|
299
|
+
}
|
|
300
|
+
function resolveMediaType(input) {
|
|
301
|
+
const declared = normalizeMediaType(input.mediaType ?? "");
|
|
302
|
+
const fromExtension = input.filename ? EXTENSION_MEDIA_TYPES[extensionOf(input.filename)] : void 0;
|
|
303
|
+
const effective = (UNDECLARED.has(declared) ? fromExtension ?? declared : declared).toLowerCase();
|
|
304
|
+
const fail = (message) => ({
|
|
305
|
+
succeeded: false,
|
|
306
|
+
error: { code: "unsupported-media-type", stage: "media-type", message, mediaType: declared || (fromExtension ?? "") }
|
|
307
|
+
});
|
|
308
|
+
if (effective === PDF_MEDIA_TYPE) return { succeeded: true, value: { format: "pdf", mediaType: PDF_MEDIA_TYPE } };
|
|
309
|
+
const docx = DOCX_MEDIA_TYPES.get(effective);
|
|
310
|
+
if (docx) return { succeeded: true, value: { format: "docx", mediaType: docx } };
|
|
311
|
+
if (LEGACY_DOC_MEDIA_TYPES.has(effective)) {
|
|
312
|
+
return fail(
|
|
313
|
+
`Legacy binary Word documents (${effective}) are not supported \u2014 they are OLE2 compound files, not OOXML packages. Save the file as .docx and upload it again.`
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
if (effective.startsWith("text/")) return { succeeded: true, value: { format: "text", mediaType: effective } };
|
|
317
|
+
if (effective === "") {
|
|
318
|
+
return fail(
|
|
319
|
+
`Cannot determine the document type: no media type was declared${input.filename ? ` and '${input.filename}' has no recognized extension` : " and no filename was supplied"}. Supported: ${PDF_MEDIA_TYPE}, ${DOCX_MEDIA_TYPE}, and any text/* type.`
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
return fail(
|
|
323
|
+
`Unsupported media type '${effective}'. Supported: ${PDF_MEDIA_TYPE}, ${DOCX_MEDIA_TYPE}, ${DOCM_MEDIA_TYPE}, and any text/* type.`
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// src/documents/extract.ts
|
|
328
|
+
var DEFAULT_MAX_DOCUMENT_BYTES = 25 * 1024 * 1024;
|
|
329
|
+
function error(code, stage, message, mediaType, classification) {
|
|
330
|
+
return { succeeded: false, error: { code, stage, message, mediaType, ...classification ? { classification } : {} } };
|
|
331
|
+
}
|
|
332
|
+
function describe(filename) {
|
|
333
|
+
return filename ? `'${filename}'` : "the document";
|
|
334
|
+
}
|
|
335
|
+
function pageList(pages) {
|
|
336
|
+
return pages.length > 8 ? `${pages.slice(0, 8).join(", ")}, \u2026 (${pages.length} total)` : pages.join(", ");
|
|
337
|
+
}
|
|
338
|
+
function classifyPdfDocument(bytes, pdf) {
|
|
339
|
+
return pdf.classify(bytes);
|
|
340
|
+
}
|
|
341
|
+
async function extractPdf(bytes, options, mediaType) {
|
|
342
|
+
const pdf = options.pdf;
|
|
343
|
+
if (!pdf) {
|
|
344
|
+
return error(
|
|
345
|
+
"pdf-engine-unavailable",
|
|
346
|
+
"classify",
|
|
347
|
+
`PDF extraction needs a PDF engine, and none was configured. Pass \`pdf\` \u2014 see @tangle-network/agent-app/documents/pdf-inspector for the wasm-backed engine and how to deliver its .wasm asset.`,
|
|
348
|
+
mediaType
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
const classified = pdf.classify(bytes);
|
|
352
|
+
if (!classified.succeeded) return classified;
|
|
353
|
+
const classification = classified.value;
|
|
354
|
+
if (classification.needsOcr) {
|
|
355
|
+
const label = classification.kind === "image-based" ? "image-based" : "a scan";
|
|
356
|
+
return error(
|
|
357
|
+
"pdf-needs-ocr",
|
|
358
|
+
"classify",
|
|
359
|
+
`${describe(options.filename)} is ${label} with no text layer on any of its ${classification.pageCount} page${classification.pageCount === 1 ? "" : "s"} \u2014 OCR is required before its text can be read.`,
|
|
360
|
+
mediaType,
|
|
361
|
+
classification
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
const warnings = [];
|
|
365
|
+
const requested = options.preferredPdfFormat ?? "text";
|
|
366
|
+
const textFormat = classification.partiallyScanned ? "markdown" : requested;
|
|
367
|
+
if (classification.partiallyScanned) {
|
|
368
|
+
const one = classification.pagesNeedingOcr.length === 1;
|
|
369
|
+
warnings.push(
|
|
370
|
+
`Page${one ? "" : "s"} ${pageList(classification.pagesNeedingOcr)} of ${classification.pageCount} ${one ? "has" : "have"} no text layer and need${one ? "s" : ""} OCR; the text below comes from the remaining pages.`
|
|
371
|
+
);
|
|
372
|
+
if (requested === "text") {
|
|
373
|
+
warnings.push("Text was extracted as markdown because the plain-text engine cannot read a partially scanned PDF.");
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
if (classification.hasEncodingIssues) {
|
|
377
|
+
warnings.push("The PDF has character-encoding problems; extracted text may contain garbled characters.");
|
|
378
|
+
}
|
|
379
|
+
const extracted = pdf.extract(bytes, textFormat);
|
|
380
|
+
if (!extracted.succeeded) return extracted;
|
|
381
|
+
return {
|
|
382
|
+
succeeded: true,
|
|
383
|
+
value: {
|
|
384
|
+
text: extracted.value,
|
|
385
|
+
detail: { classification, textFormat, partial: classification.partiallyScanned },
|
|
386
|
+
warnings
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
async function extractDocument(input, options = {}) {
|
|
391
|
+
const bytes = input instanceof Uint8Array ? input : new Uint8Array(input);
|
|
392
|
+
const declared = options.mediaType ?? "";
|
|
393
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_DOCUMENT_BYTES;
|
|
394
|
+
if (bytes.byteLength > maxBytes) {
|
|
395
|
+
return error(
|
|
396
|
+
"too-large",
|
|
397
|
+
"size-check",
|
|
398
|
+
`${describe(options.filename)} is ${bytes.byteLength} bytes, over the ${maxBytes}-byte limit.`,
|
|
399
|
+
declared
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
if (bytes.byteLength === 0) {
|
|
403
|
+
return error("empty-document", "size-check", `${describe(options.filename)} is empty (0 bytes).`, declared);
|
|
404
|
+
}
|
|
405
|
+
const resolved = resolveMediaType({ mediaType: declared, filename: options.filename });
|
|
406
|
+
if (!resolved.succeeded) return resolved;
|
|
407
|
+
const { format, mediaType } = resolved.value;
|
|
408
|
+
const warnings = [];
|
|
409
|
+
let text;
|
|
410
|
+
let pdf;
|
|
411
|
+
let docx;
|
|
412
|
+
if (format === "pdf") {
|
|
413
|
+
const outcome = await extractPdf(bytes, options, mediaType);
|
|
414
|
+
if (!outcome.succeeded) return outcome;
|
|
415
|
+
text = outcome.value.text;
|
|
416
|
+
pdf = outcome.value.detail;
|
|
417
|
+
warnings.push(...outcome.value.warnings);
|
|
418
|
+
} else if (format === "docx") {
|
|
419
|
+
const outcome = await extractDocxText(bytes, mediaType, {
|
|
420
|
+
maxUncompressedBytes: options.maxUncompressedPartBytes ?? DEFAULT_MAX_ZIP_ENTRY_BYTES
|
|
421
|
+
});
|
|
422
|
+
if (!outcome.succeeded) return outcome;
|
|
423
|
+
text = outcome.value.text;
|
|
424
|
+
docx = outcome.value.detail;
|
|
425
|
+
} else {
|
|
426
|
+
try {
|
|
427
|
+
text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
428
|
+
} catch (cause) {
|
|
429
|
+
return error(
|
|
430
|
+
"decode-failed",
|
|
431
|
+
"decode",
|
|
432
|
+
`${describe(options.filename)} is not valid UTF-8 text \u2014 ${cause instanceof Error ? cause.message : String(cause)}. Re-save it as UTF-8.`,
|
|
433
|
+
mediaType
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
if (text.charCodeAt(0) === 65279) text = text.slice(1);
|
|
437
|
+
}
|
|
438
|
+
const trimmed = text.trim();
|
|
439
|
+
if (trimmed.length === 0) {
|
|
440
|
+
return error(
|
|
441
|
+
"empty-document",
|
|
442
|
+
format === "text" ? "decode" : "extract",
|
|
443
|
+
`${describe(options.filename)} produced no text. The file parsed, but it contains nothing readable.`,
|
|
444
|
+
mediaType,
|
|
445
|
+
pdf?.classification
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
return {
|
|
449
|
+
succeeded: true,
|
|
450
|
+
value: {
|
|
451
|
+
text: trimmed,
|
|
452
|
+
format,
|
|
453
|
+
mediaType,
|
|
454
|
+
byteSize: bytes.byteLength,
|
|
455
|
+
characterCount: trimmed.length,
|
|
456
|
+
...pdf ? { pdf } : {},
|
|
457
|
+
...docx ? { docx } : {},
|
|
458
|
+
warnings
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
function createDocumentExtractor(config = {}) {
|
|
463
|
+
return {
|
|
464
|
+
extract: (input, options) => extractDocument(input, { ...options, ...config }),
|
|
465
|
+
classifyPdf: (input) => {
|
|
466
|
+
if (!config.pdf) {
|
|
467
|
+
return error(
|
|
468
|
+
"pdf-engine-unavailable",
|
|
469
|
+
"classify",
|
|
470
|
+
"This extractor was created without a PDF engine, so PDFs cannot be classified. Pass `pdf` to createDocumentExtractor.",
|
|
471
|
+
"application/pdf"
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
return classifyPdfDocument(input instanceof Uint8Array ? input : new Uint8Array(input), config.pdf);
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
export {
|
|
479
|
+
DEFAULT_MAX_DOCUMENT_BYTES,
|
|
480
|
+
DEFAULT_MAX_ZIP_ENTRY_BYTES,
|
|
481
|
+
DOCM_MEDIA_TYPE,
|
|
482
|
+
DOCX_MEDIA_TYPE,
|
|
483
|
+
PDF_MEDIA_TYPE,
|
|
484
|
+
classifyPdfDocument,
|
|
485
|
+
createDocumentExtractor,
|
|
486
|
+
decodeXmlEntities,
|
|
487
|
+
extractDocument,
|
|
488
|
+
extractDocxText,
|
|
489
|
+
normalizeMediaType,
|
|
490
|
+
readZipDirectory,
|
|
491
|
+
readZipEntry,
|
|
492
|
+
resolveMediaType,
|
|
493
|
+
wordprocessingXmlToText
|
|
494
|
+
};
|
|
495
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/documents/zip.ts","../../src/documents/docx.ts","../../src/documents/media-type.ts","../../src/documents/extract.ts"],"sourcesContent":["/**\n * The slice of PKZIP an OOXML package needs: read the central directory, then\n * inflate one named entry.\n *\n * Why not a zip library: DOCX text extraction is the only reason this module\n * needs a zip, and the runtime already supplies the hard part —\n * `DecompressionStream('deflate-raw')` is a standard API present in workerd\n * and in Node, so the whole reader is header arithmetic plus a stream. Pulling\n * a zip package in would put ten transitive Node-shaped dependencies into every\n * consumer's Worker bundle to avoid ~120 lines.\n *\n * The CENTRAL DIRECTORY is the source of truth, not the local headers: an\n * archive written with data descriptors (streamed output) carries zeros for\n * the sizes in its local headers, and reading those is the classic way a\n * hand-rolled zip reader silently returns nothing.\n */\n\nconst END_OF_CENTRAL_DIRECTORY = 0x06054b50\nconst CENTRAL_FILE_HEADER = 0x02014b50\nconst LOCAL_FILE_HEADER = 0x04034b50\nconst ZIP64_SENTINEL_32 = 0xffffffff\nconst ZIP64_SENTINEL_16 = 0xffff\nconst MAX_COMMENT_LENGTH = 0xffff\nconst END_OF_CENTRAL_DIRECTORY_SIZE = 22\nconst STORED = 0\nconst DEFLATED = 8\n\nexport type ZipOutcome<T> = { succeeded: true; value: T } | { succeeded: false; error: string }\n\nexport interface ZipEntry {\n readonly name: string\n readonly compressionMethod: number\n readonly compressedSize: number\n readonly uncompressedSize: number\n readonly localHeaderOffset: number\n readonly encrypted: boolean\n}\n\nfunction view(bytes: Uint8Array): DataView {\n return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)\n}\n\n/**\n * Scan back from the tail for the end-of-central-directory record.\n *\n * The record's own comment length must account for exactly the bytes that\n * follow it, or the \"signature\" is compressed data (or an appended decoy) that\n * happens to spell `PK\\x05\\x06`. Without that check this reader and the upload\n * gate's reader resolve DIFFERENT records on the same bytes — the gate approves\n * one archive and the extractor reads another.\n */\nfunction findEndOfCentralDirectory(bytes: Uint8Array): number {\n const dv = view(bytes)\n const earliest = Math.max(0, bytes.byteLength - END_OF_CENTRAL_DIRECTORY_SIZE - MAX_COMMENT_LENGTH)\n for (let offset = bytes.byteLength - END_OF_CENTRAL_DIRECTORY_SIZE; offset >= earliest; offset--) {\n if (dv.getUint32(offset, true) !== END_OF_CENTRAL_DIRECTORY) continue\n if (offset + END_OF_CENTRAL_DIRECTORY_SIZE + dv.getUint16(offset + 20, true) === bytes.byteLength) return offset\n }\n return -1\n}\n\n/** Read every central-directory entry. Names are decoded as UTF-8, which is\n * what OOXML producers write (general-purpose bit 11). */\nexport function readZipDirectory(bytes: Uint8Array): ZipOutcome<ZipEntry[]> {\n if (bytes.byteLength < END_OF_CENTRAL_DIRECTORY_SIZE) {\n return { succeeded: false, error: `not a zip archive — ${bytes.byteLength} bytes is shorter than an empty archive` }\n }\n const eocd = findEndOfCentralDirectory(bytes)\n if (eocd < 0) {\n return { succeeded: false, error: 'not a zip archive — no end-of-central-directory record found' }\n }\n const dv = view(bytes)\n const entryCount = dv.getUint16(eocd + 10, true)\n const directoryOffset = dv.getUint32(eocd + 16, true)\n if (entryCount === ZIP64_SENTINEL_16 || directoryOffset === ZIP64_SENTINEL_32) {\n return { succeeded: false, error: 'zip64 archives are not supported' }\n }\n\n const decoder = new TextDecoder('utf-8', { fatal: false })\n const entries: ZipEntry[] = []\n let cursor = directoryOffset\n for (let i = 0; i < entryCount; i++) {\n if (cursor + 46 > bytes.byteLength || dv.getUint32(cursor, true) !== CENTRAL_FILE_HEADER) {\n return { succeeded: false, error: `central directory is truncated at entry ${i + 1} of ${entryCount}` }\n }\n const flags = dv.getUint16(cursor + 8, true)\n const nameLength = dv.getUint16(cursor + 28, true)\n const extraLength = dv.getUint16(cursor + 30, true)\n const commentLength = dv.getUint16(cursor + 32, true)\n entries.push({\n name: decoder.decode(bytes.subarray(cursor + 46, cursor + 46 + nameLength)),\n compressionMethod: dv.getUint16(cursor + 10, true),\n compressedSize: dv.getUint32(cursor + 20, true),\n uncompressedSize: dv.getUint32(cursor + 24, true),\n localHeaderOffset: dv.getUint32(cursor + 42, true),\n encrypted: (flags & 0x0001) !== 0,\n })\n cursor += 46 + nameLength + extraLength + commentLength\n }\n return { succeeded: true, value: entries }\n}\n\n/**\n * Ceiling on what ONE entry may expand to. A deflate stream reaches ~1000:1 on\n * repetitive input, so an archive small enough to pass any upload gate can ask\n * for gigabytes: measured here, a 305,893-byte archive declared a single entry\n * expanding to 314,572,800 bytes. A Cloudflare Worker isolate has 128 MB, so\n * that is an out-of-memory crash, not a slow path — and a crash is not a typed\n * error a product can report. 32 MB is far past any real Office part (the main\n * document of a 400-page contract is single-digit MB) and far under the\n * isolate's budget.\n */\nexport const DEFAULT_MAX_ZIP_ENTRY_BYTES = 32 * 1024 * 1024\n\n/** How an inflate finished. `limit-exceeded` carries the byte count reached so\n * the caller can name it, and the stream is cancelled at that point rather\n * than drained. */\ntype InflateResult =\n | { readonly kind: 'ok'; readonly value: Uint8Array }\n | { readonly kind: 'limit-exceeded'; readonly bytes: number }\n\n/**\n * Inflate a deflate-raw payload, bounded AS IT STREAMS.\n *\n * The bound cannot come from the declared `uncompressedSize` alone: that number\n * is attacker-supplied and the check that compares it to the real output can\n * only run once the output exists. Counting chunks and cancelling mid-stream is\n * what makes the ceiling real — the allocation never happens.\n */\nasync function inflateRaw(payload: Uint8Array, limit: number): Promise<InflateResult> {\n const body = new Response(payload as BodyInit).body\n if (!body) throw new Error('payload produced no readable stream')\n const reader = body.pipeThrough(new DecompressionStream('deflate-raw')).getReader()\n const chunks: Uint8Array[] = []\n let total = 0\n for (;;) {\n const { done, value } = await reader.read()\n if (done) break\n if (value === undefined) continue\n total += value.byteLength\n if (total > limit) {\n await reader.cancel()\n return { kind: 'limit-exceeded', bytes: total }\n }\n chunks.push(value)\n }\n const out = new Uint8Array(total)\n let offset = 0\n for (const chunk of chunks) {\n out.set(chunk, offset)\n offset += chunk.byteLength\n }\n return { kind: 'ok', value: out }\n}\n\n/** Options for {@link readZipEntry}. */\nexport interface ReadZipEntryOptions {\n /** Ceiling on the entry's UNCOMPRESSED size. Defaults to\n * {@link DEFAULT_MAX_ZIP_ENTRY_BYTES}. Checked against the directory's\n * declared size before any inflate runs, and again against the real output\n * as it streams, because the declared size is the archive's claim. */\n readonly maxUncompressedBytes?: number\n}\n\n/** Inflate one entry's bytes. The local header supplies only the variable-length\n * field sizes needed to find the payload; sizes come from the directory. */\nexport async function readZipEntry(\n bytes: Uint8Array,\n entry: ZipEntry,\n options: ReadZipEntryOptions = {},\n): Promise<ZipOutcome<Uint8Array>> {\n if (entry.encrypted) {\n return { succeeded: false, error: `'${entry.name}' is encrypted — password-protected archives are not supported` }\n }\n const limit = options.maxUncompressedBytes ?? DEFAULT_MAX_ZIP_ENTRY_BYTES\n if (entry.uncompressedSize > limit) {\n return {\n succeeded: false,\n error: `'${entry.name}' declares ${entry.uncompressedSize} uncompressed bytes, over the ${limit}-byte per-entry limit`,\n }\n }\n const dv = view(bytes)\n if (entry.localHeaderOffset + 30 > bytes.byteLength || dv.getUint32(entry.localHeaderOffset, true) !== LOCAL_FILE_HEADER) {\n return { succeeded: false, error: `'${entry.name}' has no local file header at offset ${entry.localHeaderOffset}` }\n }\n const nameLength = dv.getUint16(entry.localHeaderOffset + 26, true)\n const extraLength = dv.getUint16(entry.localHeaderOffset + 28, true)\n const start = entry.localHeaderOffset + 30 + nameLength + extraLength\n const end = start + entry.compressedSize\n if (end > bytes.byteLength) {\n return { succeeded: false, error: `'${entry.name}' payload runs past the end of the archive` }\n }\n const payload = bytes.subarray(start, end)\n\n if (entry.compressionMethod === STORED) return { succeeded: true, value: payload }\n if (entry.compressionMethod !== DEFLATED) {\n return { succeeded: false, error: `'${entry.name}' uses unsupported compression method ${entry.compressionMethod}` }\n }\n try {\n const inflated = await inflateRaw(payload, limit)\n if (inflated.kind === 'limit-exceeded') {\n // The directory under-declared the size. Reported as the limit breach it\n // is, not as the size mismatch below — the mismatch check runs on bytes\n // that were allocated, and these deliberately never were.\n return {\n succeeded: false,\n error: `'${entry.name}' expands past the ${limit}-byte per-entry limit (declared ${entry.uncompressedSize})`,\n }\n }\n if (inflated.value.byteLength !== entry.uncompressedSize) {\n return {\n succeeded: false,\n error: `'${entry.name}' inflated to ${inflated.value.byteLength} bytes but the directory declares ${entry.uncompressedSize}`,\n }\n }\n return { succeeded: true, value: inflated.value }\n } catch (error) {\n return { succeeded: false, error: `'${entry.name}' failed to inflate — ${error instanceof Error ? error.message : String(error)}` }\n }\n}\n","/**\n * DOCX → plain text, one paragraph per line.\n *\n * The main document part is resolved through the OPC relationship graph\n * (`_rels/.rels` → the `officeDocument` relationship's target) rather than\n * assuming `word/document.xml`, because the part name is a producer's choice,\n * not a constant.\n *\n * WordprocessingML text lives in `<w:t>` runs. Everything else is markup, with\n * four exceptions that carry meaning a reader would otherwise lose: `<w:tab/>`,\n * `<w:br/>`, `<w:cr/>`, and the paragraph boundary `</w:p>`. Two element bodies\n * are deliberately DROPPED — `<w:instrText>` (field instruction codes such as\n * `HYPERLINK \"…\"`, which are machinery, not prose) and `<w:delText>` (text a\n * tracked-changes revision deleted; extracting it would show a reader words the\n * author removed).\n *\n * One line per paragraph, not blank-line-separated paragraphs: extractors\n * downstream match line-anchored patterns against form and contract text, so a\n * predictable line structure is the useful shape.\n */\n\nimport type { DocumentOutcome, DocxExtractionDetail } from './types'\nimport { readZipDirectory, readZipEntry, type ReadZipEntryOptions } from './zip'\n\nconst RELATIONSHIPS_PART = '_rels/.rels'\nconst OFFICE_DOCUMENT_RELATIONSHIP = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument'\n\nconst NAMED_ENTITIES: Readonly<Record<string, string>> = {\n amp: '&',\n lt: '<',\n gt: '>',\n quot: '\"',\n apos: \"'\",\n}\n\n/** Resolve XML character/entity references. Unknown entities are left verbatim\n * rather than dropped, so nothing silently disappears from a document. */\nexport function decodeXmlEntities(input: string): string {\n if (!input.includes('&')) return input\n return input.replace(/&(#x[0-9a-fA-F]+|#\\d+|[a-zA-Z]+);/g, (match, reference: string) => {\n if (reference.startsWith('#x') || reference.startsWith('#X')) {\n const code = Number.parseInt(reference.slice(2), 16)\n return Number.isFinite(code) ? String.fromCodePoint(code) : match\n }\n if (reference.startsWith('#')) {\n const code = Number.parseInt(reference.slice(1), 10)\n return Number.isFinite(code) ? String.fromCodePoint(code) : match\n }\n return NAMED_ENTITIES[reference] ?? match\n })\n}\n\ninterface Tag {\n /** Local name without prefix, e.g. `t` for `<w:t>`. */\n readonly name: string\n readonly closing: boolean\n readonly selfClosing: boolean\n}\n\n/** Parse a tag body (the text between `<` and `>`) into name + shape. */\nfunction parseTag(body: string): Tag {\n const closing = body.startsWith('/')\n const selfClosing = body.endsWith('/')\n let inner = body.slice(closing ? 1 : 0, selfClosing ? body.length - 1 : body.length)\n const space = inner.search(/[\\s/]/)\n if (space >= 0) inner = inner.slice(0, space)\n const colon = inner.indexOf(':')\n return { name: colon >= 0 ? inner.slice(colon + 1) : inner, closing, selfClosing }\n}\n\n/** Element bodies whose text is markup or retracted content, never prose. */\nconst DROPPED_ELEMENTS = new Set(['instrText', 'delInstrText', 'delText'])\n\nexport interface DocxText {\n readonly text: string\n readonly paragraphCount: number\n}\n\n/**\n * Walk WordprocessingML and emit its readable text.\n *\n * Hand-scanned rather than regex-split because comments and CDATA sections may\n * contain `>`; a `/<[^>]*>/` scan ends the tag early on the first one and\n * corrupts everything after it.\n */\nexport function wordprocessingXmlToText(xml: string): DocxText {\n const out: string[] = []\n let paragraphCount = 0\n let textDepth = 0\n let dropDepth = 0\n let cursor = 0\n\n while (cursor < xml.length) {\n const open = xml.indexOf('<', cursor)\n if (open < 0) break\n if (open > cursor && textDepth > 0 && dropDepth === 0) {\n out.push(decodeXmlEntities(xml.slice(cursor, open)))\n }\n\n if (xml.startsWith('<!--', open)) {\n const close = xml.indexOf('-->', open + 4)\n cursor = close < 0 ? xml.length : close + 3\n continue\n }\n if (xml.startsWith('<![CDATA[', open)) {\n const close = xml.indexOf(']]>', open + 9)\n const end = close < 0 ? xml.length : close\n if (textDepth > 0 && dropDepth === 0) out.push(xml.slice(open + 9, end))\n cursor = close < 0 ? xml.length : close + 3\n continue\n }\n const close = xml.indexOf('>', open + 1)\n if (close < 0) break\n const body = xml.slice(open + 1, close)\n cursor = close + 1\n if (body.startsWith('?') || body.startsWith('!')) continue\n\n const tag = parseTag(body)\n if (DROPPED_ELEMENTS.has(tag.name)) {\n if (tag.selfClosing) continue\n dropDepth += tag.closing ? -1 : 1\n if (dropDepth < 0) dropDepth = 0\n continue\n }\n if (dropDepth > 0) continue\n\n if (tag.name === 't') {\n if (tag.selfClosing) continue\n textDepth += tag.closing ? -1 : 1\n if (textDepth < 0) textDepth = 0\n continue\n }\n if (tag.name === 'tab' && !tag.closing) out.push('\\t')\n else if ((tag.name === 'br' || tag.name === 'cr') && !tag.closing) out.push('\\n')\n else if (tag.name === 'p' && tag.closing) {\n out.push('\\n')\n paragraphCount++\n }\n }\n\n return { text: out.join('').replace(/\\r\\n?/g, '\\n'), paragraphCount }\n}\n\nfunction relationshipTarget(relsXml: string): string | null {\n for (const match of relsXml.matchAll(/<[^>]*Relationship\\b[^>]*>/g)) {\n const element = match[0]\n const type = /\\bType\\s*=\\s*\"([^\"]*)\"/.exec(element)?.[1]\n if (type !== OFFICE_DOCUMENT_RELATIONSHIP) continue\n const target = /\\bTarget\\s*=\\s*\"([^\"]*)\"/.exec(element)?.[1]\n if (target) return decodeXmlEntities(target).replace(/^\\/+/, '')\n }\n return null\n}\n\n/**\n * Extract the readable text of a `.docx` / `.docm` package. Every failure names\n * the archive-level cause; a package this cannot read is an error, never an\n * empty document.\n *\n * `zip.maxUncompressedBytes` bounds what each part may expand to. It defaults\n * to the zip reader's own ceiling, which is what keeps a deflate bomb inside a\n * small `.docx` a typed error instead of an out-of-memory crash.\n */\nexport async function extractDocxText(\n bytes: Uint8Array,\n mediaType: string,\n zip: ReadZipEntryOptions = {},\n): Promise<DocumentOutcome<{ text: string; detail: DocxExtractionDetail }>> {\n const fail = (\n code: 'malformed-archive' | 'extraction-failed',\n message: string,\n ): DocumentOutcome<{ text: string; detail: DocxExtractionDetail }> => ({\n succeeded: false,\n error: { code, stage: 'extract', message, mediaType },\n })\n\n const directory = readZipDirectory(bytes)\n if (!directory.succeeded) return fail('malformed-archive', `Not a readable Office package — ${directory.error}.`)\n\n const relsEntry = directory.value.find((entry) => entry.name === RELATIONSHIPS_PART)\n if (!relsEntry) {\n return fail(\n 'malformed-archive',\n `Office package has no '${RELATIONSHIPS_PART}' part, so its main document cannot be located. Parts found: ${directory.value.map((e) => e.name).slice(0, 12).join(', ') || 'none'}.`,\n )\n }\n const relsBytes = await readZipEntry(bytes, relsEntry, zip)\n if (!relsBytes.succeeded) return fail('malformed-archive', `Could not read '${RELATIONSHIPS_PART}' — ${relsBytes.error}.`)\n\n const decoder = new TextDecoder('utf-8', { fatal: false })\n const partName = relationshipTarget(decoder.decode(relsBytes.value))\n if (partName === null) {\n return fail('malformed-archive', `Office package declares no officeDocument relationship in '${RELATIONSHIPS_PART}'.`)\n }\n\n const documentEntry = directory.value.find((entry) => entry.name === partName)\n if (!documentEntry) {\n return fail('malformed-archive', `Office package points at '${partName}' as its main document, but no such part exists in the archive.`)\n }\n const documentBytes = await readZipEntry(bytes, documentEntry, zip)\n if (!documentBytes.succeeded) return fail('malformed-archive', `Could not read '${partName}' — ${documentBytes.error}.`)\n\n const strict = new TextDecoder('utf-8', { fatal: true })\n let xml: string\n try {\n xml = strict.decode(documentBytes.value)\n } catch (error) {\n return fail('extraction-failed', `'${partName}' is not valid UTF-8 — ${error instanceof Error ? error.message : String(error)}.`)\n }\n\n const walked = wordprocessingXmlToText(xml)\n return {\n succeeded: true,\n value: { text: walked.text, detail: { part: partName, paragraphCount: walked.paragraphCount } },\n }\n}\n","/**\n * Media type → extraction format. Resolution order is declared type first,\n * filename extension second (products routinely receive `application/octet-stream`\n * from a browser upload), and nothing else — byte sniffing belongs to the\n * upload gate that reads untrusted bytes, not to the extractor that has already\n * been handed a decision.\n */\n\nimport type { DocumentFormat, DocumentOutcome } from './types'\n\n/** OOXML WordprocessingML — the `.docx` a word processor writes. */\nexport const DOCX_MEDIA_TYPE = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'\n\n/** Macro-enabled WordprocessingML (`.docm`). Same part structure, so text\n * extraction is identical; the VBA project is never read or executed. */\nexport const DOCM_MEDIA_TYPE = 'application/vnd.ms-word.document.macroEnabled.12'\n\nexport const PDF_MEDIA_TYPE = 'application/pdf'\n\n/** Legacy binary Word (`.doc`) — an OLE2 compound file, not an OOXML package.\n * Named so the failure can say \"convert to .docx\" instead of surfacing a zip\n * parse error from a reader that was handed the wrong container. */\nconst LEGACY_DOC_MEDIA_TYPES = new Set(['application/msword', 'application/vnd.ms-word'])\n\n/** Lower-cased comparison key → the registered spelling to report back. Media\n * types are case-insensitive, and the `.docm` type is registered with capitals\n * (`macroEnabled`), so matching without this folds it into \"unsupported\". */\nconst DOCX_MEDIA_TYPES = new Map([\n [DOCX_MEDIA_TYPE.toLowerCase(), DOCX_MEDIA_TYPE],\n [DOCM_MEDIA_TYPE.toLowerCase(), DOCM_MEDIA_TYPE],\n])\n\n/** Extensions that decide the format when the declared type is missing or the\n * browser's generic `application/octet-stream`. */\nconst EXTENSION_MEDIA_TYPES: Readonly<Record<string, string>> = {\n pdf: PDF_MEDIA_TYPE,\n docx: DOCX_MEDIA_TYPE,\n docm: DOCM_MEDIA_TYPE,\n doc: 'application/msword',\n txt: 'text/plain',\n text: 'text/plain',\n md: 'text/markdown',\n markdown: 'text/markdown',\n csv: 'text/csv',\n}\n\nconst UNDECLARED = new Set(['', 'application/octet-stream', 'binary/octet-stream'])\n\nexport interface ResolvedMediaType {\n readonly format: DocumentFormat\n /** Canonical media type for the resolved format. */\n readonly mediaType: string\n}\n\n/** Strip parameters and case from a media type: `Text/Plain; charset=utf-8` →\n * `text/plain`. */\nexport function normalizeMediaType(mediaType: string): string {\n const base = mediaType.split(';', 1)[0] ?? ''\n return base.trim().toLowerCase()\n}\n\nfunction extensionOf(filename: string): string {\n const dot = filename.lastIndexOf('.')\n if (dot < 0 || dot === filename.length - 1) return ''\n return filename.slice(dot + 1).toLowerCase()\n}\n\nexport interface ResolveMediaTypeInput {\n /** The type the upload declared. May be empty or `application/octet-stream`. */\n readonly mediaType?: string\n /** Consulted only when `mediaType` is missing or generic. */\n readonly filename?: string\n}\n\n/**\n * Decide which extractor handles these bytes. Fails loud rather than guessing:\n * an unrecognized type names itself and lists what is supported.\n */\nexport function resolveMediaType(input: ResolveMediaTypeInput): DocumentOutcome<ResolvedMediaType> {\n const declared = normalizeMediaType(input.mediaType ?? '')\n const fromExtension = input.filename ? EXTENSION_MEDIA_TYPES[extensionOf(input.filename)] : undefined\n const effective = (UNDECLARED.has(declared) ? fromExtension ?? declared : declared).toLowerCase()\n\n const fail = (message: string): DocumentOutcome<ResolvedMediaType> => ({\n succeeded: false,\n error: { code: 'unsupported-media-type', stage: 'media-type', message, mediaType: declared || (fromExtension ?? '') },\n })\n\n if (effective === PDF_MEDIA_TYPE) return { succeeded: true, value: { format: 'pdf', mediaType: PDF_MEDIA_TYPE } }\n const docx = DOCX_MEDIA_TYPES.get(effective)\n if (docx) return { succeeded: true, value: { format: 'docx', mediaType: docx } }\n if (LEGACY_DOC_MEDIA_TYPES.has(effective)) {\n return fail(\n `Legacy binary Word documents (${effective}) are not supported — they are OLE2 compound files, not OOXML packages. Save the file as .docx and upload it again.`,\n )\n }\n if (effective.startsWith('text/')) return { succeeded: true, value: { format: 'text', mediaType: effective } }\n if (effective === '') {\n return fail(\n `Cannot determine the document type: no media type was declared${input.filename ? ` and '${input.filename}' has no recognized extension` : ' and no filename was supplied'}. Supported: ${PDF_MEDIA_TYPE}, ${DOCX_MEDIA_TYPE}, and any text/* type.`,\n )\n }\n return fail(\n `Unsupported media type '${effective}'. Supported: ${PDF_MEDIA_TYPE}, ${DOCX_MEDIA_TYPE}, ${DOCM_MEDIA_TYPE}, and any text/* type.`,\n )\n}\n","/**\n * The extraction pipeline: size gate → media type → (PDF: classify, then\n * extract) / (DOCX: OPC read) / (text: strict decode).\n *\n * Two rules make this safe to build a product on.\n *\n * 1. **Classify before extracting a PDF.** The wasm engine's `extractText`\n * panics (`RuntimeError: unreachable`) on any page with no text layer, and\n * that is measured, not theoretical — including on `mixed` documents where\n * only ONE page is an image. Classification runs first and decides whether\n * extraction is even reachable, which is also what produces the OCR signal.\n * 2. **A scanned PDF is `pdf-needs-ocr`, never an empty string.** Both hand-\n * rolled predecessors of this module got this right and it is the single\n * behaviour a product cannot afford to lose: silent empty text lets a\n * downstream model answer confidently about a document nobody read.\n */\n\nimport { extractDocxText } from './docx'\nimport { resolveMediaType } from './media-type'\nimport { DEFAULT_MAX_ZIP_ENTRY_BYTES } from './zip'\nimport type {\n DocumentExtractionError,\n DocumentOutcome,\n ExtractedDocument,\n PdfClassification,\n PdfEngine,\n PdfExtractionDetail,\n PdfTextFormat,\n} from './types'\n\n/** 25 MB — the cap both predecessor implementations converged on, and roughly\n * the point where a Worker's memory budget stops tolerating a full decode. */\nexport const DEFAULT_MAX_DOCUMENT_BYTES = 25 * 1024 * 1024\n\nexport interface ExtractDocumentOptions {\n /** The type the upload declared. Falls back to `filename`'s extension when\n * missing or `application/octet-stream`. */\n readonly mediaType?: string\n /** Used to resolve the media type, and named in error messages. */\n readonly filename?: string\n /** Required to extract PDFs. Supplied by the caller because the wasm asset\n * must come from the consumer's bundler — see\n * `@tangle-network/agent-app/documents/pdf-inspector`. */\n readonly pdf?: PdfEngine\n /** Byte cap on the uploaded ARCHIVE, default\n * {@link DEFAULT_MAX_DOCUMENT_BYTES}. */\n readonly maxBytes?: number\n /**\n * Byte cap on what any ONE part of an Office package may expand to, default\n * {@link DEFAULT_MAX_ZIP_ENTRY_BYTES}.\n *\n * A separate dial from {@link maxBytes} because they bound different things:\n * deflate reaches ~1000:1 on repetitive input, so capping the upload does\n * nothing to cap the decompression it asks for.\n */\n readonly maxUncompressedPartBytes?: number\n /**\n * Plain text (default) preserves source line breaks, which line-anchored\n * parsers depend on; markdown carries headings and page markers but reflows\n * lines into paragraphs.\n *\n * A *preference*, because of exactly one measured case: on a partially\n * scanned PDF the plain-text engine panics, and only the markdown engine can\n * recover the pages that do have text. The result reports what you actually\n * got in {@link PdfExtractionDetail.textFormat} — check it when the\n * distinction matters.\n */\n readonly preferredPdfFormat?: PdfTextFormat\n}\n\nfunction error(\n code: DocumentExtractionError['code'],\n stage: DocumentExtractionError['stage'],\n message: string,\n mediaType: string,\n classification?: PdfClassification,\n): { succeeded: false; error: DocumentExtractionError } {\n return { succeeded: false, error: { code, stage, message, mediaType, ...(classification ? { classification } : {}) } }\n}\n\nfunction describe(filename: string | undefined): string {\n return filename ? `'${filename}'` : 'the document'\n}\n\nfunction pageList(pages: readonly number[]): string {\n return pages.length > 8 ? `${pages.slice(0, 8).join(', ')}, … (${pages.length} total)` : pages.join(', ')\n}\n\n/**\n * Classify a PDF: whether it has a text layer, and which pages do not.\n *\n * Safe on every input — classification is what makes extraction safe, so it can\n * never be the thing that panics. Call this directly when a product wants to\n * show \"3 of 40 pages need OCR\" before deciding to extract.\n */\nexport function classifyPdfDocument(bytes: Uint8Array, pdf: PdfEngine): DocumentOutcome<PdfClassification> {\n return pdf.classify(bytes)\n}\n\nasync function extractPdf(\n bytes: Uint8Array,\n options: ExtractDocumentOptions,\n mediaType: string,\n): Promise<DocumentOutcome<{ text: string; detail: PdfExtractionDetail; warnings: string[] }>> {\n const pdf = options.pdf\n if (!pdf) {\n return error(\n 'pdf-engine-unavailable',\n 'classify',\n `PDF extraction needs a PDF engine, and none was configured. Pass \\`pdf\\` — see @tangle-network/agent-app/documents/pdf-inspector for the wasm-backed engine and how to deliver its .wasm asset.`,\n mediaType,\n )\n }\n\n const classified = pdf.classify(bytes)\n if (!classified.succeeded) return classified\n const classification = classified.value\n\n if (classification.needsOcr) {\n const label = classification.kind === 'image-based' ? 'image-based' : 'a scan'\n return error(\n 'pdf-needs-ocr',\n 'classify',\n `${describe(options.filename)} is ${label} with no text layer on any of its ${classification.pageCount} page${classification.pageCount === 1 ? '' : 's'} — OCR is required before its text can be read.`,\n mediaType,\n classification,\n )\n }\n\n const warnings: string[] = []\n // extractText walks a per-page layout table that is empty for a page with no\n // text runs; the markdown engine tolerates that and returns the pages that do\n // have text, which is why a partial scan switches engines rather than failing.\n const requested = options.preferredPdfFormat ?? 'text'\n const textFormat: PdfTextFormat = classification.partiallyScanned ? 'markdown' : requested\n if (classification.partiallyScanned) {\n const one = classification.pagesNeedingOcr.length === 1\n warnings.push(\n `Page${one ? '' : 's'} ${pageList(classification.pagesNeedingOcr)} of ${classification.pageCount} ${one ? 'has' : 'have'} no text layer and need${one ? 's' : ''} OCR; the text below comes from the remaining pages.`,\n )\n if (requested === 'text') {\n warnings.push('Text was extracted as markdown because the plain-text engine cannot read a partially scanned PDF.')\n }\n }\n if (classification.hasEncodingIssues) {\n warnings.push('The PDF has character-encoding problems; extracted text may contain garbled characters.')\n }\n\n const extracted = pdf.extract(bytes, textFormat)\n if (!extracted.succeeded) return extracted\n\n return {\n succeeded: true,\n value: {\n text: extracted.value,\n detail: { classification, textFormat, partial: classification.partiallyScanned },\n warnings,\n },\n }\n}\n\n/**\n * Extract a document's text.\n *\n * Every failure is a typed {@link DocumentExtractionError} naming the stage it\n * stopped at; nothing throws, and success never carries empty text.\n */\nexport async function extractDocument(\n input: ArrayBuffer | Uint8Array,\n options: ExtractDocumentOptions = {},\n): Promise<DocumentOutcome<ExtractedDocument>> {\n const bytes = input instanceof Uint8Array ? input : new Uint8Array(input)\n const declared = options.mediaType ?? ''\n const maxBytes = options.maxBytes ?? DEFAULT_MAX_DOCUMENT_BYTES\n\n if (bytes.byteLength > maxBytes) {\n return error(\n 'too-large',\n 'size-check',\n `${describe(options.filename)} is ${bytes.byteLength} bytes, over the ${maxBytes}-byte limit.`,\n declared,\n )\n }\n if (bytes.byteLength === 0) {\n return error('empty-document', 'size-check', `${describe(options.filename)} is empty (0 bytes).`, declared)\n }\n\n const resolved = resolveMediaType({ mediaType: declared, filename: options.filename })\n if (!resolved.succeeded) return resolved\n const { format, mediaType } = resolved.value\n\n const warnings: string[] = []\n let text: string\n let pdf: PdfExtractionDetail | undefined\n let docx: ExtractedDocument['docx']\n\n if (format === 'pdf') {\n const outcome = await extractPdf(bytes, options, mediaType)\n if (!outcome.succeeded) return outcome\n text = outcome.value.text\n pdf = outcome.value.detail\n warnings.push(...outcome.value.warnings)\n } else if (format === 'docx') {\n const outcome = await extractDocxText(bytes, mediaType, {\n maxUncompressedBytes: options.maxUncompressedPartBytes ?? DEFAULT_MAX_ZIP_ENTRY_BYTES,\n })\n if (!outcome.succeeded) return outcome\n text = outcome.value.text\n docx = outcome.value.detail\n } else {\n // Strict decode: a text file that is not UTF-8 is a decoding failure the\n // uploader can act on, not mojibake handed to a model as if it were prose.\n try {\n text = new TextDecoder('utf-8', { fatal: true }).decode(bytes)\n } catch (cause) {\n return error(\n 'decode-failed',\n 'decode',\n `${describe(options.filename)} is not valid UTF-8 text — ${cause instanceof Error ? cause.message : String(cause)}. Re-save it as UTF-8.`,\n mediaType,\n )\n }\n if (text.charCodeAt(0) === 0xfeff) text = text.slice(1)\n }\n\n const trimmed = text.trim()\n if (trimmed.length === 0) {\n return error(\n 'empty-document',\n format === 'text' ? 'decode' : 'extract',\n `${describe(options.filename)} produced no text. The file parsed, but it contains nothing readable.`,\n mediaType,\n pdf?.classification,\n )\n }\n\n return {\n succeeded: true,\n value: {\n text: trimmed,\n format,\n mediaType,\n byteSize: bytes.byteLength,\n characterCount: trimmed.length,\n ...(pdf ? { pdf } : {}),\n ...(docx ? { docx } : {}),\n warnings,\n },\n }\n}\n\nexport interface DocumentExtractorConfig {\n readonly pdf?: PdfEngine\n readonly maxBytes?: number\n readonly maxUncompressedPartBytes?: number\n readonly preferredPdfFormat?: PdfTextFormat\n}\n\nexport interface DocumentExtractor {\n readonly extract: (\n input: ArrayBuffer | Uint8Array,\n options?: Omit<ExtractDocumentOptions, 'pdf' | 'maxBytes' | 'maxUncompressedPartBytes' | 'preferredPdfFormat'>,\n ) => Promise<DocumentOutcome<ExtractedDocument>>\n /** Classify a PDF without extracting. Fails with `pdf-engine-unavailable`\n * when the extractor was built without a PDF engine. */\n readonly classifyPdf: (input: ArrayBuffer | Uint8Array) => DocumentOutcome<PdfClassification>\n}\n\n/**\n * Bind an engine and limits once, at worker start, so call sites read\n * `documents.extract(bytes, { mediaType })` and never re-wire the wasm.\n */\nexport function createDocumentExtractor(config: DocumentExtractorConfig = {}): DocumentExtractor {\n return {\n extract: (input, options) => extractDocument(input, { ...options, ...config }),\n classifyPdf: (input) => {\n if (!config.pdf) {\n return error(\n 'pdf-engine-unavailable',\n 'classify',\n 'This extractor was created without a PDF engine, so PDFs cannot be classified. Pass `pdf` to createDocumentExtractor.',\n 'application/pdf',\n )\n }\n return classifyPdfDocument(input instanceof Uint8Array ? input : new Uint8Array(input), config.pdf)\n },\n }\n}\n"],"mappings":";AAiBA,IAAM,2BAA2B;AACjC,IAAM,sBAAsB;AAC5B,IAAM,oBAAoB;AAC1B,IAAM,oBAAoB;AAC1B,IAAM,oBAAoB;AAC1B,IAAM,qBAAqB;AAC3B,IAAM,gCAAgC;AACtC,IAAM,SAAS;AACf,IAAM,WAAW;AAajB,SAAS,KAAK,OAA6B;AACzC,SAAO,IAAI,SAAS,MAAM,QAAQ,MAAM,YAAY,MAAM,UAAU;AACtE;AAWA,SAAS,0BAA0B,OAA2B;AAC5D,QAAM,KAAK,KAAK,KAAK;AACrB,QAAM,WAAW,KAAK,IAAI,GAAG,MAAM,aAAa,gCAAgC,kBAAkB;AAClG,WAAS,SAAS,MAAM,aAAa,+BAA+B,UAAU,UAAU,UAAU;AAChG,QAAI,GAAG,UAAU,QAAQ,IAAI,MAAM,yBAA0B;AAC7D,QAAI,SAAS,gCAAgC,GAAG,UAAU,SAAS,IAAI,IAAI,MAAM,MAAM,WAAY,QAAO;AAAA,EAC5G;AACA,SAAO;AACT;AAIO,SAAS,iBAAiB,OAA2C;AAC1E,MAAI,MAAM,aAAa,+BAA+B;AACpD,WAAO,EAAE,WAAW,OAAO,OAAO,4BAAuB,MAAM,UAAU,0CAA0C;AAAA,EACrH;AACA,QAAM,OAAO,0BAA0B,KAAK;AAC5C,MAAI,OAAO,GAAG;AACZ,WAAO,EAAE,WAAW,OAAO,OAAO,oEAA+D;AAAA,EACnG;AACA,QAAM,KAAK,KAAK,KAAK;AACrB,QAAM,aAAa,GAAG,UAAU,OAAO,IAAI,IAAI;AAC/C,QAAM,kBAAkB,GAAG,UAAU,OAAO,IAAI,IAAI;AACpD,MAAI,eAAe,qBAAqB,oBAAoB,mBAAmB;AAC7E,WAAO,EAAE,WAAW,OAAO,OAAO,mCAAmC;AAAA,EACvE;AAEA,QAAM,UAAU,IAAI,YAA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AE,IAAI,IAAI,CAAC;AAAA,MACrB,GAAI,OAAO,EAAE,KAAK,IAAI,CAAC;AAAA,MACvB;AAAA,IACF;AAAA,EACF;AACF;AAuBO,SAAS,wBAAwB,SAAkC,CAAC,GAAsB;AAC/F,SAAO;AAAA,IACL,SAAS,CAAC,OAAO,YAAY,gBAAgB,OAAO,EAAE,GAAG,SAAS,GAAG,OAAO,CAAC;AAAA,IAC7E,aAAa,CAAC,UAAU;AACtB,UAAI,CAAC,OAAO,KAAK;AACf,eAAO;AAAA,UACL;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA,MACF;AACA,aAAO,oBAAoB,iBAAiB,aAAa,QAAQ,IAAI,WAAW,KAAK,GAAG,OAAO,GAAG;AAAA,IACpG;AAAA,EACF;AACF;","names":["error","close","error"]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { P as PdfEngine, D as DocumentOutcome } from '../types-DrBidf3O.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The wasm-backed {@link PdfEngine}, binding `@firecrawl/pdf-inspector-wasm`
|
|
5
|
+
* (MIT, Rust→wasm).
|
|
6
|
+
*
|
|
7
|
+
* **Why this is its own subpath.** The engine's `.wasm` must reach the runtime
|
|
8
|
+
* as a build-time module asset produced by the CONSUMER's bundler — workerd
|
|
9
|
+
* forbids compiling wasm from bytes at request time — so a shared library
|
|
10
|
+
* cannot import it. This file therefore imports only the JS glue and takes the
|
|
11
|
+
* compiled module as an argument. Keeping it out of
|
|
12
|
+
* `@tangle-network/agent-app/documents` also means a product that only handles
|
|
13
|
+
* DOCX and text never needs the package installed at all.
|
|
14
|
+
*
|
|
15
|
+
* **Why this engine.** It is the one PDF extractor proven to run under real
|
|
16
|
+
* workerd: the wasm imports nothing but its own JS glue (no WASI), and
|
|
17
|
+
* `initSync` instantiates an already-compiled `WebAssembly.Module`, which is
|
|
18
|
+
* exactly the case workerd permits. The napi build does not run there, and
|
|
19
|
+
* pdfjs-based extractors need DOM globals workerd does not have.
|
|
20
|
+
*
|
|
21
|
+
* **Engine behaviour this wrapper is built around, measured on real fixture
|
|
22
|
+
* PDFs (`tests/documents/pdf.test.ts` re-measures all of it):**
|
|
23
|
+
* - `extractText` PANICS (`RuntimeError: unreachable`, an out-of-bounds index
|
|
24
|
+
* in the engine's layout table) on any document with a page that has no text
|
|
25
|
+
* layer — including a `mixed` document whose other pages are fine. The
|
|
26
|
+
* instance survives the panic, but nothing recovers the text.
|
|
27
|
+
* - `detectPdf` and `processPdf` do NOT panic on those documents; `processPdf`
|
|
28
|
+
* returns the readable pages' markdown, which is why `extract` uses it for
|
|
29
|
+
* markdown and for anything partially scanned.
|
|
30
|
+
* - `detectPdf` reports OCR pages 1-INDEXED, while `classifyPdf` reports the
|
|
31
|
+
* same pages 0-indexed. This wrapper uses `detectPdf` (it carries per-page
|
|
32
|
+
* reasons and layout that `classifyPdf` does not) and publishes 1-indexed.
|
|
33
|
+
* - `processPdf`'s own `pagesNeedingOcr` is unreliable — it reported both pages
|
|
34
|
+
* of a fully text-based 2-page PDF as needing OCR — so the classification a
|
|
35
|
+
* caller sees always comes from `detectPdf`.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The compiled wasm, or a thunk that produces it on first use.
|
|
40
|
+
*
|
|
41
|
+
* A Worker passes the `.wasm` module import (a `WebAssembly.Module`); Node
|
|
42
|
+
* passes raw bytes. A thunk defers reading either until a PDF actually arrives.
|
|
43
|
+
*/
|
|
44
|
+
type PdfInspectorWasm = WebAssembly.Module | BufferSource | (() => WebAssembly.Module | BufferSource);
|
|
45
|
+
/** Instantiate the engine for this isolate. Idempotent; safe to call per
|
|
46
|
+
* request. Exported so a product can warm the wasm at startup instead of
|
|
47
|
+
* paying instantiation on a user's first upload. */
|
|
48
|
+
declare function initPdfInspector(wasm: PdfInspectorWasm): DocumentOutcome<{
|
|
49
|
+
version: string;
|
|
50
|
+
}>;
|
|
51
|
+
/** Whether the engine has been instantiated in this isolate. */
|
|
52
|
+
declare function isPdfInspectorReady(): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Build the wasm-backed engine. Instantiation is lazy — the first classify or
|
|
55
|
+
* extract call initializes it — so importing this costs nothing until a PDF
|
|
56
|
+
* arrives.
|
|
57
|
+
*/
|
|
58
|
+
declare function createPdfInspectorEngine(wasm: PdfInspectorWasm): PdfEngine;
|
|
59
|
+
|
|
60
|
+
export { type PdfInspectorWasm, createPdfInspectorEngine, initPdfInspector, isPdfInspectorReady };
|