You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. if (semver.satisfies(process.version, '^20.12 || >= 21.7')) {
  79. libs = libs.concat('node:sea');
  80. }
  81. for (var i = 0; i < libs.length; ++i) {
  82. var mod = libs[i];
  83. if (excludeList.indexOf(mod) === -1) {
  84. st.ok(data[mod], mod + ' is a core module');
  85. st.doesNotThrow(
  86. function () { require(mod); }, // eslint-disable-line no-loop-func
  87. 'requiring ' + mod + ' does not throw'
  88. );
  89. if (mod.slice(0, 5) !== 'node:') {
  90. if (supportsNodePrefix) {
  91. st.doesNotThrow(
  92. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  93. 'requiring node:' + mod + ' does not throw'
  94. );
  95. } else {
  96. st['throws'](
  97. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  98. 'requiring node:' + mod + ' throws'
  99. );
  100. }
  101. }
  102. }
  103. }
  104. }
  105. st.end();
  106. });
  107. t.test('Object.prototype pollution', function (st) {
  108. var nonKey = 'not a core module';
  109. st.teardown(mockProperty(Object.prototype, 'fs', { value: false }));
  110. st.teardown(mockProperty(Object.prototype, 'path', { value: '>= 999999999' }));
  111. st.teardown(mockProperty(Object.prototype, 'http', { value: data.http }));
  112. st.teardown(mockProperty(Object.prototype, nonKey, { value: true }));
  113. st.equal(isCore('fs'), true, 'fs is a core module even if Object.prototype lies');
  114. st.equal(isCore('path'), true, 'path is a core module even if Object.prototype lies');
  115. st.equal(isCore('http'), true, 'path is a core module even if Object.prototype matches data');
  116. st.equal(isCore(nonKey), false, '"' + nonKey + '" is not a core module even if Object.prototype lies');
  117. st.end();
  118. });
  119. t.end();
  120. });