index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict'
  2. var Transform = require('stream').Transform
  3. var crypto = require('crypto')
  4. var fs = require('graceful-fs')
  5. exports.check = check
  6. exports.checkSync = checkSync
  7. exports.get = get
  8. exports.getSync = getSync
  9. exports.stream = stream
  10. function check(file, expected, options, cb) {
  11. if (typeof options === 'function') {
  12. cb = options
  13. options = undefined
  14. }
  15. expected = expected.toLowerCase().trim()
  16. get(file, options, function (er, actual) {
  17. if (er) {
  18. if (er.message) er.message += ' while getting shasum for ' + file
  19. return cb(er)
  20. }
  21. if (actual === expected) return cb(null)
  22. cb(new Error(
  23. 'shasum check failed for ' + file + '\n'
  24. + 'Expected: ' + expected + '\n'
  25. + 'Actual: ' + actual))
  26. })
  27. }
  28. function checkSync(file, expected, options) {
  29. expected = expected.toLowerCase().trim()
  30. var actual
  31. try {
  32. actual = getSync(file, options)
  33. } catch (er) {
  34. if (er.message) er.message += ' while getting shasum for ' + file
  35. throw er
  36. }
  37. if (actual !== expected) {
  38. var ex = new Error(
  39. 'shasum check failed for ' + file + '\n'
  40. + 'Expected: ' + expected + '\n'
  41. + 'Actual: ' + actual)
  42. throw ex
  43. }
  44. }
  45. function get(file, options, cb) {
  46. if (typeof options === 'function') {
  47. cb = options
  48. options = undefined
  49. }
  50. options = options || {}
  51. var algorithm = options.algorithm || 'sha1'
  52. var hash = crypto.createHash(algorithm)
  53. var source = fs.createReadStream(file)
  54. var errState = null
  55. source
  56. .on('error', function (er) {
  57. if (errState) return
  58. return cb(errState = er)
  59. })
  60. .on('data', function (chunk) {
  61. if (errState) return
  62. hash.update(chunk)
  63. })
  64. .on('end', function () {
  65. if (errState) return
  66. var actual = hash.digest("hex").toLowerCase().trim()
  67. cb(null, actual)
  68. })
  69. }
  70. function getSync(file, options) {
  71. options = options || {}
  72. var algorithm = options.algorithm || 'sha1'
  73. var hash = crypto.createHash(algorithm)
  74. var source = fs.readFileSync(file)
  75. hash.update(source)
  76. return hash.digest("hex").toLowerCase().trim()
  77. }
  78. function stream(expected, options) {
  79. expected = expected.toLowerCase().trim()
  80. options = options || {}
  81. var algorithm = options.algorithm || 'sha1'
  82. var hash = crypto.createHash(algorithm)
  83. var stream = new Transform()
  84. stream._transform = function (chunk, encoding, callback) {
  85. hash.update(chunk)
  86. stream.push(chunk)
  87. callback()
  88. }
  89. stream._flush = function (cb) {
  90. var actual = hash.digest("hex").toLowerCase().trim()
  91. if (actual === expected) return cb(null)
  92. cb(new Error(
  93. 'shasum check failed for:\n'
  94. + ' Expected: ' + expected + '\n'
  95. + ' Actual: ' + actual))
  96. this.push(null)
  97. }
  98. return stream
  99. }