module.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * `node:module` for the worker: `createRequire` hands out the worker module
  3. * loader's synchronous require, so typert's `require.resolve('<pkg>/package.json')
  4. * + readFileSync + import()` bypass runs unmodified over the VFS.
  5. */
  6. import { requireActiveModuleLoader, type WorkerRequire } from '../../../module-system/module-loader.ts'
  7. /** Node `require` face the harness consumes. */
  8. export type NodeRequire = WorkerRequire
  9. /**
  10. * Build a `require` bound to a base path or file URL.
  11. * @param base - directory, file path, or file URL the resolution starts from.
  12. * @returns the synchronous require face.
  13. */
  14. export function createRequire(base: string | URL): NodeRequire {
  15. return requireActiveModuleLoader().createRequire(base)
  16. }
  17. /** Builtin specifiers the module proxy table answers (without the `node:` prefix). */
  18. export const builtinModules = [
  19. 'assert', 'async_hooks', 'buffer', 'child_process', 'crypto', 'events', 'fs', 'http', 'module',
  20. 'net', 'os', 'path', 'process', 'stream', 'url', 'util', 'worker_threads',
  21. ]
  22. /**
  23. * Whether a specifier names a Node builtin.
  24. * @param specifier - the module specifier.
  25. * @returns true for builtin names, with or without the `node:` prefix.
  26. */
  27. export function isBuiltin(specifier: string): boolean {
  28. return builtinModules.includes(specifier.replace(/^node:/, ''))
  29. }
  30. /**
  31. * TypeScript stripping is a Node 22+ loader feature with no worker counterpart.
  32. * @returns Never — it throws naming the unavailable member.
  33. */
  34. export function stripTypeScriptTypes(): never {
  35. throw new Error('web-preview: node:module.stripTypeScriptTypes is not available in the worker host')
  36. }
  37. /**
  38. * Loader hooks have no meaning here: the worker loader owns resolution.
  39. * @returns Never — it throws naming the unavailable member.
  40. */
  41. export function register(): never {
  42. throw new Error('web-preview: node:module.register is not available in the worker host')
  43. }
  44. /** ESM/CJS export syncing is a no-op: the worker loader materializes CommonJS only. */
  45. export function syncBuiltinESMExports(): void {
  46. // Nothing to sync: every builtin is a plain module object from the proxy table.
  47. }
  48. /** Erased type peer for the vendored loader's type-only LoadHookContext import. */
  49. export type LoadHookContext = never
  50. /** CommonJS interop marker: the worker loader hands `default` to default imports (see ./builtins.ts). */
  51. export const __esModule = true
  52. /**
  53. * The `node:module` declarations this module stands in for. `createRequire`
  54. * keeps this module's own face: the loader's require carries the call and
  55. * `resolve` the harness uses, not Node's `cache`, `extensions`, and `main`,
  56. * which describe a CommonJS module registry the worker has no counterpart for.
  57. */
  58. type NodeFace = Partial<Omit<typeof import('node:module'), 'createRequire'>> & Record<'createRequire', unknown>
  59. /** CommonJS default export: the members `require()` hands a caller of this module. */
  60. export default {
  61. createRequire, builtinModules, isBuiltin, register, syncBuiltinESMExports, stripTypeScriptTypes,
  62. } satisfies NodeFace