index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 libs = require('module').builtinModules;
  64. if (!libs) {
  65. st.skip('module.builtinModules does not exist');
  66. } else {
  67. var excludeList = [
  68. '_debug_agent',
  69. 'v8/tools/tickprocessor-driver',
  70. 'v8/tools/SourceMap',
  71. 'v8/tools/tickprocessor',
  72. 'v8/tools/profile'
  73. ];
  74. // see https://github.com/nodejs/node/issues/42785
  75. if (semver.satisfies(process.version, '>= 18')) {
  76. libs = libs.concat('node:test');
  77. }
  78. for (var i = 0; i < libs.length; ++i) {
  79. var mod = libs[i];
  80. if (excludeList.indexOf(mod) === -1) {
  81. st.ok(data[mod], mod + ' is a core module');
  82. st.doesNotThrow(
  83. function () { require(mod); }, // eslint-disable-line no-loop-func
  84. 'requiring ' + mod + ' does not throw'
  85. );
  86. if (mod.slice(0, 5) !== 'node:') {
  87. if (supportsNodePrefix) {
  88. st.doesNotThrow(
  89. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  90. 'requiring node:' + mod + ' does not throw'
  91. );
  92. } else {
  93. st['throws'](
  94. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  95. 'requiring node:' + mod + ' throws'
  96. );
  97. }
  98. }
  99. }
  100. }
  101. }
  102. st.end();
  103. });
  104. t.test('Object.prototype pollution', function (st) {
  105. var nonKey = 'not a core module';
  106. st.teardown(mockProperty(Object.prototype, 'fs', { value: false }));
  107. st.teardown(mockProperty(Object.prototype, 'path', { value: '>= 999999999' }));
  108. st.teardown(mockProperty(Object.prototype, 'http', { value: data.http }));
  109. st.teardown(mockProperty(Object.prototype, nonKey, { value: true }));
  110. st.equal(isCore('fs'), true, 'fs is a core module even if Object.prototype lies');
  111. st.equal(isCore('path'), true, 'path is a core module even if Object.prototype lies');
  112. st.equal(isCore('http'), true, 'path is a core module even if Object.prototype matches data');
  113. st.equal(isCore(nonKey), false, '"' + nonKey + '" is not a core module even if Object.prototype lies');
  114. st.end();
  115. });
  116. t.end();
  117. });