Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
0neSe7en committed Sep 5, 2017
1 parent 561e030 commit f40a921
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[*]
end_of_line = lf
insert_final_newline = true

[*.js]
indent_style = space
indent_size = 2

[{package.json}]
indent_style = space
indent_size = 2
63 changes: 63 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const crypto = require('crypto');
const execa = require('execa');
const platform = require('os').platform();
const fs = require('fs');
const MD5SUMREGEX = /^(.{32})/;
const MACMD5REGEX = /^MD5.+=\s*(.+)$/g;

/**
* Calc file md5.
* @param {String} fileName
* @return {Promise} md5 value.
*/
function calcMd5(fileName) {
const readStream = fs.createReadStream(fileName);
let md5 = crypto.createHash('md5');
return new Promise((resolve, reject) => {
readStream.on('data', chunk => md5.update(chunk));
readStream.on('error', reject);
readStream.on('end', () => {
let hash = md5.digest('hex');
resolve(hash);
})
})
}

/**
*
* @param {String} command
* @param {String} fileName
* @param {RegExp} regex
* @return {Promise.<String>}
*/
function nativeMd5(command, fileName, regex) {
return execa(command, [fileName]).then(res => {
const matches = regex.exec(res.stdout);
if (matches.length === 2) {
return matches[1]
} else {
return calcMd5(fileName);
}
}).catch(err => {
console.error(err);
return calcMd5(fileName);
})
}

/**
*
* @param fileName
* @return {Promise}
*/
module.exports = (fileName) => {
switch (platform) {
case 'darwin':
return nativeMd5('md5', fileName, MACMD5REGEX);
break;
case 'linux':
return nativeMd5('md5sum', fileName, MD5SUMREGEX);
break;
default:
return calcMd5(fileName);
}
}
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "native-md5",
"version": "0.0.1",
"description": "Calculate file md5 via md5sum on Linux and md5 on Mac.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/0neSe7en/node-native-md5.git"
},
"keywords": [
"md5",
"native",
"linux",
"mac",
"file"
],
"author": "0neSe7en",
"license": "MIT",
"bugs": {
"url": "https://github.com/0neSe7en/node-native-md5/issues"
},
"homepage": "https://github.com/0neSe7en/node-native-md5#readme",
"dependencies": {
"execa": "^0.8.0"
}
}
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const md5 = require('./index');
md5('/Users/wsy/dev/node/node-native-md5/node_modules/execa/index.js').then(res => {
console.log(res);
})
88 changes: 88 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


cross-spawn@^5.0.1:
version "5.1.0"
resolved "http://registry.npm.taobao.org/cross-spawn/download/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
dependencies:
lru-cache "^4.0.1"
shebang-command "^1.2.0"
which "^1.2.9"

execa@^0.8.0:
version "0.8.0"
resolved "http://registry.npm.taobao.org/execa/download/execa-0.8.0.tgz#d8d76bbc1b55217ed190fd6dd49d3c774ecfc8da"
dependencies:
cross-spawn "^5.0.1"
get-stream "^3.0.0"
is-stream "^1.1.0"
npm-run-path "^2.0.0"
p-finally "^1.0.0"
signal-exit "^3.0.0"
strip-eof "^1.0.0"

get-stream@^3.0.0:
version "3.0.0"
resolved "http://registry.npm.taobao.org/get-stream/download/get-stream-3.0.0.tgz#8e943d1358dc37555054ecbe2edb05aa174ede14"

is-stream@^1.1.0:
version "1.1.0"
resolved "http://registry.npm.taobao.org/is-stream/download/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"

isexe@^2.0.0:
version "2.0.0"
resolved "http://registry.npm.taobao.org/isexe/download/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"

lru-cache@^4.0.1:
version "4.1.1"
resolved "http://registry.npm.taobao.org/lru-cache/download/lru-cache-4.1.1.tgz#622e32e82488b49279114a4f9ecf45e7cd6bba55"
dependencies:
pseudomap "^1.0.2"
yallist "^2.1.2"

npm-run-path@^2.0.0:
version "2.0.2"
resolved "http://registry.npm.taobao.org/npm-run-path/download/npm-run-path-2.0.2.tgz#35a9232dfa35d7067b4cb2ddf2357b1871536c5f"
dependencies:
path-key "^2.0.0"

p-finally@^1.0.0:
version "1.0.0"
resolved "http://registry.npm.taobao.org/p-finally/download/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae"

path-key@^2.0.0:
version "2.0.1"
resolved "http://registry.npm.taobao.org/path-key/download/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"

pseudomap@^1.0.2:
version "1.0.2"
resolved "http://registry.npm.taobao.org/pseudomap/download/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"

shebang-command@^1.2.0:
version "1.2.0"
resolved "http://registry.npm.taobao.org/shebang-command/download/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea"
dependencies:
shebang-regex "^1.0.0"

shebang-regex@^1.0.0:
version "1.0.0"
resolved "http://registry.npm.taobao.org/shebang-regex/download/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"

signal-exit@^3.0.0:
version "3.0.2"
resolved "http://registry.npm.taobao.org/signal-exit/download/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"

strip-eof@^1.0.0:
version "1.0.0"
resolved "http://registry.npm.taobao.org/strip-eof/download/strip-eof-1.0.0.tgz#bb43ff5598a6eb05d89b59fcd129c983313606bf"

which@^1.2.9:
version "1.3.0"
resolved "http://registry.npm.taobao.org/which/download/which-1.3.0.tgz#ff04bdfc010ee547d780bec38e1ac1c2777d253a"
dependencies:
isexe "^2.0.0"

yallist@^2.1.2:
version "2.1.2"
resolved "http://registry.npm.taobao.org/yallist/download/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"

0 comments on commit f40a921

Please sign in to comment.