Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src, buffer: add --pending-deprecation flag #11968

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ added: v0.11.14

Throw errors for deprecations.

### `--pending-deprecation`
<!-- YAML
added: REPLACEME
-->

Emit pending deprecation warnings.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a sentence with the purpose of a pending deprecation?

Copy link
Member

@Trott Trott Mar 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Maybe also specify that it's specifically the runtime deprecation that's pending, and that stuff is frequently deprecated in the docs but with no runtime deprecation message. Or something. We differentiate between "runtime deprecation" and "docs deprecation" pretty much everywhere, so it seems like there ought to be some clarity when people are using this flag.

That said, I'm fine with this landing without that information. I know it's crunch time on this stuff right now. It can always be added in a subsequent pull request if it turns out that it's important to include.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to add an explanatory note.


*Note*: Pending deprecations are generally identical to a runtime deprecation
with the notable exception that they are turned *off* by default and will not
be emitted unless either the `--pending-deprecation` command line flag, or the
`NODE_PENDING_DEPRECATION=1` environment variable, is set. Pending deprecations
are used to provide a kind of selective "early warning" mechanism that
developers may leverage to detect deprecated API usage.

### `--no-warnings`
<!-- YAML
added: v6.0.0
Expand Down Expand Up @@ -375,6 +389,20 @@ added: v7.5.0

When set to `1`, process warnings are silenced.

### `NODE_PENDING_DEPRECATION=1`
<!-- YAML
added: REPLACEME
-->

When set to `1`, emit pending deprecation warnings.

*Note*: Pending deprecations are generally identical to a runtime deprecation
with the notable exception that they are turned *off* by default and will not
be emitted unless either the `--pending-deprecation` command line flag, or the
`NODE_PENDING_DEPRECATION=1` environment variable, is set. Pending deprecations
are used to provide a kind of selective "early warning" mechanism that
developers may leverage to detect deprecated API usage.

### `NODE_PRESERVE_SYMLINKS=1`
<!-- YAML
added: v7.1.0
Expand Down
8 changes: 8 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ Print stack traces for deprecations.
.BR \-\-throw\-deprecation
Throw errors for deprecations.

.TP
.BR \-\-pending\-deprecation
Emit pending deprecation warnings.

.TP
.BR \-\-no\-warnings
Silence all process warnings (including deprecations).
Expand Down Expand Up @@ -254,6 +258,10 @@ When set to \fI1\fR, process warnings are silenced.
.BR NODE_PATH =\fIpath\fR[:\fI...\fR]
\':\'\-separated list of directories prefixed to the module search path.

.TP
.BR NODE_PENDING_DEPRECATION = \fI1\fR
When set to \fI1\fR, emit pending deprecation warnings.

.TP
.BR NODE_REPL_HISTORY =\fIfile\fR
Path to the file used to store the persistent REPL history. The default path
Expand Down
33 changes: 32 additions & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@
'use strict';

const binding = process.binding('buffer');
const config = process.binding('config');
const { compare: compare_, compareOffset } = binding;
const { isArrayBuffer, isSharedArrayBuffer, isUint8Array } =
process.binding('util');
const bindingObj = {};
const internalUtil = require('internal/util');
const pendingDeprecation = !!config.pendingDeprecation;

class FastBuffer extends Uint8Array {
constructor(arg1, arg2, arg3) {
super(arg1, arg2, arg3);
}
}

FastBuffer.prototype.constructor = Buffer;

Buffer.prototype = FastBuffer.prototype;

exports.Buffer = Buffer;
Expand Down Expand Up @@ -84,6 +86,28 @@ function alignPool() {
}
}

var bufferWarn = true;
const bufferWarning = 'The Buffer() and new Buffer() constructors are not ' +
'recommended for use due to security and usability ' +
'concerns. Please use the new Buffer.alloc(), ' +
'Buffer.allocUnsafe(), or Buffer.from() construction ' +
'methods instead.';

function showFlaggedDeprecation() {
if (bufferWarn) {
// This is a *pending* deprecation warning. It is not emitted by
// default unless the --pending-deprecation command-line flag is
// used or the NODE_PENDING_DEPRECATION=1 envvar is set.
process.emitWarning(bufferWarning, 'DeprecationWarning', 'DEP0005');
bufferWarn = false;
}
}

const doFlaggedDeprecation =
pendingDeprecation ?
showFlaggedDeprecation :
function() {};

