manzt / zarrita.js

A JavaScript toolkit for working with chunked, compressed, n-dimensional arrays
https://zarrita.dev
MIT License
38 stars 5 forks source link

Add options for ReferenceStore #155

Closed keller-mark closed 4 months ago

keller-mark commented 4 months ago

This pull request adds two options for the ReferenceStore:

manzt commented 4 months ago

I would also be open to removing this change if it is unfavorable as it can alternatively be achieved in user-land by the user manually null-ing the URLs within the contents of the ReferenceEntrys before passing into fromSpec

Handling a special null to fallback to a target is already a bit of a deviation from the reference specification. I'm open to adding something like preferTarget, but worry dynamically trying to handle entries is challenging and likely hard to handle edge cases with a consistent API. I think I'd rather encourage "pre-processing" an existing spec if this behavior is desired (but open to being convinced otherwise).

One thing that comes to mind is that file references can have multiple URLs (e.g., a Zarr could virtually combine several TIFFs). Right now target only overrides if an entry is null, but in a reference where there is null and other urls it could get complicated.

Maybe a solution would be to make refs public, and let someone modify the refs:

let store = await ReferenceStore.fromUrl("https://example.com/spec.json", {
  target: "http://example.com/some-other-target.tiff",
}) 
for (let entry of refs.values()) {
  // replace all URL refs with 'null', allowing fallback to a new target
  if (Array.isArray(entry)) entry[0] = null; 
}

or have a method to let modifying the refs when the store it first created:

let store = await ReferenceStore.fromUrl("https://example.com/spec.json", {
  target: "http://example.com/some-other-target.tiff",
  preprocess(refs) {
    for (let entry of store.refs.values()) {
      // replace all URL refs with 'null', allowing fallback to a new target
      if (Array.isArray(entry)) entry[0] = null; 
    }
  }
});
keller-mark commented 4 months ago

I see, I hadn't considered those edge cases, I think then best to leave up to the user prior to fromSpec. I have removed that change here