Files
RSSHub/lib/shims/node-module.ts
T
dependabot[bot] 873618e99d chore(deps-dev): bump eslint-plugin-unicorn from 65.0.1 to 66.0.0 (#22274)
* chore(deps-dev): bump eslint-plugin-unicorn from 65.0.1 to 66.0.0

Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 65.0.1 to 66.0.0.
- [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases)
- [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v65.0.1...v66.0.0)

---
updated-dependencies:
- dependency-name: eslint-plugin-unicorn
  dependency-version: 66.0.0
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* style: fix unicorn-js/prefer-url-href

* style: oxlint disable

* style: fix unicorn-js/no-useless-else

* style: no-unsafe-string-replacement

* style: fix no-duplicate-loops

* style: fix prefer-number-is-safe-integer

* style: fix prefer-single-array-predicate

* style: fix no-object-methods-with-collections

* style: fix no-useless-boolean-cast

* style: fix no-useless-template-literals

* style: fix no-negated-array-predicate

* style: fix prefer-direct-iteration

* style: fix consistent-optional-chaining

* style: fix prefer-simple-sort-comparator

* style: fix prefer-global-number-constants

* style: fix prefer-type-literal-last

* style: fix require-array-sort-compare.

* style: fix prefer-early-return

* style: fix prefer-short-arrow-method

* style: fix prefer-array-from-map

* style: fix prefer-scoped-selector

* style: fix no-global-object-property-assignment

* style: fix no-unnecessary-global-this

* style: fix no-unreadable-object-destructuring

* style: fix no-invalid-argument-count

* style: fix prefer-smaller-scope

* style: fix prefer-iterator-to-array.

* style: fix prefer-object-iterable-methods

* style: fix typo

* style: fix max-nested-calls

* style: fix no-incorrect-template-string-interpolation

* style: fix no-break-in-nested-loop

* style: fix prefer-await

* style: fix no-computed-property-existence-check

* style: fix prefer-number-coercion

* style: fix no-empty-file

* style: update eslint unicorn v66 recommended preset

* style: add unicorn/prefer-spread to oxlint-disable comments

* style: fix no-array-reduce

* style: delete github/no-then as it's covered by unicorn/prefer-await

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tony <TonyRL@users.noreply.github.com>
2026-06-16 14:43:42 +08:00

201 lines
6.1 KiB
TypeScript

// oxlint-disable n/no-deprecated-api
// oxlint-disable unicorn/import-style -- need full util module for CJS compatibility
// 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';
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 (Object.hasOwn(builtinModules, id)) {
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,
};