asset-hash
Advanced tools
Comparing version 2.0.0 to 2.1.1
@@ -1,2 +0,2 @@ | ||
/*! asset-hash v2.0.0 by Sebastian Werner <s.werner@sebastian-software.de> */ | ||
/*! asset-hash v2.1.1 by Sebastian Werner <s.werner@sebastian-software.de> */ | ||
'use strict'; | ||
@@ -15,9 +15,11 @@ | ||
var BigInt = _interopDefault(require('big.js')); | ||
var XXHash = require('xxhash'); | ||
var XXHash__default = _interopDefault(XXHash); | ||
var HashThrough = _interopDefault(require('hash-through')); | ||
var metrohash = require('metrohash'); | ||
var XXHash32 = require('xxhash'); | ||
var XXHash32__default = _interopDefault(XXHash32); | ||
var DEFAULT_HASH = "xxhash", | ||
DEFAULT_ENCODING = "base52", | ||
DEFAULT_MAX_LENGTH = 16, | ||
XXHASH_CONSTRUCT = 0xCAFEBABE, | ||
var DEFAULT_HASH = "metrohash128", | ||
DEFAULT_ENCODING = "hex", | ||
DEFAULT_MAX_LENGTH = 8, | ||
XXHASH_CONSTRUCT = 0xcafebabe, | ||
baseEncodeTables = { | ||
@@ -41,7 +43,7 @@ 26: "abcdefghijklmnopqrstuvwxyz", | ||
var readLength = buffer.length; | ||
var length = buffer.length; | ||
BigInt.DP = 0; | ||
BigInt.RM = 0; | ||
for (var current = new BigInt(0), i = readLength - 1; i >= 0; i--) { | ||
for (var current = new BigInt(0), i = length - 1; i >= 0; i--) { | ||
current = current.times(256).plus(buffer[i]); | ||
@@ -62,12 +64,19 @@ } | ||
function computeDigest(buffer, _temp) { | ||
function computeDigest(bufferOrString, _temp) { | ||
var _ref = _temp === void 0 ? {} : _temp, | ||
encoding = _ref.encoding, | ||
maxLength = _ref.maxLength, | ||
output = ""; | ||
output = "", | ||
isString = typeof bufferOrString == "string"; | ||
if (encoding === "hex" || encoding === "base64" || encoding === "utf8") { | ||
output = buffer.toString(encoding); | ||
if (isString && encoding === "hex") { | ||
output = bufferOrString; | ||
} else { | ||
output = baseEncode(buffer, encoding); | ||
var buffer = isString ? Buffer.from(bufferOrString, "hex") : bufferOrString; | ||
if (encoding === "hex" || encoding === "base64" || encoding === "utf8") { | ||
output = buffer.toString(encoding); | ||
} else { | ||
output = baseEncode(buffer, encoding); | ||
} | ||
} | ||
@@ -108,6 +117,22 @@ | ||
function createHasher(hash) { | ||
return hash === "xxhash" ? new XXHash__default(XXHASH_CONSTRUCT) : crypto.createHash(hash); | ||
var hasher; | ||
if (hash === "xxhash32") { | ||
hasher = new XXHash32__default(XXHASH_CONSTRUCT); | ||
} else if (hash === "xxhash64") { | ||
hasher = new XXHash32.XXHash64(XXHASH_CONSTRUCT); | ||
} else if (hash === "metrohash64") { | ||
hasher = new metrohash.MetroHash64(); | ||
} else if (hash === "metrohash128") { | ||
hasher = new metrohash.MetroHash128(); | ||
} else { | ||
hasher = crypto.createHash(hash); | ||
} | ||
return hasher; | ||
} | ||
function createStreamingHasher(hash) { | ||
return hash === "xxhash" ? new XXHash.Stream(XXHASH_CONSTRUCT, "buffer") : crypto.createHash(hash); | ||
return HashThrough(function () { | ||
return createHasher(hash); | ||
}); | ||
} | ||
@@ -128,3 +153,3 @@ function getHash(fileName, _temp2) { | ||
try { | ||
var digest = computeDigest(hasher.read(), { | ||
var digest = computeDigest(hasher.digest("buffer"), { | ||
encoding: encoding, | ||
@@ -131,0 +156,0 @@ maxLength: maxLength |
@@ -1,2 +0,2 @@ | ||
/*! asset-hash v2.0.0 by Sebastian Werner <s.werner@sebastian-software.de> */ | ||
/*! asset-hash v2.1.1 by Sebastian Werner <s.werner@sebastian-software.de> */ | ||
import 'core-js/modules/es6.promise'; | ||
@@ -9,8 +9,10 @@ import 'core-js/modules/es6.regexp.to-string'; | ||
import BigInt from 'big.js'; | ||
import XXHash, { Stream } from 'xxhash'; | ||
import HashThrough from 'hash-through'; | ||
import { MetroHash128, MetroHash64 } from 'metrohash'; | ||
import XXHash32, { XXHash64 } from 'xxhash'; | ||
var DEFAULT_HASH = "xxhash", | ||
DEFAULT_ENCODING = "base52", | ||
DEFAULT_MAX_LENGTH = 16, | ||
XXHASH_CONSTRUCT = 0xCAFEBABE, | ||
var DEFAULT_HASH = "metrohash128", | ||
DEFAULT_ENCODING = "hex", | ||
DEFAULT_MAX_LENGTH = 8, | ||
XXHASH_CONSTRUCT = 0xcafebabe, | ||
baseEncodeTables = { | ||
@@ -34,7 +36,7 @@ 26: "abcdefghijklmnopqrstuvwxyz", | ||
var readLength = buffer.length; | ||
var length = buffer.length; | ||
BigInt.DP = 0; | ||
BigInt.RM = 0; | ||
for (var current = new BigInt(0), i = readLength - 1; i >= 0; i--) { | ||
for (var current = new BigInt(0), i = length - 1; i >= 0; i--) { | ||
current = current.times(256).plus(buffer[i]); | ||
@@ -55,12 +57,19 @@ } | ||
function computeDigest(buffer, _temp) { | ||
function computeDigest(bufferOrString, _temp) { | ||
var _ref = _temp === void 0 ? {} : _temp, | ||
encoding = _ref.encoding, | ||
maxLength = _ref.maxLength, | ||
output = ""; | ||
output = "", | ||
isString = typeof bufferOrString == "string"; | ||
if (encoding === "hex" || encoding === "base64" || encoding === "utf8") { | ||
output = buffer.toString(encoding); | ||
if (isString && encoding === "hex") { | ||
output = bufferOrString; | ||
} else { | ||
output = baseEncode(buffer, encoding); | ||
var buffer = isString ? Buffer.from(bufferOrString, "hex") : bufferOrString; | ||
if (encoding === "hex" || encoding === "base64" || encoding === "utf8") { | ||
output = buffer.toString(encoding); | ||
} else { | ||
output = baseEncode(buffer, encoding); | ||
} | ||
} | ||
@@ -101,6 +110,22 @@ | ||
function createHasher(hash) { | ||
return hash === "xxhash" ? new XXHash(XXHASH_CONSTRUCT) : createHash(hash); | ||
var hasher; | ||
if (hash === "xxhash32") { | ||
hasher = new XXHash32(XXHASH_CONSTRUCT); | ||
} else if (hash === "xxhash64") { | ||
hasher = new XXHash64(XXHASH_CONSTRUCT); | ||
} else if (hash === "metrohash64") { | ||
hasher = new MetroHash64(); | ||
} else if (hash === "metrohash128") { | ||
hasher = new MetroHash128(); | ||
} else { | ||
hasher = createHash(hash); | ||
} | ||
return hasher; | ||
} | ||
function createStreamingHasher(hash) { | ||
return hash === "xxhash" ? new Stream(XXHASH_CONSTRUCT, "buffer") : createHash(hash); | ||
return HashThrough(function () { | ||
return createHasher(hash); | ||
}); | ||
} | ||
@@ -121,3 +146,3 @@ function getHash(fileName, _temp2) { | ||
try { | ||
var digest = computeDigest(hasher.read(), { | ||
var digest = computeDigest(hasher.digest("buffer"), { | ||
encoding: encoding, | ||
@@ -124,0 +149,0 @@ maxLength: maxLength |
{ | ||
"name": "asset-hash", | ||
"version": "2.0.0", | ||
"version": "2.1.1", | ||
"description": "Very fast asset hashing function for using e.g. during front-end deployments.", | ||
@@ -40,7 +40,7 @@ "main": "lib/index.cjs.js", | ||
"babel-jest": "^23.4.0", | ||
"babel-preset-edge": "^4.9.0", | ||
"babel-preset-edge": "^4.9.1", | ||
"eslint": "^5.1.0", | ||
"eslint-config-readable": "^2.1.3", | ||
"flow-bin": "^0.76.0", | ||
"jest": "^23.4.0", | ||
"jest": "^23.4.1", | ||
"preppy": "^4.2.3", | ||
@@ -53,4 +53,6 @@ "prettier": "^1.13.7" | ||
"core-js": "^2.5.7", | ||
"hash-through": "^0.1.16", | ||
"metrohash": "^2.4.1", | ||
"xxhash": "^0.2.4" | ||
} | ||
} |
@@ -31,2 +31,8 @@ # _Asset Hash_ <br/>[![Sponsored by][sponsor-img]][sponsor] [![Version][npm-version-img]][npm] [![Downloads][npm-downloads-img]][npm] [![Build Status Unix][travis-img]][travis] [![Build Status Windows][appveyor-img]][appveyor] [![Dependencies][deps-img]][deps] | ||
## Speed | ||
For speed comparisons of different algorithms we created a small repository containing the source code and some results. [Check it out](https://github.com/sebastian-software/node-hash-comparison). TLDR: Modern non-cryptographic hashing could be way faster than cryptographic solutions like MD5 or SHA1. Best algorithm right now for our use cases seems to be MetroHash128. This is why we made it the default. | ||
## Usage | ||
@@ -38,3 +44,4 @@ | ||
- `encoding`: | ||
- `encoding`: Any valid encoding for built-in digests `hex`, `base64`, `base62`, ... | ||
- `maxLength`: Maximum length of returned digest. Keep in mind that this increases collison probability. | ||
@@ -41,0 +48,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
32431
311
87
0
6
+ Addedhash-through@^0.1.16
+ Addedmetrohash@^2.4.1
+ Added@gar/promisify@1.1.3(transitive)
+ Added@npmcli/fs@1.1.1(transitive)
+ Added@npmcli/move-file@1.1.2(transitive)
+ Added@tootallnate/once@1.1.2(transitive)
+ Addedabbrev@1.1.1(transitive)
+ Addedagent-base@6.0.2(transitive)
+ Addedagentkeepalive@4.5.0(transitive)
+ Addedaggregate-error@3.1.0(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedaproba@2.0.0(transitive)
+ Addedare-we-there-yet@3.0.1(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbindings@1.5.0(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedcacache@15.3.0(transitive)
+ Addedchownr@2.0.0(transitive)
+ Addedclean-stack@2.2.0(transitive)
+ Addedcolor-support@1.1.3(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedconsole-control-strings@1.1.0(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addeddelegates@1.0.0(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedencoding@0.1.13(transitive)
+ Addedenv-paths@2.2.1(transitive)
+ Addederr-code@2.0.3(transitive)
+ Addedfile-uri-to-path@1.0.0(transitive)
+ Addedfs-minipass@2.1.0(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedgauge@4.0.4(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhas-unicode@2.0.1(transitive)
+ Addedhash-through@0.1.16(transitive)
+ Addedhttp-cache-semantics@4.1.1(transitive)
+ Addedhttp-proxy-agent@4.0.1(transitive)
+ Addedhttps-proxy-agent@5.0.1(transitive)
+ Addedhumanize-ms@1.2.1(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedimurmurhash@0.1.4(transitive)
+ Addedindent-string@4.0.0(transitive)
+ Addedinfer-owner@1.0.4(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedip-address@9.0.5(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedis-lambda@1.0.1(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedjsbn@1.1.0(transitive)
+ Addedlru-cache@6.0.0(transitive)
+ Addedmake-fetch-happen@9.1.0(transitive)
+ Addedmetrohash@2.8.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminipass@3.3.65.0.0(transitive)
+ Addedminipass-collect@1.0.2(transitive)
+ Addedminipass-fetch@1.4.1(transitive)
+ Addedminipass-flush@1.0.5(transitive)
+ Addedminipass-pipeline@1.2.4(transitive)
+ Addedminipass-sized@1.0.3(transitive)
+ Addedminizlib@2.1.2(transitive)
+ Addedmkdirp@1.0.4(transitive)
+ Addedms@2.1.3(transitive)
+ Addednegotiator@0.6.4(transitive)
+ Addednode-gyp@8.4.1(transitive)
+ Addednopt@5.0.0(transitive)
+ Addednpmlog@6.0.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-map@4.0.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedpromise-inflight@1.0.1(transitive)
+ Addedpromise-retry@2.0.1(transitive)
+ Addedreadable-stream@2.3.83.6.2(transitive)
+ Addedretry@0.12.0(transitive)
+ Addedrimraf@3.0.2(transitive)
+ Addedsafe-buffer@5.1.25.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsemver@7.6.3(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedsmart-buffer@4.2.0(transitive)
+ Addedsocks@2.8.3(transitive)
+ Addedsocks-proxy-agent@6.2.1(transitive)
+ Addedsprintf-js@1.1.3(transitive)
+ Addedssri@8.0.1(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstring_decoder@1.1.11.3.0(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedtar@6.2.1(transitive)
+ Addedunique-filename@1.1.1(transitive)
+ Addedunique-slug@2.0.2(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwhich@2.0.2(transitive)
+ Addedwide-align@1.1.5(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedyallist@4.0.0(transitive)