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 {
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(asyncWriteFn, _opts) {
|
2020-08-19 18:27:05 +02:00
|
|
|
const opts = { objectMode: true, ..._opts }
|
2018-06-14 01:45:18 +02:00
|
|
|
super(opts)
|
|
|
|
this._asyncWriteFn = asyncWriteFn
|
|
|
|
}
|
|
|
|
|
2020-07-20 19:02:49 +02:00
|
|
|
// write from incoming stream to state
|
2020-11-03 00:41:28 +01:00
|
|
|
_write(chunk, encoding, callback) {
|
2018-06-14 01:45:18 +02:00
|
|
|
promiseToCallback(this._asyncWriteFn(chunk, encoding))(callback)
|
|
|
|
}
|
|
|
|
}
|
2019-07-05 19:01:34 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function createStreamSink(asyncWriteFn, _opts) {
|
2019-07-05 19:01:34 +02:00
|
|
|
return new AsyncWritableStream(asyncWriteFn, _opts)
|
|
|
|
}
|