sha1-file
Advanced tools
Comparing version 0.0.5 to 1.0.0
46
index.js
@@ -1,25 +0,27 @@ | ||
'use strict'; | ||
'use strict' | ||
var crypto = require('crypto'); | ||
var fs = require('fs'); | ||
var crypto = require('crypto') | ||
var fs = require('fs') | ||
module.exports = function (filename) { | ||
var sum = crypto.createHash('sha1'); | ||
sum.update(fs.readFileSync(filename)); | ||
return sum.digest('hex'); | ||
}; | ||
// if `strict` then throw error otherwise pass error through | ||
module.exports.async = function (filename, callback, strict) { | ||
fs.readFile(filename, function (error, data) { | ||
if (error) { | ||
if (strict) { | ||
throw error; | ||
module.exports = function (filename, callback) { | ||
var sum = crypto.createHash('sha1') | ||
if (callback && typeof callback === 'function') { | ||
var fileStream = fs.createReadStream(filename) | ||
fileStream.on('error', function (err) { | ||
return callback(err, null) | ||
}) | ||
fileStream.on('data', function (chunk) { | ||
try { | ||
sum.update(chunk) | ||
} catch (ex) { | ||
return callback(ex, null) | ||
} | ||
return callback(error); | ||
} | ||
var sum = crypto.createHash('sha1'); | ||
sum.update(data); | ||
return callback(sum.digest('hex')); | ||
}); | ||
}; | ||
}) | ||
fileStream.on('end', function () { | ||
return callback(null, sum.digest('hex')) | ||
}) | ||
} else { | ||
sum.update(fs.readFileSync(filename)) | ||
return sum.digest('hex') | ||
} | ||
} |
@@ -5,3 +5,3 @@ # License | ||
Copyright (c) 2014 Rory J. Bradford. | ||
Copyright (c) 2015 Rory Bradford. | ||
@@ -8,0 +8,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
@@ -7,5 +7,6 @@ { | ||
"test.js", | ||
"LICENSE.md" | ||
"LICENSE.md", | ||
"README.md" | ||
], | ||
"version": "0.0.5", | ||
"version": "1.0.0", | ||
"description": "return an sha1sum of a given file", | ||
@@ -33,4 +34,7 @@ "keywords": [ | ||
"scripts": { | ||
"test": "node test" | ||
"test": "standard && node test" | ||
}, | ||
"devDependencies": { | ||
"standard": "^3.8.0" | ||
} | ||
} |
@@ -1,4 +0,4 @@ | ||
# sha1-file [![Build Status](https://travis-ci.org/roryrjb/sha1-file.svg?branch=master)](https://travis-ci.org/roryrjb/sha1-file) | ||
# sha1-file [![Build Status](https://travis-ci.org/roryrjb/sha1-file.svg?branch=master)](https://travis-ci.org/roryrjb/sha1-file) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) | ||
> Simply return an `sha1` sum of a given file. | ||
> Simply return an `sha1` sum of a given file. If using async version (by including callback), it will stream; successfully tested on files 4 GB+. | ||
@@ -19,38 +19,17 @@ ### Installation | ||
_Sync:_ | ||
__sha1File(path, [callback])__ | ||
__sha1File(path)__ | ||
```javascript | ||
var sha1File = require('sha1-file'); | ||
var sha1File = require('sha1-file') | ||
sha1File('path/to/a_file'); // '18e904aae79b5642ed7975c0a0074936' | ||
``` | ||
// sync (no callback) | ||
_Async:_ | ||
sha1File('./path/to/a_file') // 'c8a2e2125f94492082bc484044edb4dc837f83b' | ||
__sha1File(path, callback, [strict])__ | ||
// async/streamed (if using callback) | ||
If _strict_ is `true` and there is an error it will `throw` it, otherwise it will pass an error string through the callback. | ||
```javascript | ||
sha1File.async('./README.md', function (data) { | ||
console.log(data); | ||
}); | ||
sha1File.async('./README.md', function (data) { | ||
console.log(data); | ||
}, true); | ||
// errors | ||
// non-strict: will pass through an error to `data` | ||
sha1File.async('./null', function (data) { | ||
console.log(data); | ||
}); | ||
// strict: will throw an error | ||
sha1File.async('./null', function (data) { | ||
console.log(data); | ||
}, true); | ||
sha1File('./path/to/a_file', function (error, sum) { | ||
if (error) return console.log(error) | ||
console.log(sum) // 'c8a2e2125f94492082bc484044edb4dc837f83b' | ||
}) | ||
``` | ||
@@ -57,0 +36,0 @@ |
34
test.js
@@ -1,22 +0,26 @@ | ||
'use strict'; | ||
'use strict' | ||
var sha1 = require('./index'); | ||
var assert = require('assert'); | ||
var sha1File = require('./index') | ||
var assert = require('assert') | ||
var filename = 'LICENSE.md' | ||
var preCheckedSum = '3c8a2e2125f94492082bc484044edb4dc837f83b' | ||
assert.equal(sha1('./LICENSE.md'), '635cf3c238455570c909d24a5237050cbb1837e9'); | ||
sha1File(filename, function (error, sum) { | ||
console.log('sum = ' + sum) | ||
assert(error === null) | ||
assert(sum === preCheckedSum) | ||
console.log('Pass 2/2') | ||
}) | ||
sha1.async('./LICENSE.md', function (data) { | ||
assert.equal(data, '635cf3c238455570c909d24a5237050cbb1837e9'); | ||
}); | ||
var syncSum = sha1File(filename) | ||
sha1.async('./LICENSE.md', function (data) { | ||
assert.equal(data, '635cf3c238455570c909d24a5237050cbb1837e9'); | ||
}, true); | ||
assert(syncSum === preCheckedSum) | ||
console.log('sum = ' + syncSum) | ||
console.log('Pass 1/2') | ||
// errors | ||
// non-strict: will pass through an error to `data` | ||
sha1.async('./null', function (data) { | ||
assert.equal(data.code, 'ENOENT'); | ||
}); | ||
sha1File('does not exist', function (error, sum) { | ||
assert(error) | ||
assert(!sum) | ||
}) |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4061
45
1
1
39