Limit the number of bytes that are yielded from an (async) iterable
When passed an (async)iterable that yields objects with a .byteLength
property, throw if the cumulative value of that property reaches a limit.
import limitBytes from 'it-limit-bytes'
import drain from 'it-drain'
// This can also be an iterator, generator, etc
const values = [
Uint8Array.from([0, 1, 2, 3]),
Uint8Array.from([4, 5, 6, 7])
]
drain(limitBytes(values, 5))
// throws "Read too many bytes - 8/5"Async sources must be awaited:
import limitBytes from 'it-limit-bytes'
import drain from 'it-drain'
// This can also be an iterator, generator, etc
const values = [
Uint8Array.from([0, 1, 2, 3]),
Uint8Array.from([4, 5, 6, 7])
]
await drain(limitBytes(values, 5))
// throws "Read too many bytes - 8/5"$ npm i it-limit-bytesLoading this module through a script tag will make its exports available as ItLimitBytes in the global namespace.
<script src="https://unpkg.com/it-limit-bytes/dist/index.min.js"></script>Licensed under either of
- Apache 2.0, (LICENSE-APACHE / http://www.apache.org/licenses/LICENSE-2.0)
- MIT (LICENSE-MIT / http://opensource.org/licenses/MIT)
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.