File size: 6,174 Bytes
bf48b89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Shim for node:module in Cloudflare Workers
// Provides a createRequire that returns pre-imported modules

import * as assert from 'node:assert';
import * as async_hooks from 'node:async_hooks';
import * as buffer from 'node:buffer';
import * as console_module from 'node:console';
import * as constants from 'node:constants';
import * as crypto from 'node:crypto';
import * as diagnostics_channel from 'node:diagnostics_channel';
import * as dns from 'node:dns';
// For events, we need the default export (EventEmitter class) for CJS compatibility
// CJS require('events') returns EventEmitter class directly
import events, * as eventsNamespace from 'node:events';
// Pre-import Node.js builtins that CJS modules might require
import * as fs from 'node:fs';
import * as fs_promises from 'node:fs/promises';
import * as http from 'node:http';
import * as https from 'node:https';
import * as net from 'node:net';
import * as os from 'node:os';
import path from 'node:path';
import * as perf_hooks from 'node:perf_hooks';
import * as process from 'node:process';
import * as punycode from 'node:punycode';
import * as querystring from 'node:querystring';
import * as readline from 'node:readline';
import * as stream from 'node:stream';
import * as stream_promises from 'node:stream/promises';
import * as stream_web from 'node:stream/web';
import * as string_decoder from 'node:string_decoder';
import * as timers from 'node:timers';
import * as timers_promises from 'node:timers/promises';
import * as tls from 'node:tls';
import * as tty from 'node:tty';
import * as url from 'node:url';
// eslint-disable-next-line unicorn/import-style -- need full util module for CJS compatibility
import * as util from 'node:util';
import * as util_types from 'node:util/types';
import * as worker_threads from 'node:worker_threads';
import * as zlib from 'node:zlib';

// VM shim for Cloudflare Workers
// JSDOM and some other libraries require vm module
class ScriptShim {
    private code: string;
    constructor(code: string) {
        this.code = code;
    }
    runInContext() {
        throw new Error('vm.Script.runInContext is not supported in Workers');
    }
    runInNewContext() {
        throw new Error('vm.Script.runInNewContext is not supported in Workers');
    }
    runInThisContext() {
        throw new Error('vm.Script.runInThisContext is not supported in Workers');
    }
}

const vmShim = {
    createContext: (sandbox?: object) => sandbox || {},
    runInContext: () => {
        throw new Error('vm.runInContext is not supported in Workers');
    },
    runInNewContext: () => {
        throw new Error('vm.runInNewContext is not supported in Workers');
    },
    runInThisContext: () => {
        throw new Error('vm.runInThisContext is not supported in Workers');
    },
    Script: ScriptShim,
    isContext: () => false,
    compileFunction: () => {
        throw new Error('vm.compileFunction is not supported in Workers');
    },
};

// Child process shim (inline to avoid import cycle)
const child_process = {
    execSync: (_command: string): Buffer => Buffer.from(''),
    exec: () => {
        throw new Error('exec is not supported in Cloudflare Workers');
    },
    spawn: () => {
        throw new Error('spawn is not supported in Cloudflare Workers');
    },
    fork: () => {
        throw new Error('fork is not supported in Cloudflare Workers');
    },
    execFile: () => {
        throw new Error('execFile is not supported in Cloudflare Workers');
    },
    execFileSync: () => {
        throw new Error('execFileSync is not supported in Cloudflare Workers');
    },
    spawnSync: () => {
        throw new Error('spawnSync is not supported in Cloudflare Workers');
    },
};

// Create a CJS-compatible events module
// In CJS, require('events') returns EventEmitter class directly (the default export)
// but also has named exports attached to it
const eventsModule = Object.assign(events, eventsNamespace);

// Map of module names to their exports
const builtinModules: Record<string, unknown> = {
    fs,
    path,

    util,
    stream,
    events: eventsModule,
    buffer,
    crypto,
    http,
    https,
    url,
    querystring,
    zlib,
    os,
    assert,
    tty,
    net,
    dns,
    child_process,
    string_decoder,
    timers,
    process,
    perf_hooks,
    async_hooks,
    worker_threads,
    tls,
    readline,

    punycode,

    constants,
    diagnostics_channel,
    console: console_module,
    vm: vmShim,
    // Also support node: prefix
    'node:fs': fs,
    'node:path': path,
    'node:util': util,
    'node:stream': stream,
    'node:events': eventsModule,
    'node:buffer': buffer,
    'node:crypto': crypto,
    'node:http': http,
    'node:https': https,
    'node:url': url,
    'node:querystring': querystring,
    'node:zlib': zlib,
    'node:os': os,
    'node:assert': assert,
    'node:tty': tty,
    'node:net': net,
    'node:dns': dns,
    'node:child_process': child_process,
    'node:string_decoder': string_decoder,
    'node:timers': timers,
    'node:process': process,
    'node:perf_hooks': perf_hooks,
    'node:async_hooks': async_hooks,
    'node:worker_threads': worker_threads,
    'node:tls': tls,
    'node:readline': readline,
    'node:punycode': punycode,
    'node:constants': constants,
    'node:diagnostics_channel': diagnostics_channel,
    'node:console': console_module,
    'node:fs/promises': fs_promises,
    'fs/promises': fs_promises,
    'node:stream/promises': stream_promises,
    'stream/promises': stream_promises,
    'node:stream/web': stream_web,
    'stream/web': stream_web,
    'node:util/types': util_types,
    'util/types': util_types,
    'node:timers/promises': timers_promises,
    'timers/promises': timers_promises,
    'node:vm': vmShim,
};

export function createRequire(_filename: string | URL) {
    return function require(id: string): unknown {
        if (id in builtinModules) {
            return builtinModules[id];
        }
        // For non-builtin modules, throw an error
        throw new Error(`require() is not available in Workers. Attempted to require: ${id}`);
    };
}

export default {
    createRequire,
};