summaryrefslogtreecommitdiff
path: root/test/parallel/test-internal-module-require.js
blob: c6e2057d3da1eee0c0b9268d34750c4d75d1e8e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';

// Flags: --expose-internals
// This verifies that
// 1. We do not leak internal modules unless the --require-internals option
//    is on.
// 2. We do not accidentally leak any modules to the public global scope.
// 3. Deprecated modules are properly deprecated.

const common = require('../common');

if (!common.isMainThread) {
  common.skip('Cannot test the existence of --expose-internals from worker');
}

const assert = require('assert');
const fork = require('child_process').fork;

const expectedPublicModules = new Set([
  '_http_agent',
  '_http_client',
  '_http_common',
  '_http_incoming',
  '_http_outgoing',
  '_http_server',
  '_stream_duplex',
  '_stream_passthrough',
  '_stream_readable',
  '_stream_transform',
  '_stream_wrap',
  '_stream_writable',
  '_tls_common',
  '_tls_wrap',
  'assert',
  'async_hooks',
  'buffer',
  'child_process',
  'cluster',
  'console',
  'constants',
  'crypto',
  'dgram',
  'dns',
  'domain',
  'events',
  'fs',
  'http',
  'http2',
  'https',
  'inspector',
  'module',
  'net',
  'os',
  'path',
  'perf_hooks',
  'process',
  'punycode',
  'querystring',
  'readline',
  'repl',
  'stream',
  'string_decoder',
  'sys',
  'timers',
  'tls',
  'trace_events',
  'tty',
  'url',
  'util',
  'v8',
  'vm',
  'worker_threads',
  'zlib',
]);

if (process.argv[2] === 'child') {
  assert(!process.execArgv.includes('--expose-internals'));
  process.once('message', ({ allBuiltins }) => {
    const publicModules = new Set();
    for (const id of allBuiltins) {
      if (id.startsWith('internal/')) {
        assert.throws(() => {
          require(id);
        }, {
          code: 'MODULE_NOT_FOUND',
          message: `Cannot find module '${id}'`
        });
      } else {
        require(id);
        publicModules.add(id);
      }
    }
    assert(allBuiltins.length > publicModules.size);
    // Make sure all the public modules are available through
    // require('module').builtinModules
    assert.deepStrictEqual(
      publicModules,
      new Set(require('module').builtinModules)
    );
    assert.deepStrictEqual(publicModules, expectedPublicModules);
  });
} else {
  assert(process.execArgv.includes('--expose-internals'));
  const child = fork(__filename, ['child'], {
    execArgv: []
  });
  const { builtinModules } = require('module');
  // When --expose-internals is on, require('module').builtinModules
  // contains internal modules.
  const message = { allBuiltins: builtinModules };
  child.send(message);
}