Comparing version 0.0.3 to 0.0.4
@@ -95,3 +95,3 @@ 'use strict'; | ||
if (!firstColonIndex) { | ||
throw new Error(`Error while decoding ${data.toString()}. Missing colon.`); | ||
throw new Error(`Error while decoding ${data.toString(encoding)}. Missing colon.`); | ||
} | ||
@@ -123,3 +123,3 @@ | ||
function decodeList(data: Buffer): DecodingResult { | ||
function decodeList(data: Buffer, encoding?: string): DecodingResult { | ||
// TODO: Check for 'e' symbol in the end. | ||
@@ -138,3 +138,3 @@ let result = []; | ||
if (encodesDigit(firstByte)) { | ||
({value, rest} = decodeString(rest)); | ||
({value, rest} = decodeString(rest, encoding)); | ||
result.push(value); | ||
@@ -144,3 +144,3 @@ continue; | ||
if (firstByte === Delimeters.l) { | ||
({value, rest} = decodeList(rest)); | ||
({value, rest} = decodeList(rest, encoding)); | ||
result.push(value); | ||
@@ -150,3 +150,3 @@ continue; | ||
if (firstByte === Delimeters.d) { | ||
({value, rest} = decodeDict(rest)); | ||
({value, rest} = decodeDict(rest, encoding)); | ||
result.push(value); | ||
@@ -159,3 +159,3 @@ continue; | ||
} | ||
throw new Error(`Expected d, i, l or digit, got ${rest.toString()}`) | ||
throw new Error(`Expected d, i, l or digit, got ${rest.toString(encoding)}`) | ||
} | ||
@@ -165,3 +165,3 @@ return {value: result, rest: rest}; | ||
function decodeDict(data: Buffer): DecodingResult { | ||
function decodeDict(data: Buffer, encoding?: string): DecodingResult { | ||
let result = {}; | ||
@@ -178,4 +178,4 @@ let rest = data.slice(1); // d... | ||
} | ||
({value: key, rest} = decodeString(rest)); | ||
({value, rest} = _decode(rest)); | ||
({value: key, rest} = decodeString(rest, encoding)); | ||
({value, rest} = _decode(rest, encoding)); | ||
result[key] = value; | ||
@@ -191,3 +191,3 @@ } | ||
function _decode(data: Buffer): any { | ||
function _decode(data: Buffer, encoding?: string): any { | ||
let value = null; | ||
@@ -200,9 +200,9 @@ let rest = null; | ||
else if (encodesDigit(firstByte)) { | ||
return decodeString(data); | ||
return decodeString(data, encoding); | ||
} | ||
else if (firstByte === Delimeters.l) { | ||
return decodeList(data); | ||
return decodeList(data, encoding); | ||
} | ||
else if (firstByte === Delimeters.d) { | ||
return decodeDict(data); | ||
return decodeDict(data, encoding); | ||
} | ||
@@ -224,3 +224,3 @@ else { | ||
let rest: Buffer | ||
({value, rest} = _decode(data)); | ||
({value, rest} = _decode(data, encoding)); | ||
if (rest.length) { | ||
@@ -227,0 +227,0 @@ throw new Error(`Unexpected continuation: "${rest.toString()}"`) |
@@ -79,3 +79,3 @@ 'use strict'; | ||
if (!firstColonIndex) { | ||
throw new Error("Error while decoding " + data.toString() + ". Missing colon."); | ||
throw new Error("Error while decoding " + data.toString(encoding) + ". Missing colon."); | ||
} | ||
@@ -102,3 +102,3 @@ var expectedLength = parseInt(data.slice(0, firstColonIndex).toString()); | ||
} | ||
function decodeList(data) { | ||
function decodeList(data, encoding) { | ||
// TODO: Check for 'e' symbol in the end. | ||
@@ -116,3 +116,3 @@ var result = []; | ||
if (encodesDigit(firstByte)) { | ||
(_b = decodeString(rest), value = _b.value, rest = _b.rest, _b); | ||
(_b = decodeString(rest, encoding), value = _b.value, rest = _b.rest, _b); | ||
result.push(value); | ||
@@ -122,3 +122,3 @@ continue; | ||
if (firstByte === Delimeters.l) { | ||
(_c = decodeList(rest), value = _c.value, rest = _c.rest, _c); | ||
(_c = decodeList(rest, encoding), value = _c.value, rest = _c.rest, _c); | ||
result.push(value); | ||
@@ -128,3 +128,3 @@ continue; | ||
if (firstByte === Delimeters.d) { | ||
(_d = decodeDict(rest), value = _d.value, rest = _d.rest, _d); | ||
(_d = decodeDict(rest, encoding), value = _d.value, rest = _d.rest, _d); | ||
result.push(value); | ||
@@ -137,3 +137,3 @@ continue; | ||
} | ||
throw new Error("Expected d, i, l or digit, got " + rest.toString()); | ||
throw new Error("Expected d, i, l or digit, got " + rest.toString(encoding)); | ||
} | ||
@@ -143,3 +143,3 @@ return { value: result, rest: rest }; | ||
} | ||
function decodeDict(data) { | ||
function decodeDict(data, encoding) { | ||
var result = {}; | ||
@@ -155,4 +155,4 @@ var rest = data.slice(1); // d... | ||
} | ||
(_a = decodeString(rest), key = _a.value, rest = _a.rest, _a); | ||
(_b = _decode(rest), value = _b.value, rest = _b.rest, _b); | ||
(_a = decodeString(rest, encoding), key = _a.value, rest = _a.rest, _a); | ||
(_b = _decode(rest, encoding), value = _b.value, rest = _b.rest, _b); | ||
result[key] = value; | ||
@@ -166,3 +166,3 @@ } | ||
} | ||
function _decode(data) { | ||
function _decode(data, encoding) { | ||
var value = null; | ||
@@ -175,9 +175,9 @@ var rest = null; | ||
else if (encodesDigit(firstByte)) { | ||
return decodeString(data); | ||
return decodeString(data, encoding); | ||
} | ||
else if (firstByte === Delimeters.l) { | ||
return decodeList(data); | ||
return decodeList(data, encoding); | ||
} | ||
else if (firstByte === Delimeters.d) { | ||
return decodeDict(data); | ||
return decodeDict(data, encoding); | ||
} | ||
@@ -198,3 +198,3 @@ else { | ||
var rest; | ||
(_a = _decode(data), value = _a.value, rest = _a.rest, _a); | ||
(_a = _decode(data, encoding), value = _a.value, rest = _a.rest, _a); | ||
if (rest.length) { | ||
@@ -201,0 +201,0 @@ throw new Error("Unexpected continuation: \"" + rest.toString() + "\""); |
@@ -75,2 +75,5 @@ 'use strict'; | ||
}); | ||
it('should return "привет" when decoding "12:привет"', function () { | ||
assert.equal("привет", bencoder.decode(buffer_1.Buffer.from("12:привет"), 'utf8')); | ||
}); | ||
}); | ||
@@ -77,0 +80,0 @@ describe('#decodeInteger()', function () { |
{ | ||
"name": "bencoder", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "bencode encoder/decoder written in TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "./out/bencoder.js", |
@@ -39,3 +39,14 @@ # Bencode | ||
// A few words about encodings | ||
// By default bencoder assumes everything is UTF-8. For example | ||
var x4 = Bencoder.encode('привет') | ||
Bencoder.decode(x4) // which is the same as Bencoder.decode(x4, 'utf8') | ||
'привет' | ||
// Or you can specify another encoding, if you want to | ||
Bencoder.decode(x4, 'ascii') | ||
'P?Q\u0000P8P2P5Q\u0002' | ||
``` | ||
@@ -82,2 +82,5 @@ 'use strict'; | ||
}); | ||
it('should return "привет" when decoding "12:привет"', () => { | ||
assert.equal("привет", bencoder.decode(Buffer.from("12:привет"), 'utf8')); | ||
}) | ||
}) | ||
@@ -84,0 +87,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
446275
23
3251
52