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

feat: add wasm interface #28

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
.vscode/
/target
/target
js/deno_lockfile_wasm_bg.wasm
js/deno_lockfile_wasm.generated.d.ts
js/deno_lockfile_wasm.generated.js
js/LICENSE
108 changes: 108 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ license = "MIT"
description = "An implementation of a lockfile used in Deno"
repository = "https://github.com/denoland/deno_lockfile"

[workspace]
members = ["lib"]

[lib]
name = "deno_lockfile"

[dependencies]
serde = { version = "1.0.149", features = ["derive"] }
serde_json = "1.0.85"
Expand Down
19 changes: 19 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"tasks": {
"build": "deno task pre-build && deno task wasmbuild --project deno_lockfile_wasm --out ./js",
"pre-build": "cp LICENSE js/LICENSE",
"test": "deno test --allow-read",
"test:fast": "deno task test --no-check --parallel --shuffle",
"wasmbuild": "deno run -A jsr:@deno/wasmbuild@0.17.2"
},
"workspace": [
"./js"
],
"imports": {
"@std/assert": "jsr:@std/assert@^0.226.0",
"@std/testing": "jsr:@std/testing@^0.225.2"
},
"exclude": [
"./target"
]
}
34 changes: 34 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions js/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@deno/lockfile",
"version": "0.0.0",
"exports": {
".": "./mod.ts"
},
"publish": {
"exclude": [
"!**"
]
}
}
86 changes: 86 additions & 0 deletions js/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import * as wasm from "./deno_lockfile_wasm.generated.js";
import { type JsLockfile } from "./deno_lockfile_wasm.generated.d.ts";

export interface WorkspaceMemberConfig {
dependencies?: string[];
packageJson?: {
dependencies: string[];
};
}

export interface WorkspaceConfig extends WorkspaceMemberConfig {
members?: Record<string, WorkspaceMemberConfig>;
}

export interface LockfileJson {
version: string;
packages?: {
specifiers: Record<string, string>;
jsr?: Record<string, JsrPackageInfo>;
npm?: Record<string, NpmPackageInfo>;
};
redirects?: Record<string, string>;
remote: Record<string, string>;
workspace?: WorkspaceConfig;
}

export interface JsrPackageInfo {
integrity: string;
dependencies?: string[];
}

export interface NpmPackageInfo {
integrity: string;
dependencies: Record<string, string>;
}

export interface Lockfile extends Omit<JsLockfile, "filename" | "free"> {
insertNpmPackage(name: string, packageInfo: NpmPackageInfo): void;
setWorkspaceConfig(config: WorkspaceConfig): void;
toJson(): LockfileJson;
get filename(): string;
}

export async function parseFromJson(
baseUrl: string | URL,
json: string | LockfileJson,
): Promise<Lockfile> {
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 = mod.parseFromJson(baseUrl, json);
return new Proxy(inner, {
get(target, prop, receiver) {
if (prop === "filename") {
return inner.filename();
}
return Reflect.get(target, prop, receiver);
},
}) as unknown as Lockfile;
}
Loading
Loading