Skip to content

Commit

Permalink
Removed file-type dependency per #711 (#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
Balearica committed Jun 2, 2023
1 parent fd1c8b2 commit 32acbd8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 21 deletions.
14 changes: 0 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions src/worker-script/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* @author Jerome Wu <jeromewus@gmail.com>
*/
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';
Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/worker-script/utils/setImage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const bmp = require('bmp-js');
const fileType = require('file-type');

/**
* setImage
Expand All @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit 32acbd8

Please sign in to comment.