Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

it-limit-bytes

codecov CI

Limit the number of bytes that are yielded from an (async) iterable

About

When passed an (async)iterable that yields objects with a .byteLength property, throw if the cumulative value of that property reaches a limit.

Example

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"

Install

$ npm i it-limit-bytes

Browser <script> tag

Loading 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>

API Docs

License

Licensed under either of

Contribution

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.