Skip to content

Commit

Permalink
Use Proxy store for nested chunks
Browse files Browse the repository at this point in the history
Code adapted from gzuidhof/zarr.js#88 (comment)
No Typescript types yet
  • Loading branch information
will-moore committed Mar 30, 2021
1 parent ec04cd0 commit 01a051a
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,29 @@ async function normalizeStore(source: string | ZarrArray['store']) {

export async function open(source: string | ZarrArray['store']) {
const { store, path } = await normalizeStore(source);
return openGroup(store, path).catch((err) => {

function nested(store) {
const get = (target, key) => {
if (key === 'getItem' || key === 'setItem' || key === 'containsItem') {
return (path, ...args) => {
if (path.endsWith('.zarray') || path.endsWith('.zattrs') || path.endsWith('.zgroup')) {
return target[key](path, ...args);
}
const prefix = path.split('/');
const chunkKey = prefix.pop();
const newPath = [...prefix, chunkKey.replaceAll('.', '/')].join('/');
return target[key](newPath, ...args);
}
}
return Reflect.get(target, key);
};
return new Proxy(store, { get });
}
const nested_store = nested(store);

return openGroup(nested_store, path).catch((err) => {
if (err instanceof ContainsArrayError) {
return openArray({ store, path });
return openArray({ nested_store, path });
}
throw err;
});
Expand Down

0 comments on commit 01a051a

Please sign in to comment.