Skip to content

Instantly share code, notes, and snippets.

@xhsdnn
Last active March 31, 2023 09:24
Show Gist options
  • Save xhsdnn/91d962400afb00eb700a88f60f57f119 to your computer and use it in GitHub Desktop.
Save xhsdnn/91d962400afb00eb700a88f60f57f119 to your computer and use it in GitHub Desktop.
const { createReadStream, createWriteStream } = require('fs');
const tar = require(tar);
const dest = process.cwd();
// A: remote download
// A1: use lib
let res = await axios.get(url, {
responseType: 'stream'
});
let writer1 = createWriteStream(dest, {
encoding: 'utf8'
});
res.data.pipe(writer1);
// A2: native
/* https or */http.get(url, (response) => {
response.pipe(writer1);
});
// A3: event
writer1.on('finish', () => {
// resolve();
});
writer1.on('error', (e) => {
// reject(e);
});
// B: unpack
let writer2 = tar.x({
strict: 1,
C: dest
});
// B1: use lib from remote
let res = await axios.get(url, {
responseType: 'stream'
});
res.data.pipe(writer2);
// B2: use lib from local
createReadStream(localSource).pipe(writer2);
// B3: similar to A3
// C: pack
// use lib similar to B —— use "tar"
// D: A and B can be mixed simple
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment