Last active
February 22, 2020 15:45
-
-
Save Farenheith/e709bca7c67dfba44c143d41ef79b607 to your computer and use it in GitHub Desktop.
An eventier stream handler
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
import { Readable } from 'stream'; | |
import { gzip } from 'zlib'; | |
export async function notSoSimpleStreamExample(stream: Readable) { | |
return new Promise((resolve, reject) => { | |
const data: Array<Promise<Buffer>> = []; | |
stream.on('data', info => { | |
data.push(new Promise((resolve, reject) => { | |
gzip(info, (error, result) => { | |
if (error) { | |
reject(error); | |
} else { | |
resolve(result); | |
} | |
}) | |
})); | |
}); | |
stream.on('end', () => { | |
resolve(Promise.all(data)); | |
}); | |
stream.on('error', error => { | |
reject(error); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment