diff --git a/js/mod.ts b/js/mod.ts index 8d638ec..da4a4ad 100644 --- a/js/mod.ts +++ b/js/mod.ts @@ -1,7 +1,5 @@ -import { - instantiate, - type JsLockfile, -} from "./deno_lockfile_wasm.generated.js"; +import * as wasm from "./deno_lockfile_wasm.generated.js"; +import { type JsLockfile } from "./deno_lockfile_wasm.generated.d.ts"; export interface WorkspaceMemberConfig { dependencies?: string[]; @@ -36,7 +34,8 @@ export interface NpmPackageInfo { dependencies: Record; } -export interface Lockfile extends Omit { +export interface Lockfile + extends Omit { copy(): Lockfile; insertNpmPackage(specifier: string, packageInfo: NpmPackageInfo): void; setWorkspaceConfig(config: WorkspaceConfig): void; @@ -48,14 +47,36 @@ export async function parseFromJson( baseUrl: string | URL, json: string | LockfileJson, ): Promise { - const wasm = await instantiate(); + return parseFromJsonWith(baseUrl, json, await wasm.instantiate()); +} + +export interface InstanciateResult { + parseFromJson(baseUrl: string | URL, json: string | LockfileJson): Lockfile; +} + +export async function instantiate( + opts?: wasm.InstantiateOptions, +): Promise { + const mod = await wasm.instantiate(opts); + return { + parseFromJson(baseUrl, json) { + return parseFromJsonWith(baseUrl, json, mod); + }, + }; +} + +function parseFromJsonWith( + baseUrl: string | URL, + json: string | LockfileJson, + mod: Awaited>, +): Lockfile { if (baseUrl instanceof URL) { baseUrl = baseUrl.toString(); } if (typeof json === "object") { json = JSON.stringify(json); } - const inner = wasm.parseFromJson(baseUrl, json); + const inner = mod.parseFromJson(baseUrl, json); return new Proxy(inner, { get(target, prop, receiver) { if (prop === "filename") { diff --git a/js/test.ts b/js/test.ts index 1826afa..ff22074 100644 --- a/js/test.ts +++ b/js/test.ts @@ -5,7 +5,7 @@ import { assertObjectMatch, } from "@std/assert"; import { beforeEach, describe, it } from "@std/testing/bdd"; -import { Lockfile, parseFromJson } from "./mod.ts"; +import { instantiate, Lockfile, parseFromJson } from "./mod.ts"; describe("parseFromJson", () => { const json = { @@ -40,6 +40,19 @@ describe("parseFromJson", () => { }); }); +describe("instantiate", () => { + it("should return a synchronous interface to parseFromJson", async () => { + const wasm = await instantiate(); + assertEquals( + wasm.parseFromJson("file:///deno.lock", { version: "3" }).toJson(), + { + version: "3", + remote: {}, + }, + ); + }); +}); + describe("LockFile", () => { describe("filename", () => { it("should return the filename", async () => {