• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..21-May-2021-

lib/H03-May-2022-

LICENSEH A D21-May-20211.5 KiB2623

README.mdH A D21-May-20211.8 KiB5337

package.jsonH A D21-May-2021539 2625

README.md

1# ammo
2
3HTTP Range processing utilities.
4
5[![Npm Version](https://img.shields.io/npm/v/ammo.svg)](https://npmjs.com/package/ammo)
6[![Build Status](https://secure.travis-ci.org/hapijs/ammo.png)](http://travis-ci.org/hapijs/ammo)
7
8Lead Maintainer - [Gil Pedersen](https://github.com/kanongil)
9
10## Usage
11
12```js
13// basic usage
14const range = Ammo.header('bytes=1-5', 10);
15// range --> [{ from: 1, to: 5 }]
16
17// multiple ranges
18const range = Ammo.header('bytes=1-5,7-10', 10);
19// range --> [{ from: 1, to: 5 }, { from: 7, to: 9 }]
20
21// streams (get range within a `source`)
22const range = Ammo.header('bytes=1000-4000', 5000);
23const stream = new Ammo.Stream(range[0]);
24const buffer = async Wreck.read(source.pipe(stream));
25
26// buffer is the portion of source within range
27```
28
29## API
30
31### `header(header, length)`
32
33Parses the range from a HTTP header.
34
35* `header` - A string in the form of `bytes=from-to`, where `from` and `to` are
36integers specifying the range. Both are optional. Multiple ranges can be passed
37as a comma delimited list.
38* `length` - A positive integer specifying the maximum length the range can
39cover. If a `to` value passed in the `header` string is greater than `length`,
40the `to` value is set as `length - 1`.
41
42Returns an array of objects with the properties `from` and `to`, which specify
43the beginning and ending of the range. Overlapping ranges are combined into one
44object. Returns `null` for invalid input.
45
46### `new Ammo.Stream(range)`
47
48Creates a [`Transform Stream`](https://nodejs.org/api/stream.html) that extracts
49the portion of a piped in stream within `range`.
50* `range` - an object with the properties `from` and `to` that specify the range
51of the piped in stream to read. Objects returned by `Ammo.header` can be passed
52into `range`.
53