Tests/LibWeb: Add test to prove we can pipe through a transform stream

This commit is contained in:
Kenneth Myhra 2024-04-07 13:02:12 +02:00 committed by Luke Wilde
parent d593436b6d
commit 9802cf07bd
Notes: sideshowbarker 2024-07-17 16:23:06 +09:00
2 changed files with 60 additions and 0 deletions

View file

@ -0,0 +1,3 @@
WELL,
HELLO
FRIENDS!

View file

@ -0,0 +1,57 @@
<script src="../include.js"></script>
<script>
const CHUNK1 = "well,";
const CHUNK2 = "hello";
const CHUNK3 = "friends!";
asyncTest((done) => {
const transformStream = new TransformStream({
transform(chunk, controller) {
controller.enqueue(
new TextDecoder("utf-8")
.decode(chunk)
.toUpperCase()
);
}
});
const stream = new ReadableStream({
start(controller) {
pullCount = 0;
},
pull(controller) {
const textEncoder = new TextEncoder();
++pullCount;
if (pullCount == 1) {
controller.enqueue(textEncoder.encode(CHUNK1));
} else if (pullCount == 2) {
controller.enqueue(textEncoder.encode(CHUNK2));
} else if (pullCount == 3) {
controller.enqueue(textEncoder.encode(CHUNK3));
} else {
controller.close();
}
},
cancel() {},
});
const reader = transformStream.readable.getReader();
reader.read().then(function processText({done, value}) {
if (done)
return;
println(value);
reader.read().then(processText);
}).then(() => {
done();
});
stream.pipeThrough(transformStream);
});
</script>