Tests/LibWeb: Add test to prove we can pipe through a transform stream
This commit is contained in:
parent
d593436b6d
commit
9802cf07bd
Notes:
sideshowbarker
2024-07-17 16:23:06 +09:00
Author: https://github.com/kennethmyhra Commit: https://github.com/SerenityOS/serenity/commit/9802cf07bd Pull-request: https://github.com/SerenityOS/serenity/pull/23874 Reviewed-by: https://github.com/shannonbooth ✅
2 changed files with 60 additions and 0 deletions
|
@ -0,0 +1,3 @@
|
|||
WELL,
|
||||
HELLO
|
||||
FRIENDS!
|
|
@ -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>
|
Loading…
Add table
Reference in a new issue