Skip to content

Commit

Permalink
feat(wasm): add a synchronous interface
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Jun 20, 2024
1 parent 24593d0 commit 3462fcf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
35 changes: 28 additions & 7 deletions js/mod.ts
Original file line number Diff line number Diff line change
@@ -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[];
Expand Down Expand Up @@ -36,7 +34,8 @@ export interface NpmPackageInfo {
dependencies: Record<string, string>;
}

export interface Lockfile extends Omit<JsLockfile, "copy" | "filename" | "free"> {
export interface Lockfile
extends Omit<JsLockfile, "copy" | "filename" | "free"> {
copy(): Lockfile;
insertNpmPackage(specifier: string, packageInfo: NpmPackageInfo): void;
setWorkspaceConfig(config: WorkspaceConfig): void;
Expand All @@ -48,14 +47,36 @@ export async function parseFromJson(
baseUrl: string | URL,
json: string | LockfileJson,
): Promise<Lockfile> {
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<InstanciateResult> {
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<ReturnType<typeof wasm.instantiate>>,
): 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") {
Expand Down
15 changes: 14 additions & 1 deletion js/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 3462fcf

Please sign in to comment.