🚑 Fix bencode parser
This commit is contained in:
parent
3bb970b76b
commit
c7bea67a1c
1 changed files with 4 additions and 1 deletions
|
@ -9,7 +9,7 @@ const Decoder = new TextDecoder('utf-8');
|
||||||
/** Takes a Bencoded-encoded file, parses it at the given starting position and returns a JSON object, or rejects on error. */
|
/** Takes a Bencoded-encoded file, parses it at the given starting position and returns a JSON object, or rejects on error. */
|
||||||
function bpParse(dv: DataView, posIn: number = 0): { obj: any, pos: number } {
|
function bpParse(dv: DataView, posIn: number = 0): { obj: any, pos: number } {
|
||||||
let pos = posIn;
|
let pos = posIn;
|
||||||
let obj;
|
let obj: any;
|
||||||
const header = dv.getUint8(pos); pos += 1;
|
const header = dv.getUint8(pos); pos += 1;
|
||||||
switch (header) {
|
switch (header) {
|
||||||
case 0x64: { //'d' - dictionary (key-value object)
|
case 0x64: { //'d' - dictionary (key-value object)
|
||||||
|
@ -18,6 +18,9 @@ function bpParse(dv: DataView, posIn: number = 0): { obj: any, pos: number } {
|
||||||
//read key
|
//read key
|
||||||
const out1 = bpParse(dv, pos);
|
const out1 = bpParse(dv, pos);
|
||||||
pos = out1.pos;
|
pos = out1.pos;
|
||||||
|
if (typeof out1.obj !== 'string') {
|
||||||
|
throw new Error(`Expected dictionary key to be string but it is "${typeof out1.obj}"`);
|
||||||
|
}
|
||||||
//read value
|
//read value
|
||||||
const out2 = bpParse(dv, pos);
|
const out2 = bpParse(dv, pos);
|
||||||
pos = out2.pos;
|
pos = out2.pos;
|
||||||
|
|
Loading…
Reference in a new issue