✨ Decode bencode from .solidpkg
This commit is contained in:
parent
62a877617b
commit
3f79cb438c
2 changed files with 10 additions and 10 deletions
|
@ -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 };
|
||||
}
|
||||
|
|
|
@ -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<ArrayBuffer> {
|
||||
export default async function getSolidpkg(product: Product, from: number, to: number): Promise<any> {
|
||||
//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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue