conflux
conflux copied to clipboard
http reader
I created a little http range reader i think it maybe could be useful - Follows the same File IDL stuff.
class HttpFileLike extends Request {
get size () {
const offset = this.headers.get('range').match(/\d+/g).map(Number)
return offset[1] - offset[0] + 1
}
get name () {
return this.url.substring(this.url.lastIndexOf('/') + 1)
}
slice (start, end) {
const headers = new Headers(this.headers)
const offset = headers.get('range').match(/\d+/g).map(Number)
const size = this.size
// thanks for this.
// https://github.com/bitinn/fetch-blob/blob/78ec7f45c0d54e4090423dbe56abd1d455223ba2/blob.js#L76-L106
let relativeStart, relativeEnd
if (start === undefined) {
relativeStart = 0
} else if (start < 0) {
relativeStart = Math.max(size + start, 0)
} else {
relativeStart = Math.min(start, size)
}
if (end === undefined) {
relativeEnd = size
} else if (end < 0) {
relativeEnd = Math.max(size + end, 0)
} else {
relativeEnd = Math.min(end, size)
}
const span = Math.max(relativeEnd - relativeStart, 0)
headers.set('range', `bytes=${offset[0] + relativeStart}-${offset[0] + relativeStart + span - 1}`)
return new HttpFileLike(this, { headers })
}
arrayBuffer () {
return fetch(this.clone())
.then(res => res.arrayBuffer())
}
stream () {
const ts = new TransformStream()
fetch(this.clone()).then(res => res.body.pipeTo(ts.writable))
return ts.readable
}
}
const fileLike = new HttpFileLike(url, {
headers: {
Range: 'bytes=0-1023'
}
})
req.slice(-65536).arrayBuffer() // gets the end of the zip (central dir)
req.slice(16, 255).stream() // reads one entry inside the zip file (making another range request)
feedback?
Cool, this seems like a nice gist or npm module. Not sure if we'd want this in conflux unless we have a need for it.
Interesting, can your range reader be used in conjunction with conflux to extract a specific file from a remote URL zip without having to download the entire ZIP file?
Interesting, can your range reader be used in conjunction with conflux to extract a specific file from a remote URL zip without having to download the entire ZIP file?
yes