2.0.0 • Published 10 months ago

fastify-range v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

fastify-range

ci npm JavaScript Style Guide

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.

Install

npm install fastify-range

Compatibility

Plugin versionFastify version
^2.0.0^5.0.0
^1.0.0^4.0.0

Usage

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:

  • returns undefined if the header is missing
  • if throwOnInvalid is true, throws an error if the header is an invalid string, otherwise returns undefined
  • if throwOnInvalid is true, throws an error if the range is unsatisfiable, otherwise returns undefined
  • when the header is good, it returns an object with the following example structure:
{
  unit: 'bytes',
  ranges: [
    { start: 0, end: 99 },
    { start: 100, end: 199 },
    { start: 200, end: 299 }
  ]
}

Example

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)
})

Options

You can pass the following options during the registration:

OptionDefaultDescription
throwOnInvalidfalseIf true, it throws an error if the header is invalid or unsatisfiable.

License

Licensed under MIT.

2.0.0

10 months ago

1.0.0

11 months ago