Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #37 from justmoon/iterbuferr

Iterator.seek crashes Node.js when supplying a Buffer
  • Loading branch information...
commit 2a28a0f7e6a2e357f1465cc1f792ca6faac40dde 2 parents d9c6ce4 + 3f326db
Michael Phan-Ba mikepb authored
4 src/coffee/leveldb/iterator.coffee
@@ -29,7 +29,7 @@ binding = require '../leveldb.node'
29 29 exports.Iterator = class Iterator
30 30
31 31 toBuffer = (val) ->
32   - new Buffer val unless Buffer.isBuffer val
  32 + if Buffer.isBuffer val then val else new Buffer val
33 33
34 34 toValue = (val, options) ->
35 35 unless val
@@ -108,7 +108,7 @@ exports.Iterator = class Iterator
108 108 forRange: ->
109 109
110 110 args = Array.prototype.slice.call arguments
111   -
  111 +
112 112 #Optional finished callback
113 113 if typeof(args[args.length - 1]) is 'function' and typeof(args[args.length - 2]) is 'function'
114 114 finishedCallback = args.pop()
34 test/iterator-test.coffee
@@ -59,6 +59,40 @@ describe 'Iterator', ->
59 59 iterator.next if ++i <= 200 then next else done
60 60 next()
61 61
  62 + it 'should seek to a key', (done) ->
  63 + i = 100
  64 + next = (err) ->
  65 + assert.ifError err
  66 + expectKey = "#{i}"
  67 + expectVal = "Hello #{i}"
  68 + iterator.seek expectKey, (err) ->
  69 + assert.ifError err
  70 + iterator.key (err, key) ->
  71 + assert.ifError err
  72 + assert.equal expectKey, key
  73 + iterator.value (err, val) ->
  74 + assert.ifError err
  75 + assert.equal expectVal, val
  76 + if ++i <= 100 then next() else done()
  77 + next()
  78 +
  79 + it 'should seek to a key (buffer)', (done) ->
  80 + i = 100
  81 + next = (err) ->
  82 + assert.ifError err
  83 + expectKey = "#{i}"
  84 + expectVal = "Hello #{i}"
  85 + iterator.seek new Buffer(expectKey, 'ascii'), (err) ->
  86 + assert.ifError err
  87 + iterator.key (err, key) ->
  88 + assert.ifError err
  89 + assert.equal expectKey, key
  90 + iterator.value (err, val) ->
  91 + assert.ifError err
  92 + assert.equal expectVal, val
  93 + if ++i <= 100 then next() else done()
  94 + next()
  95 +
62 96 it 'should not get invalid key', (done) ->
63 97 iterator.seek '201', (err) ->
64 98 assert.ifError err

0 comments on commit 2a28a0f

Please sign in to comment.
Something went wrong with that request. Please try again.