File tree 10 files changed +70
-30
lines changed
10 files changed +70
-30
lines changed Original file line number Diff line number Diff line change
1
+ ## 1.2.0
2
+ * Add async iterator support for LevelDB and Iterator, use with ` for await `
3
+
1
4
## 1.1.1
2
5
* Fix iterators not return all values [ #8 ] ( https://github.com/extremeheat/node-leveldb-zlib/issues/8 ) , [ #9 ] ( https://github.com/extremeheat/node-leveldb-zlib/pull/9 )
3
6
Original file line number Diff line number Diff line change @@ -6,17 +6,17 @@ if (!process.versions.electron) {
6
6
// Electron has its own crash handler, and segfault-handler
7
7
// uses NAN which is a hassle, so only load outside electron
8
8
try {
9
- var SegfaultHandler = require ( 'segfault-handler' )
9
+ const SegfaultHandler = require ( 'segfault-handler' )
10
10
SegfaultHandler . registerHandler ( 'crash.log' )
11
11
} catch ( e ) {
12
12
debug ( '[leveldb] segfault handler is not installed. If you run into crashing issues, install it with `npm i -D segfault-handler` to get debug info on native crashes' )
13
13
}
14
14
}
15
15
16
- var bindings
17
- var pathToSearch = helper . getPath ( )
16
+ let bindings
17
+ const pathToSearch = helper . getPath ( )
18
18
if ( pathToSearch ) {
19
- var rpath = path . join ( __dirname , pathToSearch , '/node-leveldb.node' )
19
+ const rpath = path . join ( __dirname , pathToSearch , '/node-leveldb.node' )
20
20
try {
21
21
bindings = require ( rpath )
22
22
} catch ( e ) {
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ function checkIfPrebuildExists () {
14
14
}
15
15
}
16
16
17
- var runCmake = true
17
+ let runCmake = true
18
18
19
19
if ( ! process . env . FORCE_BUILD ) {
20
20
if ( checkIfPrebuildExists ( ) ) {
Original file line number Diff line number Diff line change @@ -8,13 +8,10 @@ The keys and values can be either Buffers or Strings: strings will be converted
8
8
const db = new LevelDB (pathToDb, { createIfMissing: true })
9
9
await db .open () // Make sure to wait for DB to open!
10
10
await db .put (' Hello' , ' World' )
11
- const iter = db .getIterator ({ values: true , keys: true }) // Both `keys` and `values` default to true
12
- let entry
13
- while (entry = await iter .next ()) {
14
- const [ val , key ] = entry .map (k => String (k))
11
+ for await (const [key , val ] = await db .getIterator ({ keyAsBuffer: false , valueAsBuffer: false })) {
15
12
console .log (' Read' , key, val)
16
13
}
17
- await db .close () // Make sure to save and close when you're done!
14
+ await db .close () // Make sure to save and close when you're done!
18
15
```
19
16
20
17
See [ example.js] ( ./example.js ) for an simple example program.
Original file line number Diff line number Diff line change @@ -12,10 +12,7 @@ async function basicTest (pathToDb) {
12
12
async function iterate ( pathToDb ) {
13
13
const db = new LevelDB ( pathToDb )
14
14
await db . open ( ) // Make sure to wait for DB to open!
15
- const iter = db . getIterator ( { values : true , keys : true } ) // Both `keys` and `values` default to true
16
- let entry
17
- while ( entry = await iter . next ( ) ) {
18
- const [ val , key ] = entry . map ( k => String ( k ) )
15
+ for await ( const [ key , val ] of db . getIterator ( { keyAsBuffer : false , valueAsBuffer : false } ) ) {
19
16
console . log ( 'Read' , key , val )
20
17
}
21
18
await db . close ( ) // Make sure to save and close when you're done!
Original file line number Diff line number Diff line change 10
10
"build" : " tsc" ,
11
11
"postci" : " node helpers/postCI.js" ,
12
12
"prepublish" : " cd helpers && node npmPublish.js" ,
13
+ "fix" : " ts-standard --fix" ,
13
14
"test" : " npm run build && mocha"
14
15
},
15
16
"author" : " extremeheat" ,
18
19
"type" : " git" ,
19
20
"url" : " git://github.com/extremeheat/node-leveldb-zlib.git"
20
21
},
21
- "devDependencies" : {
22
- "@types/node" : " ^14.14.16" ,
23
- "leveldb-zlib" : " file:." ,
24
- "segfault-handler" : " ^1.3.0" ,
25
- "ts-node" : " ^9.1.1" ,
26
- "ts-standard" : " ^10.0.0" ,
27
- "typescript" : " ^4.1.3"
28
- },
29
22
"dependencies" : {
30
23
"bindings" : " ^1.5.0" ,
31
24
"cmake-js" : " ^6.1.0" ,
32
25
"debug" : " ^4.3.1" ,
33
- "mocha" : " ^8.4.0" ,
34
26
"node-addon-api" : " ^3.1.0"
35
27
},
28
+ "devDependencies" : {
29
+ "@types/node" : " ^14.14.16" ,
30
+ "leveldb-zlib" : " file:." ,
31
+ "ts-node" : " ^9.1.1" ,
32
+ "ts-standard" : " ^10.0.0" ,
33
+ "typescript" : " ^4.1.3" ,
34
+ "mocha" : " ^8.4.0"
35
+ },
36
36
"binary" : {
37
37
"napi_versions" : [
38
38
3
Original file line number Diff line number Diff line change @@ -102,4 +102,29 @@ it('can iterate the db', async function () {
102
102
assert . ok ( val === 'Value' )
103
103
}
104
104
assert . strictEqual ( i , 3 )
105
+ await db . close ( )
106
+ } )
107
+
108
+ it ( 'can iterate with asyncIterator' , async function ( ) {
109
+ try { fs . rmSync ( './db' , { recursive : true } ) } catch { }
110
+ const db = new LevelDB ( './db' , { createIfMissing : true } )
111
+ await db . open ( )
112
+ const keys = [ 'Key1' , 'Key2' , 'Key3' ]
113
+
114
+ for ( const key of keys ) {
115
+ await db . put ( key , 'Value' )
116
+ }
117
+
118
+ for await ( const [ key , val ] of db ) {
119
+ continue
120
+ }
121
+
122
+ let i = 0
123
+ for await ( const [ key , val ] of db . getIterator ( { keyAsBuffer : false , valueAsBuffer : false } ) ) {
124
+ assert . strictEqual ( key , keys [ i ++ ] )
125
+ assert . strictEqual ( val , 'Value' )
126
+ }
127
+
128
+ assert . strictEqual ( i , 3 )
129
+ await db . close ( )
105
130
} )
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ it('create and write new db', async () => {
12
12
await db . put ( 'Hello' , 'World!' )
13
13
await db . put ( 'I' , 'Like' )
14
14
const f32a = new Float32Array ( 10 )
15
- for ( var i = 0 ; i < 10 ; i ++ ) {
15
+ for ( let i = 0 ; i < 10 ; i ++ ) {
16
16
f32a [ i ] = ( 3.14159 * Math . random ( ) )
17
17
}
18
18
await db . put ( 'Pi' , Buffer . from ( f32a . buffer ) )
@@ -55,7 +55,7 @@ it('random read/write x10', async function () {
55
55
}
56
56
57
57
const promises = [ ]
58
- for ( var i = 0 ; i < 10 ; i ++ ) {
58
+ for ( let i = 0 ; i < 10 ; i ++ ) {
59
59
promises . push ( runTest ( i , i % 2 === 0 ) )
60
60
}
61
61
await Promise . all ( promises )
@@ -65,7 +65,7 @@ it('random read/write x10', async function () {
65
65
it ( 'minecraft' , async ( ) => {
66
66
const path = join ( __dirname , './mctestdb' )
67
67
68
- var Tag = { }
68
+ const Tag = { }
69
69
Tag [ Tag . VersionNew = 44 ] = 'VersionNew'
70
70
Tag [ Tag . Data2D = 45 ] = 'Data2D'
71
71
Tag [ Tag . Data2DLegacy = 46 ] = 'Data2DLegacy'
Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ export class Iterator {
43
43
if ( this . finished ) return null
44
44
await ( this . #lock = this . _next ( ) )
45
45
this . #lock = null
46
- return this . cache . length ? this . cache . splice ( - 2 , 2 ) : null
46
+ return ( this . cache . length > 0 ) ? this . cache . splice ( - 2 , 2 ) : null
47
47
}
48
48
49
49
async end ( ) {
@@ -53,6 +53,15 @@ export class Iterator {
53
53
} )
54
54
}
55
55
56
+ async * [ Symbol . asyncIterator ] ( ) {
57
+ let next
58
+ while ( next = await this . next ( ) ) {
59
+ const [ value , key ] = next
60
+ yield [ key , value ]
61
+ }
62
+ this . end ( )
63
+ }
64
+
56
65
//
57
66
58
67
static readonly rangeOptions = 'start end gt gte lt lte' . split ( ' ' )
@@ -62,12 +71,12 @@ export class Iterator {
62
71
}
63
72
64
73
static cleanRangeOptions ( options ) {
65
- var result = { }
74
+ const result = { }
66
75
67
- for ( var k in options ) {
76
+ for ( const k in options ) {
68
77
if ( ! Object . prototype . hasOwnProperty . call ( options , k ) ) continue
69
78
70
- var opt = options [ k ]
79
+ let opt = options [ k ]
71
80
72
81
if ( this . isRangeOption ( k ) ) {
73
82
// Note that we don't reject nullish and empty options here. While
Original file line number Diff line number Diff line change @@ -208,6 +208,15 @@ export class LevelDB {
208
208
return new Iterator ( this , options )
209
209
}
210
210
211
+ async * [ Symbol . asyncIterator ] ( ) {
212
+ const it = this . getIterator ( )
213
+ let next
214
+ while ( next = await it . next ( ) ) {
215
+ const [ value , key ] = next
216
+ yield [ key , value ]
217
+ }
218
+ }
219
+
211
220
/**
212
221
* Delete all entries or a range.
213
222
*/
You can’t perform that action at this time.
0 commit comments