From 32acbd8579463bfdec4536dc0b83b3c28c56f48a Mon Sep 17 00:00:00 2001 From: Balearica Date: Thu, 1 Jun 2023 23:32:04 -0700 Subject: [PATCH] Removed file-type dependency per #711 (#775) --- package-lock.json | 14 -------------- package.json | 1 - src/worker-script/index.js | 7 ++++--- src/worker-script/utils/setImage.js | 6 +++--- 4 files changed, 7 insertions(+), 21 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1f4ee72..af7063c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,6 @@ "license": "Apache-2.0", "dependencies": { "bmp-js": "^0.1.0", - "file-type": "^12.4.2", "idb-keyval": "^6.2.0", "is-electron": "^2.2.2", "is-url": "^1.2.4", @@ -4576,14 +4575,6 @@ "node": "^10.12.0 || >=12.0.0" } }, - "node_modules/file-type": { - "version": "12.4.2", - "resolved": "https://registry.npmjs.org/file-type/-/file-type-12.4.2.tgz", - "integrity": "sha512-UssQP5ZgIOKelfsaB5CuGAL+Y+q7EmONuiwF3N5HAH0t27rvrttgi6Ra9k/+DVaY9UF6+ybxu5pOXLUdA8N7Vg==", - "engines": { - "node": ">=8" - } - }, "node_modules/fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -13051,11 +13042,6 @@ "flat-cache": "^3.0.4" } }, - "file-type": { - "version": "12.4.2", - "resolved": "https://registry.npmjs.org/file-type/-/file-type-12.4.2.tgz", - "integrity": "sha512-UssQP5ZgIOKelfsaB5CuGAL+Y+q7EmONuiwF3N5HAH0t27rvrttgi6Ra9k/+DVaY9UF6+ybxu5pOXLUdA8N7Vg==" - }, "fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", diff --git a/package.json b/package.json index 0a4e59b..fa327cd 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,6 @@ }, "dependencies": { "bmp-js": "^0.1.0", - "file-type": "^12.4.2", "idb-keyval": "^6.2.0", "is-electron": "^2.2.2", "is-url": "^1.2.4", diff --git a/src/worker-script/index.js b/src/worker-script/index.js index 30e701f..959589e 100644 --- a/src/worker-script/index.js +++ b/src/worker-script/index.js @@ -8,7 +8,6 @@ * @author Jerome Wu */ require('regenerator-runtime/runtime'); -const fileType = require('file-type'); const isURL = require('is-url'); const dump = require('./utils/dump'); const isWebWorker = require('../utils/getEnvironment')('type') === 'webworker'; @@ -125,8 +124,10 @@ res) => { data = new Uint8Array(data); - const type = fileType(data); - if (typeof type !== 'undefined' && type.mime === 'application/gzip') { + // Check for gzip magic numbers (1F and 8B in hex) + const isGzip = (data[0] === 31 && data[1] === 139) || (data[1] === 31 && data[0] === 139); + + if (isGzip) { data = adapter.gunzip(data); } diff --git a/src/worker-script/utils/setImage.js b/src/worker-script/utils/setImage.js index d9aec9b..354521f 100644 --- a/src/worker-script/utils/setImage.js +++ b/src/worker-script/utils/setImage.js @@ -1,5 +1,4 @@ const bmp = require('bmp-js'); -const fileType = require('file-type'); /** * setImage @@ -9,7 +8,8 @@ const fileType = require('file-type'); * @access public */ module.exports = (TessModule, api, image, angle = 0) => { - const type = fileType(image); + // 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 = image.slice(0, 500).toString().match(/\x01\x12\x00\x03\x00\x00\x00\x01\x00(.)/)?.[1]?.charCodeAt(0) || 1; @@ -18,7 +18,7 @@ module.exports = (TessModule, api, image, angle = 0) => { // * @see https://github.com/DanBloomberg/leptonica/issues/607#issuecomment-1068802516 // * We therefore use bmp-js to convert all bmp files into a format Leptonica is known to support // */ - if (type && type.mime === 'image/bmp') { + if (isBmp) { // Not sure what this line actually does, but removing breaks the function const buf = Buffer.from(Array.from({ ...image, length: Object.keys(image).length })); const bmpBuf = bmp.decode(buf);