Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http reader #31

Open
jimmywarting opened this issue Jul 23, 2019 · 3 comments
Open

http reader #31

jimmywarting opened this issue Jul 23, 2019 · 3 comments
Labels
enhancement New feature or request

Comments

@jimmywarting
Copy link
Contributor

jimmywarting commented Jul 23, 2019

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?

@eligrey
Copy link
Member

eligrey commented Sep 30, 2020

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.

@ebremer
Copy link

ebremer commented Nov 1, 2021

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?

@jimmywarting
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants