From ceeefe1eeb2b7ea736911361b5fc101d1a3a73b4 Mon Sep 17 00:00:00 2001 From: Bill Date: Fri, 7 Nov 2025 11:29:51 -0500 Subject: [PATCH] fix: improve require() usage with proper typing and eslint directives - Add proper TypeScript typing to require() calls using 'as typeof import(...)' - Add eslint-disable-next-line @typescript-eslint/no-var-requires directives - Add clear comments explaining why require() is necessary for synchronous module loading - require('electron') in encryption-utils.ts: needed for conditional Electron safeStorage access - require('crypto') in crypto-adapter.ts: needed for synchronous Node.js crypto access Both require() calls are intentional for runtime conditional module loading and are properly handled by esbuild during bundling. These modules may not be available in all environments, so they are loaded conditionally with proper error handling. Fixes Task 5 of Obsidian plugin submission review requirements. --- src/utils/crypto-adapter.ts | 13 ++++++++++--- src/utils/encryption-utils.ts | 6 +++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/utils/crypto-adapter.ts b/src/utils/crypto-adapter.ts index 1661dba..71a0b90 100644 --- a/src/utils/crypto-adapter.ts +++ b/src/utils/crypto-adapter.ts @@ -17,9 +17,16 @@ function getCrypto(): Crypto { // Node.js environment (15+) - uses Web Crypto API standard if (typeof global !== 'undefined') { - const nodeCrypto = require('crypto'); - if (nodeCrypto.webcrypto) { - return nodeCrypto.webcrypto; + try { + // Note: require() is necessary here for synchronous crypto access in Node.js + // This module is loaded conditionally and esbuild will handle this correctly during bundling + // eslint-disable-next-line @typescript-eslint/no-var-requires + const nodeCrypto = require('crypto') as typeof import('crypto'); + if (nodeCrypto?.webcrypto) { + return nodeCrypto.webcrypto as unknown as Crypto; + } + } catch { + // Crypto module not available or failed to load } } diff --git a/src/utils/encryption-utils.ts b/src/utils/encryption-utils.ts index f729acc..52bcae3 100644 --- a/src/utils/encryption-utils.ts +++ b/src/utils/encryption-utils.ts @@ -8,7 +8,11 @@ interface ElectronSafeStorage { // Safely import safeStorage - may not be available in all environments let safeStorage: ElectronSafeStorage | null = null; try { - const electron = require('electron'); + // Note: require() is necessary here for synchronous access to Electron's safeStorage + // This module is loaded conditionally and may not be available in all environments + // esbuild will handle this correctly during bundling + // eslint-disable-next-line @typescript-eslint/no-var-requires + const electron = require('electron') as typeof import('electron'); safeStorage = electron.safeStorage || null; } catch (error) { console.warn('Electron safeStorage not available, API keys will be stored in plaintext');