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

Feature Request: Allow Uint8 or ObjectURL blobs in deserialize filtered query #237

Open
disarticulate opened this issue Sep 18, 2022 · 2 comments

Comments

@disarticulate
Copy link

disarticulate commented Sep 18, 2022

Was hoping to be able to place flatgeobuf into a webworker and provide range requests/filtered requests from arbitrary serialized geojson features.

However, the range request feature with spatial filter seems directly tied to the HTTP/request system.

Reviewing the code, it looks tightly coupled.

It seems like it should just be given an interface that returns various slices of data from . Is it possible to make a cheap object interface that can be substituted for the <url> input.

@bjornharrtell
Copy link
Member

Yeah I agree it would make sense to decouple it.

@disarticulate
Copy link
Author

disarticulate commented Aug 13, 2023

So, I just monkey patched the javascript library's fetch as follows:

//interceptFetch.js
const { fetch: origFetch } = globalThis

globalThis.fetch = async (...args) => {
    const [url, fetchOpts] = args

    for (const [route, handler] of handlers) {
      if (route({ url, fetchOpts })) {
        const response = handler({ url, fetchOpts })
        return response
      }
    }

    return origFetch(...args)
import rangeParser from 'range-parser' // npm package
//handler...
    function appendToUint8Array(arr, data) {
      const newArray = new Uint8Array(arr.length + data.length)
      newArray.set(arr) // copy old data
      newArray.set(data, arr.length) // copy new data after end of old data
      return newArray
    }
   const fcFgb = ...data
    if (fetchOpts?.headers) {
      let chunks
      for (const header of Object.keys(fetchOpts.headers)) {
        if (header.toLowerCase() === 'range') {
          const parser = rangeParser(fcFgb.length, fetchOpts.headers[header])
          chunks = new Uint8Array()
          for (const { start, end } of parser) {
            const chunk = fcFgb.slice(start, end)
            chunks = appendToUint8Array(chunks, chunk)
            console.log('range', { start, end })
          }
        }
      }
      if (chunks) {
        fcFgb = chunks
      }
    }
    return new Response(fcFgb, {
      status: 200,
      headers: {
        'content-type': 'application/vnd.flatgeobuf',
      },
    })

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

No branches or pull requests

2 participants