diff --git a/src/ssn/bencodeParser.ts b/src/ssn/bencodeParser.ts index 223377c..37aefc1 100644 --- a/src/ssn/bencodeParser.ts +++ b/src/ssn/bencodeParser.ts @@ -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. */ function bpParse(dv: DataView, posIn: number = 0): { obj: any, pos: number } { let pos = posIn; - let obj; + let obj: any; const header = dv.getUint8(pos); pos += 1; switch (header) { case 0x64: { //'d' - dictionary (key-value object) @@ -18,6 +18,9 @@ function bpParse(dv: DataView, posIn: number = 0): { obj: any, pos: number } { //read key const out1 = bpParse(dv, 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 const out2 = bpParse(dv, pos); pos = out2.pos;