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 deprecated new Buffer() #71

Merged
merged 1 commit into from
Apr 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ d4:dictd3:key36:This is a string within a dictionarye7:integeri12345e4:listli1ei
### Decoding

```javascript
var data = new Buffer( 'd6:string11:Hello World7:integeri12345e4:dictd3:key36:This is a string within a dictionarye4:listli1ei2ei3ei4e6:stringi5edeee' )
var data = Buffer.from('d6:string11:Hello World7:integeri12345e4:dictd3:key36:This is a string within a dictionarye4:listli1ei2ei3ei4e6:stringi5edeee')
var result = bencode.decode( data )
```

Expand Down
12 changes: 6 additions & 6 deletions lib/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ encode._floatConversionDetected = false

encode._encode = function (buffers, data) {
if (Buffer.isBuffer(data)) {
buffers.push(new Buffer(data.length + ':'))
buffers.push(Buffer.from(data.length + ':'))
buffers.push(data)
return
}
Expand All @@ -50,12 +50,12 @@ encode._encode = function (buffers, data) {
}
}

var buffE = new Buffer('e')
var buffD = new Buffer('d')
var buffL = new Buffer('l')
var buffE = Buffer.from('e')
var buffD = Buffer.from('d')
var buffL = Buffer.from('l')

encode.buffer = function (buffers, data) {
buffers.push(new Buffer(Buffer.byteLength(data) + ':' + data))
buffers.push(Buffer.from(Buffer.byteLength(data) + ':' + data))
}

