|
| 1 | +// Shim for node:module in Cloudflare Workers |
| 2 | +// Provides a createRequire that returns pre-imported modules |
| 3 | + |
| 4 | +import * as assert from 'node:assert'; |
| 5 | +import * as async_hooks from 'node:async_hooks'; |
| 6 | +import * as buffer from 'node:buffer'; |
| 7 | +import * as child_process from 'node:child_process'; |
| 8 | +import * as console_module from 'node:console'; |
| 9 | +import * as constants from 'node:constants'; |
| 10 | +import * as crypto from 'node:crypto'; |
| 11 | +import * as diagnostics_channel from 'node:diagnostics_channel'; |
| 12 | +import * as dns from 'node:dns'; |
| 13 | +// For events, we need the default export (EventEmitter class) for CJS compatibility |
| 14 | +// CJS require('events') returns EventEmitter class directly |
| 15 | +import events, * as eventsNamespace from 'node:events'; |
| 16 | +// Pre-import Node.js builtins that CJS modules might require |
| 17 | +import * as fs from 'node:fs'; |
| 18 | +import * as fs_promises from 'node:fs/promises'; |
| 19 | +import * as http from 'node:http'; |
| 20 | +import * as https from 'node:https'; |
| 21 | +import * as net from 'node:net'; |
| 22 | +import * as os from 'node:os'; |
| 23 | +import path from 'node:path'; |
| 24 | +import * as perf_hooks from 'node:perf_hooks'; |
| 25 | +import * as process from 'node:process'; |
| 26 | +import * as punycode from 'node:punycode'; |
| 27 | +import * as querystring from 'node:querystring'; |
| 28 | +import * as readline from 'node:readline'; |
| 29 | +import * as stream from 'node:stream'; |
| 30 | +import * as stream_promises from 'node:stream/promises'; |
| 31 | +import * as stream_web from 'node:stream/web'; |
| 32 | +import * as string_decoder from 'node:string_decoder'; |
| 33 | +import * as timers from 'node:timers'; |
| 34 | +import * as timers_promises from 'node:timers/promises'; |
| 35 | +import * as tls from 'node:tls'; |
| 36 | +import * as tty from 'node:tty'; |
| 37 | +import * as url from 'node:url'; |
| 38 | +// eslint-disable-next-line unicorn/import-style -- need full util module for CJS compatibility |
| 39 | +import * as util from 'node:util'; |
| 40 | +import * as util_types from 'node:util/types'; |
| 41 | +import * as worker_threads from 'node:worker_threads'; |
| 42 | +import * as zlib from 'node:zlib'; |
| 43 | + |
| 44 | +// VM shim for Cloudflare Workers |
| 45 | +// JSDOM and some other libraries require vm module |
| 46 | +class ScriptShim { |
| 47 | + private code: string; |
| 48 | + constructor(code: string) { |
| 49 | + this.code = code; |
| 50 | + } |
| 51 | + runInContext() { |
| 52 | + throw new Error('vm.Script.runInContext is not supported in Workers'); |
| 53 | + } |
| 54 | + runInNewContext() { |
| 55 | + throw new Error('vm.Script.runInNewContext is not supported in Workers'); |
| 56 | + } |
| 57 | + runInThisContext() { |
| 58 | + throw new Error('vm.Script.runInThisContext is not supported in Workers'); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +const vmShim = { |
| 63 | + createContext: (sandbox?: object) => sandbox || {}, |
| 64 | + runInContext: () => { |
| 65 | + throw new Error('vm.runInContext is not supported in Workers'); |
| 66 | + }, |
| 67 | + runInNewContext: () => { |
| 68 | + throw new Error('vm.runInNewContext is not supported in Workers'); |
| 69 | + }, |
| 70 | + runInThisContext: () => { |
| 71 | + throw new Error('vm.runInThisContext is not supported in Workers'); |
| 72 | + }, |
| 73 | + Script: ScriptShim, |
| 74 | + isContext: () => false, |
| 75 | + compileFunction: () => { |
| 76 | + throw new Error('vm.compileFunction is not supported in Workers'); |
| 77 | + }, |
| 78 | +}; |
| 79 | + |
| 80 | +// Create a CJS-compatible events module |
| 81 | +// In CJS, require('events') returns EventEmitter class directly (the default export) |
| 82 | +// but also has named exports attached to it |
| 83 | +const eventsModule = Object.assign(events, eventsNamespace); |
| 84 | + |
| 85 | +// Map of module names to their exports |
| 86 | +const builtinModules: Record<string, unknown> = { |
| 87 | + fs, |
| 88 | + path, |
| 89 | + |
| 90 | + util, |
| 91 | + stream, |
| 92 | + events: eventsModule, |
| 93 | + buffer, |
| 94 | + crypto, |
| 95 | + http, |
| 96 | + https, |
| 97 | + url, |
| 98 | + querystring, |
| 99 | + zlib, |
| 100 | + os, |
| 101 | + assert, |
| 102 | + tty, |
| 103 | + net, |
| 104 | + dns, |
| 105 | + child_process, |
| 106 | + string_decoder, |
| 107 | + timers, |
| 108 | + process, |
| 109 | + perf_hooks, |
| 110 | + async_hooks, |
| 111 | + worker_threads, |
| 112 | + tls, |
| 113 | + readline, |
| 114 | + |
| 115 | + punycode, |
| 116 | + |
| 117 | + constants, |
| 118 | + diagnostics_channel, |
| 119 | + console: console_module, |
| 120 | + vm: vmShim, |
| 121 | + // Also support node: prefix |
| 122 | + 'node:fs': fs, |
| 123 | + 'node:path': path, |
| 124 | + 'node:util': util, |
| 125 | + 'node:stream': stream, |
| 126 | + 'node:events': eventsModule, |
| 127 | + 'node:buffer': buffer, |
| 128 | + 'node:crypto': crypto, |
| 129 | + 'node:http': http, |
| 130 | + 'node:https': https, |
| 131 | + 'node:url': url, |
| 132 | + 'node:querystring': querystring, |
| 133 | + 'node:zlib': zlib, |
| 134 | + 'node:os': os, |
| 135 | + 'node:assert': assert, |
| 136 | + 'node:tty': tty, |
| 137 | + 'node:net': net, |
| 138 | + 'node:dns': dns, |
| 139 | + 'node:child_process': child_process, |
| 140 | + 'node:string_decoder': string_decoder, |
| 141 | + 'node:timers': timers, |
| 142 | + 'node:process': process, |
| 143 | + 'node:perf_hooks': perf_hooks, |
| 144 | + 'node:async_hooks': async_hooks, |
| 145 | + 'node:worker_threads': worker_threads, |
| 146 | + 'node:tls': tls, |
| 147 | + 'node:readline': readline, |
| 148 | + 'node:punycode': punycode, |
| 149 | + 'node:constants': constants, |
| 150 | + 'node:diagnostics_channel': diagnostics_channel, |
| 151 | + 'node:console': console_module, |
| 152 | + 'node:fs/promises': fs_promises, |
| 153 | + 'fs/promises': fs_promises, |
| 154 | + 'node:stream/promises': stream_promises, |
| 155 | + 'stream/promises': stream_promises, |
| 156 | + 'node:stream/web': stream_web, |
| 157 | + 'stream/web': stream_web, |
| 158 | + 'node:util/types': util_types, |
| 159 | + 'util/types': util_types, |
| 160 | + 'node:timers/promises': timers_promises, |
| 161 | + 'timers/promises': timers_promises, |
| 162 | + 'node:vm': vmShim, |
| 163 | +}; |
| 164 | + |
| 165 | +export function createRequire(_filename: string | URL) { |
| 166 | + return function require(id: string): unknown { |
| 167 | + if (id in builtinModules) { |
| 168 | + return builtinModules[id]; |
| 169 | + } |
| 170 | + // For non-builtin modules, throw an error |
| 171 | + throw new Error(`require() is not available in Workers. Attempted to require: ${id}`); |
| 172 | + }; |
| 173 | +} |
| 174 | + |
| 175 | +export default { |
| 176 | + createRequire, |
| 177 | +}; |
0 commit comments