got-to-temp
First, use Got to create retryable read stream.
@mangosteen/got-to-temp
then helps you to:
- Automatically retry on failures
- Stream
Got
response data to a temp file that is automatically created for you - Optionally, use transform streams to modify the stream data on-the-fly
If there are any failures during download, the temp file is automatically cleaned up.
If there are any errors during download and Got
ran out of retries, the error is propagated via rejected promise.
Installation
With npm do:
$ npm install @mangosteen/got-to-temp
Usage
import { downloadToTempFile } from '@mangosteen/got-to-temp';
import { DigestStream } from '@mangosteen/digest-stream';
import got from 'got';
import crypto from 'crypto';
(async () => {
const gotClient = got.extend({
retry: {
limit: 5,
},
});
const downloadResult = await downloadToTempFile(
() => gotClient.stream.get('https://nodejs.org/dist/v14.17.5/node-v14.17.5.tar.gz'),
() => [
new DigestStream({
digest: crypto.createHash('sha256'),
}),
] as const,
);
console.log(downloadResult.filePath);
console.log(
downloadResult.transforms[0].digest().toString('hex'),
);
})();