Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
buffer: add pending deprecation warning
The pending deprecation warning is off by default.
Launch the node process with --pending-deprecation
or NODE_PENDING_DEPRECATION=1 env var set.
  • Loading branch information
jasnell committed Mar 12, 2017
commit e89e5568a8d33e89a2caf2cd5c898b1367af6b80
16 changes: 16 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
'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) {
Expand Down Expand Up @@ -94,6 +96,12 @@ 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.';
/**
* 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 @@ -106,6 +114,14 @@ function alignPool() {
**/
function Buffer(arg, encodingOrOffset, length) {
// Common case.
if (pendingDeprecation && 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;
}

if (typeof arg === 'number') {
if (typeof encodingOrOffset === 'string') {
throw new Error(
Expand Down
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);