From 8a1dd3c9f0cf3fc7e284a3ae7ccf5069f7873d30 Mon Sep 17 00:00:00 2001 From: Joseph Stanton Date: Sun, 5 Apr 2026 00:24:42 -0400 Subject: [PATCH 1/2] fix: shim Node.js crypto module to fix mobile load error kdbxweb's pre-built UMD bundle unconditionally calls require("crypto") at load time. With crypto marked as external in esbuild, this left a bare require("crypto") in main.js that Obsidian mobile cannot resolve. Add a browser-compatible crypto shim (src/crypto-shim.ts) using globalThis.crypto.getRandomValues() for randomBytes; the Node.js hash/cipher fallbacks throw since kdbxweb never reaches them when Web Crypto is available. Filter crypto out of builtinModules externals and alias it to the shim so esbuild bundles it inline instead. Co-Authored-By: Claude Sonnet 4.6 --- esbuild.config.mjs | 5 ++++- src/crypto-shim.ts | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/crypto-shim.ts diff --git a/esbuild.config.mjs b/esbuild.config.mjs index 1c74a14..c3aeb28 100644 --- a/esbuild.config.mjs +++ b/esbuild.config.mjs @@ -31,7 +31,10 @@ const context = await esbuild.context({ "@lezer/common", "@lezer/highlight", "@lezer/lr", - ...builtinModules], + ...builtinModules.filter(m => m !== 'crypto')], + alias: { + 'crypto': './src/crypto-shim.ts', + }, format: "cjs", target: "es2018", logLevel: "info", diff --git a/src/crypto-shim.ts b/src/crypto-shim.ts new file mode 100644 index 0000000..8926051 --- /dev/null +++ b/src/crypto-shim.ts @@ -0,0 +1,21 @@ +// Shim for Node.js `crypto` module in browser/mobile environments. +// kdbxweb imports this module but uses Web Crypto API (globalThis.crypto.subtle) +// as its primary crypto engine — these Node.js-style exports are unreachable +// fallbacks that exist only to satisfy the static import at module load time. + +export function randomBytes(size: number): Uint8Array { + const buf = new Uint8Array(size); + globalThis.crypto.getRandomValues(buf); + return buf; +} + +const notSupported = (): never => { + throw new Error('Node.js crypto not available in this environment'); +}; + +export const createHash = notSupported; +export const createHmac = notSupported; +export const createCipheriv = notSupported; +export const createDecipheriv = notSupported; + +export default { randomBytes, createHash, createHmac, createCipheriv, createDecipheriv }; From 9f42c77b3608af2c93914fc76579006e915ad1d2 Mon Sep 17 00:00:00 2001 From: Joseph Stanton Date: Sun, 5 Apr 2026 00:35:24 -0400 Subject: [PATCH 2/2] fix: address CodeRabbit review feedback on crypto shim - Add 'node:crypto' alias alongside 'crypto' to cover the Node.js-prefixed import form used by some dependencies - Guard globalThis.crypto.getRandomValues availability in randomBytes and throw a descriptive error if Web Crypto API is absent Co-Authored-By: Claude Sonnet 4.6 --- esbuild.config.mjs | 1 + src/crypto-shim.ts | 3 +++ 2 files changed, 4 insertions(+) diff --git a/esbuild.config.mjs b/esbuild.config.mjs index c3aeb28..2da53d3 100644 --- a/esbuild.config.mjs +++ b/esbuild.config.mjs @@ -34,6 +34,7 @@ const context = await esbuild.context({ ...builtinModules.filter(m => m !== 'crypto')], alias: { 'crypto': './src/crypto-shim.ts', + 'node:crypto': './src/crypto-shim.ts', }, format: "cjs", target: "es2018", diff --git a/src/crypto-shim.ts b/src/crypto-shim.ts index 8926051..914b891 100644 --- a/src/crypto-shim.ts +++ b/src/crypto-shim.ts @@ -4,6 +4,9 @@ // fallbacks that exist only to satisfy the static import at module load time. export function randomBytes(size: number): Uint8Array { + if (typeof globalThis.crypto?.getRandomValues !== 'function') { + throw new Error('Web Crypto API (globalThis.crypto.getRandomValues) is not available in this environment'); + } const buf = new Uint8Array(size); globalThis.crypto.getRandomValues(buf); return buf;