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

ReferenceStore creation, non-async #203

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/odd-ties-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zarrita/storage": patch
---

Enable creation of ReferenceStore via non-async functions.
44 changes: 44 additions & 0 deletions packages/storage/__tests__/ref.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { afterEach, describe, expect, it, vi } from "vitest";

import ReferenceStore from "../src/ref.js";

describe("ReferenceStore", () => {
afterEach(() => {
vi.restoreAllMocks();
});

it("store creation is not async", async () => {
let spec = Promise.resolve({
version: 1,
refs: {
".zgroup": '{"zarr_format":2}',
".zattrs": '{"encoding-type":"anndat…oding-version":"0.1.0"}',
},
});
let store = ReferenceStore.fromSpec(spec);
let bytes = await store.get("/.zgroup");
expect(bytes).toBeInstanceOf(Uint8Array);
expect(JSON.parse(new TextDecoder().decode(bytes))).toMatchInlineSnapshot(`
{
"zarr_format": 2,
}
`);
});
it("store creation can still accept a non-promise", async () => {
let spec = {
version: 1,
refs: {
".zgroup": '{"zarr_format":2}',
".zattrs": '{"encoding-type":"anndat…oding-version":"0.1.0"}',
},
};
let store = ReferenceStore.fromSpec(spec);
let bytes = await store.get("/.zgroup");
expect(bytes).toBeInstanceOf(Uint8Array);
expect(JSON.parse(new TextDecoder().decode(bytes))).toMatchInlineSnapshot(`
{
"zarr_format": 2,
}
`);
});
});
18 changes: 9 additions & 9 deletions packages/storage/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ interface ReferenceStoreOptions {

/** @experimental */
class ReferenceStore implements AsyncReadable<RequestInit> {
#refs: Map<string, ReferenceEntry>;
#refs: Promise<Map<string, ReferenceEntry>>;
#opts: ReferenceStoreOptions;
#overrides: RequestInit;

constructor(
refs: Map<string, ReferenceEntry>,
refs: Promise<Map<string, ReferenceEntry>> | Map<string, ReferenceEntry>,
opts: ReferenceStoreOptions = {},
) {
this.#refs = refs;
this.#refs = Promise.resolve(refs);
this.#opts = opts;
this.#overrides = opts.overrides || {};
}
Expand All @@ -57,7 +57,7 @@ class ReferenceStore implements AsyncReadable<RequestInit> {
key: AbsolutePath,
opts: RequestInit = {},
): Promise<Uint8Array | undefined> {
let ref = this.#refs.get(strip_prefix(key));
let ref = (await this.#refs).get(strip_prefix(key));

if (!ref) return;

Expand Down Expand Up @@ -91,19 +91,19 @@ class ReferenceStore implements AsyncReadable<RequestInit> {
}

static fromSpec(
spec: Record<string, unknown>,
spec: Promise<Record<string, unknown>> | Record<string, unknown>,
opts?: ReferenceStoreOptions,
): ReferenceStore {
// @ts-expect-error - TS doesn't like the type of `parse`
let refs = parse(spec);
let refs = Promise.resolve(spec).then((spec) => parse(spec));
return new ReferenceStore(refs, opts);
}

static async fromUrl(
static fromUrl(
url: string | URL,
opts?: ReferenceStoreOptions,
): Promise<ReferenceStore> {
let spec = await fetch(url, opts?.overrides).then((res) => res.json());
): ReferenceStore {
let spec = fetch(url, opts?.overrides).then((res) => res.json());
return ReferenceStore.fromSpec(spec, opts);
}
}
Expand Down