index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict'
  2. const semver = require('semver')
  3. const permanentModules = [
  4. 'assert',
  5. 'buffer',
  6. 'child_process',
  7. 'cluster',
  8. 'console',
  9. 'constants',
  10. 'crypto',
  11. 'dgram',
  12. 'dns',
  13. 'domain',
  14. 'events',
  15. 'fs',
  16. 'http',
  17. 'https',
  18. 'module',
  19. 'net',
  20. 'os',
  21. 'path',
  22. 'punycode',
  23. 'querystring',
  24. 'readline',
  25. 'repl',
  26. 'stream',
  27. 'string_decoder',
  28. 'sys',
  29. 'timers',
  30. 'tls',
  31. 'tty',
  32. 'url',
  33. 'util',
  34. 'vm',
  35. 'zlib'
  36. ]
  37. const versionLockedModules = {
  38. freelist: '<6.0.0',
  39. v8: '>=1.0.0',
  40. process: '>=1.1.0',
  41. inspector: '>=8.0.0',
  42. async_hooks: '>=8.1.0',
  43. http2: '>=8.4.0',
  44. perf_hooks: '>=8.5.0',
  45. trace_events: '>=10.0.0',
  46. worker_threads: '>=12.0.0',
  47. 'node:test': '>=18.0.0'
  48. }
  49. const experimentalModules = {
  50. worker_threads: '>=10.5.0',
  51. wasi: '>=12.16.0',
  52. diagnostics_channel: '^14.17.0 || >=15.1.0'
  53. }
  54. module.exports = ({ version = process.version, experimental = false } = {}) => {
  55. const builtins = [...permanentModules]
  56. for (const [name, semverRange] of Object.entries(versionLockedModules)) {
  57. if (version === '*' || semver.satisfies(version, semverRange)) {
  58. builtins.push(name)
  59. }
  60. }
  61. if (experimental) {
  62. for (const [name, semverRange] of Object.entries(experimentalModules)) {
  63. if (
  64. !builtins.includes(name) &&
  65. (version === '*' || semver.satisfies(version, semverRange))
  66. ) {
  67. builtins.push(name)
  68. }
  69. }
  70. }
  71. return builtins
  72. }