Build with trunk

This streamlines the project configuration.
This commit is contained in:
Simon Bruder 2023-12-28 22:58:45 +01:00
parent 5e49335c2c
commit 9bddae5f11
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
9 changed files with 18 additions and 54 deletions

5
.gitignore vendored
View file

@ -1,6 +1,7 @@
# Rust # Rust
/backend/target/ /target/
/backend/pkg/ /pkg/
/dist/
# Nix # Nix
/result* /result*

View file

View file

@ -1,5 +1,5 @@
[package] [package]
name = "password-hash-self-service-backend" name = "password-hash-self-service"
version = "0.0.0" version = "0.0.0"
authors = ["Simon Bruder <simon@sbruder.de>"] authors = ["Simon Bruder <simon@sbruder.de>"]
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"

2
Trunk.toml Normal file
View file

@ -0,0 +1,2 @@
[build]
offline = true

8
app.js
View file

@ -1,5 +1,3 @@
import init, { hash_bcrypt, hash_sha512crypt } from "./backend/pkg/password_hash_self_service_backend.js"
function displayHash(hash) { function displayHash(hash) {
document.getElementById("hash").innerText = hash document.getElementById("hash").innerText = hash
document.getElementById("hash-container").classList.add("done") document.getElementById("hash-container").classList.add("done")
@ -31,17 +29,15 @@ function getHashSetup(formData) {
function getHasher(hashType) { function getHasher(hashType) {
switch (hashType) { switch (hashType) {
case "bcrypt": case "bcrypt":
return (password, { cost }) => hash_bcrypt(password, cost) return (password, { cost }) => wasmBindings.hash_bcrypt(password, cost)
case "sha512crypt": case "sha512crypt":
return (password, { rounds }) => hash_sha512crypt(password, rounds) return (password, { rounds }) => wasmBindings.hash_sha512crypt(password, rounds)
default: default:
throw Error("Invalid hash type specified: " + hashType) throw Error("Invalid hash type specified: " + hashType)
} }
} }
(async () => { (async () => {
await init()
document.getElementById("generate").addEventListener("click", e => { document.getElementById("generate").addEventListener("click", e => {
e.target.disabled = true e.target.disabled = true
const buttonText = e.target.innerText const buttonText = e.target.innerText

View file

@ -26,78 +26,43 @@
in in
rec { rec {
packages = { packages = {
backend = naerskLib.buildPackage { password-hash-self-service = naerskLib.buildPackage {
src = ./backend; src = self;
nativeBuildInputs = with pkgs; [ nativeBuildInputs = with pkgs; [
binaryen # wasm-opt binaryen # wasm-opt
trunk
wasm-bindgen-cli wasm-bindgen-cli
wasm-pack
]; ];
# required, otherwise the dependencies are built for the host system # required, otherwise the dependencies are built for the host system
CARGO_BUILD_TARGET = "wasm32-unknown-unknown"; CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
WASM_PACK_CACHE = "/build/wasm-pack-cache";
overrideMain = (o: o // { overrideMain = (o: o // {
buildPhase = '' buildPhase = ''
runHook preBuild runHook preBuild
# FIXME this fails when the locked wasm-pack version is different from the one in nixpkgs trunk build --release --offline --frozen
wasm-pack build --target web --release --mode no-install -- --offline
runHook postBuild runHook postBuild
''; '';
installPhase = '' installPhase = ''
runHook preInstall runHook preInstall
cp -r pkg $out cp -r dist $out
runHook postInstall runHook postInstall
''; '';
}); });
}; };
frontend = pkgs.stdenvNoCC.mkDerivation { default = packages.password-hash-self-service;
name = "password-hash-self-service";
src = self;
dontBuild = true;
installPhase = ''
runHook preInstall
mkdir $out
cp \
index.html \
style.css \
app.js \
$out
mkdir $out/backend
ln -s ${packages.backend} $out/backend/pkg
runHook postInstall
'';
};
default = packages.frontend;
}; };
devShell = pkgs.mkShell { devShell = pkgs.mkShell {
buildInputs = with pkgs; [ buildInputs = with pkgs; [
binaryen # wasm-opt binaryen # wasm-opt
rustToolchain rustToolchain
trunk
wasm-bindgen-cli wasm-bindgen-cli
wasm-pack
]; ];
}; };
apps = {
serve = {
type = "app";
program = toString (pkgs.writeShellScript "serve" ''
${pkgs.python3}/bin/python3 -m http.server
'');
};
};
}); });
} }

View file

@ -5,8 +5,7 @@
<meta name="viewport" content="width=device-width"> <meta name="viewport" content="width=device-width">
<meta name="description" content="A simple web-app to generate bcrypt and SHA512-crypt hashes"> <meta name="description" content="A simple web-app to generate bcrypt and SHA512-crypt hashes">
<title>Password Hash Self-Service</title> <title>Password Hash Self-Service</title>
<link rel="stylesheet" href="style.css"> <link rel="css" href="style.css" data-trunk>
<link rel="preload" href="backend/pkg/password_hash_self_service_backend.js" as="script" crossOrigin="anonymous">
</head> </head>
<body> <body>
<div class="container"> <div class="container">
@ -53,6 +52,7 @@
</footer> </footer>
</div> </div>
<script type="module" src="app.js"></script> <link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z"/>
<script src="app.js" data-trunk></script>
</body> </body>
</html> </html>