-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathgetCore.js
50 lines (44 loc) · 2.24 KB
/
getCore.js
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
const { simd } = require('wasm-feature-detect');
const coreVersion = require('../../../package.json').dependencies['tesseract.js-core'];
module.exports = async (lstmOnly, corePath, res) => {
if (typeof global.TesseractCore === 'undefined') {
const statusText = 'loading tesseract core';
res.progress({ status: statusText, progress: 0 });
// If the user specifies a core path, we use that
// Otherwise, default to CDN
const corePathImport = corePath || `https://cdn.jsdelivr.net/npm/tesseract.js-core@v${coreVersion.substring(1)}`;
// If a user specifies a specific JavaScript file, load that file.
// Otherwise, assume a directory has been provided, and load either
// tesseract-core.wasm.js or tesseract-core-simd.wasm.js depending
// on whether this device has SIMD support.
let corePathImportFile;
if (corePathImport.slice(-2) === 'js') {
corePathImportFile = corePathImport;
} else {
const simdSupport = await simd();
if (simdSupport) {
if (lstmOnly) {
corePathImportFile = `${corePathImport.replace(/\/$/, '')}/tesseract-core-simd-lstm.wasm.js`;
} else {
corePathImportFile = `${corePathImport.replace(/\/$/, '')}/tesseract-core-simd.wasm.js`;
}
} else if (lstmOnly) {
corePathImportFile = `${corePathImport.replace(/\/$/, '')}/tesseract-core-lstm.wasm.js`;
} else {
corePathImportFile = `${corePathImport.replace(/\/$/, '')}/tesseract-core.wasm.js`;
}
}
// Create a module named `global.TesseractCore`
global.importScripts(corePathImportFile);
// Tesseract.js-core versions through 4.0.3 create a module named `global.TesseractCoreWASM`,
// so we account for that here to preserve backwards compatibility.
// This part can be removed when Tesseract.js-core v4.0.3 becomes incompatible for other reasons
if (typeof global.TesseractCore === 'undefined' && typeof global.TesseractCoreWASM !== 'undefined' && typeof WebAssembly === 'object') {
global.TesseractCore = global.TesseractCoreWASM;
} else if (typeof global.TesseractCore === 'undefined') {
throw Error('Failed to load TesseractCore');
}
res.progress({ status: statusText, progress: 1 });
}
return global.TesseractCore;
};