UNPKG

920 BPlain TextView Raw
1import { nibblesToBuffer } from './util/nibbles'
2import { Trie as BaseTrie } from './baseTrie'
3const Readable = require('readable-stream').Readable
4
5export class TrieReadStream extends Readable {
6 private trie: BaseTrie
7 private _started: boolean
8
9 constructor(trie: BaseTrie) {
10 super({ objectMode: true })
11
12 this.trie = trie
13 this._started = false
14 }
15
16 async _read() {
17 if (this._started) {
18 return
19 }
20 this._started = true
21 try {
22 await this.trie._findValueNodes(async (nodeRef, node, key, walkController) => {
23 if (node !== null) {
24 this.push({
25 key: nibblesToBuffer(key),
26 value: node.value,
27 })
28 walkController.allChildren(node, key)
29 }
30 })
31 } catch (error: any) {
32 if (error.message == 'Missing node in DB') {
33 // pass
34 } else {
35 throw error
36 }
37 }
38 this.push(null)
39 }
40}