officeparser 7.2.0 → 7.2.1

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.
@@ -19,7 +19,7 @@ async function loadNodeEsmModule(specifier) {
19
19
  // This is especially important in Node 18 for sub-paths of packages.
20
20
  try {
21
21
  const { pathToFileURL } = await import('url');
22
- // @ts-ignore - require.resolve is available in Node.js
22
+ // @ts-ignore - require.resolve is available in Node.js CJS context
23
23
  const absolutePath = require.resolve(specifier);
24
24
  const fileUrl = pathToFileURL(absolutePath).href;
25
25
  return import(fileUrl);
@@ -29,6 +29,16 @@ async function loadNodeEsmModule(specifier) {
29
29
  return import(specifier);
30
30
  }
31
31
  }
32
+ /**
33
+ * Returns true if require.resolve is available in this runtime context.
34
+ * It is NOT available in native ESM (e.g. the .mjs wrapper) or browser environments.
35
+ * Checking this guards against a ReferenceError that would silently trigger
36
+ * the bundled fallback path for non-bundled ESM consumers.
37
+ */
38
+ function isRequireAvailable() {
39
+ // @ts-ignore - require may not exist in ESM context
40
+ return typeof require !== 'undefined' && typeof require.resolve === 'function';
41
+ }
32
42
  /**
33
43
  * Specialized loader for file-type
34
44
  */
@@ -36,10 +46,19 @@ async function loadFileType() {
36
46
  if (!envUtils_js_1.isBrowser) {
37
47
  // Ensure environment polyfills for Node.js 18 support
38
48
  (0, envUtils_js_1.ensureEnvPolyfills)();
39
- // Node.js path: Use dynamic import wrapper for CJS compatibility
40
- return loadNodeEsmModule('file-type');
49
+ if (isRequireAvailable()) {
50
+ // Check if node_modules is available at runtime.
51
+ // @ts-ignore - require.resolve is available in Node.js CJS context
52
+ require.resolve(String('file-type'));
53
+ // node_modules present: use the path-resolved loader for Node 18 ESM compatibility
54
+ return loadNodeEsmModule('file-type');
55
+ }
41
56
  }
42
- // Browser path: standard dynamic import() is handled by bundlers (e.g. esbuild/Vite)
57
+ // Covers three cases with one return:
58
+ // 1. Node.js standalone/bundled (SEA or bundled CJS): bundler inlines the module at build time.
59
+ // 2. Node.js native ESM (no require available): runtime resolves the bare specifier.
60
+ // 3. Browser: bundler (esbuild/Vite) handles the static-looking dynamic import().
61
+ // Note: bypasses the Node 18 sub-path ESM fix in loadNodeEsmModule, but bundlers handle it.
43
62
  return import('file-type');
44
63
  }
45
64
  /**
@@ -49,14 +68,39 @@ async function loadPdfJs() {
49
68
  if (!envUtils_js_1.isBrowser) {
50
69
  // Ensure environment polyfills for Node.js 18 support
51
70
  (0, envUtils_js_1.ensureEnvPolyfills)();
52
- // Node.js environment: require legacy build for stability with ESM-only main
53
- try {
54
- return await loadNodeEsmModule('pdfjs-dist/legacy/build/pdf.mjs');
55
- }
56
- catch {
57
- return await loadNodeEsmModule('pdfjs-dist');
71
+ if (isRequireAvailable()) {
72
+ // @ts-ignore - require.resolve is available in Node.js CJS context
73
+ const pkgExists = (() => { try {
74
+ require.resolve(String('pdfjs-dist'));
75
+ return true;
76
+ }
77
+ catch {
78
+ return false;
79
+ } })();
80
+ if (pkgExists) {
81
+ // node_modules present: try the legacy build path first for stability with
82
+ // ESM-only main, then fall back to the package root.
83
+ // Errors from these loaders are NOT swallowed into the bundled-fallback path.
84
+ try {
85
+ return await loadNodeEsmModule('pdfjs-dist/legacy/build/pdf.mjs');
86
+ }
87
+ catch {
88
+ return await loadNodeEsmModule('pdfjs-dist');
89
+ }
90
+ }
58
91
  }
92
+ // Standalone/bundled fallback for Node.js (SEA, bundled CJS without node_modules, or native ESM context):
93
+ // Load both PDF.js and its worker, and register the worker on globalThis to enable the offline fake-worker.
94
+ // @ts-ignore - Ignore type check for local .mjs files in node_modules/packaging
95
+ const [pdfjs, pdfjsWorker] = await Promise.all([
96
+ // @ts-ignore - mjs imports may not have types
97
+ import('pdfjs-dist/legacy/build/pdf.mjs'),
98
+ // @ts-ignore - mjs imports may not have types
99
+ import('pdfjs-dist/legacy/build/pdf.worker.mjs')
100
+ ]);
101
+ globalThis.pdfjsWorker = pdfjsWorker;
102
+ return pdfjs;
59
103
  }
60
- // Browser environment: esbuild handles standard static-looking dynamic import()
104
+ // Browser environment: bundler (esbuild/Vite) handles the standard dynamic import()
61
105
  return import('pdfjs-dist');
62
106
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "officeparser",
3
- "version": "7.2.0",
3
+ "version": "7.2.1",
4
4
  "description": "A robust, strictly-typed Node.js and Browser library for parsing office files (.docx, .pptx, .xlsx, .odt, .odp, .ods, .pdf, .rtf, .csv, .md, .html) and generating high-fidelity outputs in Markdown, HTML, CSV, RTF, and RAG-focused chunks.",
5
5
  "funding": "https://github.com/sponsors/harshankur",
6
6
  "main": "dist/index.js",
@@ -31,16 +31,18 @@
31
31
  "sync:versions": "node scripts/sync-pdfjs-versions.js",
32
32
  "sync:docs": "mkdir -p docs/dist && cp dist/officeparser.browser.iife.js docs/dist/ && cp dist/officeparser.browser.mjs docs/dist/ && mkdir -p docs/test/files && cp test/files/* docs/test/files/",
33
33
  "lint": "eslint src",
34
- "test": "npm run lint && npm run test:clean && npm run build && npm run test:license && npm run test:artifacts && npm run test:parser && npm run test:generator",
34
+ "test": "npm run lint && npm run test:clean && npm run build && npm run test:license && npm run test:artifacts && npm run test:parser && npm run test:generator && npm run test:cli",
35
35
  "test:baseline": "npm run test:parser:baseline && npm run test:generator:baseline",
36
36
  "test:parser": "npx tsx test/parser/testOfficeParser.ts",
37
37
  "test:parser:baseline": "npx tsx test/parser/testOfficeParser.ts baseline",
38
38
  "test:generator": "npx tsx test/generator/testOfficeGenerator.ts",
39
39
  "test:generator:baseline": "npx tsx test/generator/testOfficeGenerator.ts baseline",
40
40
  "test:artifacts": "npx tsx test/testShippingArtifacts.ts",
41
+ "test:cli": "npx tsx test/cli/testCli.ts",
41
42
  "test:visualizer": "node test/testVisualizer.js",
43
+ "test:integration": "node test/testIntegration.js",
42
44
  "test:license": "npm run sbom && node scripts/validate-licenses.js",
43
- "test:clean": "rm -rf test/results test/generator/results test/generator/output test/parser/results test/parser/output",
45
+ "test:clean": "rm -rf test/results test/generator/results test/generator/output test/parser/results test/parser/output test/cli/results",
44
46
  "clean": "rm -rf dist && npm run test:clean",
45
47
  "sbom": "npx --yes @cyclonedx/cyclonedx-npm --output-format json --output-file dist/sbom.cdx.json --omit dev",
46
48
  "prepublishOnly": "npm run build",
@@ -123,6 +125,7 @@
123
125
  "esbuild-plugins-node-modules-polyfill": "^1.8.1",
124
126
  "eslint": "^10.3.0",
125
127
  "husky": "^9.1.7",
128
+ "postject": "^1.0.0-alpha.6",
126
129
  "process": "^0.11.10",
127
130
  "puppeteer": "^22.15.0",
128
131
  "tsx": "^4.21.0",