Skip to content

Commit

Permalink
feat: add Deno build to WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Nov 13, 2024
1 parent 6306ccc commit aa68944
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ target/
.DS_Store
*.svg
!assets/*.svg
pkg/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "githubchart-rust"
version = "5.1.0"
version = "5.1.3"
authors = ["frytg"]
edition = "2021"
license = "MIT"
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
36 changes: 36 additions & 0 deletions web/deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as BunnySDK from "https://esm.sh/@bunny.net/[email protected]";
import { generate_github_chart } from "https://esm.sh/[email protected]/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<Response> => {
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 });
}
});

0 comments on commit aa68944

Please sign in to comment.