diff --git a/src/index.ts b/src/index.ts index 4cb0ca8..4f76142 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,6 +34,7 @@ import { import { HybridConfigurationStore } from './configuration-store/hybrid.store'; import { MemoryStore, MemoryOnlyConfigurationStore } from './configuration-store/memory.store'; import * as constants from './constants'; +import { decodePrecomputedFlag } from './decoding'; import BatchEventProcessor from './events/batch-event-processor'; import { BoundedEventQueue } from './events/bounded-event-queue'; import DefaultEventDispatcher, { @@ -47,6 +48,7 @@ import NamedEventQueue from './events/named-event-queue'; import NetworkStatusListener from './events/network-status-listener'; import HttpClient from './http-client'; import { PrecomputedFlag, Flag, ObfuscatedFlag, VariationType, FormatEnum } from './interfaces'; +import { setSaltOverrideForTests } from './obfuscation'; import { AttributeType, Attributes, @@ -125,4 +127,8 @@ export { IConfigurationWire, IPrecomputedConfigurationResponse, PrecomputedFlag, + + // Test helpers + setSaltOverrideForTests, + decodePrecomputedFlag, }; diff --git a/src/obfuscation.ts b/src/obfuscation.ts index 496bb9a..02e488b 100644 --- a/src/obfuscation.ts +++ b/src/obfuscation.ts @@ -1,8 +1,26 @@ import base64 = require('js-base64'); import * as SparkMD5 from 'spark-md5'; +import { logger } from './application-logger'; import { PrecomputedFlag } from './interfaces'; +// Import randomBytes according to the environment +let getRandomValues: (length: number) => Uint8Array; +if (typeof window !== 'undefined' && window.crypto) { + // Browser environment + getRandomValues = (length: number) => window.crypto.getRandomValues(new Uint8Array(length)); +} else { + // Node.js environment + import('crypto') + .then((crypto) => { + getRandomValues = (length: number) => new Uint8Array(crypto.randomBytes(length)); + return; + }) + .catch((error) => { + logger.error('Failed to load crypto module:', error); + }); +} + export function getMD5Hash(input: string, salt = ''): string { return new SparkMD5().append(salt).append(input).end(); } @@ -49,7 +67,5 @@ export function setSaltOverrideForTests(salt: Uint8Array | null) { } export function generateSalt(length = 16): string { - return base64.fromUint8Array( - saltOverrideBytes ? saltOverrideBytes : crypto.getRandomValues(new Uint8Array(length)), - ); + return base64.fromUint8Array(saltOverrideBytes ? saltOverrideBytes : getRandomValues(length)); } diff --git a/webpack.config.js b/webpack.config.js index 8cec840..919d9c5 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -19,6 +19,9 @@ module.exports = { }, resolve: { extensions: ['.tsx', '.ts', '.js'], + fallback: { + crypto: false, // Exclude crypto module in the browser bundle + }, }, output: { filename: 'eppo-sdk.js',