-
-
Save jakearchibald/c2052ef298459355963b8cfb79c71d1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function parseJSON() { | |
return new TransformStream({ | |
transform(chunk, controller) { | |
controller.enqueue(JSON.parse(chunk)); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function splitStream(splitOn) { | |
let buffer = ''; | |
return new TransformStream({ | |
transform(chunk, controller) { | |
buffer += chunk; | |
const parts = buffer.split(splitOn); | |
parts.slice(0, -1).forEach(part => controller.enqueue(part)); | |
buffer = parts[parts.length - 1]; | |
}, | |
flush(controller) { | |
if (buffer) controller.enqueue(buffer); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
return new TransformStream()
<-- you're instantiating one of these https://streams.spec.whatwg.org/#ts-class? I can't find anything on the web about those... People write about streams, but I can't find any other examples ofTransformStream
or their current support.Was reading this and came here to learn. Can you explain more?