Comparing version 1.0.1 to 1.1.0
44
index.js
@@ -0,1 +1,2 @@ | ||
var Transform = require('stream').Transform || require('readable-stream').Transform | ||
var crypto = require('crypto') | ||
@@ -19,15 +20,16 @@ var fs | ||
exports.get = get | ||
exports.stream = stream | ||
function check(file, expected, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = undefined; | ||
cb = options | ||
options = undefined | ||
} | ||
expected = expected.toLowerCase().trim(); | ||
expected = expected.toLowerCase().trim() | ||
get(file, options, function (er, actual) { | ||
if (er) { | ||
if (er.message) er.message += ' while getting shasum for ' + file; | ||
if (er.message) er.message += ' while getting shasum for ' + file | ||
return cb(er) | ||
} | ||
if (actual === expected) return cb(null); | ||
if (actual === expected) return cb(null) | ||
cb(new Error( | ||
@@ -42,7 +44,7 @@ 'shasum check failed for ' + file + '\n' | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = undefined; | ||
cb = options | ||
options = undefined | ||
} | ||
options = options || {}; | ||
var algorithm = options.algorithm || 'sha1'; | ||
options = options || {} | ||
var algorithm = options.algorithm || 'sha1' | ||
var hash = crypto.createHash(algorithm) | ||
@@ -66,1 +68,25 @@ var source = fs.createReadStream(file) | ||
} | ||
function stream(expected, options) { | ||
expected = expected.toLowerCase().trim() | ||
options = options || {} | ||
var algorithm = options.algorithm || 'sha1' | ||
var hash = crypto.createHash(algorithm) | ||
var stream = new Transform() | ||
stream._transform = function (chunk, encoding, callback) { | ||
hash.update(chunk) | ||
stream.push(chunk) | ||
callback() | ||
} | ||
stream._flush = function (cb) { | ||
var actual = hash.digest("hex").toLowerCase().trim() | ||
if (actual === expected) return cb(null) | ||
cb(new Error( | ||
'shasum check failed for:\n' | ||
+ ' Expected: ' + expected + '\n' | ||
+ ' Actual: ' + actual)) | ||
this.push(null) | ||
} | ||
return stream | ||
} |
{ | ||
"name": "sha", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Check and get file hashes", | ||
@@ -14,3 +14,4 @@ "scripts": { | ||
"optionalDependencies": { | ||
"graceful-fs": "1.2" | ||
"graceful-fs": "1.2", | ||
"readable-stream": "1.0" | ||
}, | ||
@@ -17,0 +18,0 @@ "devDependencies": { |
@@ -7,2 +7,3 @@ # sha | ||
[![Dependency Status](https://gemnasium.com/ForbesLindesay/sha.png)](https://gemnasium.com/ForbesLindesay/sha) | ||
[![NPM version](https://badge.fury.io/js/sha.png)](http://badge.fury.io/js/sha) | ||
@@ -31,4 +32,20 @@ ## Installation | ||
### stream(expected, [options]) | ||
Check the hash of a stream without ever buffering it. This is a pass through stream so you can do things like: | ||
```js | ||
fs.createReadStream('src') | ||
.pipe(sha.stream('expected')) | ||
.pipe(fs.createWriteStream('dest')) | ||
``` | ||
`dest` will be a complete copy of `src` and an error will be emitted if the hash did not match `'expected'`. | ||
Options: | ||
- algorithm: defaults to `sha1` and can be any of the algorithms supported by `crypto.createHash` | ||
## License | ||
BSD | ||
You may use this software under the BSD or MIT. Take your pick. If you want me to release it under another license, open a pull request. |
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license
Found 1 instance in 1 package
6885
5
83
49
4
2
80