index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. if (semver.satisfies(process.version, '>= 23.4')) {
  83. libs = libs.concat('node:sqlite');
  84. }
  85. for (var i = 0; i < libs.length; ++i) {
  86. var mod = libs[i];
  87. if (excludeList.indexOf(mod) === -1) {
  88. st.ok(data[mod], mod + ' is a core module');
  89. if (Module.isBuiltin) {
  90. st.ok(Module.isBuiltin(mod), 'module.isBuiltin(' + mod + ') is true');
  91. }
  92. st.doesNotThrow(
  93. function () { require(mod); }, // eslint-disable-line no-loop-func
  94. 'requiring ' + mod + ' does not throw'
  95. );
  96. if (process.getBuiltinModule) {
  97. st.equal(
  98. process.getBuiltinModule(mod),
  99. require(mod),
  100. 'process.getBuiltinModule(' + mod + ') === require(' + mod + ')'
  101. );
  102. }
  103. if (mod.slice(0, 5) !== 'node:') {
  104. if (supportsNodePrefix) {
  105. st.doesNotThrow(
  106. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  107. 'requiring node:' + mod + ' does not throw'
  108. );
  109. } else {
  110. st['throws'](
  111. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  112. 'requiring node:' + mod + ' throws'
  113. );
  114. }
  115. }
  116. }
  117. }
  118. }
  119. st.end();
  120. });
  121. t.test('isCore with explicit nodeVersion', function (st) {
  122. st.ok(isCore('async_hooks', '8.0.0'), 'async_hooks is core in 8.0.0 (exact match on >= specifier)');
  123. st.ok(isCore('buffer_ieee754', '0.5.0'), 'buffer_ieee754 is core in 0.5.0');
  124. st.ok(!isCore('buffer_ieee754', '0.9.7'), 'buffer_ieee754 is not core in 0.9.7');
  125. st.ok(!isCore('buffer_ieee754', '1.0.0'), 'buffer_ieee754 is not core in 1.0.0');
  126. st.ok(isCore('buffer_ieee754', '0.8.0'), 'buffer_ieee754 is core in 0.8.0');
  127. st['throws'](
  128. function () { isCore('async_hooks', null); },
  129. TypeError,
  130. 'isCore with non-string non-undefined nodeVersion throws TypeError'
  131. );
  132. st['throws'](
  133. function () { isCore('async_hooks', 123); },
  134. TypeError,
  135. 'isCore with numeric nodeVersion throws TypeError'
  136. );
  137. st.end();
  138. });
  139. t.test('isCore with non-boolean specifier and invalid nodeVersion', function (st) {
  140. st['throws'](
  141. function () { isCore('async_hooks', null); },
  142. TypeError,
  143. 'isCore with null nodeVersion on non-boolean module throws TypeError'
  144. );
  145. st['throws'](
  146. function () { isCore('async_hooks', 123); },
  147. TypeError,
  148. 'isCore with numeric nodeVersion on non-boolean module throws TypeError'
  149. );
  150. st.end();
  151. });
  152. t.test('isCore with undefined nodeVersion and non-string process.versions.node', function (st) {
  153. st.teardown(mockProperty(process, 'versions', { value: { node: null } }));
  154. st['throws'](
  155. function () { isCore('async_hooks'); },
  156. TypeError,
  157. 'isCore throws when process.versions.node is not a string'
  158. );
  159. st.end();
  160. });
  161. t.test('isCore with undefined nodeVersion and falsy process.versions', function (st) {
  162. st.teardown(mockProperty(process, 'versions', { value: null }));
  163. st['throws'](
  164. function () { isCore('async_hooks'); },
  165. TypeError,
  166. 'isCore throws when process.versions is falsy'
  167. );
  168. st.end();
  169. });
  170. t.test('specifierIncluded with = operator (bare version)', function (st) {
  171. var testKey = '__test_equal_op';
  172. data[testKey] = '14.18';
  173. st.teardown(function () { delete data[testKey]; });
  174. st.ok(!isCore(testKey, '14.17.0'), 'returns false when minor version does not match with = op');
  175. st.ok(!isCore(testKey, '15.0.0'), 'returns false when major version does not match with = op');
  176. st.ok(!isCore(testKey, '14.18.0'), 'returns false for exact match with = op (= is not >=)');
  177. st.end();
  178. });
  179. t.test('isCore with short version strings', function (st) {
  180. st.ok(isCore('async_hooks', '8'), 'async_hooks is core with single-part version "8"');
  181. st.ok(!isCore('async_hooks', '7'), 'async_hooks is not core with single-part version "7"');
  182. st.ok(isCore('cluster', '0.5'), 'cluster is core with two-part version "0.5"');
  183. st.end();
  184. });
  185. t.test('matchesRange with empty specifiers array', function (st) {
  186. var testKey = '__test_empty_split';
  187. var testRange = '__test_range__';
  188. data[testKey] = testRange;
  189. st.teardown(function () { delete data[testKey]; });
  190. var origSplit = String.prototype.split;
  191. st.teardown(mockProperty(String.prototype, 'split', {
  192. value: function () {
  193. if (String(this) === testRange) {
  194. return [];
  195. }
  196. return origSplit.apply(this, arguments);
  197. }
  198. }));
  199. st.ok(!isCore(testKey, '8.0.0'), 'returns false when specifiers array is empty');
  200. st.end();
  201. });
  202. t.test('Object.prototype pollution', function (st) {
  203. var nonKey = 'not a core module';
  204. st.teardown(mockProperty(Object.prototype, 'fs', { value: false }));
  205. st.teardown(mockProperty(Object.prototype, 'path', { value: '>= 999999999' }));
  206. st.teardown(mockProperty(Object.prototype, 'http', { value: data.http }));
  207. st.teardown(mockProperty(Object.prototype, nonKey, { value: true }));
  208. st.equal(isCore('fs'), true, 'fs is a core module even if Object.prototype lies');
  209. st.equal(isCore('path'), true, 'path is a core module even if Object.prototype lies');
  210. st.equal(isCore('http'), true, 'path is a core module even if Object.prototype matches data');
  211. st.equal(isCore(nonKey), false, '"' + nonKey + '" is not a core module even if Object.prototype lies');
  212. st.end();
  213. });
  214. t.end();
  215. });