Skip to content

Commit 7f18179

Browse files
committed
Use Buffer.alloc and Buffer.from instead of new Buffer
1 parent 7dcd5e0 commit 7f18179

File tree

7 files changed

+351
-16
lines changed

7 files changed

+351
-16
lines changed

Connection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function Connection(socket, parentOrOptions, callback) {
5555
this.protocol = undefined
5656
this.socket = socket
5757
this.readyState = this.CONNECTING
58-
this.buffer = new Buffer(0)
58+
this.buffer = Buffer.alloc(0)
5959
this.frameBuffer = null // string for text frames and InStream for binary frames
6060
this.outStream = null // current allocated OutStream object for sending binary frames
6161
this.key = null // the Sec-WebSocket-Key header
@@ -268,7 +268,7 @@ Connection.prototype.doRead = function () {
268268
*/
269269
Connection.prototype.startHandshake = function () {
270270
var str, i, key, headers, header
271-
key = new Buffer(16)
271+
key = Buffer.alloc(16)
272272
for (i = 0; i < 16; i++) {
273273
key[i] = Math.floor(Math.random() * 256)
274274
}

OutStream.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function OutStream(connection, minSize) {
1616
var that = this
1717
this.connection = connection
1818
this.minSize = minSize
19-
this.buffer = new Buffer(0)
19+
this.buffer = Buffer.alloc(0)
2020
this.hasSent = false // Indicates if any frame has been sent yet
2121
stream.Writable.call(this)
2222
this.on('finish', function () {
@@ -48,7 +48,7 @@ OutStream.prototype._write = function (chunk, encoding, callback) {
4848
frameBuffer = frame.createBinaryFrame(this.buffer, !this.connection.server, !this.hasSent, false)
4949
this.connection.socket.write(frameBuffer, encoding, callback)
5050
}
51-
this.buffer = new Buffer(0)
51+
this.buffer = Buffer.alloc(0)
5252
this.hasSent = true
5353
if (this.connection.readyState !== this.connection.OPEN) {
5454
callback()

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ var server = ws.createServer(function (conn) {
183183
console.log("New connection")
184184
conn.on("binary", function (inStream) {
185185
// Empty buffer for collecting binary data
186-
var data = new Buffer(0)
186+
var data = Buffer.alloc(0)
187187
// Read chunks of binary data and add to the buffer
188188
inStream.on("readable", function () {
189189
var newData = inStream.read()

frame.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
exports.createTextFrame = function (data, masked) {
1414
var payload, meta
1515

16-
payload = new Buffer(data)
16+
payload = Buffer.from(data)
1717
meta = generateMetaData(true, 1, masked === undefined ? false : masked, payload)
1818

1919
return Buffer.concat([meta, payload], meta.length + payload.length)
@@ -34,7 +34,7 @@ exports.createBinaryFrame = function (data, masked, first, fin) {
3434
first = first === undefined ? true : first
3535
masked = masked === undefined ? false : masked
3636
if (masked) {
37-
payload = new Buffer(data.length)
37+
payload = Buffer.alloc(data.length)
3838
data.copy(payload)
3939
} else {
4040
payload = data
@@ -56,10 +56,10 @@ exports.createCloseFrame = function (code, reason, masked) {
5656
var payload, meta
5757

5858
if (code !== undefined && code !== 1005) {
59-
payload = new Buffer(reason === undefined ? '--' : '--' + reason)
59+
payload = Buffer.from(reason === undefined ? '--' : '--' + reason)
6060
payload.writeUInt16BE(code, 0)
6161
} else {
62-
payload = new Buffer(0)
62+
payload = Buffer.alloc(0)
6363
}
6464
meta = generateMetaData(true, 8, masked === undefined ? false : masked, payload)
6565

@@ -76,7 +76,7 @@ exports.createCloseFrame = function (code, reason, masked) {
7676
exports.createPingFrame = function (data, masked) {
7777
var payload, meta
7878

79-
payload = new Buffer(data)
79+
payload = Buffer.from(data)
8080
meta = generateMetaData(true, 9, masked === undefined ? false : masked, payload)
8181

8282
return Buffer.concat([meta, payload], meta.length + payload.length)
@@ -92,7 +92,7 @@ exports.createPingFrame = function (data, masked) {
9292
exports.createPongFrame = function (data, masked) {
9393
var payload, meta
9494

95-
payload = new Buffer(data)
95+
payload = Buffer.from(data)
9696
meta = generateMetaData(true, 10, masked === undefined ? false : masked, payload)
9797

9898
return Buffer.concat([meta, payload], meta.length + payload.length)
@@ -114,7 +114,7 @@ function generateMetaData(fin, opcode, masked, payload) {
114114
len = payload.length
115115

116116
// Creates the buffer for meta-data
117-
meta = new Buffer(2 + (len < 126 ? 0 : (len < 65536 ? 2 : 8)) + (masked ? 4 : 0))
117+
meta = Buffer.alloc(2 + (len < 126 ? 0 : (len < 65536 ? 2 : 8)) + (masked ? 4 : 0))
118118

119119
// Sets fin and opcode
120120
meta[0] = (fin ? 128 : 0) + opcode
@@ -138,7 +138,7 @@ function generateMetaData(fin, opcode, masked, payload) {
138138

139139
// Set the mask-key
140140
if (masked) {
141-
mask = new Buffer(4)
141+
mask = Buffer.alloc(4)
142142
for (i = 0; i < 4; i++) {
143143
meta[start + i] = mask[i] = Math.floor(Math.random() * 256)
144144
}

0 commit comments

Comments
 (0)