Skip to content

Commit

Permalink
Import crypto for node environment
Browse files Browse the repository at this point in the history
  • Loading branch information
sameerank committed Jan 9, 2025
1 parent 52d06fb commit 7e65d42
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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,
Expand Down Expand Up @@ -125,4 +127,8 @@ export {
IConfigurationWire,
IPrecomputedConfigurationResponse,
PrecomputedFlag,

// Test helpers
setSaltOverrideForTests,
decodePrecomputedFlag,
};
22 changes: 19 additions & 3 deletions src/obfuscation.ts
Original file line number Diff line number Diff line change
@@ -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();
}
Expand Down Expand Up @@ -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));
}
3 changes: 3 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 7e65d42

Please sign in to comment.