Skip to content

Commit e939ee7

Browse files
committed
Use static factories to create NodeJS buffers
Previously code always used constructor to create NodeJS buffers. Constructor is overloaded to create buffer from an array, another buffer, string or with specified size. It is deprecated in newer NodeJS versions in favor of static factories like `Buffer.alloc()` and `Buffer.from()`. First allocates a buffer of the specified size. Second creates buffer based on the given string, array or another buffer. This commit makes code use static factories instead of the constructor, when available. This makes construction of buffers work in older NodeJS environments that only have the constructor and new environments, that potentially only have static factories.
1 parent d9a6a2b commit e939ee7

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/v1/internal/buf.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ class CombinedBuffer extends BaseBuffer {
511511
*/
512512
class NodeBuffer extends BaseBuffer {
513513
constructor(arg) {
514-
let buffer = arg instanceof _node.Buffer ? arg : new _node.Buffer(arg);
514+
const buffer = arg instanceof _node.Buffer ? arg : newNodeJSBuffer(arg);
515515
super(buffer.length);
516516
this._buffer = buffer;
517517
}
@@ -559,6 +559,16 @@ class NodeBuffer extends BaseBuffer {
559559
}
560560
}
561561

562+
function newNodeJSBuffer(arg) {
563+
if (typeof arg === 'number' && typeof _node.Buffer.alloc === 'function') {
564+
// use static factory function present in newer NodeJS versions to allocate new buffer with specified size
565+
return _node.Buffer.alloc(arg);
566+
} else {
567+
// fallback to the old, potentially deprecated constructor
568+
return new _node.Buffer(arg);
569+
}
570+
}
571+
562572
// Use HeapBuffer by default, unless Buffer API is available, see below
563573
let _DefaultBuffer = HeapBuffer;
564574
try {

src/v1/internal/utf8.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,28 @@
2020
// This module defines a cross-platform UTF-8 encoder and decoder that works
2121
// with the Buffer API defined in buf.js
2222

23-
import {alloc, NodeBuffer, HeapBuffer, CombinedBuffer} from "./buf";
23+
import {alloc, CombinedBuffer, HeapBuffer, NodeBuffer} from './buf';
2424
import {StringDecoder} from 'string_decoder';
2525
import {newError} from './../error';
26+
2627
let platformObj = {};
2728

2829

2930
try {
3031
// This will throw an exception is 'buffer' is not available
3132
require.resolve("buffer");
32-
let decoder = new StringDecoder('utf8');
33-
let node = require("buffer");
33+
const decoder = new StringDecoder('utf8');
34+
const node = require('buffer');
35+
36+
// use static factory function present in newer NodeJS versions to create a buffer containing the given string
37+
// or fallback to the old, potentially deprecated constructor
38+
const newNodeJSBuffer = typeof node.Buffer.from === 'function'
39+
? str => node.Buffer.from(str, 'utf8')
40+
: str => new node.Buffer(str, 'utf8');
3441

3542
platformObj = {
3643
"encode": function (str) {
37-
return new NodeBuffer(new node.Buffer(str, "UTF-8"));
44+
return new NodeBuffer(newNodeJSBuffer(str));
3845
},
3946
"decode": function (buffer, length) {
4047
if (buffer instanceof NodeBuffer) {

0 commit comments

Comments
 (0)