1
0
mirror of https://gitlab.com/timvisee/send.git synced 2024-11-10 21:22:35 +01:00
send/app/streams.js

37 lines
816 B
JavaScript
Raw Normal View History

2018-07-19 01:39:14 +02:00
/* global ReadableStream */
2018-07-17 20:40:01 +02:00
2018-07-19 01:39:14 +02:00
export function transform(readable, transformer) {
const reader = readable.getReader();
const tstream = new ReadableStream({
start(controller) {
if (transformer.start) {
return transformer.start(controller);
}
},
async pull(controller) {
let enqueued = false;
const c = {
enqueue(d) {
enqueued = true;
controller.enqueue(d);
}
};
while (!enqueued) {
const x = await reader.read();
if (x.done) {
if (transformer.flush) {
await transformer.flush(controller);
}
return controller.close();
}
await transformer.transform(x.value, c);
}
},
cancel() {
readable.cancel();
}
});
2018-07-17 20:40:01 +02:00
2018-07-19 01:39:14 +02:00
return tstream;
2018-07-17 20:40:01 +02:00
}