Skip to content

Commit 3c43745

Browse files
committed
Refactor worker module structure and API
Moved worker-related files into a dedicated bin/worker directory, updated API to provide both worker path and base64 data URL, and improved type definitions. Updated imports and exports to support both CommonJS and ESM, and adjusted Vite config and package.json accordingly. Removed obsolete files and reorganized code for better maintainability.
1 parent 6160980 commit 3c43745

File tree

12 files changed

+79
-63
lines changed

12 files changed

+79
-63
lines changed

bin/worker.cjs

Lines changed: 0 additions & 12 deletions
This file was deleted.

bin/worker.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

bin/worker/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference types="vite/client" />
2+
import * as WorkerUrl from 'pdfjs-dist/legacy/build/pdf.worker.mjs?url';
3+
4+
export function getWorkerSource() {
5+
return WorkerUrl.default;
6+
}

bin/worker/worker.cjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { join } = require('node:path');
2+
require('@napi-rs/canvas');
3+
require('pdfjs-dist/legacy/build/pdf.worker.mjs');
4+
const { getWorkerSource } = require('./worker_source.cjs');
5+
6+
/**
7+
* Gets the absolute path to the PDF worker file.
8+
*
9+
* @returns {string} The absolute path to the PDF worker module (pdf.worker.mjs)
10+
*/
11+
function getWorkerPath() {
12+
return join(__dirname, '../../dist/node/pdf.worker.mjs');
13+
}
14+
15+
exports.getWorkerPath = getWorkerPath;
16+
exports.getWorkerSource = getWorkerSource;

bin/worker/worker.d.cts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Gets the absolute path to the PDF worker file.
3+
* @returns {string} The absolute path to the PDF worker module (pdf.worker.mjs)
4+
*/
5+
export declare function getWorkerPath(): string;
6+
7+
/**
8+
* Gets the base64 data URL.
9+
* @returns {string} base64 data URL (pdf.worker.mjs)
10+
*/
11+
export declare function getWorkerSource(): string;

bin/worker/worker.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Gets the absolute path to the PDF worker file.
3+
* @returns {string} The absolute path to the PDF worker module (pdf.worker.mjs)
4+
*/
5+
export declare function getWorkerPath(): string;
6+
7+
/**
8+
* Gets the base64 data URL.
9+
* @returns {string} base64 data URL (pdf.worker.mjs)
10+
*/
11+
export declare function getWorkerSource(): string;
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import '@napi-rs/canvas';
12
import { dirname, join } from 'node:path';
23
import { fileURLToPath } from 'node:url';
4+
import 'pdfjs-dist/legacy/build/pdf.worker.mjs';
5+
6+
export { getWorkerSource } from './worker_source.js';
37

48
const __filename = fileURLToPath(import.meta.url);
59
const __dirname = dirname(__filename);
@@ -9,6 +13,6 @@ const __dirname = dirname(__filename);
913
*
1014
* @returns {string} The absolute path to the PDF worker module (pdf.worker.mjs)
1115
*/
12-
export function getPath() {
13-
return join(__dirname, '../dist/node/pdf.worker.mjs');
16+
export function getWorkerPath() {
17+
return join(__dirname, '../../dist/node/pdf.worker.mjs');
1418
}

bin/worker/worker_source.cjs

Lines changed: 7 additions & 0 deletions
Large diffs are not rendered by default.

bin/worker/worker_source.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pdf-parse",
3-
"version": "2.3.9",
3+
"version": "2.3.10",
44
"type": "module",
55
"main": "dist/cjs/index.cjs",
66
"module": "dist/esm/index.js",
@@ -17,15 +17,15 @@
1717
"types": "./dist/esm/index.d.ts",
1818
"require": "./dist/node/index.cjs"
1919
},
20-
"./source": {
21-
"types": "./dist/worker/index.d.ts",
22-
"import": "./dist/worker/index.js",
23-
"require": "./dist/worker/index.cjs"
24-
},
25-
"./path": {
26-
"types": "./bin/worker.d.ts",
27-
"import": "./bin/worker.js",
28-
"require": "./bin/worker.cjs"
20+
"./worker": {
21+
"import": {
22+
"types": "./bin/worker/worker.d.ts",
23+
"default": "./bin/worker/worker.js"
24+
},
25+
"require": {
26+
"types": "./bin/worker/worker.d.cts",
27+
"default": "./bin/worker/worker.cjs"
28+
}
2929
}
3030
},
3131

@@ -87,7 +87,8 @@
8787
"clean:build": "rimraf dist",
8888
"clean:site": "rimraf reports_site/test-report reports_site/coverage reports_site/live_demo/dist-browser",
8989
"clean:test": "rimraf --glob dist test/**/*.txt test/**/imgs test/**/*_images",
90-
"clean": "npm-run-all clean:build clean:site clean:test",
90+
"clean:worker": "rimraf bin/worker/worker_source.js bin/worker/worker_source.cjs",
91+
"clean": "npm-run-all clean:build clean:worker clean:site clean:test",
9192
"prepare": "npm run build",
9293
"test": "vitest run --reporter=default",
9394
"test:ui": "vitest --ui --coverage",
@@ -122,7 +123,6 @@
122123
"rimraf": "^6.0.1",
123124
"typescript": "^5.9.3",
124125
"vite": "^7.1.5",
125-
"vite-plugin-dts": "^4.5.4",
126126
"vitest": "^3.2.4"
127127
},
128128

0 commit comments

Comments
 (0)