From 6503dd4e7351d933ff831554043c5780986de967 Mon Sep 17 00:00:00 2001 From: Jerome Wu Date: Wed, 22 May 2019 13:07:35 +0800 Subject: [PATCH] Replace fetch with axios, ignore DOMException in loadLang --- package-lock.json | 43 +++++++++++++++++++++++++++++++++------ package.json | 3 ++- src/browser/index.js | 7 +++++-- src/common/workerUtils.js | 11 ++++++++++ src/node/index.js | 8 +++++--- 5 files changed, 60 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index b515578..7851853 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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 @@ "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 @@ "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 @@ "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" }, diff --git a/package.json b/package.json index 7f266be..2b6eed2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/browser/index.js b/src/browser/index.js index 8568171..1be4ee3 100644 --- a/src/browser/index.js +++ b/src/browser/index.js @@ -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) => { */ 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') { diff --git a/src/common/workerUtils.js b/src/common/workerUtils.js index a26c2dc..93f9bce 100644 --- a/src/common/workerUtils.js +++ b/src/common/workerUtils.js @@ -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 }); diff --git a/src/node/index.js b/src/node/index.js index 6e098f3..f05dbc4 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -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); */ 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); };