-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
buffer: runtime-deprecate Buffer constructor #7152
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,12 @@ | |
|
||
const binding = process.binding('buffer'); | ||
const { isArrayBuffer } = process.binding('util'); | ||
const { deprecate } = require('internal/util'); | ||
const bindingObj = {}; | ||
const internalUtil = require('internal/util'); | ||
|
||
class FastBuffer extends Uint8Array {} | ||
const FastBuffer = (() => class Buffer extends Uint8Array {})(); | ||
|
||
FastBuffer.prototype.constructor = Buffer; | ||
Buffer.prototype = FastBuffer.prototype; | ||
|
||
exports.Buffer = Buffer; | ||
|
@@ -54,17 +54,19 @@ function alignPool() { | |
} | ||
} | ||
|
||
const bufferDeprecationWarning = | ||
deprecate(() => {}, 'Buffer constructor is deprecated. ' + | ||
'Use Buffer.from, Buffer.allocUnsafe or Buffer.alloc instead.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer the order here to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just copied from the comment below, but I don't mind changing the order. |
||
|
||
/** | ||
* The Buffer() construtor is "soft deprecated" ... that is, it is deprecated | ||
* in the documentation and should not be used moving forward. Rather, | ||
* developers should use one of the three new factory APIs: Buffer.from(), | ||
* Buffer.allocUnsafe() or Buffer.alloc() based on their specific needs. There | ||
* is no hard deprecation because of the extent to which the Buffer constructor | ||
* is used in the ecosystem currently -- a hard deprecation would introduce too | ||
* much breakage at this time. It's not likely that the Buffer constructors | ||
* would ever actually be removed. | ||
* The Buffer() construtor is deprecated ... that is, it should not be used | ||
* moving forward. Rather, developers should use one of the three new factory | ||
* APIs: Buffer.from(), Buffer.allocUnsafe() or Buffer.alloc() based on their | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was actually thinking that this comment doesn't really serve any useful purpose anymore and can be removed. |
||
* specific needs. It's not likely that the Buffer constructors would ever | ||
* actually be removed. | ||
**/ | ||
function Buffer(arg, encodingOrOffset, length) { | ||
bufferDeprecationWarning(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we make this conditional, leaving it only for the cases in which There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could, but I don't think we should.
Again, hard-deprecation doesn't necessarily mean removing it later, but it at least allows us to consider it. And in any case, the Buffer constructor is already soft-deprecated, which itself is a reason for hard-deprecation (see the first point in OP). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, these are some valid points. I am actually quite sure there’s no reason to change the semantics of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @seishun addaleax@ec09d43 – Probably not the most performant way of doing anything, but it provides users with a subclassable If the goal is to make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. I don't see any immediate issues with this. However, the question is whether we want to maintain all this code just to support a deprecated feature that has an equivalent alternative. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From a purely technical viewpoint I completely agree with you, and pushing users towards @ChALkeR Could you try and grep for modules that currently use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I was wrong on this. All we'd need to do is something like: class Buffer extends Uint8Array {
constructor(string, encoding) {
const ab = binding.newArrayBufferFromString(string, encoding);
super(ab);
}
} @addaleax There have been a couple suggestions of exporting a Buffer class that can be inherited, but it always wraps back around that it should be able to extend Buffer directly. For cleanliness of API I tend to agree, even if that involves breaking an egg or two. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Exactly, which is why we should hard-deprecate first. In the 6 months that follow, we should be able to evaluate the feasibility of removing this functionality. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, good point… I’d still prefer this to be restricted to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm just back from vacation and suspect that missed lots of conversations, but generally I played with subclassing Buffer as was suggested in another thread using |
||
// Common case. | ||
if (typeof arg === 'number') { | ||
if (typeof encodingOrOffset === 'string') { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remind why this change was needed? Which code breaks without it?
#11808 and #11806 are missing this, would everything be fine there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ChALkeR it was necessary because the
FastBuffer.prototype.constructor = Buffer;
line was removed. It was removed to prevent Uint8Array methods (which call the constructor) from being affected by the deprecation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, here is the testcase:
Buffer.alloc(10).map(x => x + 1)
(just for future reference).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/cc @jasnell
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd been trying to remember where I had seen this :-)