Last active
September 14, 2021 04:58
-
-
Save ff6347/e27d9aa934b54b8293b2e5af0f3e81f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@ts-check | |
import got from "got"; | |
import stream from "stream"; | |
import zlib from "zlib"; | |
import fs from "fs"; | |
const fileList = ["file.gz", "file1.gz", "file2.gz", "file3.gz"]; | |
const tmpFolderPath = "/tmp"; | |
async function main() { | |
const transferTasks = fileList.map( | |
(file) => | |
new Promise((resolve, reject) => { | |
const outfile = file.replace(".gz", ""); | |
const inFilePath = `http://example.com/path/to/${file}`; | |
const rstream = got.stream(inFilePath); | |
const fswstream = fs.createWriteStream(`${tmpFolderPath}/${outfile}`); | |
stream.pipeline(rstream, zlib.createGunzip(), fswstream, (err) => { | |
if (err) { | |
reject(); | |
} else { | |
resolve(); | |
} | |
}); | |
}) | |
); | |
await Promise.all(transferTasks).catch((err) => { | |
console.error("Error in promise all transferTask", err); | |
}); | |
} | |
main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment