I'm working on a data-sink of sorts that is eventually to be used for Key-Value object streams in nodejs.
I've stumbled across duplex streams and started playing with them a bit to get my feet wet, but everything I try seems to not work.
At present, I've got this duplex stream:
class StorageStream extends stream.Duplex {
constructor() {
super({
objectMode: true
})
this.values = new Map();
this.entries = this.values.entries();
}
_write(data, encoding, next) {
const { key, value } = data;
this.values.set(key, value);
next();
}
_read() {
const next = this.entries.next();
if (next.value) {
this.push(next.value);
}
}
}
This is a SUPER CONTRIVED example, but essentially, when I write to this stream it should store the key and value in the Map, and when I read from this stream it should start reading from the map and passing them down the stream. However, this doesn't work, doing basically the below
const kvstream = createKVStreamSomeHow(); // a basic, readable stream with KV Pairs
const logger = createLoggerStreamSomeHow(); // writable stream, logs the data coming through
const storage = new StorageStream();
kvstream.pipe(storage).pipe(logger);
causes the process to just end. So I guess I'm just a bit confused as to what I'm supposed to be doing inside of the _read
method.