• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Flags: --expose-internals
4// This verifies that
5// 1. We do not leak internal modules unless the --require-internals option
6//    is on.
7// 2. We do not accidentally leak any modules to the public global scope.
8// 3. Deprecated modules are properly deprecated.
9
10const common = require('../common');
11
12if (!common.isMainThread) {
13  common.skip('Cannot test the existence of --expose-internals from worker');
14}
15
16const assert = require('assert');
17const fork = require('child_process').fork;
18
19const expectedPublicModules = new Set([
20  '_http_agent',
21  '_http_client',
22  '_http_common',
23  '_http_incoming',
24  '_http_outgoing',
25  '_http_server',
26  '_stream_duplex',
27  '_stream_passthrough',
28  '_stream_readable',
29  '_stream_transform',
30  '_stream_wrap',
31  '_stream_writable',
32  '_tls_common',
33  '_tls_wrap',
34  'assert',
35  'async_hooks',
36  'buffer',
37  'child_process',
38  'cluster',
39  'console',
40  'constants',
41  'crypto',
42  'dgram',
43  'dns',
44  'domain',
45  'events',
46  'fs',
47  'http',
48  'http2',
49  'https',
50  'inspector',
51  'module',
52  'net',
53  'os',
54  'path',
55  'perf_hooks',
56  'process',
57  'punycode',
58  'querystring',
59  'readline',
60  'repl',
61  'stream',
62  'string_decoder',
63  'sys',
64  'timers',
65  'tls',
66  'trace_events',
67  'tty',
68  'url',
69  'util',
70  'v8',
71  'vm',
72  'worker_threads',
73  'zlib',
74]);
75
76if (process.argv[2] === 'child') {
77  assert(!process.execArgv.includes('--expose-internals'));
78  process.once('message', ({ allBuiltins }) => {
79    const publicModules = new Set();
80    for (const id of allBuiltins) {
81      if (id.startsWith('internal/')) {
82        assert.throws(() => {
83          require(id);
84        }, {
85          code: 'MODULE_NOT_FOUND',
86          message: `Cannot find module '${id}'`
87        });
88      } else {
89        require(id);
90        publicModules.add(id);
91      }
92    }
93    assert(allBuiltins.length > publicModules.size);
94    // Make sure all the public modules are available through
95    // require('module').builtinModules
96    assert.deepStrictEqual(
97      publicModules,
98      new Set(require('module').builtinModules)
99    );
100    assert.deepStrictEqual(publicModules, expectedPublicModules);
101  });
102} else {
103  assert(process.execArgv.includes('--expose-internals'));
104  const child = fork(__filename, ['child'], {
105    execArgv: []
106  });
107  const { builtinModules } = require('module');
108  // When --expose-internals is on, require('module').builtinModules
109  // contains internal modules.
110  const message = { allBuiltins: builtinModules };
111  child.send(message);
112}
113