From aa689444fb858a155d037624db21950ad052f829 Mon Sep 17 00:00:00 2001 From: Daniel Freytag Date: Wed, 13 Nov 2024 10:23:39 +0100 Subject: [PATCH] feat: add Deno build to WASM --- .gitignore | 1 + CHANGELOG.md | 4 ++++ Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 14 ++++++++++++++ web/deno.ts | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 web/deno.ts diff --git a/.gitignore b/.gitignore index 32218a8..4d42116 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ target/ .DS_Store *.svg !assets/*.svg +pkg/ \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 5117150..08c03ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 5.1.3 / 2024-11-13 + +- feat: add Deno build to WASM + ## 5.1.0 / 2024-11-13 - feat: add WASM build diff --git a/Cargo.lock b/Cargo.lock index 070b48b..8667c09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -342,7 +342,7 @@ checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" [[package]] name = "githubchart-rust" -version = "5.1.0" +version = "5.1.3" dependencies = [ "getrandom", "js-sys", diff --git a/Cargo.toml b/Cargo.toml index fc043bc..5d32994 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "githubchart-rust" -version = "5.1.0" +version = "5.1.3" authors = ["frytg"] edition = "2021" license = "MIT" diff --git a/README.md b/README.md index dc9c4b0..6da52d5 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,20 @@ This project is already configured to build for Web with `wasm-pack`. Run this c wasm-pack build --target web ``` +or [specifically for Deno](https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#deno): + +```sh +wasm-pack build --target deno --out-dir pkg-deno +``` + +or a combined version for both: + +```sh +rm -rf pkg && wasm-pack build --target deno --out-name githubchart_rust_deno && wasm-pack build --target web && rm pkg/.gitignore +``` + +For the combined version, you will need to remove `files` from [`pkg/package.json`](./pkg/package.json) to publish all files (web+deno) to NPM. + There's also an example in [`web/example.html`](./web/example.html) that you can run locally. More docs about this: diff --git a/web/deno.ts b/web/deno.ts new file mode 100644 index 0000000..10e7d55 --- /dev/null +++ b/web/deno.ts @@ -0,0 +1,36 @@ +import * as BunnySDK from "https://esm.sh/@bunny.net/edgescript-sdk@0.11.2"; +import { generate_github_chart } from "https://esm.sh/githubchart-rust@5.1.3/githubchart_rust_deno.js"; + +/** + * Executes a WASM binary and returns the response value + * @param {Request} request - The Fetch API Request object. + * @return {Response} The HTTP response or string. + */ +BunnySDK.net.http.serve(async (req: Request): Promise => { + try { + // parse incoming request + const url = new URL(req.url); + + // extract username from path + const username = url.pathname.split("/")[1]; + + // check if username is provided + if (!username) return new Response("No username provided", { status: 400 }); + + // generate chart + const chart = await generate_github_chart(username, "default"); + + // return response + return new Response(chart, { + headers: { + "Content-Type": "image/svg+xml", + "Cache-Control": "max-age=300", + "x-generated-by": "githubchart-rust", + "x-username": username, + }, + }); + } catch (error) { + console.log(req.method, req.url, error); + return new Response("Internal Server Error", { status: 500 }); + } +});