Comparing version
'use strict'; | ||
var fs = require('fs'); | ||
var Promise = require('bluebird'); | ||
var pread = Promise.promisify(fs.read, { multiArgs: true }); | ||
var util = require('util'); | ||
var pread = util.promisify(fs.read); | ||
@@ -10,13 +10,11 @@ var DETECT_LENGTH = 16; | ||
//Determines the appropriate plugin to use for the given file descriptor. | ||
module.exports = function (fd, plugins) { | ||
return pread(fd, new Buffer(DETECT_LENGTH), 0, DETECT_LENGTH, 0) | ||
.spread(function (bytesRead, buffer) { | ||
for (var i = 0; i < plugins.length; i++) { | ||
var plugin = plugins[i]; | ||
if (plugin.detect(buffer)) { | ||
return plugin; | ||
} | ||
module.exports = async function (fd, plugins) { | ||
const readObject = await pread(fd, Buffer.alloc(DETECT_LENGTH), 0, DETECT_LENGTH, 0); | ||
for (var i = 0; i < plugins.length; i++) { | ||
var plugin = plugins[i]; | ||
if (plugin.detect(readObject.buffer)) { | ||
return plugin; | ||
} | ||
throw new Error('File type not supported'); | ||
}); | ||
} | ||
throw new Error('File type not supported'); | ||
}; |
'use strict'; | ||
var fs = require('fs'); | ||
var Promise = require('bluebird'); | ||
var popen = Promise.promisify(fs.open); | ||
var pclose = Promise.promisify(fs.close); | ||
var util = require('util'); | ||
var popen = util.promisify(fs.open); | ||
var pclose = util.promisify(fs.close); | ||
var detect = require('./detect'); | ||
function measure (plugins, path, callback) { | ||
async function measure (plugins, path, callback) { | ||
var fileDescriptor; | ||
return popen(path, 'r') | ||
.then(function (fd) { | ||
try { | ||
const fd = await popen(path, 'r'); | ||
fileDescriptor = fd; | ||
return detect(fd, plugins); | ||
}) | ||
.then(function (plugin) { | ||
return plugin.measure(path, fileDescriptor); | ||
}) | ||
.finally(function () { | ||
return pclose(fileDescriptor); | ||
}) | ||
.asCallback(callback); | ||
const plugin = await detect(fd, plugins); | ||
if (callback) return callback(undefined, await plugin.measure(path, fileDescriptor)); | ||
const measurement = await plugin.measure(path, fileDescriptor); | ||
return measurement; | ||
} | ||
catch (err) { | ||
if (callback) callback(err, undefined); | ||
throw err; | ||
} | ||
finally { | ||
if (fileDescriptor) { | ||
pclose(fileDescriptor); | ||
} | ||
} | ||
} | ||
@@ -25,0 +33,0 @@ |
{ | ||
"name": "calipers", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "The fastest Node.js library for measuring image and PDF dimensions.", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "NODE_ENV=test istanbul cover _mocha -- test --recursive --timeout 15000", | ||
"enforce": "istanbul check-coverage --statement 100 --branch 100 --function 100 --lines 100", | ||
"lint": "eslint ." | ||
"bench": "node bench/index.js", | ||
"bench:ts": "ts-node bench/index.ts", | ||
"b": "tsc bench/test", | ||
"build": "tsc", | ||
"test": "jest", | ||
"lint": "eslint .", | ||
"lint:fix": "eslint --fix .", | ||
"lint:file": "eslint" | ||
}, | ||
@@ -33,15 +38,17 @@ "repository": { | ||
"devDependencies": { | ||
"calipers-png": "2.x.x", | ||
"chai": "2.x.x", | ||
"chai-as-promised": "4.x.x", | ||
"coveralls": "2.x.x", | ||
"eslint": "1.x.x", | ||
"eslint-config-lob": "1.0.1", | ||
"istanbul": "0.x.x", | ||
"mocha": "2.x.x", | ||
"mocha-lcov-reporter": "0.x.x" | ||
}, | ||
"dependencies": { | ||
"bluebird": "3.x.x" | ||
"@types/jest": "^26.0.15", | ||
"@typescript-eslint/eslint-plugin": "^4.8.1", | ||
"@typescript-eslint/parser": "^4.8.1", | ||
"benny": "^3.7.1", | ||
"calipers-png": "2.1.0", | ||
"eslint": "^7.13.0", | ||
"eslint-config-standard": "^16.0.1", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"jest": "26.x.x", | ||
"ts-jest": "^26.4.4", | ||
"ts-node": "^9.0.0", | ||
"typescript": "^4.0.5" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# Calipers [](http://badge.fury.io/js/calipers) [](https://travis-ci.org/lob/calipers) | ||
# Calipers [](http://badge.fury.io/js/calipers) [](https://travis-ci.org/calipersjs/calipers) | ||
@@ -48,2 +48,3 @@ Current file types supported: **PDF, PNG, JPEG, GIF, BMP, WEBP, SVG** | ||
apt-get install libpoppler-cpp-dev | ||
apt-get install libpoppler-private-dev | ||
``` | ||
@@ -50,0 +51,0 @@ |
@@ -5,8 +5,7 @@ 'use strict'; | ||
var path = require('path'); | ||
var expect = require('chai').expect; | ||
var Promise = require('bluebird'); | ||
var popen = Promise.promisify(fs.open); | ||
var util = require('util'); | ||
var popen = util.promisify(fs.open); | ||
var detect = require('../lib/detect'); | ||
describe('detect', function () { | ||
describe('detect', () => { | ||
@@ -16,3 +15,3 @@ var txtPath = path.resolve(__dirname, 'fixtures/file.txt'); | ||
var fakeTruePlugin = { | ||
detect: function (buffer) { | ||
detect: (buffer) => { | ||
return buffer.toString('ascii', 0, 12) === 'A text file.'; | ||
@@ -23,3 +22,3 @@ } | ||
var fakeFalsePlugin = { | ||
detect: function () { | ||
detect: () => { | ||
return false; | ||
@@ -29,19 +28,13 @@ } | ||
it('should return the first plugin that returns true', function () { | ||
return popen(txtPath, 'r') | ||
.then(function (fd) { | ||
return detect(fd, [fakeFalsePlugin, fakeTruePlugin]); | ||
}) | ||
.then(function (plugin) { | ||
expect(plugin).to.eql(fakeTruePlugin); | ||
}); | ||
it('should return the first plugin that returns true', async () => { | ||
const fd = await popen(txtPath, 'r'); | ||
const plugin = await detect(fd, [fakeFalsePlugin, fakeTruePlugin]); | ||
expect(plugin).toBe(fakeTruePlugin); | ||
}); | ||
it('should throw an error for an unsupported file type', function () { | ||
return popen(txtPath, 'r') | ||
.then(function (fd) { | ||
return expect(detect(fd, [fakeFalsePlugin])).to.be.rejectedWith(Error); | ||
}); | ||
it('should throw an error for an unsupported file type', async () => { | ||
const fd = await popen(txtPath, 'r'); | ||
expect(detect(fd, [fakeFalsePlugin])).rejects.toThrow(Error); | ||
}); | ||
}); |
'use strict'; | ||
var path = require('path'); | ||
var expect = require('chai').expect; | ||
describe('index', function () { | ||
describe('index', () => { | ||
@@ -16,6 +15,6 @@ var txtPath = path.resolve(__dirname, 'fixtures/file.txt'); | ||
var fakeTruePlugin = { | ||
detect: function (buffer) { | ||
detect: (buffer) => { | ||
return buffer.toString('ascii', 0, 12) === 'A text file.'; | ||
}, | ||
measure: function () { | ||
measure: () => { | ||
return output; | ||
@@ -26,3 +25,3 @@ } | ||
var fakeFalsePlugin = { | ||
detect: function () { | ||
detect: () => { | ||
return false; | ||
@@ -32,6 +31,6 @@ } | ||
it('works with callbacks', function (done) { | ||
it('works with callbacks', (done) => { | ||
var calipers = require('../lib/index')(fakeFalsePlugin, 'png', fakeTruePlugin); | ||
calipers.measure(txtPath, function (err, result) { | ||
expect(result).to.eql(output); | ||
expect(result).toBe(output); | ||
done(); | ||
@@ -41,15 +40,34 @@ }); | ||
it('works with promises', function () { | ||
it('errors out correctly with callbacks', (done) => { | ||
var calipers = require('../lib/index')(fakeFalsePlugin, 'png', fakeFalsePlugin); | ||
calipers.measure(txtPath, function (err, result) { | ||
}).catch((err) => { | ||
expect(err.message).toMatch('File type not supported'); | ||
done() | ||
}); | ||
}); | ||
it('works with promises', () => { | ||
var calipers = require('../lib/index')(fakeFalsePlugin, fakeTruePlugin, 'png'); | ||
return calipers.measure(txtPath) | ||
.then(function (result) { | ||
expect(result).to.eql(output); | ||
expect(result).toBe(output); | ||
}); | ||
}); | ||
it('works with required plugins', function () { | ||
it('errors with promises', async () => { | ||
var calipers = require('../lib/index')(fakeFalsePlugin, fakeFalsePlugin, 'png'); | ||
try { | ||
await calipers.measure(txtPath); | ||
} catch(err) { | ||
expect(err.message).toMatch('File type not supported'); | ||
} | ||
}); | ||
it('works with required plugins', () => { | ||
var calipers = require('../lib/index')(fakeFalsePlugin, fakeTruePlugin, 'png'); | ||
return calipers.measure(pngPath) | ||
.then(function (result) { | ||
expect(result).to.eql({ type: 'png', pages: [{ width: 123, height: 456 }] }); | ||
expect(result).toStrictEqual({ type: 'png', pages: [{ width: 123, height: 456 }] }); | ||
}); | ||
@@ -56,0 +74,0 @@ }); |
Sorry, the diff of this file is not supported yet
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
288864
113.28%0
-100%28
100%405
200%198
0.51%14
55.56%1
Infinity%5
25%1
Infinity%- Removed
- Removed