️ Use ArrayBuffer instead of Buffer

This commit is contained in:
C-3PO 2018-06-22 16:01:29 +02:00
parent 46b0cbbf70
commit 2dae5f6c58
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
5 changed files with 12 additions and 13 deletions

View file

@ -1,6 +1,6 @@
import * as http from 'http'; import * as http from 'http';
export default function getUrlContents({ host, path }: {host: string, path: string}): Promise<Buffer> { export default function getUrlContents({ host, path }: {host: string, path: string}): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const request = http.request({ const request = http.request({
family: 4, family: 4,
@ -23,7 +23,7 @@ export default function getUrlContents({ host, path }: {host: string, path: stri
return reject(`Expected length ${headerLength} but received ${totalLength}`); return reject(`Expected length ${headerLength} but received ${totalLength}`);
} }
const fileContents = Buffer.concat(chunkList, totalLength); const fileContents = Buffer.concat(chunkList, totalLength);
resolve(fileContents); resolve(fileContents.buffer as ArrayBuffer);
}); });
}); });

View file

@ -10,19 +10,19 @@ const Decoder = new TextDecoder('utf-8');
//----- PATCHMANIFEST ----- //----- PATCHMANIFEST -----
//.patchmanifest files contain a single XML file called "manifest.xml" //.patchmanifest files contain a single XML file called "manifest.xml"
const patchmanifestBuffer = await getPatchmanifest('assets_swtor_de_de'); const patchmanifestBuffer = await getPatchmanifest('assets_swtor_de_de');
console.log(patchmanifestBuffer.length, patchmanifestBuffer); console.log(patchmanifestBuffer.byteLength, patchmanifestBuffer);
const patchmanifestFiles = readSsnFile(patchmanifestBuffer); const patchmanifestFiles = readSsnFile(patchmanifestBuffer);
console.log(patchmanifestFiles); console.log(patchmanifestFiles);
const patchmanifestFile = extractFile(patchmanifestFiles[0], [new DataView(patchmanifestBuffer.buffer)]); const patchmanifestFile = extractFile(patchmanifestFiles[0], [new DataView(patchmanifestBuffer)]);
const patchmanifestXml = Decoder.decode(patchmanifestFile); const patchmanifestXml = Decoder.decode(patchmanifestFile);
console.log(patchmanifestXml); console.log(patchmanifestXml);
//----- SOLIDPKG ----- //----- SOLIDPKG -----
//.solidpkg files contain a single Bencode file called "metafile.solid" //.solidpkg files contain a single Bencode file called "metafile.solid"
const solidpkgBuffer = await getSolidpkg('assets_swtor_de_de', -1, 0); const solidpkgBuffer = await getSolidpkg('assets_swtor_de_de', -1, 0);
console.log(solidpkgBuffer.length, solidpkgBuffer); console.log(solidpkgBuffer.byteLength, solidpkgBuffer);
const solidPkgFiles = readSsnFile(solidpkgBuffer); const solidPkgFiles = readSsnFile(solidpkgBuffer);
console.log(solidPkgFiles); console.log(solidPkgFiles);

View file

@ -2,7 +2,7 @@ import getUrlContents from '../getUrlContents';
import { Product } from '../interfaces/ISettings'; import { Product } from '../interfaces/ISettings';
import verifyProductName from '../verifyProductName'; import verifyProductName from '../verifyProductName';
export default function getPatchmanifest(product: Product): Promise<Buffer> { export default function getPatchmanifest(product: Product): Promise<ArrayBuffer> {
//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.`);

View file

@ -2,7 +2,7 @@ import getUrlContents from '../getUrlContents';
import { Product } from '../interfaces/ISettings'; import { Product } from '../interfaces/ISettings';
import verifyProductName from '../verifyProductName'; import verifyProductName from '../verifyProductName';
export default function getSolidpkg(product: Product, from: number, to: number): Promise<Buffer> { export default function getSolidpkg(product: Product, from: number, to: number): Promise<ArrayBuffer> {
//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.`);

View file

@ -14,16 +14,15 @@ const COMPRESSION_DEFLATE = 8;
const Decoder = new TextDecoder('utf-8'); const Decoder = new TextDecoder('utf-8');
export default function readSsnFile(buffer: Buffer): ISsnFileEntry[] { export default function readSsnFile(buffer: ArrayBuffer): ISsnFileEntry[] {
const arrayBuffer = buffer.buffer; const dv = new DataView(buffer);
const dv = new DataView(arrayBuffer);
const fileEntries: ISsnFileEntry[] = []; const fileEntries: ISsnFileEntry[] = [];
//--------------- READ END OF CENTRAL DIR --------------- //--------------- READ END OF CENTRAL DIR ---------------
//Go to end of file //Go to end of file
let pos = buffer.length - 22; //end of central dir is at least 22 bytes long let pos = buffer.byteLength - 22; //end of central dir is at least 22 bytes long
//Find end of central dir //Find end of central dir
while (pos >= 0 && dv.getUint32(pos, true) !== SIGNATURE_END_OF_CENTRAL_DIR) { while (pos >= 0 && dv.getUint32(pos, true) !== SIGNATURE_END_OF_CENTRAL_DIR) {
@ -106,7 +105,7 @@ export default function readSsnFile(buffer: Buffer): ISsnFileEntry[] {
/** relative offset of local header */ /** relative offset of local header */
const relOffset = dv.getUint32(pos, true); pos += 4; const relOffset = dv.getUint32(pos, true); pos += 4;
/** file name (variable size) */ /** file name (variable size) */
const fileName = Decoder.decode(new DataView(arrayBuffer, pos, fileNameLength)); pos += fileNameLength; const fileName = Decoder.decode(new DataView(buffer, pos, fileNameLength)); pos += fileNameLength;
//read password from extra field //read password from extra field
const extraFieldEnd = pos + extraFieldLength; const extraFieldEnd = pos + extraFieldLength;
@ -122,7 +121,7 @@ export default function readSsnFile(buffer: Buffer): ISsnFileEntry[] {
if (fieldLength > 120) { if (fieldLength > 120) {
throw new Error(`Password is too long, it should be 120 characters at most but it is ${fieldLength} characters long.`); throw new Error(`Password is too long, it should be 120 characters at most but it is ${fieldLength} characters long.`);
} }
encodedPassword = new Uint8Array(arrayBuffer, pos, fieldLength); encodedPassword = new Uint8Array(buffer, pos, fieldLength);
break; break;
} }
case 0x80AE: //diff type case 0x80AE: //diff type