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

Commit 66ea6a9

Browse files
ralphtheninjaJoe Hand
authored andcommitted
Fix invalid callback in .write and callbacks invoked synchronously (#1)
* add hyperdrive to devDependencies * add failing test for .create with non writable archive * fix failing test with non async callback in .create * add failing test for .write (key/value) with non writable archive * fix failing test with non async callback in .write (key/value) * add failing test for .write (data object) with non writable archive * fix failing test with invalid callback in .write (data object) * remove unnecessary archive.writable check in writeAll (checked in .write) * fix travis badges ✨
1 parent 1ae1fa0 commit 66ea6a9

4 files changed

Lines changed: 47 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Read the current `dat.json`.
7474

7575
[npm-image]: https://img.shields.io/npm/v/dat-json.svg?style=flat-square
7676
[npm-url]: https://www.npmjs.com/package/dat-json
77-
[travis-image]: https://img.shields.io/travis/joehand/dat-json.svg?style=flat-square
78-
[travis-url]: https://travis-ci.org/joehand/dat-json
77+
[travis-image]: https://img.shields.io/travis/datproject/dat-json.svg?style=flat-square
78+
[travis-url]: https://travis-ci.org/datproject/dat-json
7979
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
8080
[standard-url]: http://npm.im/standard

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ module.exports = function (archive, opts) {
1717
})
1818
},
1919
write: function (key, val, cb) {
20-
if (!archive.writable) return cb(new Error('Archive not writable'))
21-
if (typeof key === 'object') return writeAll(key, val) // assume val = cb
20+
if (typeof val === 'function') cb = val
21+
if (!archive.writable) {
22+
return process.nextTick(cb, new Error('Archive not writable'))
23+
}
24+
if (typeof key === 'object') return writeAll(key, cb)
2225
// TODO: validate things
2326
if (!fileDb) return db.write(key, val, cb)
2427

@@ -33,7 +36,9 @@ module.exports = function (archive, opts) {
3336
delete: db.delete,
3437
create: function (data, cb) {
3538
if (typeof data === 'function') return that.create(null, data)
36-
if (!archive.writable) return cb(new Error('Archive not writable'))
39+
if (!archive.writable) {
40+
return process.nextTick(cb, new Error('Archive not writable'))
41+
}
3742
data = xtend(getdefaults(), data)
3843
that.write(data, cb)
3944
}
@@ -50,7 +55,6 @@ module.exports = function (archive, opts) {
5055
}
5156

5257
function writeAll (data, cb) {
53-
if (!archive.writable) return cb(new Error('Archive not writable'))
5458
var keys = Object.keys(data)
5559
var pending = keys.length
5660
keys.map(function (key) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"url": "https://github.com/joehand/dat-json/issues"
88
},
99
"devDependencies": {
10+
"hyperdrive": "^9.12.3",
1011
"random-access-memory": "^2.4.0",
1112
"standard": "*",
1213
"tap-spec": "^4.0.2",

test/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,42 @@ test('Write dat.json to archive', function (t) {
4848
})
4949
})
5050

51+
test('.create with no writable archive errors', function (t) {
52+
var archive = { writable: false }
53+
var datjson = datJSON(archive)
54+
var async = false
55+
datjson.create(function (err) {
56+
t.is(err.message, 'Archive not writable', 'should error')
57+
t.is(async, true, 'callback is asyncronous')
58+
t.end()
59+
})
60+
async = true
61+
})
62+
63+
test('.write with key/value and no writable archive errors', function (t) {
64+
var archive = { writable: false }
65+
var datjson = datJSON(archive)
66+
var async = false
67+
datjson.write('key', 'value', function (err) {
68+
t.is(err.message, 'Archive not writable', 'should error')
69+
t.is(async, true, 'callback is asyncronous')
70+
t.end()
71+
})
72+
async = true
73+
})
74+
75+
test('.write with data object and no writable archive errors', function (t) {
76+
var archive = { writable: false }
77+
var datjson = datJSON(archive)
78+
var async = false
79+
datjson.write({specialVal: 'cat'}, function (err) {
80+
t.is(err.message, 'Archive not writable', 'should error')
81+
t.is(async, true, 'callback is asyncronous')
82+
t.end()
83+
})
84+
async = true
85+
})
86+
5187
test('Write dat.json to file and archive', function (t) {
5288
var archive = hyperdrive(ram)
5389
var file = path.join(__dirname, 'dat.json')

0 commit comments

Comments
 (0)