-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace zarr.js with zarrita.js
- Loading branch information
Showing
10 changed files
with
244 additions
and
141 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,2 @@ | ||
import { addCodec } from 'zarr'; | ||
import type { CodecConstructor } from 'numcodecs'; | ||
|
||
type CodecModule = { default: CodecConstructor<Record<string, unknown>> }; | ||
|
||
const cache: Map<string, Promise<CodecModule['default']>> = new Map(); | ||
|
||
function add(name: string, load: () => Promise<CodecModule>): void { | ||
// Cache import to avoid duplicate loads | ||
const loadAndCache = () => { | ||
if (!cache.has(name)) { | ||
const promise = load() | ||
.then((m) => m.default) | ||
.catch((err) => { | ||
cache.delete(name); | ||
throw err; | ||
}); | ||
cache.set(name, promise); | ||
} | ||
return cache.get(name)!; | ||
}; | ||
addCodec(name, loadAndCache); | ||
} | ||
|
||
// Add dynmaically imported codecs to Zarr.js registry. | ||
add('lz4', () => import('numcodecs/lz4')); | ||
add('gzip', () => import('numcodecs/gzip')); | ||
add('zlib', () => import('numcodecs/zlib')); | ||
add('zstd', () => import('numcodecs/zstd')); | ||
add('blosc', () => import('numcodecs/blosc')); | ||
add('jpeg2k', () => import('./jpeg2k')); | ||
import { registry } from '@zarrita/core'; | ||
registry.set('jpeg2k', () => import('./jpeg2k').then((m) => m.default)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,21 @@ | ||
import type { AsyncStore } from 'zarr/types/storage/types'; | ||
import type { AsyncReadable } from '@zarrita/storage'; | ||
import QuickLRU from 'quick-lru'; | ||
|
||
export class LRUCacheStore<S extends AsyncStore<ArrayBuffer>> { | ||
cache: QuickLRU<string, Promise<ArrayBuffer>>; | ||
|
||
export class LRUCacheStore<S extends AsyncReadable> { | ||
cache: QuickLRU<string, Promise<Uint8Array | undefined>>; | ||
constructor(public store: S, maxSize: number = 100) { | ||
this.cache = new QuickLRU({ maxSize }); | ||
} | ||
|
||
getItem(...args: Parameters<S['getItem']>) { | ||
get(...args: Parameters<S['get']>) { | ||
const [key, opts] = args; | ||
if (this.cache.has(key)) { | ||
return this.cache.get(key)!; | ||
} | ||
const value = this.store.getItem(key, opts).catch((err) => { | ||
const value = this.store.get(key, opts).catch((err) => { | ||
this.cache.delete(key); | ||
throw err; | ||
}); | ||
this.cache.set(key, value); | ||
return value; | ||
} | ||
|
||
async containsItem(key: string) { | ||
return this.cache.has(key) || this.store.containsItem(key); | ||
} | ||
|
||
keys() { | ||
return this.store.keys(); | ||
} | ||
|
||
deleteItem(key: string): never { | ||
throw new Error('deleteItem not implemented'); | ||
} | ||
|
||
setItem(key: string, value: ArrayBuffer): never { | ||
throw new Error('setItem not implemented'); | ||
} | ||
} |
Oops, something went wrong.