/ concept-collection / remote-hdf5-lazy-read
Sign in
concept-collection / remote-hdf5-lazy-read
remote-hdf5-lazy-read / src / nwb / dandi.ts
37 lines · 1.4 KBCodeBlameHistory
d9fc1b2Demonstrate lazy reading of remote HDF5/NWB files (direct h5wasm + LINDI)magland 1// Helpers for resolving DANDI asset URLs and locating LINDI indexes.
2// This mirrors what neurosift does in src/pages/NwbPage/hdf5Interface.ts.
4export const extractAssetId = (downloadUrl: string): string | undefined => {
5 // e.g. https://api.dandiarchive.org/api/assets/<assetId>/download/
6 const m = downloadUrl.match(/\/assets\/([^/]+)\/download/);
7 return m ? m[1] : undefined;
8};
10// DANDI download URLs are 302-redirected to a (signed) S3 object URL.
11// We follow the redirect with a GET and then abort, because a HEAD request is
12// rejected by CORS on the DANDI S3 bucket. We only want `response.url`.
13export const resolveDandiDownloadUrl = async (url: string): Promise<string> => {
14 const controller = new AbortController();
15 try {
16 const response = await fetch(url, { signal: controller.signal });
17 const resolved = response.url || url;
18 controller.abort();
19 return resolved;
20 } catch {
21 controller.abort();
22 return url;
23 }
24};
26// neurosift hosts precomputed LINDI indexes for published dandisets here.
27export const lindiUrlForAsset = (dandisetId: string, assetId: string): string =>
28 `https://lindi.neurosift.org/dandi/dandisets/${dandisetId}/assets/${assetId}/nwb.lindi.json`;
30export const lindiIndexExists = async (lindiUrl: string): Promise<boolean> => {
31 try {
32 const resp = await fetch(lindiUrl, { method: "HEAD" });
33 return resp.ok;
34 } catch {
35 return false;
36 }
37};
moveopenescclose