优化后端
This commit is contained in:
Generated
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
const version = require('../../../package.json').version;
|
||||
const defaultOptions = require('../../constants/defaultOptions');
|
||||
|
||||
/*
|
||||
* Default options for browser worker
|
||||
*/
|
||||
module.exports = {
|
||||
...defaultOptions,
|
||||
workerPath: `https://cdn.jsdelivr.net/npm/tesseract.js@v${version}/dist/worker.min.js`,
|
||||
};
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
*
|
||||
* Tesseract Worker adapter for browser
|
||||
*
|
||||
* @fileoverview Tesseract Worker adapter for browser
|
||||
* @author Kevin Kwok <antimatter15@gmail.com>
|
||||
* @author Guillermo Webster <gui@mit.edu>
|
||||
* @author Jerome Wu <jeromewus@gmail.com>
|
||||
*/
|
||||
const defaultOptions = require('./defaultOptions');
|
||||
const spawnWorker = require('./spawnWorker');
|
||||
const terminateWorker = require('./terminateWorker');
|
||||
const onMessage = require('./onMessage');
|
||||
const send = require('./send');
|
||||
const loadImage = require('./loadImage');
|
||||
|
||||
module.exports = {
|
||||
defaultOptions,
|
||||
spawnWorker,
|
||||
terminateWorker,
|
||||
onMessage,
|
||||
send,
|
||||
loadImage,
|
||||
};
|
||||
Generated
Vendored
+71
@@ -0,0 +1,71 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* readFromBlobOrFile
|
||||
*
|
||||
* @name readFromBlobOrFile
|
||||
* @function
|
||||
* @access private
|
||||
*/
|
||||
const readFromBlobOrFile = (blob) => (
|
||||
new Promise((resolve, reject) => {
|
||||
const fileReader = new FileReader();
|
||||
fileReader.onload = () => {
|
||||
resolve(fileReader.result);
|
||||
};
|
||||
fileReader.onerror = ({ target: { error: { code } } }) => {
|
||||
reject(Error(`File could not be read! Code=${code}`));
|
||||
};
|
||||
fileReader.readAsArrayBuffer(blob);
|
||||
})
|
||||
);
|
||||
|
||||
/**
|
||||
* loadImage
|
||||
*
|
||||
* @name loadImage
|
||||
* @function load image from different source
|
||||
* @access private
|
||||
*/
|
||||
const loadImage = async (image) => {
|
||||
let data = image;
|
||||
if (typeof image === 'undefined') {
|
||||
return 'undefined';
|
||||
}
|
||||
|
||||
if (typeof image === 'string') {
|
||||
// Base64 Image
|
||||
if (/data:image\/([a-zA-Z]*);base64,([^"]*)/.test(image)) {
|
||||
data = atob(image.split(',')[1])
|
||||
.split('')
|
||||
.map((c) => c.charCodeAt(0));
|
||||
} else {
|
||||
const resp = await fetch(image);
|
||||
data = await resp.arrayBuffer();
|
||||
}
|
||||
} else if (typeof HTMLElement !== 'undefined' && image instanceof HTMLElement) {
|
||||
if (image.tagName === 'IMG') {
|
||||
data = await loadImage(image.src);
|
||||
}
|
||||
if (image.tagName === 'VIDEO') {
|
||||
data = await loadImage(image.poster);
|
||||
}
|
||||
if (image.tagName === 'CANVAS') {
|
||||
await new Promise((resolve) => {
|
||||
image.toBlob(async (blob) => {
|
||||
data = await readFromBlobOrFile(blob);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
} else if (typeof OffscreenCanvas !== 'undefined' && image instanceof OffscreenCanvas) {
|
||||
const blob = await image.convertToBlob();
|
||||
data = await readFromBlobOrFile(blob);
|
||||
} else if (image instanceof File || image instanceof Blob) {
|
||||
data = await readFromBlobOrFile(image);
|
||||
}
|
||||
|
||||
return new Uint8Array(data);
|
||||
};
|
||||
|
||||
module.exports = loadImage;
|
||||
Generated
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = (worker, handler) => {
|
||||
worker.onmessage = ({ data }) => { // eslint-disable-line
|
||||
handler(data);
|
||||
};
|
||||
};
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* send
|
||||
*
|
||||
* @name send
|
||||
* @function send packet to worker and create a job
|
||||
* @access public
|
||||
*/
|
||||
module.exports = async (worker, packet) => {
|
||||
worker.postMessage(packet);
|
||||
};
|
||||
Generated
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* spawnWorker
|
||||
*
|
||||
* @name spawnWorker
|
||||
* @function create a new Worker in browser
|
||||
* @access public
|
||||
*/
|
||||
module.exports = ({ workerPath, workerBlobURL }) => {
|
||||
let worker;
|
||||
if (Blob && URL && workerBlobURL) {
|
||||
const blob = new Blob([`importScripts("${workerPath}");`], {
|
||||
type: 'application/javascript',
|
||||
});
|
||||
worker = new Worker(URL.createObjectURL(blob));
|
||||
} else {
|
||||
worker = new Worker(workerPath);
|
||||
}
|
||||
|
||||
return worker;
|
||||
};
|
||||
Generated
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* terminateWorker
|
||||
*
|
||||
* @name terminateWorker
|
||||
* @function terminate worker
|
||||
* @access public
|
||||
*/
|
||||
module.exports = (worker) => {
|
||||
worker.terminate();
|
||||
};
|
||||
Generated
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
const defaultOptions = require('../../constants/defaultOptions');
|
||||
|
||||
/*
|
||||
* Default options for node worker
|
||||
*/
|
||||
module.exports = {
|
||||
...defaultOptions,
|
||||
workerPath: path.join(__dirname, '..', '..', 'worker-script', 'node', 'index.js'),
|
||||
};
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
*
|
||||
* Tesseract Worker impl. for node (using child_process)
|
||||
*
|
||||
* @fileoverview Tesseract Worker impl. for node
|
||||
* @author Kevin Kwok <antimatter15@gmail.com>
|
||||
* @author Guillermo Webster <gui@mit.edu>
|
||||
* @author Jerome Wu <jeromewus@gmail.com>
|
||||
*/
|
||||
const defaultOptions = require('./defaultOptions');
|
||||
const spawnWorker = require('./spawnWorker');
|
||||
const terminateWorker = require('./terminateWorker');
|
||||
const onMessage = require('./onMessage');
|
||||
const send = require('./send');
|
||||
const loadImage = require('./loadImage');
|
||||
|
||||
module.exports = {
|
||||
defaultOptions,
|
||||
spawnWorker,
|
||||
terminateWorker,
|
||||
onMessage,
|
||||
send,
|
||||
loadImage,
|
||||
};
|
||||
Generated
Vendored
+38
@@ -0,0 +1,38 @@
|
||||
'use strict';
|
||||
|
||||
const util = require('util');
|
||||
const fs = require('fs');
|
||||
// Use built-in fetch if available, otherwise fallback to node-fetch
|
||||
const fetch = global.fetch || require('node-fetch');
|
||||
const isURL = require('is-url');
|
||||
|
||||
const readFile = util.promisify(fs.readFile);
|
||||
|
||||
/**
|
||||
* loadImage
|
||||
*
|
||||
* @name loadImage
|
||||
* @function load image from different source
|
||||
* @access public
|
||||
*/
|
||||
module.exports = async (image) => {
|
||||
let data = image;
|
||||
if (typeof image === 'undefined') {
|
||||
return image;
|
||||
}
|
||||
|
||||
if (typeof image === 'string') {
|
||||
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)) {
|
||||
data = Buffer.from(image.split(',')[1], 'base64');
|
||||
} else {
|
||||
data = await readFile(image);
|
||||
}
|
||||
} else if (Buffer.isBuffer(image)) {
|
||||
data = image;
|
||||
}
|
||||
|
||||
return new Uint8Array(data);
|
||||
};
|
||||
Generated
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = (worker, handler) => {
|
||||
worker.on('message', handler);
|
||||
};
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* send
|
||||
*
|
||||
* @name send
|
||||
* @function send packet to worker and create a job
|
||||
* @access public
|
||||
*/
|
||||
module.exports = async (worker, packet) => {
|
||||
worker.postMessage(packet);
|
||||
};
|
||||
Generated
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
const { Worker } = require('worker_threads');
|
||||
|
||||
/**
|
||||
* spawnWorker
|
||||
*
|
||||
* @name spawnWorker
|
||||
* @function fork a new process in node
|
||||
* @access public
|
||||
*/
|
||||
module.exports = ({ workerPath }) => new Worker(workerPath);
|
||||
Generated
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* terminateWorker
|
||||
*
|
||||
* @name terminateWorker
|
||||
* @function kill worker
|
||||
* @access public
|
||||
*/
|
||||
module.exports = (worker) => {
|
||||
worker.terminate();
|
||||
};
|
||||
Reference in New Issue
Block a user