Skip to content
This repository has been archived by the owner on Jul 14, 2019. It is now read-only.

Commit

Permalink
feat(hasher): set and hash now accept arguments for the hasher (#7)
Browse files Browse the repository at this point in the history
For cases where the hasher might accept arguments it's not suffient to set them in the constructor.

BREAKING CHANGE: Constructor now only takes a single argument for creating the hasher.
  • Loading branch information
mikeal committed Jul 29, 2017
1 parent 71b12c6 commit 088e874
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
26 changes: 14 additions & 12 deletions fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@ const createHasher = require('hashes-stream')
const isDirectory = dir => fs.statSync(dir).isDirectory()

class FileSystemContentAddressableStorage {
constructor (dir, algo = 'sha256', _createHasher = createHasher) {
constructor (dir, _createHasher = (cb) => createHasher('sha256', cb)) {
/* This statement is tested but because it gets wrapped in a try/catch
the coverage report doesn't notice. */
/* istanbul ignore if */
if (!isDirectory(dir)) throw new Error('Not a directory.')
this.dir = dir
this._algo = algo
this._createHasher = _createHasher
}
set (value, cb) {
set (value, ...args) {
if (Buffer.isBuffer(value)) {
return this._setBuffer(value, cb)
return this._setBuffer(value, ...args)
}
if (value && typeof value === 'object' && value.readable) {
return this._setStream(value, cb)
return this._setStream(value, ...args)
}
let cb = args.pop()
process.nextTick(() => cb(new Error('value is a not a valid type')))
}
hash (value, cb) {
let hasher = this._createHasher(this._algo, cb)
hash (value, ...args) {
let cb = args.pop()
let hasher = this._createHasher(...args, cb)
if (Buffer.isBuffer(value)) {
hasher.write(value)
hasher.end()
Expand All @@ -36,17 +37,18 @@ class FileSystemContentAddressableStorage {
}
process.nextTick(() => cb(new Error('value is a not a valid type')))
}
_setBuffer (value, cb) {
this.hash(value, (err, hash) => {
_setBuffer (value, ...args) {
let cb = args.pop()
this.hash(value, ...args, (err, hash) => {
if (err) return cb(err)
fs.writeFile(path.join(this.dir, hash), value, err => {
if (err) return cb(err)
cb(null, hash)
})
})
}
_setStream (value, cb) {
cb = once(cb)
_setStream (value, ...args) {
let cb = once(args.pop())
let hash
let closed
let tmpfile = path.join(this.dir, '.' + Date.now() + Math.random())
Expand All @@ -56,7 +58,7 @@ class FileSystemContentAddressableStorage {
cb(null, hash)
})
}
let hasher = this._createHasher(this._algo, (err, _hash) => {
let hasher = this._createHasher(...args, (err, _hash) => {
if (err) return cb(err)
hash = _hash
if (closed) finish()
Expand Down
13 changes: 7 additions & 6 deletions inmemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ const proxy = () => through(function (data, enc, cb) {
})

class InMemoryContentAddressableStorage {
constructor (algo = 'sha256', _createHasher = createHasher) {
constructor (_createHasher = (cb) => createHasher('sha256', cb)) {
this._store = new Map()
this._algo = algo
this._createHasher = _createHasher
}

Expand All @@ -30,8 +29,9 @@ class InMemoryContentAddressableStorage {
return stream
}

hash (value, cb) {
let hasher = this._createHasher(this._algo, (err, hash) => {
hash (value, ...args) {
let cb = args.pop()
let hasher = this._createHasher(...args, (err, hash) => {
if (err) return cb(err)
cb(null, hash)
})
Expand All @@ -46,9 +46,10 @@ class InMemoryContentAddressableStorage {
process.nextTick(() => cb(new Error('value is a not a valid type')))
}

set (value, cb) {
set (value, ...args) {
let cb = args.pop()
let _value = bl()
let hasher = this._createHasher(this._algo, (err, hash) => {
let hasher = this._createHasher(...args, (err, hash) => {
if (err) return cb(err)
this._store.set(hash, _value)
cb(null, hash)
Expand Down
10 changes: 5 additions & 5 deletions tests/test-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require('../lib/test-basics')('fs', fsStore(testdir))
const test = require('tap').test
const through = require('through2')

const failHasher = (algo, cb) => {
const failHasher = cb => {
process.nextTick(() => cb(new Error('Test Error')))
return through(() => {})
}
Expand All @@ -23,15 +23,15 @@ test('fs(implementation): directory does not exist', t => {

test('fs(implementation): hash error in hash()', t => {
t.plan(1)
let store = fsStore(testdir, 'noop', failHasher)
let store = fsStore(testdir, failHasher)
store.hash(Buffer.from('asdf'), err => {
t.type(err, 'Error')
})
})

test('fs(implementation): hash error in set()', t => {
t.plan(2)
let store = fsStore(testdir, 'noop', failHasher)
let store = fsStore(testdir, failHasher)
store.set(Buffer.from('asdf'), err => {
t.type(err, 'Error')
})
Expand Down Expand Up @@ -70,10 +70,10 @@ test('fs(implementation): filesystem errors, fs.writeFile()', t => {

test('fs(implementation): slow hasher', t => {
t.plan(2)
const slowHasher = (algo, cb) => {
const slowHasher = cb => {
return through(() => setTimeout(() => cb(null, 'asdf'), 100))
}
let store = fsStore(testdir, 'noop', slowHasher)
let store = fsStore(testdir, slowHasher)
let stream = bl()
store.set(stream, (err, hash) => {
t.error(err)
Expand Down
6 changes: 3 additions & 3 deletions tests/test-inmemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ const test = require('tap').test
const inmemory = require('../inmemory')
const through = require('through2')

const failHasher = (algo, cb) => {
const failHasher = (cb) => {
process.nextTick(() => cb(new Error('Test Error')))
return through(() => {})
}

test('inmemory: (implementation) hash error in hash()', t => {
t.plan(1)
let store = inmemory('noop', failHasher)
let store = inmemory(failHasher)
store.hash(Buffer.from('asdf'), err => {
t.type(err, 'Error')
})
})

test('inmemory: (implementation) hash error in set()', t => {
t.plan(1)
let store = inmemory('noop', failHasher)
let store = inmemory(failHasher)
store.set(Buffer.from('asdf'), err => {
t.type(err, 'Error')
})
Expand Down

0 comments on commit 088e874

Please sign in to comment.