Created
November 29, 2017 19:05
-
-
Save ValentinFunk/cebfb56dd1672c82fa602bfcce25a7e9 to your computer and use it in GitHub Desktop.
Unzip a stream in Node.JS
This file contains hidden or 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
| 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