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