index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict';
  2. var test = require('tape');
  3. var keys = require('object-keys');
  4. var semver = require('semver');
  5. var mockProperty = require('mock-property');
  6. var isCore = require('../');
  7. var data = require('../core.json');
  8. var supportsNodePrefix = semver.satisfies(process.versions.node, '^14.18 || >= 16', { includePrerelease: true });
  9. test('core modules', function (t) {
  10. t.test('isCore()', function (st) {
  11. st.ok(isCore('fs'));
  12. st.ok(isCore('net'));
  13. st.ok(isCore('http'));
  14. st.ok(!isCore('seq'));
  15. st.ok(!isCore('../'));
  16. st.ok(!isCore('toString'));
  17. st.end();
  18. });
  19. t.test('core list', function (st) {
  20. var cores = keys(data);
  21. st.plan(cores.length);
  22. for (var i = 0; i < cores.length; ++i) {
  23. var mod = cores[i];
  24. var requireFunc = function () { require(mod); }; // eslint-disable-line no-loop-func
  25. if (isCore(mod)) {
  26. st.doesNotThrow(requireFunc, mod + ' supported; requiring does not throw');
  27. } else {
  28. st['throws'](requireFunc, mod + ' not supported; requiring throws');
  29. }
  30. }
  31. st.end();
  32. });
  33. t.test('core via repl module', { skip: !data.repl }, function (st) {
  34. var libs = require('repl')._builtinLibs; // eslint-disable-line no-underscore-dangle
  35. if (!libs) {
  36. st.skip('repl._builtinLibs does not exist');
  37. } else {
  38. for (var i = 0; i < libs.length; ++i) {
  39. var mod = libs[i];
  40. st.ok(data[mod], mod + ' is a core module');
  41. st.doesNotThrow(
  42. function () { require(mod); }, // eslint-disable-line no-loop-func
  43. 'requiring ' + mod + ' does not throw'
  44. );
  45. if (mod.slice(0, 5) !== 'node:') {
  46. if (supportsNodePrefix) {
  47. st.doesNotThrow(
  48. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  49. 'requiring node:' + mod + ' does not throw'
  50. );
  51. } else {
  52. st['throws'](
  53. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  54. 'requiring node:' + mod + ' throws'
  55. );
  56. }
  57. }
  58. }
  59. }
  60. st.end();
  61. });
  62. t.test('core via builtinModules list', { skip: !data.module }, function (st) {
  63. var Module = require('module');
  64. var libs = Module.builtinModules;
  65. if (!libs) {
  66. st.skip('module.builtinModules does not exist');
  67. } else {
  68. var excludeList = [
  69. '_debug_agent',
  70. 'v8/tools/tickprocessor-driver',
  71. 'v8/tools/SourceMap',
  72. 'v8/tools/tickprocessor',
  73. 'v8/tools/profile'
  74. ];
  75. // see https://github.com/nodejs/node/issues/42785
  76. if (semver.satisfies(process.version, '>= 18')) {
  77. libs = libs.concat('node:test');
  78. }
  79. if (semver.satisfies(process.version, '^20.12 || >= 21.7')) {
  80. libs = libs.concat('node:sea');
  81. }
  82. for (var i = 0; i < libs.length; ++i) {
  83. var mod = libs[i];
  84. if (excludeList.indexOf(mod) === -1) {
  85. st.ok(data[mod], mod + ' is a core module');
  86. if (Module.isBuiltin) {
  87. st.ok(Module.isBuiltin(mod), 'module.isBuiltin(' + mod + ') is true');
  88. }
  89. st.doesNotThrow(
  90. function () { require(mod); }, // eslint-disable-line no-loop-func
  91. 'requiring ' + mod + ' does not throw'
  92. );
  93. if (process.getBuiltinModule) {
  94. st.equal(
  95. process.getBuiltinModule(mod),
  96. require(mod),
  97. 'process.getBuiltinModule(' + mod + ') === require(' + mod + ')'
  98. );
  99. }
  100. if (mod.slice(0, 5) !== 'node:') {
  101. if (supportsNodePrefix) {
  102. st.doesNotThrow(
  103. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  104. 'requiring node:' + mod + ' does not throw'
  105. );
  106. } else {
  107. st['throws'](
  108. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  109. 'requiring node:' + mod + ' throws'
  110. );
  111. }
  112. }
  113. }
  114. }
  115. }
  116. st.end();
  117. });
  118. t.test('Object.prototype pollution', function (st) {
  119. var nonKey = 'not a core module';
  120. st.teardown(mockProperty(Object.prototype, 'fs', { value: false }));
  121. st.teardown(mockProperty(Object.prototype, 'path', { value: '>= 999999999' }));
  122. st.teardown(mockProperty(Object.prototype, 'http', { value: data.http }));
  123. st.teardown(mockProperty(Object.prototype, nonKey, { value: true }));
  124. st.equal(isCore('fs'), true, 'fs is a core module even if Object.prototype lies');
  125. st.equal(isCore('path'), true, 'path is a core module even if Object.prototype lies');
  126. st.equal(isCore('http'), true, 'path is a core module even if Object.prototype matches data');
  127. st.equal(isCore(nonKey), false, '"' + nonKey + '" is not a core module even if Object.prototype lies');
  128. st.end();
  129. });
  130. t.end();
  131. });