Skip to content

Instantly share code, notes, and snippets.

@ValentinFunk
Created November 29, 2017 19:05
Show Gist options
  • Select an option

  • Save ValentinFunk/cebfb56dd1672c82fa602bfcce25a7e9 to your computer and use it in GitHub Desktop.

Select an option

Save ValentinFunk/cebfb56dd1672c82fa602bfcce25a7e9 to your computer and use it in GitHub Desktop.
Unzip a stream in Node.JS
import * as request from 'request';
import * as shx from 'shelljs';
import * as fs from 'fs';
import * as unzip from 'unzipper';
import * as through from 'through2';
import * as Vinyl from 'vinyl';
function downloadLatestArtifact(gitlabToken: string, tokenType: 'PRIVATE-TOKEN' | 'JOB-TOKEN') {
// Download and unzip
const files = [];
return new Promise((resolve, reject) => {
request.get('https://gitlab.com/travelmat.es/frontend/-/jobs/artifacts/master/download?job=build_universal_prod', {
headers: {
[tokenType]: gitlabToken
}
}).pipe(unzip.Parse())
.on('entry', entry => {
const chunks = [];
entry.pipe(through.obj((chunk, enc, cb) => {
chunks.push(chunk);
cb();
}, cb => {
if (entry.type == 'File') {
files.push(new Vinyl({
path: entry.path,
contents: Buffer.concat(chunks)
}));
}
cb();
}))
})
.on('error', e => {
reject(e);
})
.on('close', () => {
resolve(files);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment