sumchecker
Advanced tools
Comparing version 2.0.2 to 3.0.0
97
index.js
/* | ||
Copyright 2016 Mark Lee | ||
Copyright 2016, 2017, 2019 Mark Lee and contributors | ||
@@ -17,4 +17,2 @@ Licensed under the Apache License, Version 2.0 (the "License"); | ||
'use strict' | ||
const debug = require('debug')('sumchecker') | ||
@@ -24,4 +22,6 @@ const crypto = require('crypto') | ||
const path = require('path') | ||
const Promise = global.Promise || require('es6-promise').Promise | ||
const { promisify } = require('util') | ||
const readFile = promisify(fs.readFile) | ||
const CHECKSUM_LINE = /^([\da-fA-F]+) ([ *])(.+)$/ | ||
@@ -77,41 +77,28 @@ | ||
parseChecksumFile (data) { | ||
let that = this | ||
return new Promise((resolve, reject) => { | ||
debug('Parsing checksum file') | ||
that.checksums = {} | ||
let lineNumber = 0 | ||
data.trim().split(/[\r\n]+/).forEach(line => { | ||
lineNumber += 1 | ||
let result = CHECKSUM_LINE.exec(line) | ||
if (result === null) { | ||
debug(`Could not parse line number ${lineNumber}`) | ||
reject(new ChecksumParseError(lineNumber, line)) | ||
} else { | ||
// destructuring isn't available until Node 6 | ||
let filename = result[3] | ||
let isBinary = result[2] === '*' | ||
let checksum = result[1] | ||
debug('Parsing checksum file') | ||
this.checksums = {} | ||
let lineNumber = 0 | ||
for (const line of data.trim().split(/[\r\n]+/)) { | ||
lineNumber += 1 | ||
const result = CHECKSUM_LINE.exec(line) | ||
if (result === null) { | ||
debug(`Could not parse line number ${lineNumber}`) | ||
throw new ChecksumParseError(lineNumber, line) | ||
} else { | ||
result.shift() | ||
const [checksum, binaryMarker, filename] = result | ||
const isBinary = binaryMarker === '*' | ||
that.checksums[filename] = [checksum, isBinary] | ||
} | ||
}) | ||
debug('Parsed checksums:', that.checksums) | ||
resolve() | ||
}) | ||
this.checksums[filename] = [checksum, isBinary] | ||
} | ||
} | ||
debug('Parsed checksums:', this.checksums) | ||
} | ||
readFile (filename, binary) { | ||
async readFile (filename, binary) { | ||
debug(`Reading "${filename} (binary mode: ${binary})"`) | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(filename, this.encoding(binary), (err, data) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve(data) | ||
} | ||
}) | ||
}) | ||
return readFile(filename, this.encoding(binary)) | ||
} | ||
validate (baseDir, filesToCheck) { | ||
async validate (baseDir, filesToCheck) { | ||
if (typeof filesToCheck === 'string') { | ||
@@ -121,14 +108,12 @@ filesToCheck = [filesToCheck] | ||
return this.readFile(this.checksumFilename, false) | ||
.then(this.parseChecksumFile.bind(this)) | ||
.then(() => { | ||
return this.validateFiles(baseDir, filesToCheck) | ||
}) | ||
const data = await this.readFile(this.checksumFilename, false) | ||
this.parseChecksumFile(data) | ||
return this.validateFiles(baseDir, filesToCheck) | ||
} | ||
validateFile (baseDir, filename) { | ||
async validateFile (baseDir, filename) { | ||
return new Promise((resolve, reject) => { | ||
debug(`validateFile: ${filename}`) | ||
let metadata = this.checksums[filename] | ||
const metadata = this.checksums[filename] | ||
if (!metadata) { | ||
@@ -138,14 +123,11 @@ return reject(new NoChecksumFoundError(filename)) | ||
// destructuring isn't available until Node 6 | ||
let checksum = metadata[0] | ||
let binary = metadata[1] | ||
let fullPath = path.resolve(baseDir, filename) | ||
const [checksum, binary] = metadata | ||
const fullPath = path.resolve(baseDir, filename) | ||
debug(`Reading file with "${this.encoding(binary)}" encoding`) | ||
let stream = fs.createReadStream(fullPath, {encoding: this.encoding(binary)}) | ||
let hasher = crypto.createHash(this.algorithm, {defaultEncoding: 'binary'}) | ||
const stream = fs.createReadStream(fullPath, { encoding: this.encoding(binary) }) | ||
const hasher = crypto.createHash(this.algorithm, { defaultEncoding: 'binary' }) | ||
hasher.on('readable', () => { | ||
let data = hasher.read() | ||
const data = hasher.read() | ||
if (data) { | ||
let calculated = data.toString('hex') | ||
const calculated = data.toString('hex') | ||
@@ -164,11 +146,8 @@ debug(`Expected checksum: ${checksum}; Actual: ${calculated}`) | ||
validateFiles (baseDir, filesToCheck) { | ||
let that = this | ||
return Promise.all(filesToCheck.map((filename) => { | ||
return that.validateFile(baseDir, filename) | ||
})) | ||
async validateFiles (baseDir, filesToCheck) { | ||
return Promise.all(filesToCheck.map(filename => this.validateFile(baseDir, filename))) | ||
} | ||
} | ||
let sumchecker = function sumchecker (algorithm, checksumFilename, baseDir, filesToCheck) { | ||
const sumchecker = async function sumchecker (algorithm, checksumFilename, baseDir, filesToCheck) { | ||
return new ChecksumValidator(algorithm, checksumFilename).validate(baseDir, filesToCheck) | ||
@@ -175,0 +154,0 @@ } |
12
NEWS.md
@@ -5,2 +5,14 @@ # Changes by Version | ||
## [3.0.0] - 2019-04-30 | ||
[3.0.0]: https://github.com/malept/sumchecker/compare/v2.0.2...v3.0.0 | ||
### Added | ||
* TypeScript typings file (#12) | ||
### Removed | ||
* Support for Node < 8 (#10) | ||
## [2.0.2] - 2017-03-11 | ||
@@ -7,0 +19,0 @@ |
{ | ||
"name": "sumchecker", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"author": "Mark Lee", | ||
@@ -8,2 +8,3 @@ "license": "Apache-2.0", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"repository": { | ||
@@ -22,29 +23,55 @@ "type": "git", | ||
"engines": { | ||
"node": ">= 4.0" | ||
"node": ">= 8.0" | ||
}, | ||
"devDependencies": { | ||
"codeclimate-test-reporter": "^0.4.0", | ||
"eslint": "^3.3.1", | ||
"eslint-config-standard": "^6.0.0-beta.3", | ||
"eslint-plugin-promise": "^3.3.0", | ||
"eslint-plugin-standard": "^2.0.0", | ||
"intern": "^3.3.1", | ||
"nyc": "^8.1.0" | ||
"ava": "^1.4.1", | ||
"codecov": "^3.3.0", | ||
"eslint": "^5.16.0", | ||
"eslint-config-standard": "^12.0.0", | ||
"eslint-plugin-ava": "^6.0.0", | ||
"eslint-plugin-import": "^2.17.2", | ||
"eslint-plugin-node": "^8.0.1", | ||
"eslint-plugin-promise": "^4.0.1", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"nyc": "^14.0.0", | ||
"tsd": "^0.7.2" | ||
}, | ||
"dependencies": { | ||
"debug": "^2.2.0" | ||
"debug": "^4.1.0" | ||
}, | ||
"scripts": { | ||
"ci": "eslint . && intern-client config=tests/intern suites=tests/index reporters=Lcov reporters=Console && codeclimate-test-reporter < lcov.info", | ||
"test": "eslint . && intern-client config=tests/intern suites=tests/index reporters=Pretty" | ||
"ava": "ava test/index.js", | ||
"ci": "npm run lint && npm run coverage && npm run tsd", | ||
"codecov": "nyc report --reporter=text-lcov > coverage.lcov && codecov", | ||
"coverage": "nyc ava test/index.js", | ||
"lint": "eslint .", | ||
"test": "npm run lint && npm run ava && npm run tsd", | ||
"tsd": "tsd" | ||
}, | ||
"ava": { | ||
"babel": false, | ||
"compileEnhancements": false | ||
}, | ||
"eslintConfig": { | ||
"env": { | ||
"amd": true | ||
}, | ||
"extends": "standard", | ||
"parserOptions": { | ||
"sourceType": "script" | ||
}, | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:ava/recommended", | ||
"plugin:import/errors", | ||
"plugin:import/warnings", | ||
"plugin:node/recommended", | ||
"plugin:promise/recommended", | ||
"standard" | ||
], | ||
"plugins": [ | ||
"ava" | ||
], | ||
"rules": { | ||
"node/no-unpublished-require": [ | ||
"error", | ||
{ | ||
"allowModules": [ | ||
"ava" | ||
] | ||
} | ||
], | ||
"strict": [ | ||
@@ -51,0 +78,0 @@ "error" |
@@ -5,5 +5,6 @@ # Sumchecker | ||
[![AppVeyor CI](https://ci.appveyor.com/api/projects/status/wm4n2r11nlff8ify?svg=true)](https://ci.appveyor.com/project/malept/sumchecker) | ||
[![Code Climate](https://codeclimate.com/github/malept/sumchecker/badges/gpa.svg)](https://codeclimate.com/github/malept/sumchecker) | ||
[![Test Coverage](https://codeclimate.com/github/malept/sumchecker/badges/coverage.svg)](https://codeclimate.com/github/malept/sumchecker/coverage) | ||
[![Code Coverage](https://codecov.io/gh/malept/sumchecker/branch/master/graph/badge.svg)](https://codecov.io/gh/malept/sumchecker) | ||
![Dependency Status](https://tidelift.com/badges/github/malept/sumchecker) | ||
Sumchecker is a pure Node.js solution to validating files specified in a checksum file, which are | ||
@@ -15,12 +16,14 @@ usually generated by programs such as [`sha256sum`](https://en.wikipedia.org/wiki/Sha256sum). | ||
```javascript | ||
sumchecker(algorithm, checksumFilename, baseDir, filesToCheck) | ||
.then(() => { | ||
console.log('All files validate!'); | ||
}, (error) => { | ||
console.error('An error occurred', error); | ||
}); | ||
const sumchecker = require('sumchecker'); | ||
try { | ||
await sumchecker(algorithm, checksumFilename, baseDir, filesToCheck); | ||
console.log('All files validate!'); | ||
} catch (error) { | ||
console.error('An error occurred', error); | ||
} | ||
``` | ||
Returns a [`Promise`](https://www.promisejs.org/). The promise is resolved when all files specified | ||
in [`filesToCheck`](#filesToCheck) are validated. The promise is rejected otherwise. | ||
Returns a [`Promise`]. The promise is resolved when all files specified in | ||
[`filesToCheck`](#filesToCheck) are validated. The promise is rejected otherwise. | ||
@@ -74,4 +77,13 @@ ### Parameters | ||
* `filename` (`String`)- a filename from [`filesToCheck`](#filesToCheck) | ||
* `filename` (`String`) - a filename from [`filesToCheck`](#filesToCheck) | ||
## Support | ||
[Get supported sumchecker with the Tidelift Subscription](https://tidelift.com/subscription/pkg/npm-sumchecker?utm_source=npm-sumchecker&utm_medium=referral&utm_campaign=readme). | ||
## Security contact information | ||
To report a security vulnerability, please use the [Tidelift security | ||
contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. | ||
## Legal | ||
@@ -82,3 +94,3 @@ | ||
[`crypto.createHash()`]: https://nodejs.org/dist/latest-v4.x/docs/api/crypto.html#crypto_crypto_createhash_algorithm | ||
[`Promise.all`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all | ||
[`Promise`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise | ||
[Apache 2.0 License]: http://www.apache.org/licenses/LICENSE-2.0 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50562
12
93
1
11
190
1
+ Addeddebug@4.3.7(transitive)
+ Addedms@2.1.3(transitive)
- Removeddebug@2.6.9(transitive)
- Removedms@2.0.0(transitive)
Updateddebug@^4.1.0