Skip to content
This repository has been archived by the owner on Jan 7, 2022. It is now read-only.

noop on duplicates #9

Merged
merged 1 commit into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ List all `archives` in the `multidrive`.

### drive.create(data, callback(err, drive))
Create a new Hyperdrive archive. `data` is passed into `createArchive`.
If an archive with the same key already exists, returns that instead.

### drive.close(key, callback(err))
Remove an archive by its public key. Calls `closeArchive()`
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ function multidrive (store, createArchive, closeArchive, cb) {
createArchive(data, function (err, archive) {
if (err) return cb(err)
var key = archive.key

var duplicates = archives.filter(function (_archive) {
return _archive.key === key
})
if (duplicates.length) return cb(null, duplicates[0])

var _data
if (data) _data = JSON.stringify(data)
store.write(key, _data, function (err) {
Expand Down
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ test('drive.create', function (t) {
done(null, archive)
}
})

t.test('should noop on duplicates', function (t) {
t.plan(4)
flushToilet()
var store = toilet('state.json')
var db = memdb()
var drive = hyperdrive(db)
multidrive(store, createArchive, noop, function (err, drive) {
t.ifError(err, 'no err')

drive.create({ hello: 'world' }, function (err, archive) {
t.ifError(err, 'no err')

drive.create({ key: archive.key }, function (err, _archive) {
t.ifError(err, 'no err')
t.equal(_archive, archive)
})
})
})

function createArchive (data, done) {
var archive = drive.createArchive({ key: data.key })
done(null, archive)
}
})
})

test('drive.list', function (t) {
Expand Down