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.
This commit is contained in:
2025-11-07 11:29:51 -05:00
parent e18321daea
commit ceeefe1eeb
2 changed files with 15 additions and 4 deletions

View File

@@ -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
}
}

View File

@@ -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');