Skip to content

Loading…

should not throw when getting non-existant keys #1

Closed
wants to merge 1 commit into from

3 participants

@Raynos

No description provided.

@juliangruber

+1 for this. without that kv won't work with scuttlebutt on the server

@juliangruber

This works but is ugly:

kv.has(key, function (err) {
  if (err) {
    onSync()
  } else {
    model.on('sync', onSync)
    kv.get(name).pipe(model.createWriteStream())
  }

  function onSync() {
    model.createReadStream().pipe(kv.put(name, { flags : 'a' }))
  }
})
@dominictarr
Owner

This patch is wrong, It doesn't open the gate if there is no has fails, so end won't be emitted.
also, I don't really think you want an empty stream. that is weird. this behaviour seems more surprising,

Currently, this has the same behavior as fs, to emit 'error'

@Raynos

Closing this due to staleness

@Raynos Raynos closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 1, 2012
  1. @Raynos
Showing with 19 additions and 6 deletions.
  1. +16 −6 endpoints.js
  2. +3 −0 test/index.js
View
22 endpoints.js
@@ -15,9 +15,9 @@ module.exports = function (basedir, exports) {
/*
okay, so this is gonna run in the browser, attached to localStorage also.
-
-*/
+
+*/
exports.put = function (key, opts) {
var _key = hash(key)
var dir = _key.substring(0, 2)
@@ -40,19 +40,29 @@ module.exports = function (basedir, exports) {
exports.get = function (key, opts) {
var _key = hash(key)
var dir = _key.substring(0, 2)
- return fs.createReadStream(join(basedir, dir, key), opts)
+ var stream = es.gate(true)
+ exports.has(key, function (err, stat) {
+ if (err)
+ return stream.end()
+
+ stream.open()
+
+ fs.createReadStream(join(basedir, dir, key), opts)
+ .pipe(stream)
+ })
+ return stream
}
exports.has = function (key, callback) {
var _key = hash(key)
var dir = _key.substring(0, 2)
- fs.stat(join(basedir, dir, key), callback)
+ fs.stat(join(basedir, dir, key), callback)
}
-
+
function del(key, cb) {
var _key = hash(key)
var dir = _key.substring(0, 2)
- var file = _key.substring(2)
+ var file = _key.substring(2)
}
return exports
View
3 test/index.js
@@ -20,5 +20,8 @@ rm('/tmp/kv_test', function () {
assert.equal(data.hello, r)
})
})
+
+ // should not throw for getting keys that does not exist
+ kv.get("bar")
})
Something went wrong with that request. Please try again.