From bbbfeff840f26973fbffc16d4863ae6d120079f3 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Sat, 4 May 2019 10:34:59 +0000 Subject: [PATCH] Require Node.js 8, add TypeScript definition (#12) --- .editorconfig | 2 +- .gitattributes | 3 +- .gitignore | 1 + .npmrc | 1 + .travis.yml | 1 - index.d.ts | 23 +++++++++++++++ index.js | 14 ++++----- index.test-d.ts | 11 +++++++ license | 20 +++---------- package.json | 77 +++++++++++++++++++++++++------------------------ readme.md | 8 ++--- 11 files changed, 93 insertions(+), 68 deletions(-) create mode 100644 .npmrc create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/.editorconfig b/.editorconfig index 98a761d..1c6314a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,6 +7,6 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true -[{package.json,*.yml}] +[*.yml] indent_style = space indent_size = 2 diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.gitignore b/.gitignore index 3c3629e..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +yarn.lock diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml index e155464..f98fed0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,3 @@ node_js: - '12' - '10' - '8' - - '6' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..a2d1e96 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,23 @@ +/// + +/** +Create a MD5 hash with hex encoding. + +@param data - Prefer buffers as they're faster to hash, but strings can be useful for small things. + +Pass an array instead of concatenating strings and/or buffers. The output is the same, but arrays do not incur the overhead of concatenation. + +@example +``` +import * as fs from 'fs'; +import md5Hex = require('md5-hex'); + +const buffer = fs.readFileSync('unicorn.png'); + +md5Hex(buffer); +//=> '1abcb33beeb811dca15f0ac3e47b88d9' +``` +*/ +declare function md5Hex(data: Buffer | string | ReadonlyArray): string; + +export = md5Hex; diff --git a/index.js b/index.js index 82cfae3..5dd5aab 100644 --- a/index.js +++ b/index.js @@ -1,22 +1,22 @@ 'use strict'; const crypto = require('crypto'); -module.exports = function (input) { +module.exports = function (data) { const hash = crypto.createHash('md5'); - const update = buf => { - const inputEncoding = typeof buf === 'string' ? 'utf8' : undefined; - hash.update(buf, inputEncoding); + const update = buffer => { + const inputEncoding = typeof buffer === 'string' ? 'utf8' : undefined; + hash.update(buffer, inputEncoding); }; if (arguments.length > 1) { throw new Error('Too many arguments. Try specifying an array.'); } - if (Array.isArray(input)) { - input.forEach(update); + if (Array.isArray(data)) { + data.forEach(update); } else { - update(input); + update(data); } return hash.digest('hex'); diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..2105e7d --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,11 @@ +import {expectType} from 'tsd'; +import * as fs from 'fs'; +import md5Hex = require('.'); + +const buffer = fs.readFileSync('unicorn.png'); + +expectType(md5Hex(buffer)); +expectType(md5Hex([buffer])); +expectType(md5Hex('foo')); +expectType(md5Hex(['foo'])); +expectType(md5Hex([buffer, 'foo'])); diff --git a/license b/license index 654d0bf..e7af2f7 100644 --- a/license +++ b/license @@ -1,21 +1,9 @@ -The MIT License (MIT) +MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) -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: +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: -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/package.json b/package.json index a87ce15..2fcde14 100644 --- a/package.json +++ b/package.json @@ -1,39 +1,42 @@ { - "name": "md5-hex", - "version": "2.0.0", - "description": "Create a MD5 hash with hex encoding", - "license": "MIT", - "repository": "sindresorhus/md5-hex", - "author": { - "name": "Sindre Sorhus", - "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" - }, - "engines": { - "node": ">=4" - }, - "scripts": { - "test": "xo && ava" - }, - "files": [ - "index.js", - "browser.js" - ], - "keywords": [ - "hash", - "crypto", - "md5", - "hex", - "buffer", - "browser", - "browserify" - ], - "dependencies": { - "md5-o-matic": "^0.1.1" - }, - "devDependencies": { - "ava": "*", - "xo": "*" - }, - "browser": "browser.js" + "name": "md5-hex", + "version": "2.0.0", + "description": "Create a MD5 hash with hex encoding", + "license": "MIT", + "repository": "sindresorhus/md5-hex", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=8" + }, + "scripts": { + "test": "xo && ava && tsd" + }, + "files": [ + "index.js", + "index.d.ts", + "browser.js" + ], + "keywords": [ + "hash", + "crypto", + "md5", + "hex", + "buffer", + "browser", + "browserify" + ], + "dependencies": { + "md5-o-matic": "^0.1.1" + }, + "devDependencies": { + "@types/node": "^12.0.0", + "ava": "^1.4.1", + "tsd": "^0.7.2", + "xo": "^0.24.0" + }, + "browser": "browser.js" } diff --git a/readme.md b/readme.md index fb995ef..bab0b78 100644 --- a/readme.md +++ b/readme.md @@ -12,7 +12,7 @@ Checkout [`hasha`](https://github.com/sindresorhus/hasha) if you need something ## Install ``` -$ npm install --save md5-hex +$ npm install md5-hex ``` @@ -30,11 +30,11 @@ md5Hex(buffer); ## API -### md5Hex(input) +### md5Hex(data) -#### input +#### data -Type: `Buffer` `string` `Buffer[]` `string[]` +Type: `Buffer | string | Array` Prefer buffers as they're faster to hash, but strings can be useful for small things.