fp/packages/link2cid/src/utils/download.js
Chris Grimmett fb13c3ab1b
Some checks failed
ci / build (push) Failing after 7m39s
rewrite
2024-04-24 21:42:03 +00:00

20 lines
562 B
JavaScript

const fs = require("fs");
const { Readable } = require('stream');
const { finished } = require('stream/promises');
const path = require("path");
/**
* Download a file at url to local disk filePath
* @param {String} url
* @param {String} filePath
*
* greetz https://stackoverflow.com/a/51302466/1004931
*/
const download = (async (url, filePath) => {
const res = await fetch(url);
const fileStream = fs.createWriteStream(filePath, { flags: 'wx' });
await finished(Readable.fromWeb(res.body).pipe(fileStream));
});
module.exports = download;