fix: use globalThis.crypto instead of require('crypto')
This commit is contained in:
@@ -15,19 +15,9 @@ function getCrypto(): Crypto {
|
|||||||
return window.crypto;
|
return window.crypto;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Node.js environment (15+) - uses Web Crypto API standard
|
// Node.js/Electron environment - globalThis.crypto available in modern runtimes
|
||||||
if (typeof global !== 'undefined') {
|
if (typeof globalThis !== 'undefined' && globalThis.crypto) {
|
||||||
try {
|
return globalThis.crypto;
|
||||||
// Using require() is necessary for synchronous crypto access in Obsidian desktop plugins
|
|
||||||
// ES6 dynamic imports would create race conditions as crypto must be available synchronously
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires -- Synchronous Node.js crypto API access required
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error('No Web Crypto API available in this environment');
|
throw new Error('No Web Crypto API available in this environment');
|
||||||
|
|||||||
@@ -138,11 +138,18 @@ describe('crypto-adapter', () => {
|
|||||||
const globalRef = global as any;
|
const globalRef = global as any;
|
||||||
const originalWindow = globalRef.window;
|
const originalWindow = globalRef.window;
|
||||||
const originalGlobal = globalRef.global;
|
const originalGlobal = globalRef.global;
|
||||||
|
const originalGlobalThisCrypto = globalThis.crypto;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Remove window.crypto and global access
|
// Remove window.crypto, global access, and globalThis.crypto
|
||||||
delete globalRef.window;
|
delete globalRef.window;
|
||||||
delete globalRef.global;
|
delete globalRef.global;
|
||||||
|
// In modern Node.js, globalThis.crypto is always available, so we must mock it too
|
||||||
|
Object.defineProperty(globalThis, 'crypto', {
|
||||||
|
value: undefined,
|
||||||
|
writable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
// Clear module cache to force re-evaluation
|
// Clear module cache to force re-evaluation
|
||||||
jest.resetModules();
|
jest.resetModules();
|
||||||
@@ -157,6 +164,12 @@ describe('crypto-adapter', () => {
|
|||||||
// Restore original values
|
// Restore original values
|
||||||
globalRef.window = originalWindow;
|
globalRef.window = originalWindow;
|
||||||
globalRef.global = originalGlobal;
|
globalRef.global = originalGlobal;
|
||||||
|
// Restore globalThis.crypto
|
||||||
|
Object.defineProperty(globalThis, 'crypto', {
|
||||||
|
value: originalGlobalThisCrypto,
|
||||||
|
writable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
// Clear module cache again to restore normal state
|
// Clear module cache again to restore normal state
|
||||||
jest.resetModules();
|
jest.resetModules();
|
||||||
|
|||||||
Reference in New Issue
Block a user