Skip to content

Commit e470a0b

Browse files
committed
Update build-worker.js
1 parent f821643 commit e470a0b

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

scripts/build-worker.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,122 @@ async function buildWorker() {
3232
external: [
3333
// Cloudflare runtime APIs are provided by the runtime
3434
'@cloudflare/workers-types'
35+
],
36+
plugins: [
37+
{
38+
name: 'node-builtins-polyfill',
39+
setup(build) {
40+
// Handle Node.js built-in modules that Stripe tries to import
41+
const nodeBuiltins = ['crypto', 'events', 'child_process', 'http', 'https']
42+
43+
nodeBuiltins.forEach(module => {
44+
build.onResolve({ filter: new RegExp(`^${module}$`) }, () => {
45+
return { path: module, namespace: 'node-builtin-polyfill' }
46+
})
47+
})
48+
49+
// Provide polyfills for Node.js built-ins
50+
build.onLoad({ filter: /.*/, namespace: 'node-builtin-polyfill' }, (args) => {
51+
const moduleName = args.path
52+
53+
if (moduleName === 'crypto') {
54+
// Use Web Crypto API available in Workers
55+
return {
56+
contents: `
57+
// Crypto polyfill using Web Crypto API
58+
const cryptoImpl = globalThis.crypto || {};
59+
export const randomBytes = (size) => {
60+
const arr = new Uint8Array(size);
61+
cryptoImpl.getRandomValues(arr);
62+
return arr;
63+
};
64+
export const createHash = (algorithm) => ({
65+
update: () => {},
66+
digest: (encoding) => ''
67+
});
68+
export default cryptoImpl;
69+
`,
70+
loader: 'js'
71+
}
72+
}
73+
74+
if (moduleName === 'events') {
75+
return {
76+
contents: `
77+
// Events polyfill - minimal EventEmitter stub
78+
export class EventEmitter {
79+
constructor() {}
80+
on() { return this; }
81+
emit() { return false; }
82+
once() { return this; }
83+
removeListener() { return this; }
84+
}
85+
export default EventEmitter;
86+
`,
87+
loader: 'js'
88+
}
89+
}
90+
91+
if (moduleName === 'child_process') {
92+
return {
93+
contents: `
94+
// child_process stub - not used in Workers
95+
export const exec = () => Promise.resolve({ stdout: '', stderr: '' });
96+
export default {};
97+
`,
98+
loader: 'js'
99+
}
100+
}
101+
102+
if (moduleName === 'http' || moduleName === 'https') {
103+
return {
104+
contents: `
105+
// HTTP/HTTPS stubs - Workers use fetch API instead
106+
export default {};
107+
export const request = () => {};
108+
export const get = () => {};
109+
`,
110+
loader: 'js'
111+
}
112+
}
113+
114+
return {
115+
contents: `export default {};`,
116+
loader: 'js'
117+
}
118+
})
119+
}
120+
},
121+
{
122+
name: 'qs-polyfill',
123+
setup(build) {
124+
// Handle qs package - provide a minimal implementation
125+
build.onResolve({ filter: /^qs$/ }, () => {
126+
return { path: 'qs', namespace: 'qs-polyfill' }
127+
})
128+
129+
build.onLoad({ filter: /.*/, namespace: 'qs-polyfill' }, () => {
130+
return {
131+
contents: `
132+
// Minimal qs polyfill for Workers
133+
export const stringify = (obj) => {
134+
return new URLSearchParams(obj).toString();
135+
};
136+
export const parse = (str) => {
137+
const params = new URLSearchParams(str);
138+
const result = {};
139+
for (const [key, value] of params) {
140+
result[key] = value;
141+
}
142+
return result;
143+
};
144+
export default { stringify, parse };
145+
`,
146+
loader: 'js'
147+
}
148+
})
149+
}
150+
}
35151
]
36152
})
37153

0 commit comments

Comments
 (0)