Prefer modern Buffer allocation methods over the deprecated Buffer constructor.
✅ This rule is included in the node logical presets.
The new Buffer() constructor has been deprecated since Node.js 4 due to security and usability concerns.
Modern alternatives provide safer and more explicit ways to create buffers.
Use Buffer.from() for creating a Buffer from existing data (strings, arrays, or ArrayBuffers).
Use Buffer.alloc() for creating a Buffer of a specific size with zero-filled memory.
If array is an Array-like object (that is, one with a length property of
type number), it is treated as if it is an array, unless it is a Buffer or
a Uint8Array. This means all other TypedArray variants get treated as an
Array. To create a Buffer from the bytes backing a TypedArray, use
Buffer.copyBytesFrom().
A TypeError will be thrown if array is not an Array or another type
appropriate for Buffer.from() variants.
Buffer.from(array) and Buffer.from(string) may also use the internal
Buffer pool like Buffer.allocUnsafe() does.
Calling Buffer.alloc() can be measurably slower than the alternative Buffer.allocUnsafe() but ensures that the newly created Buffer instance
contents will never contain sensitive data from previous allocations, including
data that might not have been allocated for Buffers.
A TypeError will be thrown if size is not a number.
@since ― v5.10.0
@param ― size The desired length of the new Buffer.
@param ― fill A value to pre-fill the new Buffer with.
@param ― encoding If fill is a string, this is its encoding.
If you are working with legacy code that specifically requires the old Buffer constructor (which is highly unlikely in modern codebases), you might choose not to enable this rule.