2020-01-09 04:34:58 +01:00
|
|
|
import { Writable as WritableStream } from 'readable-stream'
|
|
|
|
import promiseToCallback from 'promise-to-callback'
|
2018-06-14 01:45:18 +02:00
|
|
|
|
|
|
|
class AsyncWritableStream extends WritableStream {
|
|
|
|
|
|
|
|
constructor (asyncWriteFn, _opts) {
|
|
|
|
const opts = Object.assign({ objectMode: true }, _opts)
|
|
|
|
super(opts)
|
|
|
|
this._asyncWriteFn = asyncWriteFn
|
|
|
|
}
|
|
|
|
|
|
|
|
// write from incomming stream to state
|
|
|
|
_write (chunk, encoding, callback) {
|
|
|
|
promiseToCallback(this._asyncWriteFn(chunk, encoding))(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-07-05 19:01:34 +02:00
|
|
|
|
|
|
|
function createStreamSink (asyncWriteFn, _opts) {
|
|
|
|
return new AsyncWritableStream(asyncWriteFn, _opts)
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default createStreamSink
|