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

sha.js « utils « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 17b8c38f9c8c2810507c53ab9363c249db1c4f3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

var fs = require("graceful-fs")
  , crypto = require("crypto")
  , log = require("./log.js")
  , binding

try { binding = process.binding("crypto") }
catch (e) { binding = null }

exports.check = check
exports.get = get

function check (file, sum, cb) {
  if (!binding) {
    log.warn("crypto binding not found. Cannot verify shasum.", "shasum")
    return cb()
  }
  get(file, function (er, actual) {
    if (er) return log.er(cb, "Error getting shasum")(er)
    var expected = sum.toLowerCase().trim()
      , ok = actual === expected
    cb(ok ? null : new Error(
      "shasum check failed for "+file+"\n"
      +"Expected: "+expected+"\n"
      +"Actual:   "+actual))
  })
}

function get (file, cb) {
  if (!binding) {
    log.warn("crypto binding not found. Cannot verify shasum.", "shasum")
    return cb()
  }
  var h = crypto.createHash("sha1")
    , s = fs.createReadStream(file)
    , errState = null
  s.on("error", function (er) {
    if (errState) return
    log.silly(er.stack || er.message, "sha error")
    return cb(errState = er)
  }).on("data", function (chunk) {
    if (errState) return
    log.silly(chunk.length, "updated sha bytes")
    h.update(chunk)
  }).on("end", function () {
    if (errState) return
    var actual = h.digest("hex").toLowerCase().trim()
    log(actual+"\n"+file, "shasum")
    cb(null, actual)
  })
}