Repository navigation
Expand file tree
/
Copy pathpdfjs.js
More file actions
137 lines (121 loc) · 4.5 KB
/
Copy pathpdfjs.js
File metadata and controls
137 lines (121 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// pdfjs.js — 飞鼠格式 PDF.js 加载域:入口解析、打包/开发路径差异处理、模块加载器。
// 第三批抽取自 server.js(零逻辑改动,纯搬移)。
const path = require("path");
const fsp = require("fs/promises");
const { fileURLToPath, pathToFileURL } = require("url");
// PDF.js 6's Node factory treats a file URL string as a filesystem path, but
// the JS decoder fallback requires a file URL for dynamic import on Windows.
// Keep both paths local and use URL objects when reading decoder resources.
class LocalPdfjsBinaryDataFactory {
constructor(options) {
this.urls = options;
}
async fetch({ kind, filename }) {
if (!["cMapUrl", "standardFontDataUrl", "wasmUrl"].includes(kind)
|| typeof filename !== "string" || !/^[a-zA-Z0-9_.-]+$/.test(filename)
|| filename === "." || filename === "..") {
throw new Error("Invalid PDF.js resource request");
}
const baseUrl = this.urls[kind];
if (!baseUrl) throw new Error(`Ensure that the \`${kind}\` API parameter is provided.`);
const resourceUrl = new URL(filename, baseUrl);
if (resourceUrl.protocol !== "file:") throw new Error("PDF.js resources must be local files");
return new Uint8Array(await fsp.readFile(resourceUrl));
}
}
function getPdfjsDocumentOptions(options = {}) {
return {
...options,
wasmUrl: pathToFileURL(path.join(__dirname, "node_modules", "pdfjs-dist", "wasm") + path.sep).href,
BinaryDataFactory: LocalPdfjsBinaryDataFactory,
useWorkerFetch: false,
isEvalSupported: false
};
}
function normalizePdfjsEntry(value) {
let normalized = String(value || "");
if (normalized.startsWith("file:")) {
try {
normalized = fileURLToPath(normalized);
} catch {
// Fall through to text normalization for malformed file URLs.
}
}
normalized = normalized.replaceAll("\\", "/");
try {
normalized = decodeURIComponent(normalized);
} catch {
// Keep the original text when an error URL contains malformed escapes.
}
return normalized;
}
function isMissingPdfjsEntry(error, specifier) {
if (!error || !["ERR_MODULE_NOT_FOUND", "MODULE_NOT_FOUND"].includes(error.code)) {
return false;
}
const expected = normalizePdfjsEntry(specifier);
const matchesExpected = (value) => {
const target = normalizePdfjsEntry(value);
return target === expected || target.endsWith(`/${expected}`);
};
if (error.url && matchesExpected(error.url)) {
return true;
}
const missingTarget = String(error.message || "").match(/Cannot find (?:module|package)\s+['"]([^'"]+)['"]/i);
return Boolean(missingTarget && matchesExpected(missingTarget[1]));
}
function resolvePdfjsEntrySpecifiers(packageJsonResolver = require.resolve, appRoot = __dirname) {
const packageJsonPath = path.resolve(packageJsonResolver("pdfjs-dist/package.json"));
const expectedPackageJsonPath = path.resolve(appRoot, "node_modules", "pdfjs-dist", "package.json");
const comparablePath = (filePath) => process.platform === "win32" ? filePath.toLowerCase() : filePath;
if (comparablePath(packageJsonPath) !== comparablePath(expectedPackageJsonPath)) {
throw new Error(
`PDF.js package must resolve inside the app root at ${expectedPackageJsonPath}; got ${packageJsonPath}`
);
}
const packageRoot = path.dirname(packageJsonPath);
return {
modernSpecifier: pathToFileURL(path.join(packageRoot, "legacy", "build", "pdf.mjs")).href,
legacySpecifier: pathToFileURL(path.join(packageRoot, "legacy", "build", "pdf.js")).href
};
}
async function loadPdfjsModule({
appRoot = __dirname,
importer = (specifier) => import(specifier),
packageJsonResolver = require.resolve,
modernSpecifier,
legacySpecifier
} = {}) {
if (!modernSpecifier || !legacySpecifier) {
({ modernSpecifier, legacySpecifier } = resolvePdfjsEntrySpecifiers(packageJsonResolver, appRoot));
}
let mod;
try {
mod = await importer(modernSpecifier);
} catch (error) {
if (!isMissingPdfjsEntry(error, modernSpecifier)) {
throw error;
}
mod = await importer(legacySpecifier);
}
return mod.default || mod;
}
function createPdfjsLoader(options) {
let cachedPdfjsPromise = null;
return function loadPdfjs() {
if (!cachedPdfjsPromise) {
cachedPdfjsPromise = loadPdfjsModule(options);
}
return cachedPdfjsPromise;
};
}
const loadPdfjs = createPdfjsLoader();
module.exports = {
getPdfjsDocumentOptions,
normalizePdfjsEntry,
isMissingPdfjsEntry,
resolvePdfjsEntrySpecifiers,
loadPdfjsModule,
createPdfjsLoader,
loadPdfjs
};