@gmickel/gno 1.45.1 → 2.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/README.md +1 -1
- package/THIRD_PARTY_NOTICES.md +46 -0
- package/assets/skill/SKILL.md +7 -6
- package/assets/skill/cli-reference.md +14 -6
- package/assets/skill/mcp-reference.md +4 -1
- package/assets/spa-production.json.gz +0 -0
- package/browser-extension/artifacts/gno-browser-clipper-v2.0.0.zip +0 -0
- package/browser-extension/artifacts/gno-browser-clipper-v2.0.0.zip.sha256 +1 -0
- package/browser-extension/dist/chunk-4tc9v0ja.js +74 -0
- package/browser-extension/dist/manifest.json +1 -1
- package/browser-extension/dist/preview.html +1 -1
- package/browser-extension/dist/service-worker.js +32 -33
- package/bunfig.toml +2 -0
- package/package.json +40 -26
- package/spec/cli.md +30 -11
- package/spec/db/schema.sql +146 -1
- package/spec/mcp.md +26 -0
- package/src/app/context-runtime-types.ts +3 -0
- package/src/app/context-runtime.ts +2 -0
- package/src/cli/commands/ask.ts +6 -1
- package/src/cli/commands/daemon.ts +21 -8
- package/src/cli/commands/embed.ts +77 -41
- package/src/cli/commands/mcp/install.ts +20 -0
- package/src/cli/commands/mcp/paths.ts +25 -0
- package/src/cli/commands/mcp/status.ts +6 -0
- package/src/cli/detach.ts +3 -2
- package/src/cli/program.ts +6 -0
- package/src/config/types.ts +3 -3
- package/src/converters/adapters/markitdownTs/adapter.ts +1 -2
- package/src/converters/adapters/officeparser/adapter.ts +1 -2
- package/src/converters/versions.ts +6 -8
- package/src/core/context-evidence.ts +8 -4
- package/src/core/job-manager.ts +95 -13
- package/src/core/network-boundary-inventory.ts +10 -0
- package/src/core/shutdown-budget.ts +45 -0
- package/src/embed/backlog.ts +107 -4
- package/src/embed/batch.ts +42 -2
- package/src/embed/fingerprint.ts +16 -0
- package/src/embed/retry.ts +113 -5
- package/src/embed/variant-backlog.ts +105 -0
- package/src/embed/variant-plan.ts +62 -0
- package/src/embed/variant-retry.ts +113 -0
- package/src/ingestion/graph-reconciliation.ts +327 -0
- package/src/ingestion/sync.ts +9 -272
- package/src/llm/http-inference.ts +6 -0
- package/src/llm/httpEmbedding.ts +37 -6
- package/src/llm/httpGeneration.ts +18 -3
- package/src/llm/httpRerank.ts +23 -5
- package/src/llm/inference-cancellation.ts +168 -0
- package/src/llm/inference-scope.ts +202 -0
- package/src/llm/lazy-ports.ts +115 -0
- package/src/llm/native-worker/client.ts +541 -0
- package/src/llm/native-worker/dispatcher.ts +228 -0
- package/src/llm/native-worker/embedding-identity.ts +33 -0
- package/src/llm/native-worker/entry.ts +173 -0
- package/src/llm/native-worker/errors.ts +32 -0
- package/src/llm/native-worker/evaluation.ts +16 -0
- package/src/llm/native-worker/owned-exit.ts +108 -0
- package/src/llm/native-worker/owner.ts +141 -0
- package/src/llm/native-worker/ports.ts +317 -0
- package/src/llm/native-worker/protocol.ts +442 -0
- package/src/llm/native-worker/runtime-config.ts +92 -0
- package/src/llm/nodeLlamaCpp/adapter.ts +77 -20
- package/src/llm/nodeLlamaCpp/embedding.ts +130 -46
- package/src/llm/nodeLlamaCpp/generation.ts +34 -5
- package/src/llm/nodeLlamaCpp/lifecycle-options.ts +99 -0
- package/src/llm/nodeLlamaCpp/lifecycle.ts +209 -204
- package/src/llm/nodeLlamaCpp/rerank-capacity.ts +111 -0
- package/src/llm/nodeLlamaCpp/rerank.ts +118 -27
- package/src/llm/nodeLlamaCpp/simulator-handle.ts +73 -0
- package/src/llm/nodeLlamaCpp/simulator-install.ts +124 -0
- package/src/llm/nodeLlamaCpp/simulator-session.ts +240 -0
- package/src/llm/nodeLlamaCpp/simulator-types.ts +80 -0
- package/src/llm/types.ts +35 -5
- package/src/mcp/context.ts +27 -0
- package/src/mcp/http-transport.ts +12 -10
- package/src/mcp/server.ts +3 -0
- package/src/mcp/tool-profile.ts +30 -8
- package/src/mcp/tools/context.ts +8 -11
- package/src/mcp/tools/embed.ts +1 -1
- package/src/mcp/tools/index-cmd.ts +1 -1
- package/src/mcp/tools/index.ts +10 -8
- package/src/mcp/tools/query.ts +14 -30
- package/src/mcp/tools/vsearch.ts +1 -1
- package/src/pipeline/answer.ts +23 -3
- package/src/pipeline/claim-verifier.ts +6 -0
- package/src/pipeline/expansion.ts +43 -40
- package/src/pipeline/explain.ts +6 -2
- package/src/pipeline/filters.ts +63 -0
- package/src/pipeline/fusion.ts +29 -9
- package/src/pipeline/graph-retrieval.ts +29 -9
- package/src/pipeline/hybrid.ts +198 -55
- package/src/pipeline/hydration.ts +161 -0
- package/src/pipeline/owner-fusion.ts +87 -0
- package/src/pipeline/rerank.ts +35 -11
- package/src/pipeline/search.ts +13 -2
- package/src/pipeline/types.ts +5 -3
- package/src/pipeline/vsearch.ts +87 -7
- package/src/sdk/client.ts +47 -3
- package/src/sdk/embed.ts +63 -39
- package/src/serve/background-runtime.ts +1 -1
- package/src/serve/context.ts +41 -56
- package/src/serve/embed-scheduler.ts +58 -35
- package/src/serve/public/components/IndexingProgress.tsx +46 -60
- package/src/serve/public/globals.built.css +1 -1
- package/src/serve/public/lib/shiki-language-ids.ts +14 -0
- package/src/serve/resident-admission.ts +36 -36
- package/src/serve/resident-background-work.ts +20 -2
- package/src/serve/resident-request.ts +11 -5
- package/src/serve/resident-runtime.ts +97 -61
- package/src/serve/resident-shutdown.ts +153 -0
- package/src/serve/routes/api.ts +3 -1
- package/src/serve/server.ts +47 -26
- package/src/store/migrations/028-vector-variants.ts +54 -0
- package/src/store/migrations/029-graph-reference-state.ts +77 -0
- package/src/store/migrations/index.ts +4 -0
- package/src/store/sqlite/adapter.ts +251 -183
- package/src/store/sqlite/eligibility.ts +174 -0
- package/src/store/sqlite/graph-edge-application.ts +66 -0
- package/src/store/sqlite/graph-reference-state.ts +194 -0
- package/src/store/sqlite/legacy-vector-ownership.ts +79 -0
- package/src/store/types.ts +80 -12
- package/src/store/vector/eligibility.ts +36 -0
- package/src/store/vector/freshness.ts +33 -6
- package/src/store/vector/lazy.ts +81 -0
- package/src/store/vector/sqlite-vec.ts +106 -54
- package/src/store/vector/stats.ts +14 -3
- package/src/store/vector/types.ts +35 -2
- package/src/store/vector/variant-search.ts +192 -0
- package/src/store/vector/variants.ts +451 -0
- package/vendor/converters/markitdown-ts/LICENSE +21 -0
- package/vendor/converters/markitdown-ts/dist/index.cjs +1180 -0
- package/vendor/converters/markitdown-ts/dist/index.d.cts +46 -0
- package/vendor/converters/markitdown-ts/dist/index.d.mts +46 -0
- package/vendor/converters/markitdown-ts/dist/index.d.ts +46 -0
- package/vendor/converters/markitdown-ts/dist/index.mjs +1152 -0
- package/vendor/converters/markitdown-ts/package.json +77 -0
- package/vendor/converters/officeparser/LICENSE +21 -0
- package/vendor/converters/officeparser/dist/OfficeConverter.d.ts +47 -0
- package/vendor/converters/officeparser/dist/OfficeConverter.js +76 -0
- package/vendor/converters/officeparser/dist/OfficeGenerator.d.ts +23 -0
- package/vendor/converters/officeparser/dist/OfficeGenerator.js +73 -0
- package/vendor/converters/officeparser/dist/OfficeParser.d.ts +106 -0
- package/vendor/converters/officeparser/dist/OfficeParser.js +332 -0
- package/vendor/converters/officeparser/dist/cli.d.ts +28 -0
- package/vendor/converters/officeparser/dist/cli.js +381 -0
- package/vendor/converters/officeparser/dist/defaults.d.ts +41 -0
- package/vendor/converters/officeparser/dist/defaults.js +218 -0
- package/vendor/converters/officeparser/dist/generators/BaseGenerator.d.ts +107 -0
- package/vendor/converters/officeparser/dist/generators/BaseGenerator.js +248 -0
- package/vendor/converters/officeparser/dist/generators/ChunkingGenerator.d.ts +82 -0
- package/vendor/converters/officeparser/dist/generators/ChunkingGenerator.js +797 -0
- package/vendor/converters/officeparser/dist/generators/CsvGenerator.d.ts +38 -0
- package/vendor/converters/officeparser/dist/generators/CsvGenerator.js +245 -0
- package/vendor/converters/officeparser/dist/generators/EpubGenerator.d.ts +43 -0
- package/vendor/converters/officeparser/dist/generators/EpubGenerator.js +315 -0
- package/vendor/converters/officeparser/dist/generators/HtmlGenerator.d.ts +59 -0
- package/vendor/converters/officeparser/dist/generators/HtmlGenerator.js +1942 -0
- package/vendor/converters/officeparser/dist/generators/MarkdownGenerator.d.ts +96 -0
- package/vendor/converters/officeparser/dist/generators/MarkdownGenerator.js +1175 -0
- package/vendor/converters/officeparser/dist/generators/PdfGenerator.d.ts +22 -0
- package/vendor/converters/officeparser/dist/generators/PdfGenerator.js +194 -0
- package/vendor/converters/officeparser/dist/generators/RtfGenerator.d.ts +29 -0
- package/vendor/converters/officeparser/dist/generators/RtfGenerator.js +316 -0
- package/vendor/converters/officeparser/dist/generators/TextGenerator.d.ts +13 -0
- package/vendor/converters/officeparser/dist/generators/TextGenerator.js +201 -0
- package/vendor/converters/officeparser/dist/index.d.ts +60 -0
- package/vendor/converters/officeparser/dist/index.js +72 -0
- package/vendor/converters/officeparser/dist/index.mjs +18 -0
- package/vendor/converters/officeparser/dist/officeparser.browser.d.ts +2621 -0
- package/vendor/converters/officeparser/dist/officeparser.browser.iife.js +1336 -0
- package/vendor/converters/officeparser/dist/officeparser.browser.mjs +1335 -0
- package/vendor/converters/officeparser/dist/officeparser.browser.slim.d.ts +2621 -0
- package/vendor/converters/officeparser/dist/officeparser.browser.slim.iife.js +1336 -0
- package/vendor/converters/officeparser/dist/officeparser.browser.slim.mjs +1335 -0
- package/vendor/converters/officeparser/dist/parsers/CsvParser.d.ts +9 -0
- package/vendor/converters/officeparser/dist/parsers/CsvParser.js +115 -0
- package/vendor/converters/officeparser/dist/parsers/EpubParser.d.ts +8 -0
- package/vendor/converters/officeparser/dist/parsers/EpubParser.js +217 -0
- package/vendor/converters/officeparser/dist/parsers/ExcelParser.d.ts +32 -0
- package/vendor/converters/officeparser/dist/parsers/ExcelParser.js +736 -0
- package/vendor/converters/officeparser/dist/parsers/HtmlParser.d.ts +2 -0
- package/vendor/converters/officeparser/dist/parsers/HtmlParser.js +1287 -0
- package/vendor/converters/officeparser/dist/parsers/MarkdownParser.d.ts +2 -0
- package/vendor/converters/officeparser/dist/parsers/MarkdownParser.js +1272 -0
- package/vendor/converters/officeparser/dist/parsers/OpenOfficeParser.d.ts +31 -0
- package/vendor/converters/officeparser/dist/parsers/OpenOfficeParser.js +1819 -0
- package/vendor/converters/officeparser/dist/parsers/PdfParser.d.ts +67 -0
- package/vendor/converters/officeparser/dist/parsers/PdfParser.js +848 -0
- package/vendor/converters/officeparser/dist/parsers/PowerPointParser.d.ts +32 -0
- package/vendor/converters/officeparser/dist/parsers/PowerPointParser.js +950 -0
- package/vendor/converters/officeparser/dist/parsers/RtfParser.d.ts +187 -0
- package/vendor/converters/officeparser/dist/parsers/RtfParser.js +1801 -0
- package/vendor/converters/officeparser/dist/parsers/WordParser.d.ts +79 -0
- package/vendor/converters/officeparser/dist/parsers/WordParser.js +1177 -0
- package/vendor/converters/officeparser/dist/sbom.cdx.json +1763 -0
- package/vendor/converters/officeparser/dist/types.d.ts +2507 -0
- package/vendor/converters/officeparser/dist/types.js +107 -0
- package/vendor/converters/officeparser/dist/utils/astUtils.d.ts +16 -0
- package/vendor/converters/officeparser/dist/utils/astUtils.js +33 -0
- package/vendor/converters/officeparser/dist/utils/chartUtils.d.ts +6 -0
- package/vendor/converters/officeparser/dist/utils/chartUtils.js +257 -0
- package/vendor/converters/officeparser/dist/utils/configUtils.d.ts +44 -0
- package/vendor/converters/officeparser/dist/utils/configUtils.js +315 -0
- package/vendor/converters/officeparser/dist/utils/dateUtils.d.ts +17 -0
- package/vendor/converters/officeparser/dist/utils/dateUtils.js +69 -0
- package/vendor/converters/officeparser/dist/utils/envUtils.d.ts +29 -0
- package/vendor/converters/officeparser/dist/utils/envUtils.js +152 -0
- package/vendor/converters/officeparser/dist/utils/errorUtils.d.ts +72 -0
- package/vendor/converters/officeparser/dist/utils/errorUtils.js +245 -0
- package/vendor/converters/officeparser/dist/utils/imageUtils.d.ts +66 -0
- package/vendor/converters/officeparser/dist/utils/imageUtils.js +133 -0
- package/vendor/converters/officeparser/dist/utils/mathUtils.d.ts +42 -0
- package/vendor/converters/officeparser/dist/utils/mathUtils.js +385 -0
- package/vendor/converters/officeparser/dist/utils/moduleLoader.d.ts +18 -0
- package/vendor/converters/officeparser/dist/utils/moduleLoader.js +106 -0
- package/vendor/converters/officeparser/dist/utils/ocrUtils.d.ts +42 -0
- package/vendor/converters/officeparser/dist/utils/ocrUtils.js +428 -0
- package/vendor/converters/officeparser/dist/utils/sanitize.d.ts +148 -0
- package/vendor/converters/officeparser/dist/utils/sanitize.js +344 -0
- package/vendor/converters/officeparser/dist/utils/sheetUtils.d.ts +7 -0
- package/vendor/converters/officeparser/dist/utils/sheetUtils.js +35 -0
- package/vendor/converters/officeparser/dist/utils/styleMapper.d.ts +36 -0
- package/vendor/converters/officeparser/dist/utils/styleMapper.js +224 -0
- package/vendor/converters/officeparser/dist/utils/xmlUtils.d.ts +163 -0
- package/vendor/converters/officeparser/dist/utils/xmlUtils.js +461 -0
- package/vendor/converters/officeparser/dist/utils/zipUtils.d.ts +134 -0
- package/vendor/converters/officeparser/dist/utils/zipUtils.js +337 -0
- package/vendor/converters/officeparser/package.json +147 -0
- package/vendor/converters/upstream-manifest.json +124 -0
- package/vendor/dependency-fixes/README.md +77 -0
- package/vendor/dependency-fixes/vendor-converters.py +83 -0
- package/browser-extension/artifacts/gno-browser-clipper-v1.45.1.zip +0 -0
- package/browser-extension/artifacts/gno-browser-clipper-v1.45.1.zip.sha256 +0 -1
- package/browser-extension/dist/chunk-627emwpj.js +0 -75
- /package/browser-extension/dist/{chunk-ydfx5d7p.css → chunk-z74y8n8c.css} +0 -0
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Error Handling Utilities
|
|
4
|
+
*
|
|
5
|
+
* This module provides centralized error management for the OfficeParser library.
|
|
6
|
+
* It defines standard error types, messages, and handling logic to ensure
|
|
7
|
+
* consistent error reporting across all parsers and the main entry point.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.checkAbortSignal = exports.getAbortError = exports.logWarning = exports.getWrappedError = exports.getOfficeError = exports.getWarningMessage = void 0;
|
|
11
|
+
const types_js_1 = require("../types.js");
|
|
12
|
+
/** Error header prefix for all error messages */
|
|
13
|
+
const ERRORHEADER = "[OfficeParser]: ";
|
|
14
|
+
// `OfficeError` (the public shape callers catch) lives in types.ts alongside `OfficeIssue`.
|
|
15
|
+
// Every error built by getOfficeError is branded with its issue, which serves two purposes:
|
|
16
|
+
// consumers branch on `err.officeIssue.code` instead of matching message text, and
|
|
17
|
+
// getWrappedError recognizes an error it has already reported and prefixed, so it neither
|
|
18
|
+
// reports it twice nor prepends a second header.
|
|
19
|
+
/**
|
|
20
|
+
* Lookup table for error messages.
|
|
21
|
+
* Some entries are functions that take parameters to build dynamic messages.
|
|
22
|
+
*/
|
|
23
|
+
const ERROR_MESSAGES = {
|
|
24
|
+
[types_js_1.OfficeErrorType.EXTENSION_UNSUPPORTED]: (ext) => `Sorry, OfficeParser currently supports docx, pptx, xlsx, odt, odp, ods, pdf, rtf, md, html, csv, epub files only. Create a ticket in Issues on github to add support for ${ext} files. Stay tuned for further updates.`,
|
|
25
|
+
[types_js_1.OfficeErrorType.FORMAT_UNSUPPORTED]: (format) => `Sorry, OfficeGenerator does not support generating '${format}' files. Supported formats: json, text, md, html, csv, rtf, pdf, chunks, epub.`,
|
|
26
|
+
[types_js_1.OfficeErrorType.FILE_CORRUPTED]: (filepath) => `Your file ${filepath} seems to be corrupted. If you are sure it is fine, please create a ticket in Issues on github with the file to reproduce error.`,
|
|
27
|
+
[types_js_1.OfficeErrorType.FILE_DOES_NOT_EXIST]: (filepath) => `File ${filepath} could not be found! Check if the file exists or verify if the relative path to the file is correct from your terminal's location.`,
|
|
28
|
+
[types_js_1.OfficeErrorType.LOCATION_NOT_FOUND]: (location) => `Entered location ${location} is not reachable! Please make sure that the entered directory location exists. Check relative paths and reenter.`,
|
|
29
|
+
[types_js_1.OfficeErrorType.IMPROPER_ARGUMENTS]: `Improper arguments`,
|
|
30
|
+
[types_js_1.OfficeErrorType.IMPROPER_BUFFERS]: `Auto-detection of file type from buffer failed. This can happen if the format lacks magic bytes (like md, html, or csv) or if the detection library is incompatible with your Node.js version. Please provide the 'fileType' hint in your configuration (e.g., { fileType: 'docx' }) to proceed.`,
|
|
31
|
+
[types_js_1.OfficeErrorType.INVALID_INPUT]: `Invalid input type: Expected a Buffer or a valid file path`,
|
|
32
|
+
[types_js_1.OfficeErrorType.PDF_WORKER_MISSING]: `Missing PDF worker configuration. PDF parsing in browser environments requires a worker source. Please provide "pdfWorkerSrc" in your configuration.`,
|
|
33
|
+
[types_js_1.OfficeErrorType.FEATURE_NOT_SUPPORTED_IN_BROWSER]: (feature) => `'${feature}' is not supported in the browser. Browser users must pass file content as Buffer or ArrayBuffer directly.`,
|
|
34
|
+
[types_js_1.OfficeErrorType.INVALID_STYLE_MAPPING]: (mapping) => `Invalid style mapping string: ${mapping}`,
|
|
35
|
+
[types_js_1.OfficeErrorType.INVALID_SELECTOR]: (selector) => `Invalid selector: ${selector}`,
|
|
36
|
+
[types_js_1.OfficeErrorType.INVALID_OUTPUT_MAPPING]: (output) => `Invalid output mapping: ${output}`,
|
|
37
|
+
[types_js_1.OfficeErrorType.MISSING_EMBEDDING_FUNCTION]: `Semantic chunking requires an "embeddingFunction" to be provided in chunksConfig. This function must accept a string and return a Promise resolving to a number array (vector).`,
|
|
38
|
+
[types_js_1.OfficeErrorType.OPERATION_ABORTED]: `The operation was aborted.`,
|
|
39
|
+
[types_js_1.OfficeErrorType.ZIP_ENTRY_COUNT_LIMIT_EXCEEDED]: (limit) => `ZIP entry count exceeds limit (${limit})`,
|
|
40
|
+
[types_js_1.OfficeErrorType.ZIP_ENTRY_INVALID_SIZE]: `ZIP entry missing a valid declared size`,
|
|
41
|
+
[types_js_1.OfficeErrorType.ZIP_SIZE_LIMIT_EXCEEDED]: (limit) => `ZIP uncompressed size limit exceeded (${limit} bytes)`,
|
|
42
|
+
[types_js_1.OfficeErrorType.ZIP_NO_ENTRIES_FOUND]: `No readable entries found in ZIP data. The input is corrupt, truncated, or not a ZIP archive: every ZIP-based document format requires at least one entry.`,
|
|
43
|
+
[types_js_1.OfficeErrorType.ZIP_TRUNCATED]: `Malformed ZIP data: no End of Central Directory record was found at the end of the input. Either the file was cut off during download or transfer, or extra data follows the archive; in both cases the entries recovered from it cannot be trusted to be the whole document.`,
|
|
44
|
+
[types_js_1.OfficeErrorType.REQUIRED_PART_MISSING]: (info) => `Your ${info.fileType} file is a readable ZIP archive but is missing its required '${info.part}' part, so it cannot be a valid ${info.fileType} document. The file is corrupt, incomplete, or mislabeled. If you are sure it is fine, please create a ticket in Issues on github with the file to reproduce the error.`,
|
|
45
|
+
[types_js_1.OfficeErrorType.MAX_NESTING_DEPTH_EXCEEDED]: `Document nesting depth exceeded the safe limit (possible denial-of-service input)`,
|
|
46
|
+
[types_js_1.OfficeErrorType.EMBEDDING_TIMEOUT]: (timeout) => `Embedding call timed out after ${timeout}ms`
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Lookup table for warning messages.
|
|
50
|
+
*/
|
|
51
|
+
const WARNING_MESSAGES = {
|
|
52
|
+
[types_js_1.OfficeWarningType.PERFORMANCE_TIP]: (tip) => `⚡️ Performance Tip: ${tip}`,
|
|
53
|
+
[types_js_1.OfficeWarningType.OCR_FAILED]: (name) => `OCR failed for ${name}:`,
|
|
54
|
+
[types_js_1.OfficeWarningType.CHART_DATA_EXTRACTION_FAILED]: (path) => `Failed to extract chart data from ${path}:`,
|
|
55
|
+
[types_js_1.OfficeWarningType.PDF_WORKER_FALLBACK]: `Could not auto-resolve local worker path, falling back to CDN:`,
|
|
56
|
+
[types_js_1.OfficeWarningType.ATTACHMENT_EXTRACTION_FAILED]: `Error extracting embedded attachments:`,
|
|
57
|
+
[types_js_1.OfficeWarningType.PAGE_LOAD_FAILED]: (page) => `Error loading page ${page}:`,
|
|
58
|
+
[types_js_1.OfficeWarningType.DEPENDENCY_LOAD_FAILED]: (dep) => `Failed to load dependency ${dep}:`,
|
|
59
|
+
[types_js_1.OfficeWarningType.IMAGE_EXTRACTION_FAILED]: (context) => `Error extracting images ${context}:`,
|
|
60
|
+
[types_js_1.OfficeWarningType.ANNOTATION_EXTRACTION_FAILED]: (page) => `Error extracting annotations from page ${page}:`,
|
|
61
|
+
[types_js_1.OfficeWarningType.IMAGE_PROCESSING_FAILED]: `Failed to extract from ImageBitmap:`,
|
|
62
|
+
[types_js_1.OfficeWarningType.BROWSER_GENERATION_LIMITATION]: (msg) => msg,
|
|
63
|
+
[types_js_1.OfficeWarningType.SHEET_RANGE_NOT_FOUND]: (range) => `No sheets found matching the range: ${range}`,
|
|
64
|
+
[types_js_1.OfficeWarningType.BUFFER_TYPE_MISMATCH]: (info) => `File content type mismatch: Detected '${info.detected}' but expected/provided '${info.expected}'. Parsing will proceed with '${info.expected}' as requested.`,
|
|
65
|
+
[types_js_1.OfficeWarningType.FILE_TYPE_DETECTION_FAILED]: `Auto-detection of file type failed. This can happen on older Node.js versions with modern file-type versions. Please provide the 'fileType' hint in the configuration if parsing fails.`,
|
|
66
|
+
[types_js_1.OfficeWarningType.EMPTY_CHUNK_GENERATED]: (strategy) => `No chunks generated for document. Check if the document content is compatible with the '${strategy}' strategy.`,
|
|
67
|
+
[types_js_1.OfficeWarningType.WHITESPACE_NODE_SKIPPED]: (nodeType) => `Skipped whitespace-only node of type: ${nodeType}`,
|
|
68
|
+
[types_js_1.OfficeWarningType.TABLE_CELL_LIMIT_EXCEEDED]: (limit) => `Table cell limit (${limit}) reached while expanding repeated ODF cells/rows; the remaining cells were not materialized. A few hundred bytes of XML can request an unbounded number of cells via table:number-columns-repeated / table:number-rows-repeated, so this is capped. Raise decompressionLimits.maxTableCells if your documents legitimately exceed it.`,
|
|
69
|
+
[types_js_1.OfficeWarningType.INVALID_CONTAINER_WIDTH]: (val) => `Invalid HTML containerWidth: ${JSON.stringify(val)}. Falling back to "auto". Width must be a positive number, a valid CSS length string (e.g., "900px", "100%", "50vw"), or "auto".`,
|
|
70
|
+
[types_js_1.OfficeWarningType.METADATA_NOT_REPRESENTABLE]: (info) => `Custom metadata ${info.keys.map(k => `'${k}'`).join(', ')} could not be written to ${info.format} output: the format has a fixed metadata vocabulary with no place for caller-defined keys. The named metadata fields (title, author, etc.) were still applied.`,
|
|
71
|
+
[types_js_1.OfficeWarningType.NO_WORKSHEETS_FOUND]: `Workbook contains no worksheet parts (xl/worksheets/). If the workbook holds only chartsheets this is expected and there is simply no cell text to extract; otherwise the file may be incomplete.`,
|
|
72
|
+
[types_js_1.OfficeWarningType.NO_SLIDES_FOUND]: `Presentation contains no slides (ppt/slides/). A legitimately empty presentation produces this too, but if you expected content the file may be incomplete.`,
|
|
73
|
+
[types_js_1.OfficeWarningType.INVALID_STYLE_MAP_TAG]: (tag) => `styleMap output.tag ${JSON.stringify(tag)} is not an allowed element name and was ignored; the node's default tag was used instead. A tag name is written into both the opening and closing tag, so only a known-safe set of block, heading and inline elements is accepted.`
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Creates a formatted warning message for a specific warning type.
|
|
77
|
+
*
|
|
78
|
+
* @param type - The type of warning
|
|
79
|
+
* @param info - Optional additional information
|
|
80
|
+
* @returns The formatted warning message string
|
|
81
|
+
*/
|
|
82
|
+
const getWarningMessage = (type, info) => {
|
|
83
|
+
const msg = WARNING_MESSAGES[type];
|
|
84
|
+
const message = typeof msg === 'function' ? msg(info) : msg;
|
|
85
|
+
return message;
|
|
86
|
+
};
|
|
87
|
+
exports.getWarningMessage = getWarningMessage;
|
|
88
|
+
/**
|
|
89
|
+
* Creates a formatted error message for a specific error type.
|
|
90
|
+
*
|
|
91
|
+
* @param type - The type of error
|
|
92
|
+
* @param info - Optional additional information (e.g., filepath, extension)
|
|
93
|
+
* @returns The formatted error message string
|
|
94
|
+
*/
|
|
95
|
+
const createOfficeError = (type, info) => {
|
|
96
|
+
const msg = ERROR_MESSAGES[type];
|
|
97
|
+
const message = typeof msg === 'function' ? msg(info) : msg;
|
|
98
|
+
return message;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Core reporting logic for all issues.
|
|
102
|
+
* Ensures consistent logging and callback execution.
|
|
103
|
+
*/
|
|
104
|
+
const reportIssue = (issue, config) => {
|
|
105
|
+
if (config?.onWarning) {
|
|
106
|
+
config.onWarning(issue);
|
|
107
|
+
}
|
|
108
|
+
else if (!config || config.outputErrorToConsole) {
|
|
109
|
+
const formatted = ERRORHEADER + issue.message;
|
|
110
|
+
if (issue.type === 'error') {
|
|
111
|
+
console.error(formatted, issue.details || '');
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
console.warn(formatted, issue.details || '');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Creates, optionally logs to console, and returns a formatted OfficeParser error.
|
|
120
|
+
*
|
|
121
|
+
* @param type - The type of error
|
|
122
|
+
* @param config - Optional parser configuration (checks outputErrorToConsole)
|
|
123
|
+
* @param info - Optional additional information
|
|
124
|
+
* @returns The Error object to be thrown
|
|
125
|
+
*/
|
|
126
|
+
const getOfficeError = (type, config, info) => {
|
|
127
|
+
const message = createOfficeError(type, info);
|
|
128
|
+
const issue = {
|
|
129
|
+
type: 'error',
|
|
130
|
+
code: type,
|
|
131
|
+
message,
|
|
132
|
+
details: info
|
|
133
|
+
};
|
|
134
|
+
reportIssue(issue, config);
|
|
135
|
+
const error = new Error(ERRORHEADER + message);
|
|
136
|
+
// Brand the error with the issue that produced it so getWrappedError can tell an
|
|
137
|
+
// already-reported, already-prefixed OfficeParser error from a raw third-party one.
|
|
138
|
+
error.officeIssue = issue;
|
|
139
|
+
return error;
|
|
140
|
+
};
|
|
141
|
+
exports.getOfficeError = getOfficeError;
|
|
142
|
+
/**
|
|
143
|
+
* Wraps an existing error with OfficeParser context and performs corruption detection.
|
|
144
|
+
* Optionally logs the error to console.
|
|
145
|
+
*
|
|
146
|
+
* An error already built by {@link getOfficeError} is returned untouched: it carries an
|
|
147
|
+
* `officeIssue`, meaning it has been reported once and already bears the `[OfficeParser]: `
|
|
148
|
+
* header. Re-wrapping it would report the same issue a second time, prepend a second header,
|
|
149
|
+
* and flatten its specific error code to `FILE_CORRUPTED`. This is a marker check on the error
|
|
150
|
+
* object rather than a test against its message text, so it stays independent of wording.
|
|
151
|
+
*
|
|
152
|
+
* **Important**: Do NOT pass AbortErrors to this function. AbortErrors (err.name === 'AbortError')
|
|
153
|
+
* represent deliberate user cancellation and must be re-thrown as-is from the catch block so that
|
|
154
|
+
* callers can reliably detect them via `err.name === 'AbortError'` or `err instanceof DOMException`.
|
|
155
|
+
* This function always returns a plain `new Error(...)`, which would strip the AbortError identity.
|
|
156
|
+
*
|
|
157
|
+
* @param error - The original error object
|
|
158
|
+
* @param config - Parser configuration
|
|
159
|
+
* @param filePath - Optional file path for context
|
|
160
|
+
* @returns The wrapped Error object to be thrown
|
|
161
|
+
*/
|
|
162
|
+
const getWrappedError = (error, config, filePath) => {
|
|
163
|
+
if (error?.officeIssue)
|
|
164
|
+
return error;
|
|
165
|
+
let message = error.message || error;
|
|
166
|
+
let code = types_js_1.OfficeErrorType.FILE_CORRUPTED; // Default for wrapped errors
|
|
167
|
+
// Detect file corruption from common library error messages
|
|
168
|
+
if (filePath && (message.includes('end of central directory record') ||
|
|
169
|
+
message.includes('invalid XML') ||
|
|
170
|
+
message.includes('Failed to open zip file') ||
|
|
171
|
+
message.includes('invalid distance too far back'))) {
|
|
172
|
+
message = createOfficeError(types_js_1.OfficeErrorType.FILE_CORRUPTED, filePath);
|
|
173
|
+
}
|
|
174
|
+
const issue = {
|
|
175
|
+
type: 'error',
|
|
176
|
+
code: types_js_1.OfficeErrorType.FILE_CORRUPTED,
|
|
177
|
+
message,
|
|
178
|
+
details: filePath ? { filePath, originalError: error } : error
|
|
179
|
+
};
|
|
180
|
+
reportIssue(issue, config);
|
|
181
|
+
return new Error(ERRORHEADER + message);
|
|
182
|
+
};
|
|
183
|
+
exports.getWrappedError = getWrappedError;
|
|
184
|
+
/**
|
|
185
|
+
* Centralized logging utility for non-fatal warnings or issues.
|
|
186
|
+
* Routes messages to config.onWarning if provided, or console.warn/error
|
|
187
|
+
* if config.outputErrorToConsole is true.
|
|
188
|
+
*
|
|
189
|
+
* @param messageOrType - The warning message or warning type
|
|
190
|
+
* @param config - Optional parser configuration
|
|
191
|
+
* @param info - Optional additional information for dynamic messages or context
|
|
192
|
+
* @param error - Optional original error object
|
|
193
|
+
*/
|
|
194
|
+
const logWarning = (type, config, info, error) => {
|
|
195
|
+
let message;
|
|
196
|
+
let details = info;
|
|
197
|
+
const msg = WARNING_MESSAGES[type];
|
|
198
|
+
if (typeof msg === 'function') {
|
|
199
|
+
message = msg(info);
|
|
200
|
+
details = error || info;
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
message = msg;
|
|
204
|
+
if (info instanceof Error && !error) {
|
|
205
|
+
details = info;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const issue = {
|
|
209
|
+
type: 'warning',
|
|
210
|
+
code: type,
|
|
211
|
+
message,
|
|
212
|
+
details
|
|
213
|
+
};
|
|
214
|
+
reportIssue(issue, config);
|
|
215
|
+
};
|
|
216
|
+
exports.logWarning = logWarning;
|
|
217
|
+
/**
|
|
218
|
+
* Creates and returns a standard AbortError (DOMException if available).
|
|
219
|
+
* Used when the user signals cancellation of the parser operation.
|
|
220
|
+
*
|
|
221
|
+
* @returns Error object representing the abort action
|
|
222
|
+
*/
|
|
223
|
+
const getAbortError = () => {
|
|
224
|
+
const message = ERROR_MESSAGES[types_js_1.OfficeErrorType.OPERATION_ABORTED];
|
|
225
|
+
if (typeof DOMException !== 'undefined') {
|
|
226
|
+
return new DOMException(message, 'AbortError');
|
|
227
|
+
}
|
|
228
|
+
const err = new Error(message);
|
|
229
|
+
err.name = 'AbortError';
|
|
230
|
+
return err;
|
|
231
|
+
};
|
|
232
|
+
exports.getAbortError = getAbortError;
|
|
233
|
+
/**
|
|
234
|
+
* Checks the provided AbortSignal and throws an AbortError if it was aborted.
|
|
235
|
+
* Helps cleanly interrupt loops and asynchronous phases of parsing.
|
|
236
|
+
*
|
|
237
|
+
* @param signal - Optional AbortSignal to inspect
|
|
238
|
+
* @throws {DOMException} If the signal has been aborted
|
|
239
|
+
*/
|
|
240
|
+
const checkAbortSignal = (signal) => {
|
|
241
|
+
if (signal?.aborted) {
|
|
242
|
+
throw (0, exports.getAbortError)();
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
exports.checkAbortSignal = checkAbortSignal;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Image Processing Utilities
|
|
3
|
+
*
|
|
4
|
+
* Provides helper functions for working with image attachments extracted from office documents.
|
|
5
|
+
* Handles MIME type conversions, file extension mapping, and attachment object creation.
|
|
6
|
+
*
|
|
7
|
+
* @module imageUtils
|
|
8
|
+
*/
|
|
9
|
+
import { OfficeAttachment } from '../types';
|
|
10
|
+
/**
|
|
11
|
+
* Converts a file extension to its corresponding MIME type.
|
|
12
|
+
*
|
|
13
|
+
* Used when creating attachments to determine the MIME type from a filename.
|
|
14
|
+
* The extension check is case-insensitive.
|
|
15
|
+
*
|
|
16
|
+
* @param ext - The file extension (with or without a dot, e.g., 'png', '.png')
|
|
17
|
+
* @returns The corresponding MIME type string
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* getMimeFromExtension('png'); // Returns 'image/png'
|
|
21
|
+
* getMimeFromExtension('JPG'); // Returns 'image/jpeg' (case-insensitive)
|
|
22
|
+
* getMimeFromExtension('unknown'); // Returns 'application/octet-stream'
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare const getMimeFromExtension: (ext: string) => string;
|
|
26
|
+
/**
|
|
27
|
+
* Detects the MIME type from file magic bytes (file signature).
|
|
28
|
+
*
|
|
29
|
+
* This is useful for files with incorrect or missing extensions (like .tmp files).
|
|
30
|
+
* Inspects the first few bytes of the file to determine the actual format.
|
|
31
|
+
*
|
|
32
|
+
* @param buffer - The file content as a Buffer
|
|
33
|
+
* @returns The detected MIME type, or undefined if not recognized
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const pngBuffer = fs.readFileSync('image.tmp');
|
|
37
|
+
* getMimeFromBytes(pngBuffer); // Returns 'image/png' if it's a PNG file
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare const getMimeFromBytes: (buffer: Buffer) => string | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Creates an OfficeAttachment object from image data.
|
|
43
|
+
*
|
|
44
|
+
* This is a convenience function that:
|
|
45
|
+
* 1. Extracts the file extension from the filename
|
|
46
|
+
* 2. Determines the MIME type from the extension
|
|
47
|
+
* 3. Encodes the image buffer as Base64
|
|
48
|
+
* 4. Constructs a properly formatted OfficeAttachment object
|
|
49
|
+
*
|
|
50
|
+
* @param name - The filename of the image (e.g., 'image1.png', 'chart.jpg')
|
|
51
|
+
* @param content - The image data as a Node.js Buffer
|
|
52
|
+
* @returns An OfficeAttachment object ready to be added to the attachments array
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* const imageBuffer = fs.readFileSync('photo.png');
|
|
57
|
+
* const attachment = createAttachment('photo.png', imageBuffer);
|
|
58
|
+
*
|
|
59
|
+
* console.log(attachment.type); // 'image'
|
|
60
|
+
* console.log(attachment.mimeType); // 'image/png'
|
|
61
|
+
* console.log(attachment.name); // 'photo.png'
|
|
62
|
+
* console.log(attachment.extension); // 'png'
|
|
63
|
+
* console.log(attachment.data); // 'iVBORw0KGgoAAAANSUhEUgAA...' (Base64)
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare const createAttachment: (name: string, content: Buffer) => OfficeAttachment;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Image Processing Utilities
|
|
4
|
+
*
|
|
5
|
+
* Provides helper functions for working with image attachments extracted from office documents.
|
|
6
|
+
* Handles MIME type conversions, file extension mapping, and attachment object creation.
|
|
7
|
+
*
|
|
8
|
+
* @module imageUtils
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.createAttachment = exports.getMimeFromBytes = exports.getMimeFromExtension = void 0;
|
|
12
|
+
/**
|
|
13
|
+
* Converts a file extension to its corresponding MIME type.
|
|
14
|
+
*
|
|
15
|
+
* Used when creating attachments to determine the MIME type from a filename.
|
|
16
|
+
* The extension check is case-insensitive.
|
|
17
|
+
*
|
|
18
|
+
* @param ext - The file extension (with or without a dot, e.g., 'png', '.png')
|
|
19
|
+
* @returns The corresponding MIME type string
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* getMimeFromExtension('png'); // Returns 'image/png'
|
|
23
|
+
* getMimeFromExtension('JPG'); // Returns 'image/jpeg' (case-insensitive)
|
|
24
|
+
* getMimeFromExtension('unknown'); // Returns 'application/octet-stream'
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
const getMimeFromExtension = (ext) => {
|
|
28
|
+
switch (ext.toLowerCase()) {
|
|
29
|
+
case 'jpg':
|
|
30
|
+
case 'jpeg': return 'image/jpeg';
|
|
31
|
+
case 'png': return 'image/png';
|
|
32
|
+
case 'gif': return 'image/gif';
|
|
33
|
+
case 'bmp': return 'image/bmp';
|
|
34
|
+
case 'tiff': return 'image/tiff';
|
|
35
|
+
case 'webp': return 'image/webp';
|
|
36
|
+
default: return 'application/octet-stream'; // Generic binary MIME type
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
exports.getMimeFromExtension = getMimeFromExtension;
|
|
40
|
+
/**
|
|
41
|
+
* Detects the MIME type from file magic bytes (file signature).
|
|
42
|
+
*
|
|
43
|
+
* This is useful for files with incorrect or missing extensions (like .tmp files).
|
|
44
|
+
* Inspects the first few bytes of the file to determine the actual format.
|
|
45
|
+
*
|
|
46
|
+
* @param buffer - The file content as a Buffer
|
|
47
|
+
* @returns The detected MIME type, or undefined if not recognized
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const pngBuffer = fs.readFileSync('image.tmp');
|
|
51
|
+
* getMimeFromBytes(pngBuffer); // Returns 'image/png' if it's a PNG file
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
const getMimeFromBytes = (buffer) => {
|
|
55
|
+
if (buffer.length < 4)
|
|
56
|
+
return undefined;
|
|
57
|
+
// PNG: 89 50 4E 47 (0x89 "PNG")
|
|
58
|
+
if (buffer[0] === 0x89 && buffer[1] === 0x50 && buffer[2] === 0x4E && buffer[3] === 0x47) {
|
|
59
|
+
return 'image/png';
|
|
60
|
+
}
|
|
61
|
+
// JPEG: FF D8 FF
|
|
62
|
+
if (buffer[0] === 0xFF && buffer[1] === 0xD8 && buffer[2] === 0xFF) {
|
|
63
|
+
return 'image/jpeg';
|
|
64
|
+
}
|
|
65
|
+
// GIF: 47 49 46 38 ("GIF8")
|
|
66
|
+
if (buffer[0] === 0x47 && buffer[1] === 0x49 && buffer[2] === 0x46 && buffer[3] === 0x38) {
|
|
67
|
+
return 'image/gif';
|
|
68
|
+
}
|
|
69
|
+
// BMP: 42 4D ("BM")
|
|
70
|
+
if (buffer[0] === 0x42 && buffer[1] === 0x4D) {
|
|
71
|
+
return 'image/bmp';
|
|
72
|
+
}
|
|
73
|
+
// TIFF: 49 49 2A 00 (little endian) or 4D 4D 00 2A (big endian)
|
|
74
|
+
if ((buffer[0] === 0x49 && buffer[1] === 0x49 && buffer[2] === 0x2A && buffer[3] === 0x00) ||
|
|
75
|
+
(buffer[0] === 0x4D && buffer[1] === 0x4D && buffer[2] === 0x00 && buffer[3] === 0x2A)) {
|
|
76
|
+
return 'image/tiff';
|
|
77
|
+
}
|
|
78
|
+
// WebP: 52 49 46 46 ... 57 45 42 50 ("RIFF" ... "WEBP")
|
|
79
|
+
if (buffer.length >= 12 &&
|
|
80
|
+
buffer[0] === 0x52 && buffer[1] === 0x49 && buffer[2] === 0x46 && buffer[3] === 0x46 &&
|
|
81
|
+
buffer[8] === 0x57 && buffer[9] === 0x45 && buffer[10] === 0x42 && buffer[11] === 0x50) {
|
|
82
|
+
return 'image/webp';
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
};
|
|
86
|
+
exports.getMimeFromBytes = getMimeFromBytes;
|
|
87
|
+
/**
|
|
88
|
+
* Creates an OfficeAttachment object from image data.
|
|
89
|
+
*
|
|
90
|
+
* This is a convenience function that:
|
|
91
|
+
* 1. Extracts the file extension from the filename
|
|
92
|
+
* 2. Determines the MIME type from the extension
|
|
93
|
+
* 3. Encodes the image buffer as Base64
|
|
94
|
+
* 4. Constructs a properly formatted OfficeAttachment object
|
|
95
|
+
*
|
|
96
|
+
* @param name - The filename of the image (e.g., 'image1.png', 'chart.jpg')
|
|
97
|
+
* @param content - The image data as a Node.js Buffer
|
|
98
|
+
* @returns An OfficeAttachment object ready to be added to the attachments array
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* const imageBuffer = fs.readFileSync('photo.png');
|
|
103
|
+
* const attachment = createAttachment('photo.png', imageBuffer);
|
|
104
|
+
*
|
|
105
|
+
* console.log(attachment.type); // 'image'
|
|
106
|
+
* console.log(attachment.mimeType); // 'image/png'
|
|
107
|
+
* console.log(attachment.name); // 'photo.png'
|
|
108
|
+
* console.log(attachment.extension); // 'png'
|
|
109
|
+
* console.log(attachment.data); // 'iVBORw0KGgoAAAANSUhEUgAA...' (Base64)
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
const createAttachment = (name, content) => {
|
|
113
|
+
// Step 1: Extract file extension from the filename
|
|
114
|
+
// Example: 'image1.png' -> 'png'
|
|
115
|
+
const ext = name.split('.').pop() || '';
|
|
116
|
+
// Step 2: Try to detect MIME type from magic bytes first (more reliable)
|
|
117
|
+
// This handles cases like .tmp files or mismatched extensions
|
|
118
|
+
let mime = (0, exports.getMimeFromBytes)(content);
|
|
119
|
+
// Step 3: Fallback to extension if magic bytes couldn't detect it
|
|
120
|
+
// Useful for SVG or formats not covered by getMimeFromBytes
|
|
121
|
+
if (!mime) {
|
|
122
|
+
mime = (0, exports.getMimeFromExtension)(ext);
|
|
123
|
+
}
|
|
124
|
+
// Step 4: Create and return the attachment object
|
|
125
|
+
return {
|
|
126
|
+
type: 'image', // All attachments created here are images
|
|
127
|
+
mimeType: mime, // Cast to our supported MIME types
|
|
128
|
+
data: content.toString('base64'), // Convert buffer to Base64 string
|
|
129
|
+
name: name, // Original filename
|
|
130
|
+
extension: ext // File extension for reference
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
exports.createAttachment = createAttachment;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts an OMML subtree (`<m:oMath>` or any node within one) to LaTeX.
|
|
3
|
+
*
|
|
4
|
+
* Covers the constructs that actually appear in office documents: fractions, sub/superscripts,
|
|
5
|
+
* delimiters, radicals, n-ary operators, functions, accents, bars, boxes and matrices. Anything
|
|
6
|
+
* unrecognized falls through to concatenating its children, which is the old behaviour and the
|
|
7
|
+
* right degradation for a construct that carries no grouping of its own.
|
|
8
|
+
*/
|
|
9
|
+
export declare const ommlToLatex: (node: Node, depth?: number) => string;
|
|
10
|
+
/**
|
|
11
|
+
* The subset of a parsed element every MathML source here can present.
|
|
12
|
+
*
|
|
13
|
+
* MathML reaches this module from two different tree types - the XML DOM (ODF embedded objects)
|
|
14
|
+
* and `HtmlParser`'s own lightweight node (HTML, and EPUB via its spine items) - so the converter
|
|
15
|
+
* is written against this shape and each caller adapts into it. That keeps one implementation of
|
|
16
|
+
* the conversion rather than one per tree type, which is how HTML came to have no MathML support
|
|
17
|
+
* at all while ODF did.
|
|
18
|
+
*/
|
|
19
|
+
export interface MathNode {
|
|
20
|
+
/** Tag name, namespace prefix included or not; `undefined` marks a text node. */
|
|
21
|
+
tagName?: string;
|
|
22
|
+
attributes?: Record<string, string>;
|
|
23
|
+
/** Literal text, for text nodes and for leaf tokens. */
|
|
24
|
+
text?: string;
|
|
25
|
+
children: MathNode[];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Converts a MathML subtree to LaTeX.
|
|
29
|
+
*
|
|
30
|
+
* When the document carries a TeX annotation (`<annotation encoding="application/x-tex">`), that
|
|
31
|
+
* is the author's own source and is used verbatim in preference to anything reconstructed here.
|
|
32
|
+
* ODF's `<annotation encoding="StarMath 5.0">` is deliberately not used - StarMath is not LaTeX,
|
|
33
|
+
* and emitting it would put a second notation back into the output this module exists to unify.
|
|
34
|
+
*/
|
|
35
|
+
export declare const mathmlTreeToLatex: (node: MathNode, depth?: number) => string;
|
|
36
|
+
/** Converts a MathML subtree held in an XML DOM (ODF embedded objects) to LaTeX. */
|
|
37
|
+
export declare const mathmlToLatex: (node: Node, depth?: number) => string;
|
|
38
|
+
/**
|
|
39
|
+
* True when a converted equation carries nothing worth emitting, so callers can drop the node
|
|
40
|
+
* instead of pushing an empty `$$` into the output.
|
|
41
|
+
*/
|
|
42
|
+
export declare const isEmptyMath: (latex: string) => boolean;
|