Skip to content

Commit

Permalink
Merge pull request #2 from sormy/master
Browse files Browse the repository at this point in the history
Make xxhash optional
  • Loading branch information
swernerx authored Feb 27, 2019
2 parents 0851182 + 4c90bc6 commit 464676b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
"@babel/runtime": "^7.3.1",
"big.js": "^5.2.2",
"core-js": "^2.6.3",
"metrohash": "^2.5.1",
"metrohash": "^2.5.1"
},
"optionalDependencies": {
"xxhash": "^0.2.4"
}
}
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { createReadStream } from "fs"
import { extname } from "path"
import BigInt from "big.js"
import { MetroHash128, MetroHash64 } from "metrohash"
import { default as XXHash32, XXHash64 } from "xxhash"

// optional xxhash module
let xxhash = null
try { xxhash = require("xxhash") } catch (e) {}
const XXHash32 = xxhash ? xxhash : null
const XXHash64 = xxhash ? xxhash.XXHash64 : null

const DEFAULT_HASH = "metrohash128"
const DEFAULT_ENCODING = "base52"
Expand Down Expand Up @@ -100,8 +105,14 @@ export function createHasher(hash) {
let hasher

if (hash === "xxhash32") {
if (!XXHash32) {
throw new Error("Install xxhash module to use xxhash32 hasher")
}
hasher = new XXHash32(XXHASH_CONSTRUCT)
} else if (hash === "xxhash64") {
if (!XXHash64) {
throw new Error("Install xxhash module to use xxhash64 hasher")
}
hasher = new XXHash64(XXHASH_CONSTRUCT)
} else if (hash === "metrohash64") {
hasher = new MetroHash64()
Expand Down

0 comments on commit 464676b

Please sign in to comment.