Comparing version 3.1.1 to 4.0.0
@@ -39,4 +39,3 @@ 'use strict'; | ||
for (let i = 0; i < ranges.length; ++i) { | ||
let range = ranges[i]; | ||
for (let range of ranges) { | ||
if (range.length === 1) { // '-' | ||
@@ -105,3 +104,3 @@ return null; | ||
exports.Stream = internals.Stream = class extends Stream.Transform { | ||
exports.Clip = class extends Stream.Transform { | ||
@@ -130,36 +129,55 @@ constructor(range) { | ||
this._next = 0; | ||
this._pipes = new Set(); | ||
this.on('pipe', (pipe) => this._pipes.add(pipe)); | ||
this.on('unpipe', (pipe) => this._pipes.delete(pipe)); | ||
} | ||
processChunk(chunk) { | ||
_transform(chunk, encoding, done) { | ||
// Read desired range from a stream | ||
try { | ||
internals.processChunk(this, chunk); | ||
} | ||
catch (err) { | ||
return done(err); | ||
} | ||
const pos = this._next; | ||
this._next = this._next + chunk.length; | ||
return done(); | ||
} | ||
if (this._next <= this._range.from || // Before range | ||
pos > this._range.to) { // After range | ||
_flush(done) { | ||
return; | ||
} | ||
this._pipes.clear(); | ||
done(); | ||
} | ||
}; | ||
// Calc bounds of chunk to read | ||
const from = Math.max(0, this._range.from - pos); | ||
const to = Math.min(chunk.length, this._range.to - pos + 1); | ||
internals.processChunk = function (stream, chunk) { | ||
this.push(chunk.slice(from, to)); | ||
// Read desired range from a stream | ||
const pos = stream._next; | ||
stream._next = stream._next + chunk.length; | ||
if (stream._next <= stream._range.from) { // Before range | ||
return; | ||
} | ||
_transform(chunk, encoding, done) { | ||
try { | ||
this.processChunk(chunk); | ||
if (pos > stream._range.to) { // After range | ||
for (const pipe of stream._pipes) { | ||
pipe.unpipe(stream); | ||
} | ||
catch (err) { | ||
return done(err); | ||
} | ||
return done(); | ||
stream._pipes.clear(); | ||
stream.end(); | ||
return; | ||
} | ||
// Calculate bounds of chunk to read | ||
const from = Math.max(0, stream._range.from - pos); | ||
const to = Math.min(chunk.length, stream._range.to - pos + 1); | ||
stream.push(chunk.slice(from, to)); | ||
}; |
{ | ||
"name": "@hapi/ammo", | ||
"description": "HTTP Range processing utilities", | ||
"version": "3.1.1", | ||
"repository": "git://github.com/hapijs/ammo", | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
"http", | ||
"range", | ||
"utilities" | ||
], | ||
"dependencies": { | ||
"@hapi/hoek": "8.x.x" | ||
}, | ||
"devDependencies": { | ||
"@hapi/code": "6.x.x", | ||
"@hapi/lab": "20.x.x", | ||
"@hapi/wreck": "15.x.x" | ||
}, | ||
"scripts": { | ||
"test": "lab -a @hapi/code -t 100 -L", | ||
"test-cov-html": "lab -a @hapi/code -r html -o coverage.html" | ||
}, | ||
"license": "BSD-3-Clause" | ||
"name": "@hapi/ammo", | ||
"description": "HTTP Range processing utilities", | ||
"version": "4.0.0", | ||
"repository": "git://github.com/hapijs/ammo", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"keywords": [ | ||
"http", | ||
"range", | ||
"utilities" | ||
], | ||
"files": [ | ||
"lib" | ||
], | ||
"dependencies": { | ||
"@hapi/hoek": "8.x.x" | ||
}, | ||
"devDependencies": { | ||
"@hapi/code": "7.x.x", | ||
"@hapi/lab": "21.x.x", | ||
"@hapi/wreck": "16.x.x" | ||
}, | ||
"scripts": { | ||
"test": "lab -a @hapi/code -t 100 -L -Y", | ||
"test-cov-html": "lab -a @hapi/code -r html -o coverage.html" | ||
}, | ||
"license": "BSD-3-Clause" | ||
} |
@@ -1,51 +0,16 @@ | ||
<a href="http://hapijs.com"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a> | ||
<a href="https://hapi.dev"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a> | ||
# ammo | ||
# @hapi/ammo | ||
HTTP Range processing utilities. | ||
#### HTTP Range processing utilities. | ||
[![Build Status](https://secure.travis-ci.org/hapijs/ammo.png)](http://travis-ci.org/hapijs/ammo) | ||
**ammo** is part of the **hapi** ecosystem and was designed to work seamlessly with the [hapi web framework](https://hapi.dev) and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out [hapi](https://hapi.dev) – they work even better together. | ||
## Usage | ||
### Visit the [hapi.dev](https://hapi.dev) Developer Portal for tutorials, documentation, and support | ||
```js | ||
// basic usage | ||
const range = Ammo.header('bytes=1-5', 10); | ||
// range --> [{ from: 1, to: 5 }] | ||
## Useful resources | ||
// 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 | ||
``` | ||
## API | ||
### `header(header, length)` | ||
Parses the range from a HTTP header. | ||
* `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`](https://nodejs.org/api/stream.html) that extracts | ||
the portion of a piped in stream within `range`. | ||
* `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`. | ||
- [Documentation and API](https://hapi.dev/family/ammo/) | ||
- [Versions status](https://hapi.dev/resources/status/#ammo) (builds, dependencies, node versions, licenses, eol) | ||
- [Project policies](https://hapi.dev/policies/) | ||
- [Free and commercial support options](https://hapi.dev/support/) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
8304
159
17