2021-02-04 19:15:23 +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) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const opts = { objectMode: true, ..._opts };
|
|
|
|
super(opts);
|
|
|
|
this._asyncWriteFn = asyncWriteFn;
|
2018-06-14 01:45:18 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-02-04 19:15:23 +01:00
|
|
|
promiseToCallback(this._asyncWriteFn(chunk, encoding))(callback);
|
2018-06-14 01:45:18 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-05 19:01:34 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function createStreamSink(asyncWriteFn, _opts) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return new AsyncWritableStream(asyncWriteFn, _opts);
|
2019-07-05 19:01:34 +02:00
|
|
|
}
|