ammo
Installation:

npm: npm install @hapi/ammo

yarn: yarn add @hapi/ammo

Usage

// basic usage
const range = Ammo.header('bytes=1-5', 10);
// range --> [{ from: 1, to: 5 }]

// multiple ranges
const range = Ammo.header('bytes=1-5,7-10', 10);
// range --> [{ from: 1, to: 5 }, { from: 7, to: 9 }]

// streams (get range within a `source`)
const range = Ammo.header('bytes=1000-4000', 5000);
const stream = new Ammo.Stream(range[0]);
const buffer = await Wreck.read(source.pipe(stream));

// buffer is the portion of source within range

Methods

header(header, length)

Parses the range from a HTTP header, where:

  • header - A string in the form of bytes=from-to, where from and to are integers specifying the range. Both are optional. Multiple ranges can be passed as a comma delimited list.
  • length - A positive integer specifying the maximum length the range can cover. If a to value passed in the header string is greater than length, the to value is set as length - 1.

Returns an array of objects with the properties from and to, which specify the beginning and ending of the range. Overlapping ranges are combined into one object. Returns null for invalid input.

new Ammo.Stream(range)

Creates a Transform Stream that extracts the portion of a piped in stream within range, where:

  • range - an object with the properties from and to that specify the range of the piped in stream to read. Objects returned by Ammo.header can be passed into range.

Changelog

4.0.0
breaking changes
#32
Add types
#31
Drop node 8
#16
Stream early exit
#25
Update deps
#23
Change module namespace
#20
Remove engines
#19
Update hoek v6
#18
Improve error handling
3.0.0
breaking changes
#17
Node 8
#22
Commercial version of v2 branch
#15
Update deps.
#14
Update deps
#10
dependency update
#8
Update deps
#6
Test on node v6, update dependencies
2.0.0
breaking changes
#4
ES6 style changes and node v4
#3
Apply hapi project style
clipboard