Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

multipart-byte-range

alanshaw285Apache-2.0 OR MIT3.0.1TypeScript support: included

Encode and decode multipart/byteranges stream.

range, multipart, byteranges, multipart/byteranges, bytes, header, http, parse, parser, encode, decode

readme

multipart-byte-range

Build JavaScript Style Guide

Encode and decode multipart/byteranges stream.

Install

npm install multipart-byte-range

Usage

Create

import { MultipartByteRangeEncoder } from 'multipart-byte-range'

const data = new Blob(['data'.repeat(500)])

// Range: bytes=3-6, 12-100, 110-
const ranges = [[3, 6], [12, 100], [110]]

// fetch the bytes for the passed range
const getRange = async range => data.slice(range[0], range[1] + 1).stream()

// optionally specify the total size or the content type
const options = { totalSize: data.length, contentType: 'application/octet-stream' }

new MultipartByteRangeEncoder(ranges, getRange, options).pipeTo(new WritableStream())

Parse

import { MultipartByteRangeDecoder, getBoundary, decodePartHeader } from 'multipart-byte-range'

const res = await fetch(url, { headers: { Range: `bytes=3-6,18-29` } })
const boundary = getBoundary(res.headers)

await res.body
  .pipeThrough(new MultipartByteRangeDecoder(boundary))
  .pipeTo(new WritableStream({
    write (part) {
      const headers = decodePartHeader(part.header)
      const bytes = part.content
    }
  }))

Contributing

Feel free to join in. All welcome. Open an issue!

License

Dual-licensed under MIT / Apache 2.0