multipart-byte-range
v3.0.1
Published
Encode and decode multipart/byteranges stream.
Downloads
192
Maintainers
Readme
multipart-byte-range
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