I'm searching to send (return) a readableStream to a method (myMethod) with axios in a node program -> i'd like to stream the response to a ReadableStream that I can use to send to the myMethod() callers :
// This code does'nt work but that's what I'm trying to do
function myMethod() {
var readableStream = createReadableStream(); // does not exists but that's here to show the concept
const axiosObj = axios({
method: 'get',
url: 'https://www.google.fr',
responseType: 'stream'
}).then(function(res) {
res.data.pipe(readableStream);
}).catch(function(err) {
console.log(err);
});
return readableStream;
}
function myMethodCaller() {
myMethod().on('data', function(chunk) {
// Do some stuffs with the chunks
});
}
I know that we can only do in res.data a pipe to a writableStream. I'm stuck in returning a readableStream usable by the myMethod() callers. Have you got any ideas on that ?
Regards, Blured.
I've found an implementation
var axios = require('axios');
const Transform = require('stream').Transform;
function streamFromAxios() {
// Create Stream, Writable AND Readable
const inoutStream = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk);
callback();
},
});
// Return promise
const axiosObj = axios({
method: 'get',
url: 'https://www.google.fr',
responseType: 'stream'
}).then(function(res) {
res.data.pipe(inoutStream);
}).catch(function(err) {
console.log(err);
});
return inoutStream;
}
function caller() {
streamFromAxios().on('data', chunk => {
console.log(chunk);
});
}
caller();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With