summaryrefslogtreecommitdiffhomepage
path: root/src/app/uuid.ts
diff options
context:
space:
mode:
authorAdam Malczewski <[email protected]>2026-06-07 00:54:08 +0900
committerAdam Malczewski <[email protected]>2026-06-07 00:54:08 +0900
commit7889918d23ffa428cf266e52d42b9683f16160fa (patch)
tree7f403136663562dd278b6c680449ec29af612d06 /src/app/uuid.ts
parent8015ed30a9904a89efe083067f505eb11fa46034 (diff)
downloaddispatch-web-7889918d23ffa428cf266e52d42b9683f16160fa.tar.gz
dispatch-web-7889918d23ffa428cf266e52d42b9683f16160fa.zip
fix: blank page on non-localhost HTTP (secure-context crypto.randomUUID)
crypto.randomUUID() is secure-context-only — undefined on plain-HTTP non-localhost origins (e.g. http://arch-razer:24204), so createAppStore threw during mount and nothing rendered. Add src/app/uuid.ts randomId(): prefer crypto.randomUUID when present, else build a v4 from crypto.getRandomValues (available in insecure contexts), else Math.random fallback. Use it for the conversation id. Verified: svelte-check 0/0, vitest 221, build ok.
Diffstat (limited to 'src/app/uuid.ts')
-rw-r--r--src/app/uuid.ts65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/app/uuid.ts b/src/app/uuid.ts
new file mode 100644
index 0000000..ae39d4d
--- /dev/null
+++ b/src/app/uuid.ts
@@ -0,0 +1,65 @@
+const HEX = "0123456789abcdef";
+
+function hexChar(n: number): string {
+ return HEX.charAt(n & 0xf);
+}
+
+function hexFromBytes(bytes: Uint8Array): string {
+ let out = "";
+ for (let i = 0; i < bytes.length; i++) {
+ const b = bytes[i] as number;
+ out += hexChar(b >> 4);
+ out += hexChar(b);
+ }
+ return out;
+}
+
+function formatV4(rand: Uint8Array): string {
+ const h = hexFromBytes(rand);
+ return (
+ h.slice(0, 8) +
+ "-" +
+ h.slice(8, 12) +
+ "-4" +
+ h.slice(13, 16) +
+ "-" +
+ ((parseInt(h.slice(16, 18), 16) & 0x3f) | 0x80).toString(16).padStart(2, "0") +
+ h.slice(18, 20) +
+ "-" +
+ h.slice(20, 32)
+ );
+}
+
+function uuidFromGetRandomValues(): string {
+ const buf = new Uint8Array(16);
+ crypto.getRandomValues(buf);
+ buf[6] = ((buf[6] as number) & 0x0f) | 0x40;
+ buf[8] = ((buf[8] as number) & 0x3f) | 0x80;
+ return formatV4(buf);
+}
+
+function uuidFromMathRandom(): string {
+ let s = "";
+ for (let i = 0; i < 36; i++) {
+ if (i === 8 || i === 13 || i === 18 || i === 23) {
+ s += "-";
+ } else if (i === 14) {
+ s += "4";
+ } else if (i === 19) {
+ s += hexChar(Math.floor(Math.random() * 4) + 8);
+ } else {
+ s += hexChar(Math.floor(Math.random() * 16));
+ }
+ }
+ return s;
+}
+
+export function randomId(): string {
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
+ return crypto.randomUUID();
+ }
+ if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
+ return uuidFromGetRandomValues();
+ }
+ return uuidFromMathRandom();
+}