Skip to content

Commit b9fe2f1

Browse files
committed
Added encoding parameter to #decode()
Changed the boolean `toString` parameter on `#decode()` to the `encoding` parameter, which lets you specify the encoding the bytestring buffer will be converted into.
1 parent f780452 commit b9fe2f1

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

bencode.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,17 @@ encode.list = function( data ) {
6363
* Decodes bencoded data.
6464
*
6565
* @param {Buffer} data
66-
* @param {Boolean} toString
66+
* @param {String} encoding
6767
* @return {Object|Array|Buffer|String|Number}
6868
*/
69-
function decode( data, toString ) {
69+
function decode( data, encoding ) {
7070

7171
if( !(this instanceof decode) ) {
72-
return new decode( data, toString )
72+
return new decode( data, encoding )
7373
}
7474

75-
this.stringify = !!toString
76-
77-
this.data = data
75+
this.encoding = encoding || null
76+
this.data = data
7877

7978
return this.next()
8079

@@ -88,7 +87,7 @@ decode.prototype = {
8887
case 0x64: return this.dictionary()
8988
case 0x6C: return this.list()
9089
case 0x69: return this.integer()
91-
default: return this.bytes()
90+
default: return this.bytes()
9291
}
9392

9493
},
@@ -166,8 +165,8 @@ decode.prototype = {
166165

167166
this.forward( sepl )
168167

169-
return this.stringify
170-
? bytes.toString()
168+
return this.encoding
169+
? bytes.toString( this.encoding )
171170
: bytes
172171

173172
}

0 commit comments

Comments
 (0)