Skip to content

Commit 0e10717

Browse files
committed
build: runtime-deprecate requiring deps
PR-URL: #16392 Fixes: #15566 (comment) Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 14d24cc commit 0e10717

File tree

5 files changed

+117
-13
lines changed

5 files changed

+117
-13
lines changed

doc/api/deprecations.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,37 @@ be set to `false` to disable ECDH entirely on the server only. This mode is
747747
deprecated in preparation for migrating to OpenSSL 1.1.0 and consistency with
748748
the client. Use the `ciphers` parameter instead.
749749
750+
<a id="DEP0084"></a>
751+
### DEP0084: requiring bundled internal dependencies
752+
753+
Type: Runtime
754+
755+
Since Node.js versions 4.4.0 and 5.2.0, several modules only intended for
756+
internal usage are mistakenly exposed to user code through `require()`. These
757+
modules are:
758+
759+
- `v8/tools/codemap`
760+
- `v8/tools/consarray`
761+
- `v8/tools/csvparser`
762+
- `v8/tools/logreader`
763+
- `v8/tools/profile_view`
764+
- `v8/tools/profile`
765+
- `v8/tools/SourceMap`
766+
- `v8/tools/splaytree`
767+
- `v8/tools/tickprocessor-driver`
768+
- `v8/tools/tickprocessor`
769+
- `node-inspect/lib/_inspect` (from 7.6.0)
770+
- `node-inspect/lib/internal/inspect_client` (from 7.6.0)
771+
- `node-inspect/lib/internal/inspect_repl` (from 7.6.0)
772+
773+
The `v8/*` modules do not have any exports, and if not imported in a specific
774+
order would in fact throw errors. As such there are virtually no legitimate use
775+
cases for importing them through `require()`.
776+
777+
On the other hand, `node-inspect` may be installed locally through a package
778+
manager, as it is published on the npm registry under the same name. No source
779+
code modification is necessary if that is done.
780+
750781
751782
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
752783
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array

lib/internal/bootstrap_node.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130

131131
// Start the debugger agent
132132
process.nextTick(function() {
133-
NativeModule.require('node-inspect/lib/_inspect').start();
133+
NativeModule.require('internal/deps/node-inspect/lib/_inspect').start();
134134
});
135135

136136
} else if (process.profProcess) {
@@ -548,6 +548,16 @@
548548
return nativeModule.exports;
549549
};
550550

551+
NativeModule.requireForDeps = function(id) {
552+
if (!NativeModule.exists(id) ||
553+
// TODO(TimothyGu): remove when DEP0084 reaches end of life.
554+
id.startsWith('node-inspect/') ||
555+
id.startsWith('v8/')) {
556+
id = `internal/deps/${id}`;
557+
}
558+
return NativeModule.require(id);
559+
};
560+
551561
NativeModule.getCached = function(id) {
552562
return NativeModule._cache[id];
553563
};
@@ -598,7 +608,10 @@
598608
lineOffset: 0,
599609
displayErrors: true
600610
});
601-
fn(this.exports, NativeModule.require, this, internalBinding);
611+
const requireFn = this.id.startsWith('internal/deps/') ?
612+
NativeModule.requireForDeps :
613+
NativeModule.require;
614+
fn(this.exports, requireFn, this, internalBinding);
602615