encode.number = function (buffers, data) {
Expand All @@ -64,7 +64,7 @@ encode.number = function (buffers, data) {
var lo = (data % maxLo) << 0
var val = hi * maxLo + lo

buffers.push(new Buffer('i' + val + 'e'))
buffers.push(Buffer.from('i' + val + 'e'))

if (val !== data && !encode._floatConversionDetected) {
encode._floatConversionDetected = true
Expand Down
2 changes: 1 addition & 1 deletion test/BEP-0023.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('BEP 0023', function (t) {
incomplete: 3,
interval: 1800,
'min interval': 1800,
peers: new Buffer('2ebd1b641a1f51d54c0546cc342190401a1f626ee9c6c8d5cb0d92131a1fac4e689a3c6b180f3d5746db', 'hex')
peers: Buffer.from('2ebd1b641a1f51d54c0546cc342190401a1f626ee9c6c8d5cb0d92131a1fac4e689a3c6b180f3d5746db', 'hex')
})
})

Expand Down
16 changes: 8 additions & 8 deletions test/abstract-encoding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var test = require('tape').test
test('abstract encoding', function (t) {
t.test('encodingLength( value )', function (t) {
var input = { string: 'Hello World', integer: 12345 }
var output = new Buffer('d7:integeri12345e6:string11:Hello Worlde')
var output = Buffer.from('d7:integeri12345e6:string11:Hello Worlde')
t.plan(1)
t.equal(bencode.encodingLength(input), output.length)
})
Expand All @@ -17,33 +17,33 @@ test('abstract encoding', function (t) {

t.test('encode into an existing buffer', function (t) {
var input = { string: 'Hello World', integer: 12345 }
var output = new Buffer('d7:integeri12345e6:string11:Hello Worlde')
var target = new Buffer(output.length)
var output = Buffer.from('d7:integeri12345e6:string11:Hello Worlde')
var target = Buffer.allocUnsafe(output.length)
bencode.encode(input, target)
t.plan(1)
t.deepEqual(target, output)
})

t.test('encode into a buffer with an offset', function (t) {
var input = { string: 'Hello World', integer: 12345 }
var output = new Buffer('d7:integeri12345e6:string11:Hello Worlde')
var target = new Buffer(64 + output.length) // Pad with 64 bytes
var output = Buffer.from('d7:integeri12345e6:string11:Hello Worlde')
var target = Buffer.allocUnsafe(64 + output.length) // Pad with 64 bytes
var offset = 48
bencode.encode(input, target, offset)
t.plan(1)
t.deepEqual(target.slice(offset, offset + output.length), output)
})

t.test('decode.bytes', function (t) {
var input = new Buffer('d7:integeri12345e6:string11:Hello Worlde')
var input = Buffer.from('d7:integeri12345e6:string11:Hello Worlde')
bencode.decode(input)
t.plan(1)
t.equal(bencode.decode.bytes, input.length)
})

t.test('decode from an offset', function (t) {
var pad = '_______________________________'
var input = new Buffer(pad + 'd7:integeri12345e6:string11:Hello Worlde')
var input = Buffer.from(pad + 'd7:integeri12345e6:string11:Hello Worlde')
var output = bencode.decode(input, pad.length, 'utf8')
t.plan(1)
t.deepEqual(output, { string: 'Hello World', integer: 12345 })
Expand All @@ -52,7 +52,7 @@ test('abstract encoding', function (t) {
t.test('decode between an offset and end', function (t) {
var pad = '_______________________________'
var data = 'd7:integeri12345e6:string11:Hello Worlde'
var input = new Buffer(pad + data + pad)
var input = Buffer.from(pad + data + pad)
var output = bencode.decode(input, pad.length, pad.length + data.length, 'utf8')
t.plan(1)
t.deepEqual(output, { string: 'Hello World', integer: 12345 })
Expand Down
8 changes: 4 additions & 4 deletions test/data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
binKeyData: new Buffer('ZDU6ZmlsZXNkMzY6N++/vVXvv73go5rvv71L77+9z6fXlu+/ve+/ve+/ve+/vSR3ZDg6Y29tcGxldGVpMGUxMDpkb3dubG9hZGVkaTEwZTEwOmluY29tcGxldGVpMGVlZWU=', 'base64'),
binKeyName: new Buffer('N++/vVXvv73go5rvv71L77+9z6fXlu+/ve+/ve+/ve+/vSR3', 'base64'),
binResultData: new Buffer('NzrDtsKxc2Rm', 'base64'),
binStringData: new Buffer('w7bCsXNkZg==', 'base64')
binKeyData: Buffer.from('ZDU6ZmlsZXNkMzY6N++/vVXvv73go5rvv71L77+9z6fXlu+/ve+/ve+/ve+/vSR3ZDg6Y29tcGxldGVpMGUxMDpkb3dubG9hZGVkaTEwZTEwOmluY29tcGxldGVpMGVlZWU=', 'base64'),
binKeyName: Buffer.from('N++/vVXvv73go5rvv71L77+9z6fXlu+/ve+/ve+/ve+/vSR3', 'base64'),
binResultData: Buffer.from('NzrDtsKxc2Rm', 'base64'),
binStringData: Buffer.from('w7bCsXNkZg==', 'base64')
}
26 changes: 13 additions & 13 deletions test/decode.buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test('bencode#decode(x)', function (t) {

t.test('should be able to decode a string', function (t) {
t.plan(2)
t.deepEqual(bencode.decode('5:asdfe'), new Buffer('asdfe'))
t.deepEqual(bencode.decode('5:asdfe'), Buffer.from('asdfe'))
t.deepEqual(bencode.decode(data.binResultData.toString()), data.binStringData)
})

Expand All @@ -48,23 +48,23 @@ test('bencode#decode(x)', function (t) {
t.deepEqual(
bencode.decode('d3:cow3:moo4:spam4:eggse'),
{
cow: new Buffer('moo'),
spam: new Buffer('eggs')
cow: Buffer.from('moo'),
spam: Buffer.from('eggs')
}
)
t.deepEqual(
bencode.decode('d4:spaml1:a1:bee'),
{ spam: [
new Buffer('a'),
new Buffer('b')
Buffer.from('a'),
Buffer.from('b')
] }
)
t.deepEqual(
bencode.decode('d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee'),
{
'publisher': new Buffer('bob'),
'publisher-webpage': new Buffer('www.example.com'),
'publisher.location': new Buffer('home')
'publisher': Buffer.from('bob'),
'publisher-webpage': Buffer.from('www.example.com'),
'publisher.location': Buffer.from('home')
}
)
})
Expand All @@ -73,8 +73,8 @@ test('bencode#decode(x)', function (t) {
t.plan(1)
t.deepEqual(
bencode.decode('l4:spam4:eggse'),
[ new Buffer('spam'),
new Buffer('eggs') ]
[ Buffer.from('spam'),
Buffer.from('eggs') ]
)
})
t.test('should return the correct type', function (t) {
Expand All @@ -94,8 +94,8 @@ test('bencode#decode(x)', function (t) {
var result = bencode.encode(someData)
var dat = bencode.decode(result)
t.equal(dat.integer, 12345)
t.deepEqual(dat.string, new Buffer('Hello World'))
t.deepEqual(dat.dict.key, new Buffer('This is a string within a dictionary'))
t.deepEqual(dat.list, [1, 2, 3, 4, new Buffer('string'), 5, {}])
t.deepEqual(dat.string, Buffer.from('Hello World'))
t.deepEqual(dat.dict.key, Buffer.from('This is a string within a dictionary'))
t.deepEqual(dat.list, [1, 2, 3, 4, Buffer.from('string'), 5, {}])
})
})
6 changes: 3 additions & 3 deletions test/encode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ test('bencode#encode()', function (t) {
})
t.test('should be able to encode a buffer', function (t) {
t.plan(2)
t.equal(bencode.encode(new Buffer('asdf')).toString(), '4:asdf')
t.equal(bencode.encode(new Buffer(':asdf:')).toString(), '6::asdf:')
t.equal(bencode.encode(Buffer.from('asdf')).toString(), '4:asdf')
t.equal(bencode.encode(Buffer.from(':asdf:')).toString(), '6::asdf:')
})
t.test('should be able to encode an array', function (t) {
t.plan(2)
Expand All @@ -113,6 +113,6 @@ test('bencode#encode()', function (t) {
t.plan(3)
t.equal(bencode.encode({'a': 'bc'}).toString(), 'd1:a2:bce')
t.equal(bencode.encode({'a': '45', 'b': 45}).toString(), 'd1:a2:451:bi45ee')
t.equal(bencode.encode({'a': new Buffer('bc')}).toString(), 'd1:a2:bce')
t.equal(bencode.encode({'a': Buffer.from('bc')}).toString(), 'd1:a2:bce')
})
})
6 changes: 3 additions & 3 deletions test/null-values.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ var test = require('tape').test
test('Data with null values', function (t) {
t.test('should return an empty value when encoding either null or undefined', function (t) {
t.plan(2)
t.deepEqual(bencode.encode(null), new Buffer(0))
t.deepEqual(bencode.encode(undefined), new Buffer(0))
t.deepEqual(bencode.encode(null), Buffer.allocUnsafe(0))
t.deepEqual(bencode.encode(undefined), Buffer.allocUnsafe(0))
})

t.test('should return null when decoding an empty value', function (t) {
t.plan(2)
t.deepEqual(bencode.decode(new Buffer(0)), null)
t.deepEqual(bencode.decode(Buffer.allocUnsafe(0)), null)
t.deepEqual(bencode.decode(''), null)
})

Expand Down