-
-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
streams api for upload/download? #237
Comments
This could be added to the |
Recently I've been giving some thought to this feature. What do you think would be the best way to implement this @ronag ? Here are some alternatives I've been thinking: Curl.prototype.perform would return a Duplex stream when the
|
I've decided to go with the latter, here is the proposed API (showing both download and upload): curly const inFilePath = './some.in.file.zip'
const outFilePath = './some.out.file.zip'
const readableStreamToUpload = fs.createReadStream(inFilePath)
const { statusCode, data: streamToDownload, headers } = await curly.get<Readable>(
'http://some-url.com/api/upload',
{
// Upload related options
// PUT upload
upload: true,
// so we do not need to specify the payload size
httpHeader: ['Transfer-Encoding: chunked'],
// smallest buffer possible to cause the max amount of buffering
bufferSize: 16 * 1024,
// the stream to upload
curlyStreamUpload: readableStreamToUpload,
// Download as a stream
curlyStreamResponse: true,
// show libcurl default progress bar
curlyProgressCallback() {
return CurlProgressFunc.Continue
},
},
)
const writableStream = fs.createWriteStream(outFilePath)
// then we can just pipe it
streamToDownload.pipe(writableStream)
// or we can use async iterators:
for await (const chunk of streamToDownload) {
writableStream.write(chunk)
}
writableStream.end() Curl const inFilePath = './some.in.file.zip'
const outFilePath = './some.out.file.zip'
const readableStreamToUpload = fs.createReadStream(inFilePath)
const curl = new Curl()
curl.enable(CurlFeature.StreamResponse)
curl.setOpt('URL', 'http://some-url.com/api/upload')
// PUT upload
curl.setOpt('UPLOAD', true)
// so we do not need to specify the payload size
curl.setOpt('HTTPHEADER', ['Transfer-Encoding: chunked'])
// smallest buffer possible to cause the max amount of buffering
curl.setOpt(Curl.option.UPLOAD_BUFFERSIZE, 16 * 1024)
// the stream to upload
curl.setUploadStream(readableStreamToUpload)
// show libcurl default progress bar
curl.setStreamProgressCallback(() => {
return CurlProgressFunc.Continue
})
curl.on('end', (statusCode, data) => {
console.log('\n'.repeat(5))
console.log(
`curl - end - status: ${statusCode} - data length: ${data.length}`,
)
curl.close()
})
curl.on('error', (error, errorCode) => {
console.log('\n'.repeat(5))
console.error('curl - error: ', error, errorCode)
curl.close()
})
curl.on('stream', async (streamToDownload, _statusCode, _headers) => {
const writableStream = fs.createWriteStream(outFilePath)
// pipe
streamToDownload.pipe(writableStream)
// or any other usage...
})
curl.perform() Notes
I should be able to make this available later this week. |
Closed via #252 |
Support for node streams in an upload/download API would be useful.
The text was updated successfully, but these errors were encountered: