'How to implement a writable stream
I want to pipe data from an amazon kinesis stream to a an s3 log or a bunyan log.
The sample works with a file write stream or stdout. How would I implmeny my own writable stream?
//this works
var file = fs.createWriteStream('my.log')
kinesisSource.pipe(file)
this doesn't work saying it has no method 'on'
var stream = {}; //process.stdout works however
stream.writable = true;
stream.write =function(data){
console.log(data);
};
kinesisSource.pipe(stream);
what methods do I have to implement for my own custom writable stream, the docs seem to indicate I need to implement 'write' and not 'on'
Solution 1:[1]
Actually to create a writeable stream is quite simple. Here's is the example:
var fs = require('fs');
var Stream = require('stream');
var ws = new Stream;
ws.writable = true;
ws.bytes = 0;
ws.write = function(buf) {
ws.bytes += buf.length;
}
ws.end = function(buf) {
if(arguments.length) ws.write(buf);
ws.writable = false;
console.log('bytes length: ' + ws.bytes);
}
fs.createReadStream('file path').pipe(ws);
Also if you want to create your own class, @Paul give a good answer.
Solution 2:[2]
Here is an example directly from nodejs docs
https://nodejs.org/api/stream.html#an-example-writable-stream
const { Writable } = require('stream');
class MyWritable extends Writable {
_write(chunk, encoding, callback) {
if (chunk.toString().indexOf('a') >= 0) {
callback(new Error('chunk is invalid'));
} else {
callback();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ionică Bizău |
| Solution 2 | Ragnoroct |
