Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

calipers

Package Overview
Dependencies
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

calipers - npm Package Compare versions

Comparing version 1.5.1 to 2.0.0

test/fixtures/123x456.png

16

lib/detect.js

@@ -5,16 +5,14 @@ 'use strict';

var Promise = require('bluebird');
var pread = Promise.promisify(fs.read);
var types = require('./types');
var pread = Promise.promisify(fs.read, { multiArgs: true });
var DETECT_LENGTH = 16;
// Detects the type of file based on the buffer, and returns a handler
// capable of measuring the file type.
module.exports = function (fd) {
//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 type in types) {
var handler = types[type];
if (handler.detect(buffer)) {
return handler;
for (var i = 0; i < plugins.length; i++) {
var plugin = plugins[i];
if (plugin.detect(buffer)) {
return plugin;
}

@@ -21,0 +19,0 @@ }

@@ -9,3 +9,3 @@ 'use strict';

exports.measure = function (path, callback) {
function measure (plugins, path, callback) {
return popen(path, 'r')

@@ -15,15 +15,27 @@ .bind({})

this.fd = fd;
return detect(fd);
return detect(fd, plugins);
})
.then(function (handler) {
this.handler = handler;
return handler.measure(path, this.fd);
.then(function (plugin) {
return plugin.measure(path, this.fd);
})
.then(function (result) {
return result;
})
.finally(function () {
return pclose(this.fd);
})
.nodeify(callback);
.asCallback(callback);
}
module.exports = function () {
var args = Array.prototype.slice.call(arguments);
var plugins = args.map(function (arg) {
if (typeof arg === 'object') {
return arg;
} else {
return require('calipers-' + arg);
}
});
return {
measure: measure.bind(null, plugins)
};
};
{
"name": "calipers",
"version": "1.5.1",
"description": "The fastest Node.js library for measuring PDF, PNG, and JPEG dimensions.",
"version": "2.0.0",
"description": "The fastest Node.js library for measuring image and PDF dimensions.",
"main": "lib/index.js",

@@ -20,3 +20,7 @@ "scripts": {

"png",
"jpeg"
"jpeg",
"gif",
"bmp",
"svg",
"webp"
],

@@ -30,15 +34,15 @@ "author": "Marcus Gartner",

"devDependencies": {
"chai": "^2.2.0",
"chai-as-promised": "^4.3.0",
"coveralls": "^2.11.2",
"eslint": "^1.9.0",
"eslint-config-lob": "^1.0.1",
"istanbul": "^0.3.13",
"mocha": "^2.1.0",
"mocha-lcov-reporter": "0.0.2"
"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.x.x",
"istanbul": "0.x.x",
"mocha": "2.x.x",
"mocha-lcov-reporter": "0.x.x"
},
"dependencies": {
"bluebird": "^2.9.13",
"poppler-simple": "^0.2.2"
"bluebird": "3.x.x"
}
}

@@ -5,3 +5,3 @@ <img src="https://s3-us-west-2.amazonaws.com/devassets.lob.com/calipers2.jpg" width="700">

Current file types supported: **PDF, PNG, JPEG, GIF, BMP, WEBP**
Current file types supported: **PDF, PNG, JPEG, GIF**

@@ -14,4 +14,21 @@ Calipers was built to provide a method of determining the dimensions of an image or PDF much faster and less resource-intensive than shelling-out to ImageMagick. At [Lob](https://lob.com) we must validate image and PDF sizes during the lifecyle of an API request. The simplest way to do this is to shell-out to ImageMagick to identify the type and size of a file. For high-traffic servers, this becomes a major bottleneck due to the innefficiency of shelling-out.

The [Poppler](http://poppler.freedesktop.org/) library C++ interface is required for PDF support.
Calipers uses a plugin architecture to allow users to include support for only the specific file types they need to measure. This helps avoid wasting CPU cycles measuring file types that an application doesn't support, and ensures users must only install dependencies that are absolutely needed (e.g. Poppler for PDF support).
To use Calipers, you must install the core library and at least one plugin. For example, for PNG support:
```
npm install --save calipers calipers-png
```
Here is a list of officially supported plugins:
File Type | Plugin
--------- | ------
PNG | [calipers-png](https://github.com/calipersjs/calipers-png)
JPEG | [calipers-jpeg](https://github.com/calipersjs/calipers-jpeg)
PDF † | [calipers-pdf](https://github.com/calipersjs/calipers-pdf)
GIF | [calipers-gif](https://github.com/calipersjs/calipers-gif)
† The [Poppler](http://poppler.freedesktop.org/) library C++ interface is required for PDF support. You must install Poppler before running `npm install calipers-pdf`.
To install Poppler on Mac OS X using Homebrew:

@@ -38,9 +55,18 @@

Calipers must be initialized by calling the required function with supported file types passed in. Use the plugin name's suffix (everything after the first "-") as an argument.
```javascript
// Initializes Calipers with support for calipers-png, calipers-jpeg, calipers-pdf.
var calipers = require('calipers')('png', 'jpeg', 'pdf');
```
Calipers exposes a single function, `measure`, once initialized.
### `measure(filePath, [callback])`
Measures the PDF, PNG, GIF, or JPEG file at the given path.
Measures the file at the given path.
- `filePath` - The path of the file.
- `callback` - called when the file has been measured
- `err` - An Error is thrown for unsupported file types or corrupt files.
- `result` - Contains keys `type` and `pages`, where `type` is one of `'png'`, `'pdf'`, `'gif'`, `'jpeg'`, `'bmp'`, or `'webp'`, and `pages` is an array of objects with keys `width` and `height`. For PNG and JPEG files, `pages` always has 1 element and `width` and `height` are the integer pixel dimensions. For PDF `width` and `height` are floating-point PostScript Point dimensions.
- `result` - Contains keys `type` and `pages`, where `type` is a string representing the file type (e.g. `'png'`), and `pages` is an array of objects with keys `width` and `height`. For image files, `pages` always has 1 element and `width` and `height` are the integer pixel dimensions. For PDF `width` and `height` are floating-point PostScript Point dimensions.

@@ -50,3 +76,3 @@ # Examples

```js
var calipers = require('calipers');
var calipers = require('calipers')('png', 'pdf');

@@ -87,2 +113,6 @@ // You can use a callback:

# Custom Plugins
*More information coming soon.*
# Benchmarks

@@ -105,6 +135,12 @@

The easiest and most helpful way to contribute is to find a file that calipers incorrectly measures, and submit a PR with the file. The tests automatically run against all files in the `test/fixtures` directory, so simply drop it into the appropriate subdirectory, and name it according to its size `<width>x<height>.png`. If it's a PDF, include the page count and round the PostScript Point to the nearest integer: `<width>x<height>.<page count>.pdf`. Fixes for these files are welcome, but not necessary.
### Bug Reporting
The easiest and most helpful way to contribute is to find a file that Calipers incorrectly measures, and submit an issue or PR with the file.
### Creating a new Plugin
*More information coming soon.*
#### Inspiration
Inspired by netroy's image-size library: https://github.com/netroy/image-size

@@ -9,12 +9,26 @@ 'use strict';

var detect = require('../lib/detect');
var pdf = require('../lib/types/pdf');
describe('detect', function () {
it('should return the first handler for which detect returns true', function () {
var pdfPath = path.resolve(__dirname + '/fixtures/pdf/123x456.1.pdf');
return popen(pdfPath, 'r')
.then(detect)
.then(function (handler) {
expect(handler).to.eql(pdf);
var txtPath = path.resolve(__dirname, 'fixtures/file.txt');
var fakeTruePlugin = {
detect: function (buffer) {
return buffer.toString('ascii', 0, 12) === 'A text file.';
}
};
var fakeFalsePlugin = {
detect: function () {
return false;
}
};
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);
});

@@ -24,6 +38,5 @@ });

it('should throw an error for an unsupported file type', function () {
var txtPath = path.resolve(__dirname + '/fixtures/txt/file.txt');
return popen(txtPath, 'r')
.then(function (fd) {
return expect(detect(fd)).to.be.rejectedWith(Error);
return expect(detect(fd, [fakeFalsePlugin])).to.be.rejectedWith(Error);
});

@@ -30,0 +43,0 @@ });

@@ -8,11 +8,29 @@ 'use strict';

describe('index', function () {
var pdfPath = path.resolve(__dirname + '/fixtures/pdf/123x456.1.pdf');
var expectedOutput = {
type: 'pdf',
pages: [{ width: 123, height: 456 }]
var txtPath = path.resolve(__dirname, 'fixtures/file.txt');
var pngPath = path.resolve(__dirname, 'fixtures/123x456.png');
var output = {
type: 'txt',
pages: [{ width: 0, height: 0 }]
};
it('should return the correct dimensions for 123x456.pdf with callbacks', function (done) {
calipers.measure(pdfPath, function (err, result) {
expect(result).to.eql(expectedOutput);
var fakeTruePlugin = {
detect: function (buffer) {
return buffer.toString('ascii', 0, 12) === 'A text file.';
},
measure: function () {
return output;
}
};
var fakeFalsePlugin = {
detect: function () {
return false;
}
};
it('works with callbacks', function (done) {
var calipers = require('../lib/index')(fakeFalsePlugin, 'png', fakeTruePlugin);
calipers.measure(txtPath, function (err, result) {
expect(result).to.eql(output);
done();

@@ -22,18 +40,18 @@ });

it('should return the correct dimensions for 123x456.pdf with promises', function () {
return calipers.measure(pdfPath)
.bind({})
it('works with promises', function () {
var calipers = require('../lib/index')(fakeFalsePlugin, fakeTruePlugin, 'png');
return calipers.measure(txtPath)
.then(function (result) {
this.result = result;
})
.finally(function () {
expect(this.result).to.eql(expectedOutput);
expect(result).to.eql(output);
});
});
it('should error for an unsupported file type', function () {
var txtPath = path.resolve(__dirname + '/fixtures/txt/file.txt');
return expect(calipers.measure(txtPath)).to.be.rejectedWith(Error);
it('works with required plugins', function () {
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 }] });
});
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc