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