diff --git a/package-lock.json b/package-lock.json index fcf2e62..0548853 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1113,6 +1113,12 @@ "integrity": "sha512-tMUqwBWfLFbJbizRmEcWSLw6HnFzfdJs2sOJEOwwtVPMoH/0Ay+E703oZz78VSXZiiDcZrQ5XKjPIUQixhmgVw==", "dev": true }, + "acorn-walk": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-6.2.0.tgz", + "integrity": "sha512-7evsyfH1cLOCdAzZAd43Cic04yKydNx0cF+7tiA19p1XnLLPU4dpCQOqpjqwokFe//vS0QqfqqjCS2JkiIs0cA==", + "dev": true + }, "agent-base": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-4.3.0.tgz", @@ -1512,22 +1518,6 @@ "integrity": "sha512-Uvq6hVe90D0B2WEnUqtdgY1bATGz3mw33nH9Y+dmA+w5DHvUmBgkr5rM/KCHpCsiFNRUfokW/szpPPgMK2hm4A==", "dev": true }, - "axios": { - "version": "0.18.1", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.18.1.tgz", - "integrity": "sha512-0BfJq4NSfQXd+SkFdrvFbG7addhYSBA2mQwISr46pD6E5iqkWg02RAs8vyTT/j0RTnoYmeXauBuSv1qKwR179g==", - "requires": { - "follow-redirects": "1.5.10", - "is-buffer": "^2.0.2" - }, - "dependencies": { - "is-buffer": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.4.tgz", - "integrity": "sha512-Kq1rokWXOPXWuaMAqZiJW4XxsmD9zGx9q4aePabbn3qCRGedtH7Cm+zV8WETitMfu1wdh+Rvd6w5egwSngUX2A==" - } - } - }, "axobject-query": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.1.1.tgz", @@ -1643,6 +1633,18 @@ "tweetnacl": "^0.14.3" } }, + "bfj": { + "version": "6.1.2", + "resolved": "https://registry.npmjs.org/bfj/-/bfj-6.1.2.tgz", + "integrity": "sha512-BmBJa4Lip6BPRINSZ0BPEIfB1wUY/9rwbwvIHQA1KjX9om29B6id0wnWXq7m3bn5JrUVjeOTnVuhPT1FiHwPGw==", + "dev": true, + "requires": { + "bluebird": "^3.5.5", + "check-types": "^8.0.3", + "hoopy": "^0.1.4", + "tryer": "^1.0.1" + } + }, "big.js": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/big.js/-/big.js-3.2.0.tgz", @@ -1988,6 +1990,12 @@ "integrity": "sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==", "dev": true }, + "check-types": { + "version": "8.0.3", + "resolved": "https://registry.npmjs.org/check-types/-/check-types-8.0.3.tgz", + "integrity": "sha512-YpeKZngUmG65rLudJ4taU7VLkOCTMhNl/u4ctNC56LQS/zJTyNH0Lrtwm1tfTsbLlwvlfsA2d1c8vCf/Kh2KwQ==", + "dev": true + }, "chokidar": { "version": "2.1.8", "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-2.1.8.tgz", @@ -2400,6 +2408,7 @@ "version": "3.1.0", "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "dev": true, "requires": { "ms": "2.0.0" } @@ -2546,6 +2555,12 @@ "integrity": "sha512-jnjyiM6eRyZl2H+W8Q/zLMA481hzi0eszAaBUzIVnmYVDBbnLxVNnfu1HgEBvCbL+71FrxMl3E6lpKH7Ge3OXA==", "dev": true }, + "duplexer": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", + "integrity": "sha1-rOb/gIwc5mtX0ev5eXessCM0z8E=", + "dev": true + }, "duplexify": { "version": "3.7.1", "resolved": "https://registry.npmjs.org/duplexify/-/duplexify-3.7.1.tgz", @@ -2574,6 +2589,12 @@ "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=", "dev": true }, + "ejs": { + "version": "2.7.4", + "resolved": "https://registry.npmjs.org/ejs/-/ejs-2.7.4.tgz", + "integrity": "sha512-7vmuyh5+kuUyJKePhQfRQBhXV5Ce+RnaeeQArKu1EAMpL3WbgMt5WG6uQZpEVvYSSsxMXRKOewtDk9RaTKXRlA==", + "dev": true + }, "electron-to-chromium": { "version": "1.3.322", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.322.tgz", @@ -3399,6 +3420,12 @@ "dev": true, "optional": true }, + "filesize": { + "version": "3.6.1", + "resolved": "https://registry.npmjs.org/filesize/-/filesize-3.6.1.tgz", + "integrity": "sha512-7KjR1vv6qnicaPMi1iiTcI85CyYwRO/PSFCu6SvqL8jN2Wjt/NIYQTFtFs7fSDCYOstUkEWIQGFUg5YZQfjlcg==", + "dev": true + }, "fill-range": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-4.0.0.tgz", @@ -3592,14 +3619,6 @@ "readable-stream": "^2.3.6" } }, - "follow-redirects": { - "version": "1.5.10", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", - "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", - "requires": { - "debug": "=3.1.0" - } - }, "for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", @@ -4360,6 +4379,24 @@ "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==", "dev": true }, + "gzip-size": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/gzip-size/-/gzip-size-5.1.1.tgz", + "integrity": "sha512-FNHi6mmoHvs1mxZAds4PpdCS6QG8B4C1krxJsMutgxl5t3+GlRTzzI3NEkifXx2pVsOvJdOGSmIgDhQ55FwdPA==", + "dev": true, + "requires": { + "duplexer": "^0.1.1", + "pify": "^4.0.1" + }, + "dependencies": { + "pify": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz", + "integrity": "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==", + "dev": true + } + } + }, "har-schema": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz", @@ -4475,6 +4512,12 @@ "parse-passwd": "^1.0.0" } }, + "hoopy": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/hoopy/-/hoopy-0.1.4.tgz", + "integrity": "sha512-HRcs+2mr52W0K+x8RzcLzuPPmVIKMSv97RGHy0Ea9y/mpcaK+xTrjICA04KAHi4GRzxliNqNJEFYWHghy3rSfQ==", + "dev": true + }, "hosted-git-info": { "version": "2.7.1", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.7.1.tgz", @@ -5233,7 +5276,7 @@ }, "media-typer": { "version": "0.3.0", - "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "resolved": "http://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=", "dev": true }, @@ -5478,7 +5521,8 @@ "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", - "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true }, "mute-stream": { "version": "0.0.7", @@ -5536,6 +5580,11 @@ "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", "dev": true }, + "node-fetch": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" + }, "node-libs-browser": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/node-libs-browser/-/node-libs-browser-2.2.1.tgz", @@ -6990,6 +7039,12 @@ "resolved": "https://registry.npmjs.org/opencollective-postinstall/-/opencollective-postinstall-2.0.2.tgz", "integrity": "sha512-pVOEP16TrAO2/fjej1IdOyupJY8KDUM1CvsaScRbw6oddvpQoOfGk4ywha0HKKVAD6RkW4x6Q+tNBwhf3Bgpuw==" }, + "opener": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/opener/-/opener-1.5.1.tgz", + "integrity": "sha512-goYSy5c2UXE4Ra1xixabeVh1guIX/ZV/YokJksb6q2lubWu6UbvPQ20p542/sFIll1nl8JnCyK9oBaOcCWXwvA==", + "dev": true + }, "optionator": { "version": "0.8.3", "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.8.3.tgz", @@ -7870,7 +7925,7 @@ }, "safe-regex": { "version": "1.1.0", - "resolved": "https://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", + "resolved": "http://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", "integrity": "sha1-QKNmnzsHfR6UPURinhV91IAjvy4=", "dev": true, "requires": { @@ -8446,7 +8501,7 @@ }, "strip-eof": { "version": "1.0.0", - "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", + "resolved": "http://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=", "dev": true }, @@ -8757,6 +8812,12 @@ } } }, + "tryer": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/tryer/-/tryer-1.0.1.tgz", + "integrity": "sha512-c3zayb8/kWWpycWYg87P71E1S1ZL6b6IJxfb5fvsUgsf0S2MVGaDhDXXjDMpdCpfWXqptc+4mXwmiy1ypXqRAA==", + "dev": true + }, "tslib": { "version": "1.10.0", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.10.0.tgz", @@ -9121,6 +9182,27 @@ } } }, + "webpack-bundle-analyzer": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/webpack-bundle-analyzer/-/webpack-bundle-analyzer-3.6.0.tgz", + "integrity": "sha512-orUfvVYEfBMDXgEKAKVvab5iQ2wXneIEorGNsyuOyVYpjYrI7CUOhhXNDd3huMwQ3vNNWWlGP+hzflMFYNzi2g==", + "dev": true, + "requires": { + "acorn": "^6.0.7", + "acorn-walk": "^6.1.1", + "bfj": "^6.1.1", + "chalk": "^2.4.1", + "commander": "^2.18.0", + "ejs": "^2.6.1", + "express": "^4.16.3", + "filesize": "^3.6.1", + "gzip-size": "^5.0.0", + "lodash": "^4.17.15", + "mkdirp": "^0.5.1", + "opener": "^1.5.1", + "ws": "^6.0.0" + } + }, "webpack-cli": { "version": "3.3.10", "resolved": "https://registry.npmjs.org/webpack-cli/-/webpack-cli-3.3.10.tgz", diff --git a/package.json b/package.json index e105f2b..e04899d 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,8 @@ "scripts": { "start": "node scripts/server.js", "build": "rimraf dist && webpack --config scripts/webpack.config.prod.js", + "profile:tesseract": "webpack-bundle-analyzer dist/tesseract-stats.json", + "profile:worker": "webpack-bundle-analyzer dist/worker-stats.json", "prepublishOnly": "npm run build", "wait": "rimraf dist && wait-on http://localhost:3000/dist/tesseract.dev.js", "test": "npm-run-all -p -r start test:all", @@ -50,15 +52,16 @@ "rimraf": "^2.7.1", "wait-on": "^3.3.0", "webpack": "^4.41.4", + "webpack-bundle-analyzer": "^3.6.0", "webpack-cli": "^3.3.10", "webpack-dev-middleware": "^3.7.2" }, "dependencies": { - "axios": "^0.18.0", "bmp-js": "^0.1.0", "file-type": "^12.4.1", "idb-keyval": "^3.2.0", "is-url": "1.2.2", + "node-fetch": "^2.6.0", "opencollective-postinstall": "^2.0.2", "regenerator-runtime": "^0.13.3", "resolve-url": "^0.2.1", diff --git a/scripts/webpack.config.dev.js b/scripts/webpack.config.dev.js index 4ae782a..f2a3181 100644 --- a/scripts/webpack.config.dev.js +++ b/scripts/webpack.config.dev.js @@ -1,5 +1,6 @@ const path = require('path'); const webpack = require('webpack'); +const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer'); const common = require('./webpack.config.common'); const genConfig = ({ @@ -19,6 +20,11 @@ const genConfig = ({ TESS_ENV: JSON.stringify('development'), }, }), + new BundleAnalyzerPlugin({ + analyzerMode: 'disable', + statsFilename: `${filename.split('.')[0]}-stats.json`, + generateStatsFile: true + }), ], devServer: { allowedHosts: ['localhost', '.gitpod.io'], diff --git a/src/utils/getEnvironment.js b/src/utils/getEnvironment.js index 71b1c01..c7bee8f 100644 --- a/src/utils/getEnvironment.js +++ b/src/utils/getEnvironment.js @@ -1,10 +1,17 @@ module.exports = (key) => { - const env = { - type: (typeof window !== 'undefined') && (typeof window.document !== 'undefined') ? 'browser' : 'node', - }; + const env = {}; + + if (typeof window === 'object') { + env.type = 'browser'; + } else if (typeof importScripts === 'function') { + env.type = 'webworker'; + } else if (typeof process === 'object' && typeof require === 'function') { + env.type = 'node'; + } if (typeof key === 'undefined') { return env; } + return env[key]; }; diff --git a/src/worker-script/browser/index.js b/src/worker-script/browser/index.js index 3199996..06fd336 100644 --- a/src/worker-script/browser/index.js +++ b/src/worker-script/browser/index.js @@ -27,5 +27,6 @@ global.addEventListener('message', ({ data }) => { worker.setAdapter({ getCore, gunzip, + fetch: () => {}, ...cache, }); diff --git a/src/worker-script/index.js b/src/worker-script/index.js index 56cdb4f..074c2dd 100644 --- a/src/worker-script/index.js +++ b/src/worker-script/index.js @@ -9,10 +9,9 @@ */ require('regenerator-runtime/runtime'); const fileType = require('file-type'); -const axios = require('axios'); const isURL = require('is-url'); const dump = require('./utils/dump'); -const isBrowser = require('../utils/getEnvironment')('type') === 'browser'; +const isWebWorker = require('../utils/getEnvironment')('type') === 'webworker'; const setImage = require('./utils/setImage'); const defaultParams = require('./constants/defaultParams'); const { log, setLogging } = require('../utils/log'); @@ -94,11 +93,8 @@ const loadLanguage = async ({ } if (path !== null) { - const { data: _data } = await axios.get( - `${path}/${lang}.traineddata${gzip ? '.gz' : ''}`, - { responseType: 'arraybuffer' }, - ); - data = _data; + const resp = await (isWebWorker ? fetch : adapter.fetch)(`${path}/${lang}.traineddata${gzip ? '.gz' : ''}`); + data = await resp.arrayBuffer(); } else { data = await adapter.readCache(`${langPath}/${lang}.traineddata${gzip ? '.gz' : ''}`); } diff --git a/src/worker-script/node/index.js b/src/worker-script/node/index.js index 0c24204..ded8f68 100644 --- a/src/worker-script/node/index.js +++ b/src/worker-script/node/index.js @@ -8,6 +8,7 @@ * @author Jerome Wu */ +const fetch = require('node-fetch'); const worker = require('../'); const getCore = require('./getCore'); const gunzip = require('./gunzip'); @@ -23,5 +24,6 @@ process.on('message', (packet) => { worker.setAdapter({ getCore, gunzip, + fetch, ...cache, }); diff --git a/src/worker/browser/loadImage.js b/src/worker/browser/loadImage.js index 864eede..675dc2e 100644 --- a/src/worker/browser/loadImage.js +++ b/src/worker/browser/loadImage.js @@ -1,4 +1,3 @@ -const axios = require('axios'); const resolveURL = require('resolve-url'); /** @@ -41,8 +40,8 @@ const loadImage = async (image) => { .split('') .map(c => c.charCodeAt(0)); } else { - const { data: _data } = await axios.get(resolveURL(image), { responseType: 'arraybuffer' }); - data = _data; + const resp = await fetch(resolveURL(image)); + data = await resp.arrayBuffer(); } } else if (image instanceof HTMLElement) { if (image.tagName === 'IMG') { diff --git a/src/worker/node/loadImage.js b/src/worker/node/loadImage.js index 96f7734..0b5a660 100644 --- a/src/worker/node/loadImage.js +++ b/src/worker/node/loadImage.js @@ -1,6 +1,6 @@ const util = require('util'); const fs = require('fs'); -const axios = require('axios'); +const fetch = require('node-fetch'); const isURL = require('is-url'); const readFile = util.promisify(fs.readFile); @@ -20,8 +20,8 @@ module.exports = async (image) => { if (typeof image === 'string') { if (isURL(image) || image.startsWith('moz-extension://') || image.startsWith('chrome-extension://') || image.startsWith('file://')) { - const { data: _data } = await axios.get(image, { responseType: 'arraybuffer' }); - data = _data; + 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 {