multipart-byte-range
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

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

Package Sidebar

Install

npm i multipart-byte-range

Weekly Downloads

46

Version

3.0.1

License

Apache-2.0 OR MIT

Unpacked Size

41.6 kB

Total Files

17

Last publish

Collaborators

  • alanshaw