Skip to content

Cache regex #1017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/worker-script/browser/getCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const { simd } = require('wasm-feature-detect');
const coreVersion = require('../../../package.json').dependencies['tesseract.js-core'];

const trailingSlashRegex = /\/$/;

module.exports = async (lstmOnly, corePath, res) => {
if (typeof global.TesseractCore === 'undefined') {
const statusText = 'loading tesseract core';
Expand All @@ -24,14 +26,14 @@ module.exports = async (lstmOnly, corePath, res) => {
const simdSupport = await simd();
if (simdSupport) {
if (lstmOnly) {
corePathImportFile = `${corePathImport.replace(/\/$/, '')}/tesseract-core-simd-lstm.wasm.js`;
corePathImportFile = `${corePathImport.replace(trailingSlashRegex, '')}/tesseract-core-simd-lstm.wasm.js`;
} else {
corePathImportFile = `${corePathImport.replace(/\/$/, '')}/tesseract-core-simd.wasm.js`;
corePathImportFile = `${corePathImport.replace(trailingSlashRegex, '')}/tesseract-core-simd.wasm.js`;
}
} else if (lstmOnly) {
corePathImportFile = `${corePathImport.replace(/\/$/, '')}/tesseract-core-lstm.wasm.js`;
corePathImportFile = `${corePathImport.replace(trailingSlashRegex, '')}/tesseract-core-lstm.wasm.js`;
} else {
corePathImportFile = `${corePathImport.replace(/\/$/, '')}/tesseract-core.wasm.js`;
corePathImportFile = `${corePathImport.replace(trailingSlashRegex, '')}/tesseract-core.wasm.js`;
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/worker-script/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const defaultOutput = require('./constants/defaultOutput');
const { log, setLogging } = require('../utils/log');
const PSM = require('../constants/PSM');

const componentRegex = /components are not present/;
const trailingSlashRegex = /\/$/;

/*
* Tesseract Module returned by TesseractCore.
*/
Expand Down Expand Up @@ -133,7 +136,7 @@ res) => {
// The is-url package is used to tell the difference
// For the browser version, langPath is assumed to be a URL
if (env !== 'node' || isURL(langPathDownload) || langPathDownload.startsWith('moz-extension://') || langPathDownload.startsWith('chrome-extension://') || langPathDownload.startsWith('file://')) { /** When langPathDownload is an URL */
path = langPathDownload.replace(/\/$/, '');
path = langPathDownload.replace(trailingSlashRegex, '');
}

// langPathDownload is a URL, fetch from server
Expand Down Expand Up @@ -286,7 +289,7 @@ const initialize = async ({
// The .wasm build of Tesseract saves this message in a separate file
// (in addition to the normal debug file location).
const debugStr = TessModule.FS.readFile('/debugDev.txt', { encoding: 'utf8', flags: 'a+' });
if (dataFromCache && /components are not present/.test(debugStr)) {
if (dataFromCache && componentRegex.test(debugStr)) {
log('Data from cache missing requested OEM model. Attempting to refresh cache with new language data.');
// In this case, language data is re-loaded
await loadLanguage({ workerId, payload: { langs: loadLanguageLangsWorker, options: loadLanguageOptionsWorker } }); // eslint-disable-line max-len
Expand Down
4 changes: 3 additions & 1 deletion src/worker-script/utils/setImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const bmp = require('bmp-js');

const exifRegex = /1 18 0 3 0 0 0 1 0 (\d)/;

/**
* setImage
*
Expand All @@ -13,7 +15,7 @@ module.exports = (TessModule, api, image, angle = 0) => {
// Check for bmp magic numbers (42 and 4D in hex)
const isBmp = (image[0] === 66 && image[1] === 77) || (image[1] === 66 && image[0] === 77);

const exif = parseInt(image.slice(0, 500).join(' ').match(/1 18 0 3 0 0 0 1 0 (\d)/)?.[1], 10) || 1;
const exif = parseInt(image.slice(0, 500).join(' ').match(exifRegex)?.[1], 10) || 1;

// /*
// * Leptonica supports some but not all bmp files
Expand Down
4 changes: 3 additions & 1 deletion src/worker/browser/loadImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const readFromBlobOrFile = (blob) => (
})
);

const imageRegex = /data:image\/([a-zA-Z]*);base64,([^"]*)/;

/**
* loadImage
*
Expand All @@ -35,7 +37,7 @@ const loadImage = async (image) => {

if (typeof image === 'string') {
// Base64 Image
if (/data:image\/([a-zA-Z]*);base64,([^"]*)/.test(image)) {
if (imageRegex.test(image)) {
data = atob(image.split(',')[1])
.split('')
.map((c) => c.charCodeAt(0));
Expand Down
3 changes: 2 additions & 1 deletion src/worker/node/loadImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const fetch = global.fetch || require('node-fetch');
const isURL = require('is-url');

const readFile = util.promisify(fs.readFile);
const imageRegex = /data:image\/([a-zA-Z]*);base64,([^"]*)/;

/**
* loadImage
Expand All @@ -25,7 +26,7 @@ module.exports = async (image) => {
if (isURL(image) || image.startsWith('moz-extension://') || image.startsWith('chrome-extension://') || image.startsWith('file://')) {
const resp = await fetch(image);
data = await resp.arrayBuffer();
} else if (/data:image\/([a-zA-Z]*);base64,([^"]*)/.test(image)) {
} else if (imageRegex.test(image)) {
data = Buffer.from(image.split(',')[1], 'base64');
} else {
data = await readFile(image);
Expand Down
Loading