From 3f79cb438c2909c4cb860b0d7b5dd5b09a0bcad3 Mon Sep 17 00:00:00 2001 From: C-3PO Date: Fri, 22 Jun 2018 16:19:21 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Decode=20bencode=20from=20.solidpkg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ssn/bencodeParser.ts | 14 ++++++-------- src/ssn/getSolidpkg.ts | 6 ++++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ssn/bencodeParser.ts b/src/ssn/bencodeParser.ts index 73d6c05..7173825 100644 --- a/src/ssn/bencodeParser.ts +++ b/src/ssn/bencodeParser.ts @@ -7,7 +7,7 @@ import { TextDecoder } from 'util'; 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. */ -export default function bpParse(dv: DataView, posIn: number, reject: (reason?: any) => void): { obj: any, pos: number } { +export default function bpParse(dv: DataView, posIn: number = 0): { obj: any, pos: number } { let pos = posIn; let obj; const header = dv.getUint8(pos++); @@ -16,10 +16,10 @@ export default function bpParse(dv: DataView, posIn: number, reject: (reason?: a obj = []; do { //read key - const out1 = bpParse(dv, pos, reject); + const out1 = bpParse(dv, pos); pos = out1.pos; //read value - const out2 = bpParse(dv, pos, reject); + const out2 = bpParse(dv, pos); pos = out2.pos; obj[out1.obj] = out2.obj; } while (dv.getUint8(pos) !== 0x65); //'e' - end @@ -30,7 +30,7 @@ export default function bpParse(dv: DataView, posIn: number, reject: (reason?: a obj = []; do { //read entry - const out = bpParse(dv, pos, reject); + const out = bpParse(dv, pos); pos = out.pos; obj.push(out.obj); } while (dv.getUint8(pos) !== 0x65); //'e' - end @@ -42,8 +42,7 @@ export default function bpParse(dv: DataView, posIn: number, reject: (reason?: a let curNumber = 0; while (curChar !== 0x65) { //'e' - end if (curChar < 0x30 || curChar > 0x39) { - reject('Unexpected int char during bencode parsing'); - return { obj: null, pos }; + throw new Error('Unexpected int char during bencode parsing'); } curNumber *= 10; curNumber += curChar - 0x30; @@ -68,8 +67,7 @@ export default function bpParse(dv: DataView, posIn: number, reject: (reason?: a break; } default: - reject('Unexpected leading char during bencode parsing.'); - return { obj: null, pos }; + throw new Error('Unexpected leading char during bencode parsing.'); } return { obj, pos }; } diff --git a/src/ssn/getSolidpkg.ts b/src/ssn/getSolidpkg.ts index d2870ae..5447761 100644 --- a/src/ssn/getSolidpkg.ts +++ b/src/ssn/getSolidpkg.ts @@ -1,10 +1,11 @@ import getUrlContents from '../getUrlContents'; import { Product } from '../interfaces/ISettings'; import verifyProductName from '../verifyProductName'; +import bpParse from './bencodeParser'; import extractFile from './extractFile'; import readSsnFile from './readSsnFile'; -export default async function getSolidpkg(product: Product, from: number, to: number): Promise { +export default async function getSolidpkg(product: Product, from: number, to: number): Promise { //Verify function arguments if (!verifyProductName(product)) { throw new Error(`"${product}" is not a valid product.`); @@ -36,6 +37,7 @@ export default async function getSolidpkg(product: Product, from: number, to: nu //Extract metafile.solid file const solidFile = await extractFile(firstFile, [new DataView(ssnFile)]); + const solidContents = bpParse(new DataView(solidFile)).obj; - return solidFile; + return solidContents; }