balena-cli/lib/utils/streams.ts
2018-01-11 10:33:08 +01:00

24 lines
563 B
TypeScript

import Promise = require('bluebird');
import fs = require('fs');
export function buffer(
stream: NodeJS.ReadableStream,
bufferFile: string,
): Promise<NodeJS.ReadableStream> {
const fileWriteStream = fs.createWriteStream(bufferFile);
return new Promise(function(resolve, reject) {
stream
.on('error', reject)
.on('end', resolve)
.pipe(fileWriteStream);
}).then(
() =>
new Promise(function(resolve, reject) {
const stream = fs.createReadStream(bufferFile);
stream.on('open', () => resolve(stream)).on('error', reject);
}),
);
}