Skip to content

Commit

Permalink
jsr を使うために esbuild を使う, secp256k1 で自動パスワード
Browse files Browse the repository at this point in the history
  • Loading branch information
narumincho committed Jul 7, 2024
1 parent c0d9fbe commit bd2f2b6
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 36 deletions.
71 changes: 71 additions & 0 deletions client/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
Dispatch,
StateUpdater,
} from "https://esm.sh/[email protected]/hooks?pin=v135";
import { h } from "https://esm.sh/[email protected]?pin=v135";
import { encodeBase64Url } from "jsr:@std/encoding/base64url";

export const App = (props: {
readonly state: number;
readonly privateKey: Uint8Array | null;
readonly setState: Dispatch<StateUpdater<number>>;
readonly signUp: () => void;
readonly copyPassword: () => void;
}) => {
return (
<div
style={{
display: "grid",
gap: 16,
}}
>
<h1>definy</h1>
<button
type="button"
onClick={() => {
props.setState((prev) => prev + 1);
}}
>
count: {props.state}
</button>
<button type="button" onClick={props.signUp}>
Sign Up
</button>

{props.privateKey === null ? <div></div> : (
<dialog
open
style={{
width: "80%",
boxShadow: "0px 20px 36px 0px rgba(0, 0, 0, 0.6)",
}}
>
<form
style={{
display: "grid",
}}
>
<label>
Username
<input type="text" required />
</label>
<label>
Password (auto created. If you lose this password, you will not be
able to log in.)
<input
value={encodeBase64Url(props.privateKey)}
readOnly={true}
type="password"
autoComplete="new-password"
/>
<button type="button" onClick={props.copyPassword}>
copy password
</button>
</label>
<button type="submit">sign up!</button>
</form>
</dialog>
)}
</div>
);
};
23 changes: 0 additions & 23 deletions client/main.tsx

This file was deleted.

23 changes: 21 additions & 2 deletions client/start.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { App } from "./main.tsx";
import { App } from "./app.tsx";
import { h, hydrate, JSX } from "https://esm.sh/[email protected]?pin=v135";
import { useState } from "https://esm.sh/[email protected]/hooks?pin=v135";
import { utils } from "jsr:@noble/secp256k1";
import { encodeBase64Url } from "jsr:@std/encoding/base64url";

const root = document.getElementById("root");
if (root === null) {
Expand All @@ -9,7 +11,24 @@ if (root === null) {

const AppWithState = (): JSX.Element => {
const [state, setState] = useState(0);
return <App state={state} setState={setState} />;
const [privateKey, setPrivateKey] = useState<Uint8Array | null>(null);

return (
<App
state={state}
privateKey={privateKey}
setState={setState}
signUp={() => {
setPrivateKey(utils.randomPrivateKey());
}}
copyPassword={() => {
if (privateKey === null) {
return;
}
navigator.clipboard.writeText(encodeBase64Url(privateKey));
}}
/>
);
};

hydrate(<AppWithState />, root);
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
},
"tasks": {
"server-dev": "deno run --check --watch --allow-env --allow-net --allow-read --allow-write=./dist.json ./script/startInLocal.ts",
"client-build": "deno run --check --allow-env --allow-net --allow-read --allow-write=./dist.json ./script/buildClient.ts"
"client-build": "deno run --check --allow-run --allow-env --allow-net --allow-read --allow-write=./dist.json --watch=client ./script/buildClient.ts"
}
}
162 changes: 162 additions & 0 deletions deno.lock

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

2 changes: 1 addition & 1 deletion dist.json

Large diffs are not rendered by default.

20 changes: 13 additions & 7 deletions script/buildClient.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { bundle } from "jsr:@deno/emit";
import { encodeHex } from "jsr:@std/encoding/hex";
import * as esbuild from "npm:esbuild";
import { denoPlugins } from "jsr:@luca/esbuild-deno-loader";

const { code } = await bundle(
new URL("../client/start.tsx", import.meta.url),
{
compilerOptions: { jsxFactory: "h" },
},
);
const buildResult = await esbuild.build({
entryPoints: ["./client/start.tsx"],
bundle: true,
plugins: [...denoPlugins()],
write: false,
jsxFactory: "h",
});

const code = buildResult.outputFiles[0]?.text ?? "";

await Deno.writeTextFile(
new URL("../dist.json", import.meta.url),
Expand All @@ -20,3 +24,5 @@ await Deno.writeTextFile(
),
}),
);

await esbuild.stop();
Loading

0 comments on commit bd2f2b6

Please sign in to comment.