✨ 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');
|
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. */
|
||||||
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 pos = posIn;
|
||||||
let obj;
|
let obj;
|
||||||
const header = dv.getUint8(pos++);
|
const header = dv.getUint8(pos++);
|
||||||
|
@ -16,10 +16,10 @@ export default function bpParse(dv: DataView, posIn: number, reject: (reason?: a
|
||||||
obj = [];
|
obj = [];
|
||||||
do {
|
do {
|
||||||
//read key
|
//read key
|
||||||
const out1 = bpParse(dv, pos, reject);
|
const out1 = bpParse(dv, pos);
|
||||||
pos = out1.pos;
|
pos = out1.pos;
|
||||||
//read value
|
//read value
|
||||||
const out2 = bpParse(dv, pos, reject);
|
const out2 = bpParse(dv, pos);
|
||||||
pos = out2.pos;
|
pos = out2.pos;
|
||||||
obj[out1.obj] = out2.obj;
|
obj[out1.obj] = out2.obj;
|
||||||
} while (dv.getUint8(pos) !== 0x65); //'e' - end
|
} while (dv.getUint8(pos) !== 0x65); //'e' - end
|
||||||
|
@ -30,7 +30,7 @@ export default function bpParse(dv: DataView, posIn: number, reject: (reason?: a
|
||||||
obj = [];
|
obj = [];
|
||||||
do {
|
do {
|
||||||
//read entry
|
//read entry
|
||||||
const out = bpParse(dv, pos, reject);
|
const out = bpParse(dv, pos);
|
||||||
pos = out.pos;
|
pos = out.pos;
|
||||||
obj.push(out.obj);
|
obj.push(out.obj);
|
||||||
} while (dv.getUint8(pos) !== 0x65); //'e' - end
|
} 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;
|
let curNumber = 0;
|
||||||
while (curChar !== 0x65) { //'e' - end
|
while (curChar !== 0x65) { //'e' - end
|
||||||
if (curChar < 0x30 || curChar > 0x39) {
|
if (curChar < 0x30 || curChar > 0x39) {
|
||||||
reject('Unexpected int char during bencode parsing');
|
throw new Error('Unexpected int char during bencode parsing');
|
||||||
return { obj: null, pos };
|
|
||||||
}
|
}
|
||||||
curNumber *= 10;
|
curNumber *= 10;
|
||||||
curNumber += curChar - 0x30;
|
curNumber += curChar - 0x30;
|
||||||
|
@ -68,8 +67,7 @@ export default function bpParse(dv: DataView, posIn: number, reject: (reason?: a
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
reject('Unexpected leading char during bencode parsing.');
|
throw new Error('Unexpected leading char during bencode parsing.');
|
||||||
return { obj: null, pos };
|
|
||||||
}
|
}
|
||||||
return { obj, pos };
|
return { obj, pos };
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import getUrlContents from '../getUrlContents';
|
import getUrlContents from '../getUrlContents';
|
||||||
import { Product } from '../interfaces/ISettings';
|
import { Product } from '../interfaces/ISettings';
|
||||||
import verifyProductName from '../verifyProductName';
|
import verifyProductName from '../verifyProductName';
|
||||||
|
import bpParse from './bencodeParser';
|
||||||
import extractFile from './extractFile';
|
import extractFile from './extractFile';
|
||||||
import readSsnFile from './readSsnFile';
|
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
|
//Verify function arguments
|
||||||
if (!verifyProductName(product)) {
|
if (!verifyProductName(product)) {
|
||||||
throw new Error(`"${product}" is not a valid 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
|
//Extract metafile.solid file
|
||||||
const solidFile = await extractFile(firstFile, [new DataView(ssnFile)]);
|
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