JSPM

byte-range-stream

3.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 138
  • Score
    100M100P100Q81149F
  • License MIT

Create a multipart/byteranges stream based on passed ranges

Package Exports

  • byte-range-stream
  • byte-range-stream/src/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (byte-range-stream) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

byte-range-stream

npm version

Create a multipart/byteranges stream based on passed ranges.

Code heavily inspired by form-data

Installation

npm install --save byte-range-stream

Usage

const fs = require('fs')
const path = require('path')
const ByteRangeStream = require('byte-range-stream')

const filePath = path.join(__dirname, 'example.js')
const totalSize = fs.statSync(filePath).size
const getChunk = (range) => fs.createReadStream(filePath, {start: range.start, end: range.end})

const byteStream = new ByteRangeStream({
  range: 'bytes=0-100',
  getChunk,
  totalSize,
  contentType: 'text/javascript',
})

// Invalid could mean unsupported range type (only "bytes" is supported)
// or incorrect syntax
if (!byteStream.isValid()) {
  console.log('Invalid')
}

// Unsatisfiable could mean out of range
if (!byteStream.isSatisfiable()) {
  console.log('Unsatisfiable')
}

// You will need the generated boundary for the multipart response,
// as well as content length
const headers = byteStream.getHeaders()

process.stdout.write('206 Partial Content\n\n')
for (const header in headers) {
  process.stdout.write(`${header}: ${headers[header]}\n`)
}

process.stdout.write('\n')
byteStream.pipe(process.stdout)

License

MIT © Espen Hovlandsdal