603616
this.loaded = true;
604617
} finally {

lib/internal/v8_prof_processor.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/* eslint-disable strict */
22
const scriptFiles = [
33
'internal/v8_prof_polyfill',
4-
'v8/tools/splaytree',
5-
'v8/tools/codemap',
6-
'v8/tools/csvparser',
7-
'v8/tools/consarray',
8-
'v8/tools/profile',
9-
'v8/tools/profile_view',
10-
'v8/tools/logreader',
11-
'v8/tools/tickprocessor',
12-
'v8/tools/SourceMap',
13-
'v8/tools/tickprocessor-driver'
4+
'internal/deps/v8/tools/splaytree',
5+
'internal/deps/v8/tools/codemap',
6+
'internal/deps/v8/tools/csvparser',
7+
'internal/deps/v8/tools/consarray',
8+
'internal/deps/v8/tools/profile',
9+
'internal/deps/v8/tools/profile_view',
10+
'internal/deps/v8/tools/logreader',
11+
'internal/deps/v8/tools/tickprocessor',
12+
'internal/deps/v8/tools/SourceMap',
13+
'internal/deps/v8/tools/tickprocessor-driver'
1414
];
1515
var script = '';
1616

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
// The v8 modules when imported leak globals. Disable global check.
5+
common.globalCheck = false;
6+
7+
const deprecatedModules = [
8+
'node-inspect/lib/_inspect',
9+
'node-inspect/lib/internal/inspect_client',
10+
'node-inspect/lib/internal/inspect_repl',
11+
'v8/tools/SourceMap',
12+
'v8/tools/codemap',
13+
'v8/tools/consarray',
14+
'v8/tools/csvparser',
15+
'v8/tools/logreader',
16+
'v8/tools/profile',
17+
'v8/tools/profile_view',
18+
'v8/tools/splaytree',
19+
'v8/tools/tickprocessor',
20+
'v8/tools/tickprocessor-driver'
21+
];
22+
23+
common.expectWarning('DeprecationWarning', deprecatedModules.map((m) => {
24+
return `Requiring Node.js-bundled '${m}' module is deprecated. ` +
25+
'Please install the necessary module locally.';
26+
}));
27+
28+
for (const m of deprecatedModules) {
29+
try {
30+
require(m);
31+
} catch (err) {}
32+
}

tools/js2c.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ def ReadMacros(lines):
218218
{value}.ToStringChecked(env->isolate())).FromJust());
219219
"""
220220

221+
DEPRECATED_DEPS = """\
222+
'use strict';
223+
process.emitWarning(
224+
'Requiring Node.js-bundled \\'{module}\\' module is deprecated. Please ' +
225+
'install the necessary module locally.', 'DeprecationWarning', 'DEP0084');
226+
module.exports = require('internal/deps/{module}');
227+
"""
228+
221229

222230
def Render(var, data):
223231
# Treat non-ASCII as UTF-8 and convert it to UTF-16.
@@ -256,10 +264,19 @@ def JS2C(source, target):
256264
lines = ExpandConstants(lines, consts)
257265
lines = ExpandMacros(lines, macros)
258266

267+
deprecated_deps = None
268+
259269
# On Windows, "./foo.bar" in the .gyp file is passed as "foo.bar"
260270
# so don't assume there is always a slash in the file path.
261271
if '/' in name or '\\' in name:
262-
name = '/'.join(re.split('/|\\\\', name)[1:])
272+
split = re.split('/|\\\\', name)
273+
if split[0] == 'deps':
274+
if split[1] == 'node-inspect' or split[1] == 'v8':
275+
deprecated_deps = split[1:]
276+
split = ['internal'] + split
277+
else:
278+
split = split[1:]
279+
name = '/'.join(split)
263280

264281
name = name.split('.', 1)[0]
265282
var = name.replace('-', '_').replace('/', '_')
@@ -270,6 +287,17 @@ def JS2C(source, target):
270287
definitions.append(Render(value, lines))
271288
initializers.append(INITIALIZER.format(key=key, value=value))
272289

290+
if deprecated_deps is not None:
291+
name = '/'.join(deprecated_deps)
292+
name = name.split('.', 1)[0]
293+
var = name.replace('-', '_').replace('/', '_')
294+
key = '%s_key' % var
295+
value = '%s_value' % var
296+
297+
definitions.append(Render(key, name))
298+
definitions.append(Render(value, DEPRECATED_DEPS.format(module=name)))
299+
initializers.append(INITIALIZER.format(key=key, value=value))
300+
273301
# Emit result
274302
output = open(str(target[0]), "w")
275303
output.write(TEMPLATE.format(definitions=''.join(definitions),

0 commit comments

Comments
 (0)