Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2013-07-22 23:14:47 +0400
committerisaacs <i@izs.me>2013-07-22 23:15:03 +0400
commit786e06ef81265351c73f2395b0f6dbdb5a5f01c8 (patch)
tree7d0c15aa644a07b874d3a22ceb09407af1803e2a /node_modules/sha/index.js
parent349b6983bdefdbc634efbca0b54bdb8c9ff71965 (diff)
Bump many deps for graceful-fs@2 upgrade
Diffstat (limited to 'node_modules/sha/index.js')
-rw-r--r--node_modules/sha/index.js75
1 files changed, 66 insertions, 9 deletions
diff --git a/node_modules/sha/index.js b/node_modules/sha/index.js
index 8e4e13683..4d05a1888 100644
--- a/node_modules/sha/index.js
+++ b/node_modules/sha/index.js
@@ -1,3 +1,6 @@
+'use strict'
+
+var Transform = require('stream').Transform || require('readable-stream').Transform
var crypto = require('crypto')
var fs
try {
@@ -15,34 +18,55 @@ try {
}
exports.check = check
+exports.checkSync = checkSync
exports.get = get
+exports.getSync = getSync
+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(
'shasum check failed for ' + file + '\n'
+ 'Expected: ' + expected + '\n'
+ 'Actual: ' + actual))
})
}
+function checkSync(file, expected, options, cb) {
+ expected = expected.toLowerCase().trim()
+ var actual
+ try {
+ actual = getSync(file, options)
+ } catch (er) {
+ if (er.message) er.message += ' while getting shasum for ' + file
+ throw er
+ }
+ if (actual !== expected) {
+ var ex = new Error(
+ 'shasum check failed for ' + file + '\n'
+ + 'Expected: ' + expected + '\n'
+ + 'Actual: ' + actual)
+ throw ex
+ }
+}
+
function get(file, options, cb) {
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)
var source = fs.createReadStream(file)
var errState = null
@@ -61,3 +85,36 @@ function get(file, options, cb) {
cb(null, actual)
})
}
+
+function getSync(file, options) {
+ options = options || {}
+ var algorithm = options.algorithm || 'sha1'
+ var hash = crypto.createHash(algorithm)
+ var source = fs.readFileSync(file)
+ hash.update(source)
+ return hash.digest("hex").toLowerCase().trim()
+}
+
+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
+} \ No newline at end of file