Parse the Range header easily
MIT License
This Fastify plugin adds support for the Range
header.
It provides the same API as expressjs
's req.range(size[, options])
with a consistent response.
npm install fastify-range
Plugin version | Fastify version |
---|---|
^2.0.0 |
^5.0.0 |
^1.0.0 |
^4.0.0 |
Register the plugin and use request.range(size[, options])
in your routes.
The options
object is optional and it will be passed to the range-parser
module under the hood.
The decorator parses the Range
header, capping to the given size
. It returns the following response:
undefined
if the header is missingthrowOnInvalid
is true, throws an error if the header is an invalid string, otherwise returns undefined
throwOnInvalid
is true, throws an error if the range is unsatisfiable, otherwise returns undefined
{
unit: 'bytes',
ranges: [
{ start: 0, end: 99 },
{ start: 100, end: 199 },
{ start: 200, end: 299 }
]
}
const fastify = require('fastify')
const range = require('fastify-range')
const app = fastify()
app.register(range, { throwOnInvalid: true })
app.get('/', (request, reply) => {
const size = 1000
const options = { combine: true }
return 'Read: ' + request.range(size, options)
})
You can pass the following options during the registration:
Option | Default | Description |
---|---|---|
throwOnInvalid |
false |
If true , it throws an error if the header is invalid or unsatisfiable. |
Licensed under MIT.