Browse Source

Replace fetch with axios, ignore DOMException in loadLang

feature/aio
Jerome Wu 6 years ago
parent
commit
6503dd4e73
  1. 43
      package-lock.json
  2. 3
      package.json
  3. 7
      src/browser/index.js
  4. 11
      src/common/workerUtils.js
  5. 8
      src/node/index.js

43
package-lock.json generated

@ -636,6 +636,15 @@ @@ -636,6 +636,15 @@
"integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==",
"dev": true
},
"axios": {
"version": "0.18.0",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.18.0.tgz",
"integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=",
"requires": {
"follow-redirects": "^1.3.0",
"is-buffer": "^1.1.5"
}
},
"axobject-query": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.0.2.tgz",
@ -2442,6 +2451,29 @@ @@ -2442,6 +2451,29 @@
"readable-stream": "^2.0.4"
}
},
"follow-redirects": {
"version": "1.7.0",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.7.0.tgz",
"integrity": "sha512-m/pZQy4Gj287eNy94nivy5wchN3Kp+Q5WgUPNy5lJSZ3sgkVKSYV/ZChMAQVIgx1SqfZ2zBZtPA2YlXIWxxJOQ==",
"requires": {
"debug": "^3.2.6"
},
"dependencies": {
"debug": {
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
"integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
"requires": {
"ms": "^2.1.1"
}
},
"ms": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz",
"integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg=="
}
}
},
"for-in": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",
@ -3554,8 +3586,7 @@ @@ -3554,8 +3586,7 @@
"is-buffer": {
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.5.tgz",
"integrity": "sha1-Hzsm72E7IUuIy8ojzGwB2Hlh7sw=",
"dev": true
"integrity": "sha1-Hzsm72E7IUuIy8ojzGwB2Hlh7sw="
},
"is-builtin-module": {
"version": "1.0.0",
@ -7152,15 +7183,15 @@ @@ -7152,15 +7183,15 @@
"integrity": "sha512-QmNgMA9m5ES5uMTqpOAPysrUA80vUx/6WKQlfkK3zhOeAgqv8DjwwcDv9tQv2TgRzOQ+LFKrJn94Y2rw5b2IGw=="
},
"tesseract.js-utils": {
"version": "1.0.0-beta.5",
"resolved": "https://registry.npmjs.org/tesseract.js-utils/-/tesseract.js-utils-1.0.0-beta.5.tgz",
"integrity": "sha512-EIZB5F5KCcTGLRkP2RxTil7fE/rMGC67SKbnGsE/y1lHxQv8rXfO0rrCXNocD//XAqfZlXFTikWTKkUu/dfcqw==",
"version": "1.0.0-beta.6",
"resolved": "https://registry.npmjs.org/tesseract.js-utils/-/tesseract.js-utils-1.0.0-beta.6.tgz",
"integrity": "sha512-AENYhkqafwysayWmKtyApV0gR4abLJ426plLNHs/++8oHt+ekooyp77ew/q4+QLE7cbUDyxiNGawcraOWE/RuQ==",
"requires": {
"axios": "^0.18.0",
"bmp-js": "^0.1.0",
"file-type": "^10.5.0",
"idb-keyval": "^3.1.0",
"is-url": "^1.2.4",
"node-fetch": "^2.3.0",
"resolve-url": "^0.2.1",
"zlibjs": "^0.3.1"
},

3
package.json

@ -44,12 +44,13 @@ @@ -44,12 +44,13 @@
"webpack-dev-middleware": "^3.4.0"
},
"dependencies": {
"axios": "^0.18.0",
"check-types": "^7.4.0",
"is-url": "1.2.2",
"node-fetch": "^2.3.0",
"resolve-url": "^0.2.1",
"tesseract.js-core": "^2.0.0-beta.10",
"tesseract.js-utils": "^1.0.0-beta.5"
"tesseract.js-utils": "^1.0.0-beta.6"
},
"repository": {
"type": "git",

7
src/browser/index.js

@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
*/
const check = require('check-types');
const resolveURL = require('resolve-url');
const axios = require('axios');
const { defaultOptions } = require('../common/options');
const { version } = require('../../package.json');
@ -45,8 +46,10 @@ const readFromBlobOrFile = (blob, res) => { @@ -45,8 +46,10 @@ const readFromBlobOrFile = (blob, res) => {
*/
const loadImage = (image) => {
if (check.string(image)) {
return fetch(resolveURL(image))
.then(resp => resp.arrayBuffer());
return axios.get(resolveURL(image), {
responseType: 'arraybuffer',
})
.then(resp => resp.data);
}
if (check.instance(image, HTMLElement)) {
if (image.tagName === 'IMG') {

11
src/common/workerUtils.js

@ -190,6 +190,17 @@ const handleRecognize = ({ @@ -190,6 +190,17 @@ const handleRecognize = ({
handleInit(options, res)
.then(() => (
loadLanguage({ lang, options }, res)
.catch((e) => {
if (e instanceof DOMException) {
/*
* For some reason google chrome throw DOMException in loadLang,
* while other browser is OK, for now we ignore this exception
* and hopefully to find the root cause one day.
*/
} else {
throw e;
}
})
.then(() => {
const progressUpdate = (progress) => {
res.progress({ status: 'initializing api', progress });

8
src/node/index.js

@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
*/
const util = require('util');
const fs = require('fs');
const fetch = require('node-fetch');
const axios = require('axios');
const isURL = require('is-url');
const { fork } = require('child_process');
const path = require('path');
@ -29,8 +29,10 @@ const readFile = util.promisify(fs.readFile); @@ -29,8 +29,10 @@ const readFile = util.promisify(fs.readFile);
*/
const loadImage = (image) => {
if (isURL(image)) {
return fetch(image)
.then(resp => resp.arrayBuffer());
return axios.get(image, {
responseType: 'arraybuffer',
})
.then(resp => resp.data);
}
return readFile(image);
};

Loading…
Cancel
Save