stream-to-it
Version:
Convert Node.js streams to streaming iterables
23 lines • 850 B
JavaScript
import { duplex } from './duplex.js';
/**
* Convert a [`Transform`](https://nodejs.org/dist/latest/docs/api/stream.html#class-streamtransform)
* stream to an [iterable transform](https://achingbrain.github.io/it-stream-types/interfaces/Transform.html).
*/
export function transform(transform) {
return async function* (source) {
const d = duplex(transform);
// In a transform the sink and source are connected, an error in the sink
// will be thrown in the source also. Catch the sink error to avoid unhandled
// rejections and yield from the source.
let sinkError;
d.sink(source)
.catch((err) => {
sinkError = err;
});
yield* d.source;
if (sinkError != null) {
throw sinkError;
}
};
}
//# sourceMappingURL=transform.js.map