/**
* The Buffer() construtor is deprecated in documentation and should not be
* used moving forward. Rather, developers should use one of the three new
Expand All @@ -95,6 +119,7 @@ function alignPool() {
* Deprecation Code: DEP0005
**/
function Buffer(arg, encodingOrOffset, length) {
doFlaggedDeprecation();
// Common case.
if (typeof arg === 'number') {
if (typeof encodingOrOffset === 'string') {
Expand All @@ -107,6 +132,12 @@ function Buffer(arg, encodingOrOffset, length) {
return Buffer.from(arg, encodingOrOffset, length);
}

Object.defineProperty(Buffer, Symbol.species, {
enumerable: false,
configurable: true,
get() { return FastBuffer; }
});

/**
* Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
* if value is a number.
Expand Down
12 changes: 12 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ bool trace_warnings = false;
// that is used by lib/module.js
bool config_preserve_symlinks = false;

// Set by ParseArgs when --pending-deprecation or NODE_PENDING_DEPRECATION
// is used.
bool config_pending_deprecation = false;

// Set in node.cc by ParseArgs when --redirect-warnings= is used.
std::string config_warning_file; // NOLINT(runtime/string)

Expand Down Expand Up @@ -3741,6 +3745,8 @@ static void ParseArgs(int* argc,
short_circuit = true;
} else if (strcmp(arg, "--zero-fill-buffers") == 0) {
zero_fill_all_buffers = true;
} else if (strcmp(arg, "--pending-deprecation") == 0) {
config_pending_deprecation = true;
} else if (strcmp(arg, "--v8-options") == 0) {
new_v8_argv[new_v8_argc] = "--help";
new_v8_argc += 1;
Expand Down Expand Up @@ -4263,6 +4269,12 @@ void Init(int* argc,
V8::SetFlagsFromString(NODE_V8_OPTIONS, sizeof(NODE_V8_OPTIONS) - 1);
#endif

{
std::string text;
config_pending_deprecation =
SafeGetenv("NODE_PENDING_DEPRECATION", &text) && text[0] == '1';
}

// Allow for environment set preserving symlinks.
{
std::string text;
Expand Down
3 changes: 3 additions & 0 deletions src/node_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ void InitConfig(Local<Object> target,
if (config_preserve_symlinks)
READONLY_BOOLEAN_PROPERTY("preserveSymlinks");

if (config_pending_deprecation)
READONLY_BOOLEAN_PROPERTY("pendingDeprecation");

if (!config_warning_file.empty()) {
Local<String> name = OneByteString(env->isolate(), "warningFile");
Local<String> value = String::NewFromUtf8(env->isolate(),
Expand Down
4 changes: 4 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ extern bool config_preserve_symlinks;
// it to stderr.
extern std::string config_warning_file; // NOLINT(runtime/string)

// Set in node.cc by ParseArgs when --pending-deprecation or
// NODE_PENDING_DEPRECATION is used
extern bool config_pending_deprecation;

// Tells whether it is safe to call v8::Isolate::GetCurrent().
extern bool v8_initialized;

Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-buffer-nopendingdep-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Flags: --no-warnings --pending-deprecation
'use strict';

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

process.on('warning', common.mustNotCall('A warning should not be emitted'));

// With the --pending-deprecation flag, the deprecation warning for
// new Buffer() should not be emitted when Uint8Array methods are called.

Buffer.from('abc').map((i) => i);
Buffer.from('abc').filter((i) => i);
Buffer.from('abc').slice(1, 2);
15 changes: 15 additions & 0 deletions test/parallel/test-buffer-pending-deprecation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Flags: --pending-deprecation --no-warnings
'use strict';

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

const bufferWarning = 'The Buffer() and new Buffer() constructors are not ' +
'recommended for use due to security and usability ' +
'concerns. Please use the new Buffer.alloc(), ' +
'Buffer.allocUnsafe(), or Buffer.from() construction ' +
'methods instead.';

common.expectWarning('DeprecationWarning', bufferWarning);

new Buffer(10);
45 changes: 45 additions & 0 deletions test/parallel/test-pending-deprecation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

// Tests that --pending-deprecation and NODE_PENDING_DEPRECATION both
// set the process.binding('config').pendingDeprecation flag that is
// used to determine if pending deprecation messages should be shown.
// The test is performed by launching two child processes that run
// this same test script with different arguments. If those exit with
// code 0, then the test passes. If they don't, it fails.

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

const assert = require('assert');
const config = process.binding('config');
const fork = require('child_process').fork;

function message(name) {
return `${name} did not set the process.binding('config').` +
'pendingDeprecation flag.';
}

switch (process.argv[2]) {
case 'env':
case 'switch':
assert.strictEqual(config.pendingDeprecation, true);
break;
default:
// Verify that the flag is off by default.
assert.strictEqual(config.pendingDeprecation, undefined);

// Test the --pending-deprecation command line switch.
fork(__filename, ['switch'], {
execArgv: ['--pending-deprecation'],
silent: true
}).on('exit', common.mustCall((code) => {
assert.strictEqual(code, 0, message('--pending-deprecation'));
}));

// Test the NODE_PENDING_DEPRECATION environment var.
fork(__filename, ['env'], {
env: {NODE_PENDING_DEPRECATION: 1},
silent: true
}).on('exit', common.mustCall((code) => {
assert.strictEqual(code, 0, message('NODE_PENDING_DEPRECATION'));
}));
}