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
|
|
|
|
}
|
|
|
|
|
2020-07-20 19:02:49 +02:00
|
|
|
// write from incoming stream to state
|
2018-06-14 01:45:18 +02:00
|
|
|
_write (chunk, encoding, callback) {
|
|
|
|
promiseToCallback(this._asyncWriteFn(chunk, encoding))(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-07-05 19:01:34 +02:00
|
|
|
|
2020-05-06 00:19:38 +02:00
|
|
|
export default function createStreamSink (asyncWriteFn, _opts) {
|
2019-07-05 19:01:34 +02:00
|
|
|
return new AsyncWritableStream(asyncWriteFn, _opts)
|
|
|
|
}
|