probe-image-size
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,11 @@ | ||
1.2.0 / 2016-05-28 | ||
------------------ | ||
- Added `.sync.probe()` method. | ||
- Splited to separate files (simplify browserification). | ||
- Faster return on positive result & faster resource release. | ||
- Fix stream error handling. | ||
- 100% tests coverage. | ||
1.1.0 / 2016-05-25 | ||
@@ -2,0 +12,0 @@ ------------------ |
37
index.js
'use strict'; | ||
module.exports = require('./lib'); | ||
var probeStream = require('./stream'); | ||
var probeHttp = require('./http'); | ||
// Cache for promise implementation | ||
var P; | ||
/* eslint-disable consistent-return */ | ||
module.exports = function get_image_size(src, callback) { | ||
var prober; | ||
if (typeof src.on === 'function' && typeof src.emit === 'function') { | ||
// looks like an EventEmitter, treating it as a stream | ||
prober = probeStream; | ||
} else { | ||
prober = probeHttp; | ||
} | ||
if (!callback) { | ||
P = P || require('any-promise'); | ||
return new P(function (resolve, reject) { | ||
prober(src, function (err, data) { | ||
if (err) reject(err); | ||
else resolve(data); | ||
}); | ||
}); | ||
} | ||
prober(src, callback); | ||
}; | ||
module.exports.parsers = require('./lib/parsers_stream'); | ||
module.exports.sync = require('./sync'); |
@@ -37,8 +37,15 @@ 'use strict'; | ||
exports.str2arr = function (str) { | ||
var arr = new Array(str.length); | ||
exports.str2arr = function (str, format) { | ||
var arr = [], i = 0; | ||
for (var i = 0; i < arr.length; i++) { | ||
/* eslint-disable no-bitwise */ | ||
arr[i] = str.charCodeAt(i) & 0xFF; | ||
if (format && format === 'hex') { | ||
while (i < str.length) { | ||
arr.push(parseInt(str.slice(i, i + 2), 16)); | ||
i += 2; | ||
} | ||
} else { | ||
for (; i < str.length; i++) { | ||
/* eslint-disable no-bitwise */ | ||
arr.push(str.charCodeAt(i) & 0xFF); | ||
} | ||
} | ||
@@ -48,1 +55,32 @@ | ||
}; | ||
exports.readUInt16LE = function (data, offset) { | ||
return data[offset] | (data[offset + 1] << 8); | ||
}; | ||
exports.readUInt16BE = function (data, offset) { | ||
return data[offset + 1] | (data[offset] << 8); | ||
}; | ||
exports.readUInt32LE = function (data, offset) { | ||
return data[offset] | | ||
(data[offset + 1] << 8) | | ||
(data[offset + 2] << 16) | | ||
(data[offset + 3] * 0x1000000); | ||
}; | ||
exports.readUInt32BE = function (data, offset) { | ||
return data[offset + 3] | | ||
(data[offset + 2] << 8) | | ||
(data[offset + 1] << 16) | | ||
(data[offset] * 0x1000000); | ||
}; | ||
exports.error = function (message, code, status) { | ||
var err = new Error(message); | ||
if (code) err.code = code; | ||
if (status) err.status = status; | ||
return err; | ||
}; |
@@ -17,7 +17,2 @@ 'use strict'; | ||
parser.on('unpipe', function () { | ||
callback(); | ||
return; | ||
}); | ||
parser._bytes(26, function (data) { | ||
@@ -40,2 +35,4 @@ parser._skipBytes(Infinity); | ||
input.pipe(parser); | ||
return parser; | ||
}; |
@@ -18,7 +18,2 @@ 'use strict'; | ||
parser.on('unpipe', function () { | ||
callback(); | ||
return; | ||
}); | ||
parser._bytes(10, function (data) { | ||
@@ -41,2 +36,4 @@ parser._skipBytes(Infinity); | ||
input.pipe(parser); | ||
return parser; | ||
}; |
@@ -88,7 +88,2 @@ 'use strict'; | ||
parser.on('unpipe', function () { | ||
callback(); | ||
return; | ||
}); | ||
parser._bytes(2, function (data) { | ||
@@ -106,2 +101,4 @@ if (data[0] !== 0xFF || data[1] !== 0xD8) { | ||
input.pipe(parser); | ||
return parser; | ||
}; |
@@ -18,7 +18,2 @@ 'use strict'; | ||
parser.on('unpipe', function () { | ||
callback(); | ||
return; | ||
}); | ||
parser._bytes(24, function (data) { | ||
@@ -48,2 +43,4 @@ parser._skipBytes(Infinity); | ||
input.pipe(parser); | ||
return parser; | ||
}; |
@@ -17,7 +17,2 @@ 'use strict'; | ||
parser.on('unpipe', function () { | ||
callback(); | ||
return; | ||
}); | ||
parser._bytes(6, function (data) { | ||
@@ -43,2 +38,4 @@ // signature + version | ||
input.pipe(parser); | ||
return parser; | ||
}; |
@@ -28,5 +28,5 @@ 'use strict'; | ||
var type = readUInt16(data, data_offset + 2, is_big_endian); | ||
var values = readUInt16(data, data_offset + 4, is_big_endian); | ||
var values = readUInt32(data, data_offset + 4, is_big_endian); | ||
if (values !== 1 && (type !== 3 && type !== 4)) { | ||
if (values !== 1 || (type !== 3 && type !== 4)) { | ||
return null; | ||
@@ -44,13 +44,6 @@ } | ||
parser.on('unpipe', function () { | ||
callback(); | ||
return; | ||
}); | ||
// read header | ||
parser._bytes(8, function (data) { | ||
// check TIFF signature | ||
var sig = data; // signature is 4 bytes only, we keep all 8 to avoid unneeded ops | ||
if (!sliceEq(sig, 0, SIG_1) && !sliceEq(sig, 0, SIG_2)) { | ||
if (!sliceEq(data, 0, SIG_1) && !sliceEq(data, 0, SIG_2)) { | ||
parser._skipBytes(Infinity); | ||
@@ -83,5 +76,5 @@ callback(); | ||
parser._bytes(2, function (data) { | ||
var count = readUInt16(data, 0, is_big_endian) * 12; | ||
var ifd_size = readUInt16(data, 0, is_big_endian) * 12; | ||
if (count <= 0) { | ||
if (ifd_size <= 0) { | ||
parser._skipBytes(Infinity); | ||
@@ -93,3 +86,3 @@ callback(); | ||
// read all IFD entries | ||
parser._bytes(count, function (data) { | ||
parser._bytes(ifd_size, function (data) { | ||
parser._skipBytes(Infinity); | ||
@@ -99,3 +92,3 @@ | ||
for (i = 0; i < data.length; i += 12) { | ||
for (i = 0; i < ifd_size; i += 12) { | ||
tag = readUInt16(data, i, is_big_endian); | ||
@@ -124,2 +117,4 @@ | ||
input.pipe(parser); | ||
return parser; | ||
}; |
@@ -77,7 +77,2 @@ 'use strict'; | ||
parser.on('unpipe', function () { | ||
callback(); | ||
return; | ||
}); | ||
parser._bytes(16, function (data) { | ||
@@ -101,2 +96,4 @@ | ||
input.pipe(parser); | ||
return parser; | ||
}; |
{ | ||
"name": "probe-image-size", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Get image size without full download (JPG, GIF, PNG, WebP, BMP, TIFF, PSD)", | ||
@@ -21,10 +21,15 @@ "keywords": [ | ||
"index.js", | ||
"http.js", | ||
"stream.js", | ||
"sync.js", | ||
"lib/" | ||
], | ||
"scripts": { | ||
"test": "make test" | ||
"lint": "./node_modules/.bin/eslint .", | ||
"test": "npm run lint && ./node_modules/.bin/mocha", | ||
"coverage": "rm -rf coverage && ./node_modules/.bin/istanbul cover node_modules/.bin/_mocha", | ||
"report-coveralls": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" | ||
}, | ||
"dependencies": { | ||
"any-promise": "^1.3.0", | ||
"async": "^1.4.2", | ||
"readable-stream": "^2.1.4", | ||
@@ -35,2 +40,3 @@ "request": "^2.60.0", | ||
"devDependencies": { | ||
"coveralls": "^2.11.9", | ||
"eslint": "2.7.0", | ||
@@ -37,0 +43,0 @@ "from2": "^2.1", |
@@ -6,2 +6,3 @@ probe-image-size | ||
[![NPM version](https://img.shields.io/npm/v/probe-image-size.svg?style=flat)](https://www.npmjs.org/package/probe-image-size) | ||
[![Coverage Status](https://coveralls.io/repos/github/nodeca/probe-image-size/badge.svg?branch=master)](https://coveralls.io/github/nodeca/probe-image-size?branch=master) | ||
@@ -54,2 +55,8 @@ > Get image size without full download. Supported image types: | ||
}); | ||
// From a Buffer | ||
// | ||
var data = require('fs').readFileSync('image.jpg'); | ||
console.log(probe.sync(data)); // => { width: xx, height: yy, type: 'jpg', mime: 'image/jpeg' } | ||
``` | ||
@@ -89,7 +96,19 @@ | ||
__Note.__ If you use stream as source, it's your responsibility to terminate | ||
reading in callback. That will release resources as soon as possible. On | ||
http requests that's done automatically. | ||
__Note.__ If you use `Stream` as source, it's your responsibility to close that | ||
stream in callback. In other case you can get memory leak, because stream will | ||
be left in paused state. With http requests that's not a problem - everything | ||
is released automatically, as soon as possible. | ||
### sync.probe(src) -> result|null | ||
Sync version can eat arrays, typed arrays and buffers. On success it returns | ||
the same result as async version. On fail it returns null. | ||
__Note.__ Formats like JPEG & TIFF can store size anywhere (far from the head). | ||
That usually does not happens, but if you need guarantees - always provide full | ||
file content to sync methods. We strongly recommend to use async version | ||
as memory-friendly. | ||
Similar projects | ||
@@ -96,0 +115,0 @@ ---------------- |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
31239
4
25
827
122
0
5
- Removedasync@^1.4.2
- Removedasync@1.5.2(transitive)