22// Secure polyfill initialization - minimal global modifications
33import { Buffer } from 'buffer' ;
44import { devLog , logWarn , logError } from '../utils/logger' ;
5+ // Import the os polyfill
6+ import osPolyfill from './os-browser.js' ;
57
68// Safe initialization function that doesn't directly modify globals
79function initializeBufferPolyfills ( ) {
@@ -62,14 +64,65 @@ function initializeCryptoPolyfills() {
6264 }
6365}
6466
67+ // Initialize OS module polyfill for dependencies like Anchor
68+ function initializeOsPolyfill ( ) {
69+ const globalScope = ( function ( ) {
70+ if ( typeof window !== 'undefined' ) return window ;
71+ if ( typeof global !== 'undefined' ) return global ;
72+ return { } ;
73+ } ) ( ) ;
74+
75+ if ( globalScope && typeof globalScope === 'object' ) {
76+ // Create or enhance the require function to handle os module
77+ if ( typeof globalScope . require === 'undefined' ) {
78+ globalScope . require = function ( moduleName ) {
79+ if ( moduleName === 'os' ) {
80+ return osPolyfill ;
81+ }
82+ throw new Error ( `Module ${ moduleName } not found` ) ;
83+ } ;
84+ } else {
85+ const originalRequire = globalScope . require ;
86+ globalScope . require = function ( moduleName ) {
87+ if ( moduleName === 'os' ) {
88+ return osPolyfill ;
89+ }
90+ try {
91+ return originalRequire ( moduleName ) ;
92+ } catch ( error ) {
93+ if ( moduleName === 'os' ) {
94+ return osPolyfill ;
95+ }
96+ throw error ;
97+ }
98+ } ;
99+ }
100+
101+ // Also ensure os is available as a global module if needed
102+ if ( ! globalScope . os ) {
103+ Object . defineProperty ( globalScope , 'os' , {
104+ value : osPolyfill ,
105+ writable : false ,
106+ configurable : false
107+ } ) ;
108+ }
109+ }
110+ }
111+
65112// Secure error handler for crypto-related errors
66113function setupSecureErrorHandling ( ) {
67114 // Only in browser environment
68115 if ( typeof window !== 'undefined' ) {
69116 const originalOnError = window . onerror ;
70117 const secureErrorHandler = function ( message , source , lineno , colno , error ) {
71- if ( typeof message === 'string' && message . includes ( "Cannot read properties of undefined (reading 'buffer')" ) ) {
72- logError ( 'Buffer access error intercepted:' , message ) ;
118+ // Handle homedir errors specifically
119+ if ( typeof message === 'string' && (
120+ message . includes ( "c.homedir is not a function" ) ||
121+ message . includes ( "homedir is not a function" ) ||
122+ message . includes ( "Cannot read properties of undefined (reading 'homedir')" ) ||
123+ message . includes ( "Cannot read properties of undefined (reading 'buffer')" )
124+ ) ) {
125+ logError ( 'OS/Buffer access error intercepted:' , message ) ;
73126
74127 // Show user-friendly error instead of crashing
75128 if ( document . body && ! document . body . querySelector ( '.crypto-error-message' ) ) {
@@ -88,14 +141,14 @@ function setupSecureErrorHandling() {
88141 font-size: 14px;
89142 max-width: 300px;
90143 ` ;
91- errorDiv . textContent = 'Crypto library compatibility issue detected. Please refresh the page .' ;
144+ errorDiv . textContent = 'Compatibility issue detected. Attempting to recover.. .' ;
92145 document . body . appendChild ( errorDiv ) ;
93146
94147 setTimeout ( ( ) => {
95148 if ( errorDiv . parentNode ) {
96149 errorDiv . parentNode . removeChild ( errorDiv ) ;
97150 }
98- } , 5000 ) ;
151+ } , 3000 ) ;
99152 }
100153
101154 return true ; // Prevent default error handling
@@ -122,8 +175,9 @@ function initializeAll() {
122175 try {
123176 initializeBufferPolyfills ( ) ;
124177 initializeCryptoPolyfills ( ) ;
178+ initializeOsPolyfill ( ) ;
125179 setupSecureErrorHandling ( ) ;
126- devLog ( 'Secure buffer and crypto polyfills initialized' ) ;
180+ devLog ( 'Secure buffer, crypto, and OS polyfills initialized' ) ;
127181 } catch ( error ) {
128182 logError ( 'Failed to initialize polyfills:' , error ) ;
129183 }
0 commit comments