alanshaw / multipart-byte-range

Create multipart/byteranges stream.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

multipart-byte-range

Build JavaScript Style Guide

Encode and decode multipart/byteranges stream.

Install

npm install multipart-byte-range

Usage

Encode

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

Decode

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

About

Create multipart/byteranges stream.

License:Other


Languages

Language:JavaScript 91.7%Language:TypeScript 8.3%