| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a16b570 commit d2d32ea
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,18 +22,20 @@ | |||
| 22 | 22 | 'use strict'; | |
| 23 | 23 | ||
| 24 | 24 | const binding = process.binding('buffer'); | |
| 25 | + const config = process.binding('config'); | ||
| 25 | 26 | const { compare: compare_, compareOffset } = binding; | |
| 26 | 27 | const { isAnyArrayBuffer, isUint8Array } = process.binding('util'); | |
| 27 | 28 | const bindingObj = {}; | |
| 28 | 29 | const internalUtil = require('internal/util'); | |
| 30 | + const pendingDeprecation = !!config.pendingDeprecation; | ||
| 29 | 31 | ||
| 30 | 32 | class FastBuffer extends Uint8Array { | |
| 31 | 33 | constructor(arg1, arg2, arg3) { | |
| 32 | 34 | super(arg1, arg2, arg3); | |
| 33 | 35 | } | |
| 34 | 36 | } | |
| 35 | - | ||
| 36 | 37 | FastBuffer.prototype.constructor = Buffer; | |
| 38 | + | ||
| 37 | 39 | Buffer.prototype = FastBuffer.prototype; | |
| 38 | 40 | ||
| 39 | 41 | exports.Buffer = Buffer; | |
@@ -83,6 +85,28 @@ function alignPool() { | |||
| 83 | 85 | } | |
| 84 | 86 | } | |
| 85 | 87 | ||
| 88 | + var bufferWarn = true; | ||
| 89 | + const bufferWarning = 'The Buffer() and new Buffer() constructors are not ' + | ||
| 90 | + 'recommended for use due to security and usability ' + | ||
| 91 | + 'concerns. Please use the new Buffer.alloc(), ' + | ||
| 92 | + 'Buffer.allocUnsafe(), or Buffer.from() construction ' + | ||
| 93 | + 'methods instead.'; | ||
| 94 | + | ||
| 95 | + function showFlaggedDeprecation() { | ||
| 96 | + if (bufferWarn) { | ||
| 97 | + // This is a *pending* deprecation warning. It is not emitted by | ||
| 98 | + // default unless the --pending-deprecation command-line flag is | ||
| 99 | + // used or the NODE_PENDING_DEPRECATION=1 envvar is set. | ||
| 100 | + process.emitWarning(bufferWarning, 'DeprecationWarning', 'DEP0005'); | ||
| 101 | + bufferWarn = false; | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + const doFlaggedDeprecation = | ||
| 106 | + pendingDeprecation ? | ||
| 107 | + showFlaggedDeprecation : | ||
| 108 | + function() {}; | ||
| 109 | + | ||
| 86 | 110 | /** | |
| 87 | 111 | * The Buffer() construtor is deprecated in documentation and should not be | |
| 88 | 112 | * used moving forward. Rather, developers should use one of the three new | |
@@ -94,6 +118,7 @@ function alignPool() { | |||
| 94 | 118 | * Deprecation Code: DEP0005 | |
| 95 | 119 | **/ | |
| 96 | 120 | function Buffer(arg, encodingOrOffset, length) { | |
| 121 | + doFlaggedDeprecation(); | ||
| 97 | 122 | // Common case. | |
| 98 | 123 | if (typeof arg === 'number') { | |
| 99 | 124 | if (typeof encodingOrOffset === 'string') { | |
@@ -106,6 +131,12 @@ function Buffer(arg, encodingOrOffset, length) { | |||
| 106 | 131 | return Buffer.from(arg, encodingOrOffset, length); | |
| 107 | 132 | } | |
| 108 | 133 | ||
| 134 | + Object.defineProperty(Buffer, Symbol.species, { | ||
| 135 | + enumerable: false, | ||
| 136 | + configurable: true, | ||
| 137 | + get() { return FastBuffer; } | ||
| 138 | + }); | ||
| 139 | + | ||
| 109 | 140 | /** | |
| 110 | 141 | * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError | |
| 111 | 142 | * if value is a number. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,14 @@ | |||
| 1 | + // Flags: --no-warnings --pending-deprecation | ||
| 2 | + 'use strict'; | ||
| 3 | + | ||
| 4 | + const common = require('../common'); | ||
| 5 | + const Buffer = require('buffer').Buffer; | ||
| 6 | + | ||
| 7 | + process.on('warning', common.mustNotCall('A warning should not be emitted')); | ||
| 8 | + | ||
| 9 | + // With the --pending-deprecation flag, the deprecation warning for | ||
| 10 | + // new Buffer() should not be emitted when Uint8Array methods are called. | ||
| 11 | + | ||
| 12 | + Buffer.from('abc').map((i) => i); | ||
| 13 | + Buffer.from('abc').filter((i) => i); | ||
| 14 | + Buffer.from('abc').slice(1, 2); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,15 @@ | |||
| 1 | + // Flags: --pending-deprecation --no-warnings | ||
| 2 | + 'use strict'; | ||
| 3 | + | ||
| 4 | + const common = require('../common'); | ||
| 5 | + const Buffer = require('buffer').Buffer; | ||
| 6 | + | ||
| 7 | + const bufferWarning = 'The Buffer() and new Buffer() constructors are not ' + | ||
| 8 | + 'recommended for use due to security and usability ' + | ||
| 9 | + 'concerns. Please use the new Buffer.alloc(), ' + | ||
| 10 | + 'Buffer.allocUnsafe(), or Buffer.from() construction ' + | ||
| 11 | + 'methods instead.'; | ||
| 12 | + | ||
| 13 | + common.expectWarning('DeprecationWarning', bufferWarning); | ||
| 14 | + | ||
| 15 | + new Buffer(10); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments