dist / fileSearch / documents.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MAX_DOCUMENTS = exports.MAX_COLLECTION_BYTES = exports.MAX_FILE_BYTES = void 0;
exports.supports = supports;
exports.extractDocument = extractDocument;
exports.extractText = extractText;
const node_path_1 = require("node:path");
const encoding_1 = require("./encoding");
const json_1 = require("./json");
const html_1 = require("./web/html");
exports.MAX_FILE_BYTES = 32 * 1024 * 1024;
exports.MAX_COLLECTION_BYTES = 128 * 1024 * 1024;
exports.MAX_DOCUMENTS = 200;
const extensions = new Set([".txt", ".text", ".md", ".markdown", ".json", ".jsonl", ".ndjson", ".html", ".htm", ".csv", ".tsv", ".log", ".xml", ".yaml", ".yml"]);
function supports(name) { return extensions.has((0, node_path_1.extname)(name).toLowerCase()); }
function extractDocument(name, bytes, encoding = "auto", sourceUrl) {
if (!supports(name))
throw new Error(`Unsupported file type: ${name}. PDF and Office are not supported yet.`);
if (bytes.length > exports.MAX_FILE_BYTES)
throw new Error(`File exceeds 32 MiB: ${name}`);
const decoded = (0, encoding_1.decodeDocument)(name, bytes, encoding);
let text = decoded.text;
if (text.includes("\0"))
throw new Error(`Binary data is not supported: ${name}`);
text = text.replace(/\r\n?/g, "\n");
const sourceHtml = /\.html?$/i.test(name) ? text : undefined;
const extractedHtml = sourceHtml !== undefined ? (0, html_1.extractHtml)(sourceHtml, sourceUrl) : undefined;
if (extractedHtml)
text = extractedHtml.text;
const json = /\.json$/i.test(name) ? (0, json_1.parseJson)(text) : undefined;
return { ...decoded, text, json, sourceHtml, html: extractedHtml?.html };
}
function extractText(name, bytes, encoding = "auto") {
return extractDocument(name, bytes, encoding).text;
}