slice-source
A readable stream reader that takes a length argument, allowing you to pull the specified number of bytes from the underlying readable stream. For example:
<!DOCTYPE html>
<script src="https://unpkg.com/slice-source@0"></script>
<script>
fetch("https://cors-anywhere.herokuapp.com/").then((response) => {
return (function read(reader) {
return reader.read(40).then((result) => {
if (result.done) return;
console.log(result.value);
return read(reader);
});
})(sources.slice(response.body));
}).catch((error) => console.error(error.stack));
</script>
API Reference
# slice(stream) <>
# slice(buffer)
# slice(array)
Returns a sliceable source for the specified readable stream. The source may also be specified as an ArrayBuffer or a Uint8Array for reading from something that is already in-memory.
# source.read(length) <>
Returns a Promise for the next chunk of data from the underlying stream. The yielded result is an object with the following properties:
value
- a Uint8Array, or undefined if the stream endeddone
- a boolean which is true if the stream ended
The promise will be yielded with a value of length bytes, or the remaining bytes of the underlying stream if the underlying stream has more than zero but fewer than length bytes remaining. When no bytes remain in the stream, the yielded value will be undefined, and done will be true.
# source.cancel() <>
Returns a Promise which is resolved when the underlying stream has been destroyed.