Skip to content

Commit 6ac3760

Browse files
Copilot0xrinegade
andcommitted
Fix c.homedir runtime error with browser-compatible os polyfill
Co-authored-by: 0xrinegade <[email protected]>
1 parent 39dce81 commit 6ac3760

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

craco.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ module.exports = {
7676
assert: require.resolve('assert'),
7777
util: require.resolve('util'),
7878
url: require.resolve('url'),
79-
os: require.resolve('os'),
79+
os: require.resolve('./src/polyfills/os-browser.js'),
8080
path: require.resolve('path-browserify'),
8181
vm: require.resolve('vm-browserify'),
8282
fs: false,

src/polyfills/os-browser.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Browser-compatible polyfill for Node.js 'os' module
3+
* Provides stub implementations that don't crash in browser environment
4+
*/
5+
6+
// Browser-safe implementations
7+
const EOL = '\n';
8+
9+
function arch() {
10+
return 'browser';
11+
}
12+
13+
function platform() {
14+
if (typeof navigator !== 'undefined') {
15+
const userAgent = navigator.userAgent.toLowerCase();
16+
if (userAgent.includes('mac')) return 'darwin';
17+
if (userAgent.includes('win')) return 'win32';
18+
if (userAgent.includes('linux')) return 'linux';
19+
}
20+
return 'browser';
21+
}
22+
23+
function release() {
24+
return '1.0.0';
25+
}
26+
27+
function hostname() {
28+
if (typeof window !== 'undefined' && window.location) {
29+
return window.location.hostname || 'localhost';
30+
}
31+
return 'localhost';
32+
}
33+
34+
function homedir() {
35+
// In browser, return a safe default instead of throwing
36+
return '/home/user';
37+
}
38+
39+
function tmpdir() {
40+
return '/tmp';
41+
}
42+
43+
function userInfo() {
44+
return {
45+
uid: 1000,
46+
gid: 1000,
47+
username: 'user',
48+
homedir: homedir(),
49+
shell: '/bin/bash'
50+
};
51+
}
52+
53+
function cpus() {
54+
// Return stub CPU info
55+
const numCores = typeof navigator !== 'undefined' && navigator.hardwareConcurrency
56+
? navigator.hardwareConcurrency
57+
: 4;
58+
59+
return Array(numCores).fill({
60+
model: 'Browser CPU',
61+
speed: 2400,
62+
times: {
63+
user: 0,
64+
nice: 0,
65+
sys: 0,
66+
idle: 0,
67+
irq: 0
68+
}
69+
});
70+
}
71+
72+
function freemem() {
73+
// Return estimated free memory (256MB)
74+
return 256 * 1024 * 1024;
75+
}
76+
77+
function totalmem() {
78+
// Return estimated total memory (2GB)
79+
return 2 * 1024 * 1024 * 1024;
80+
}
81+
82+
function loadavg() {
83+
return [0.1, 0.1, 0.1];
84+
}
85+
86+
function uptime() {
87+
return Math.floor(Date.now() / 1000);
88+
}
89+
90+
function networkInterfaces() {
91+
return {};
92+
}
93+
94+
function type() {
95+
return platform();
96+
}
97+
98+
// Export all the functions
99+
module.exports = {
100+
EOL,
101+
arch,
102+
platform,
103+
release,
104+
hostname,
105+
homedir,
106+
tmpdir,
107+
userInfo,
108+
cpus,
109+
freemem,
110+
totalmem,
111+
loadavg,
112+
uptime,
113+
networkInterfaces,
114+
type
115+
};
116+
117+
// Also provide default export for ES6 imports
118+
module.exports.default = module.exports;

0 commit comments

Comments
 (0)