G
G
Geckyhit2019-10-05 16:09:55
JavaScript
Geckyhit, 2019-10-05 16:09:55

How to transform a stream from write to read?

Guys, I’ll say right away that I’m more on the front, so I’ll ask you not to throw tomatoes for ignorance.
In general, I can not solve such a problem related to flows.
The task is such that we download the file using request and then send this file to some API (this API accepts a read stream - ReadStream).
at the moment, the implementation looks roughly like this:
(does not suit that you need to create a temporary file)

request(audioLink)
            .pipe(fs.createWriteStream(`./source/${fileName}`))
            .on('close', function () {
                ctx.replyWithVoice({
                    source: fs.createReadStream(`./source/${fileName}`)
                })
            })

i.e. we have to pipe into the write stream and, upon completion, create a read stream, which we pass to another method (API).
How can this be done without creating this temporary file?
I’ll probably seem like an incredible fool now, but isn’t it possible to transform request into a reading stream so as not to create a temporary file and immediately call the API method by passing data directly from request to it (I repeat that the API method accepts ReadStream)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-10-05
@Xuxicheta

it? https://www.npmjs.com/package/request-stream
In general, in theory, you can simply do

ctx.replyWithVoice({
  source: request(audioLink)
})

I certainly won't check.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question