@saulx/utils
Advanced tools
Comparing version 1.2.0 to 1.2.1
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const stream_1 = require("stream"); | ||
exports.default = (stream) => new Promise((resolve, reject) => { | ||
stream.on('error', err => { | ||
let pipit = false; | ||
let s = stream; | ||
if (stream instanceof stream_1.Readable || stream instanceof stream_1.PassThrough) { | ||
s = stream; | ||
} | ||
else if (stream instanceof stream_1.Writable || stream instanceof stream_1.Duplex) { | ||
s = new stream_1.PassThrough(); | ||
pipit = true; | ||
} | ||
else { | ||
console.warn('not an instance of stream - trying writable'); | ||
// @ts-ignore | ||
s = new stream_1.PassThrough(stream); | ||
} | ||
s.on('error', err => { | ||
reject(err); | ||
}); | ||
let buffers = []; | ||
stream.on('data', s => { | ||
s.on('data', s => { | ||
buffers.push(s); | ||
}); | ||
stream.on('end', () => { | ||
s.on('end', () => { | ||
resolve(Buffer.from(buffers)); | ||
}); | ||
if (pipit) { | ||
// @ts-ignore | ||
stream.pipe(s); | ||
} | ||
}); | ||
//# sourceMappingURL=readStream.js.map |
{ | ||
"name": "@saulx/utils", | ||
"main": "./dist/index.js", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"scripts": { | ||
@@ -6,0 +6,0 @@ "build": "tsc", |
@@ -1,15 +0,32 @@ | ||
import { Stream } from 'stream' | ||
import { Stream, Readable, PassThrough, Writable, Duplex } from 'stream' | ||
export default (stream: Stream): Promise<Buffer> => | ||
new Promise((resolve, reject) => { | ||
stream.on('error', err => { | ||
let pipit = false | ||
let s = stream | ||
if (stream instanceof Readable || stream instanceof PassThrough) { | ||
s = stream | ||
} else if (stream instanceof Writable || stream instanceof Duplex) { | ||
s = new PassThrough() | ||
pipit = true | ||
} else { | ||
console.warn('not an instance of stream - trying writable') | ||
// @ts-ignore | ||
s = new PassThrough(stream) | ||
} | ||
s.on('error', err => { | ||
reject(err) | ||
}) | ||
let buffers = [] | ||
stream.on('data', s => { | ||
s.on('data', s => { | ||
buffers.push(s) | ||
}) | ||
stream.on('end', () => { | ||
s.on('end', () => { | ||
resolve(Buffer.from(buffers)) | ||
}) | ||
if (pipit) { | ||
// @ts-ignore | ||
stream.pipe(s) | ||
} | ||
}) |
@@ -606,16 +606,9 @@ import test from 'ava' | ||
test.cb('readStream', t => { | ||
const { PassThrough, Writable } = require('stream') | ||
const { PassThrough } = require('stream') | ||
const pass = new PassThrough() | ||
const writable = new Writable() | ||
pass.write('ok') // Will not emit 'data'. | ||
readStream(pass).then(v => { | ||
console.log('done!', v.toString()) | ||
t.end() | ||
}) | ||
pass.pipe(writable) | ||
pass.unpipe(writable) | ||
// readableFlowing is now false. | ||
pass.resume() | ||
setTimeout(() => { | ||
pass.end('x') | ||
}, 100) | ||
pass.end(Buffer.from('flap')) | ||
}) |
Sorry, the diff of this file is not supported yet
65644
1623