Skip to content
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

Replace buffer constructor with buffer-alloc and buffer-from #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion bench.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var multiplex = require('./')
var bufferAlloc = require('buffer-alloc');
var time = Date.now()

var plex = multiplex(function (stream, name) {
Expand All @@ -8,7 +9,7 @@ var plex = multiplex(function (stream, name) {
plex.pipe(plex)

var stream = plex.createStream()
var hello = new Buffer(16 * 1024 - 1)
var hello = bufferAlloc(16 * 1024 - 1)
var sent = 100000
var rcvd = 0

Expand Down
20 changes: 11 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ var EventEmitter = require('events').EventEmitter
var xtend = require('xtend')
var inherits = require('inherits')
var duplexify = require('duplexify')
var bufferAlloc = require('buffer-alloc')
var bufferFrom = require('buffer-from')

var SIGNAL_FLUSH = new Buffer([0])
var SIGNAL_FLUSH = bufferAlloc(1)

var empty = new Buffer(0)
var pool = new Buffer(10 * 1024)
var empty = bufferAlloc(0)
var pool = bufferAlloc(10 * 1024)
var used = 0

var Channel = function (name, plex, opts) {
Expand Down Expand Up @@ -67,7 +69,7 @@ Channel.prototype._destroy = function (err, local) {
if (local && this._opened) {
if (this._lazy && this.initiator) this._open()
try {
this._multiplex._send(this.channel << 3 | (this.initiator ? 6 : 5), err ? new Buffer(err.message) : null)
this._multiplex._send(this.channel << 3 | (this.initiator ? 6 : 5), err ? bufferFrom(err.message) : null)
} catch (e) {}
}
this._finalize()
Expand Down Expand Up @@ -104,7 +106,7 @@ Channel.prototype._read = function () {
Channel.prototype._open = function () {
var buf = null
if (Buffer.isBuffer(this.name)) buf = this.name
else if (this.name !== this.channel.toString()) buf = new Buffer(this.name)
else if (this.name !== this.channel.toString()) buf = bufferFrom(this.name)
this._lazy = false
this._multiplex._send(this.channel << 3 | 0, buf)
}
Expand Down Expand Up @@ -145,7 +147,7 @@ var Multiplex = function (opts, onchannel) {
this._channel = 0
this._missing = 0
this._message = null
this._buf = new Buffer(this.limit ? varint.encodingLength(this.limit) : 100)
this._buf = bufferAlloc(this.limit ? varint.encodingLength(this.limit) : 100)
this._ptr = 0
this._awaitChannelDrains = 0
this._onwritedrain = null
Expand Down Expand Up @@ -181,7 +183,7 @@ Multiplex.prototype.createSharedStream = function (name, opts) {

Multiplex.prototype._name = function (name) {
if (!this._binaryName) return name.toString()
return Buffer.isBuffer(name) ? name : new Buffer(name)
return Buffer.isBuffer(name) ? name : bufferFrom(name)
}

Multiplex.prototype._send = function (header, data) {
Expand All @@ -197,7 +199,7 @@ Multiplex.prototype._send = function (header, data) {
drained = this.push(pool.slice(oldUsed, used))

if (pool.length - used < 100) {
pool = new Buffer(10 * 1024)
pool = bufferAlloc(10 * 1024)
used = 0
}

Expand Down Expand Up @@ -261,7 +263,7 @@ Multiplex.prototype._writeMessage = function (data, offset) {
this._push(data.slice(offset, data.length))
return data.length
}
this._message = new Buffer(missing)
this._message = bufferAlloc(missing)
}

data.copy(this._message, this._ptr, offset, offset + missing)
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"author": "max ogden",
"license": "BSD 2-Clause",
"dependencies": {
"buffer-alloc": "^1.1.0",
"buffer-from": "^1.0.0",
"duplexify": "^3.4.2",
"inherits": "^2.0.1",
"readable-stream": "^2.0.2",
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ var plex2 = multiplex(function onStream(stream, id) {

plex1.pipe(plex2)

stream1.write(new Buffer('stream one!'))
stream2.write(new Buffer('stream two!'))
stream1.write(Buffer.from('stream one!'))
stream2.write(Buffer.from('stream two!'))
```

### contributing
Expand Down
28 changes: 15 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var through = require('through2')
var multiplex = require('./')
var net = require('net')
var chunky = require('chunky')
var bufferFrom = require('buffer-from')
var bufferAlloc = require('buffer-alloc')

test('one way piping work with 2 sub-streams', function (t) {
var plex1 = multiplex()
Expand All @@ -16,8 +18,8 @@ test('one way piping work with 2 sub-streams', function (t) {

plex1.pipe(plex2)

stream1.write(new Buffer('hello'))
stream2.write(new Buffer('world'))
stream1.write(bufferFrom('hello'))
stream2.write(bufferFrom('world'))
stream1.end()
stream2.end()

Expand All @@ -42,7 +44,7 @@ test('two way piping works with 2 sub-streams', function (t) {

var plex2 = multiplex(function onStream (stream, id) {
var uppercaser = through(function (chunk, e, done) {
this.push(new Buffer(chunk.toString().toUpperCase()))
this.push(bufferFrom(chunk.toString().toUpperCase()))
this.end()
done()
})
Expand All @@ -57,8 +59,8 @@ test('two way piping works with 2 sub-streams', function (t) {
stream1.pipe(collect())
stream2.pipe(collect())

stream1.write(new Buffer('hello'))
stream2.write(new Buffer('world'))
stream1.write(bufferFrom('hello'))
stream2.write(bufferFrom('world'))

var pending = 2
var results = []
Expand Down Expand Up @@ -89,7 +91,7 @@ test('stream id should be exposed as stream.name', function (t) {

plex1.pipe(plex2)

stream1.write(new Buffer('hello'))
stream1.write(bufferFrom('hello'))
stream1.end()
})

Expand All @@ -106,7 +108,7 @@ test('stream id can be a long string', function (t) {

plex1.pipe(plex2)

stream1.write(new Buffer('hello'))
stream1.write(bufferFrom('hello'))
stream1.end()
})

Expand All @@ -123,7 +125,7 @@ test('destroy', function (t) {

plex1.pipe(plex2)

stream1.write(new Buffer('hello'))
stream1.write(bufferFrom('hello'))
stream1.destroy(new Error('0 had an error'))
})

Expand Down Expand Up @@ -152,7 +154,7 @@ test('overflow', function (t) {
})

plex1.pipe(plex2).pipe(plex1)
plex1.createStream().write(new Buffer(11))
plex1.createStream().write(bufferAlloc(11))
})

test('2 buffers packed into 1 chunk', function (t) {
Expand Down Expand Up @@ -200,8 +202,8 @@ test('chunks', function (t) {
next()
})).pipe(plex2)

stream1.write(new Buffer('hello'))
stream2.write(new Buffer('world'))
stream1.write(bufferFrom('hello'))
stream2.write(bufferFrom('world'))
stream1.end()
stream2.end()
})()
Expand Down Expand Up @@ -255,7 +257,7 @@ test('quick message', function (t) {
setTimeout(function () {
var stream = plex2.createStream()
stream.on('data', function (data) {
t.same(data, new Buffer('hello world'))
t.same(data, bufferFrom('hello world'))
t.end()
})
}, 100)
Expand All @@ -276,7 +278,7 @@ test('if onstream is not passed, stream is emitted', function (t) {

var stream = plex1.createStream()
stream.on('data', function (data) {
t.same(data, new Buffer('hello world'))
t.same(data, bufferFrom('hello world'))
stream.end()
t.end()
})
Expand Down