diff options
Diffstat (limited to 'src/worker-script/browser/getCore.js')
-rw-r--r-- | src/worker-script/browser/getCore.js | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/src/worker-script/browser/getCore.js b/src/worker-script/browser/getCore.js index faea34e..f9d256e 100644 --- a/src/worker-script/browser/getCore.js +++ b/src/worker-script/browser/getCore.js @@ -1,15 +1,26 @@ -module.exports = (corePath, res) => { +const { simd } = require('wasm-feature-detect'); +const { dependencies } = require('../../../package.json'); + +module.exports = async (corePath, res) => { if (typeof global.TesseractCore === 'undefined') { res.progress({ status: 'loading tesseract core', progress: 0 }); - global.importScripts(corePath); - /* - * Depending on whether the browser supports WebAssembly, - * the version of the TesseractCore will be different. - */ + + // If the user specifies a core path, we use that + // Otherwise, we detect the correct core based on SIMD support + let corePathImport = corePath; + if (!corePathImport) { + const simdSupport = await simd(); + if (simdSupport) { + corePathImport = `https://unpkg.com/tesseract.js-core@v${dependencies['tesseract.js-core'].substring(1)}/tesseract-core.wasm.js`; + } else { + corePathImport = `https://unpkg.com/tesseract.js-core@v${dependencies['tesseract.js-core'].substring(1)}/tesseract-core-simd.wasm.js`; + } + } + + global.importScripts(corePathImport); + if (typeof global.TesseractCoreWASM !== 'undefined' && typeof WebAssembly === 'object') { global.TesseractCore = global.TesseractCoreWASM; - } else if (typeof global.TesseractCoreASM !== 'undefined') { - global.TesseractCore = global.TesseractCoreASM; } else { throw Error('Failed to load TesseractCore'); } |