conflux icon indicating copy to clipboard operation
conflux copied to clipboard

http reader

Open jimmywarting opened this issue 5 years ago • 3 comments

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?

jimmywarting avatar Jul 23 '19 14:07 jimmywarting

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.

eligrey avatar Sep 30 '20 23:09 eligrey

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?

ebremer avatar Nov 01 '21 14:11 ebremer

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

jimmywarting avatar Nov 01 '21 15:11 jimmywarting