From 2dae5f6c586d29d6128d8e581609c9fec59e5e29 Mon Sep 17 00:00:00 2001 From: C-3PO Date: Fri, 22 Jun 2018 16:01:29 +0200 Subject: [PATCH] =?UTF-8?q?=EF=B8=8F=20Use=20ArrayBuffer=20instead=20of=20?= =?UTF-8?q?Buffer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/getUrlContents.ts | 4 ++-- src/installPatch.ts | 6 +++--- src/ssn/getPatchmanifest.ts | 2 +- src/ssn/getSolidpkg.ts | 2 +- src/ssn/readSsnFile.ts | 11 +++++------ 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/getUrlContents.ts b/src/getUrlContents.ts index 874ae9d..6715394 100644 --- a/src/getUrlContents.ts +++ b/src/getUrlContents.ts @@ -1,6 +1,6 @@ import * as http from 'http'; -export default function getUrlContents({ host, path }: {host: string, path: string}): Promise { +export default function getUrlContents({ host, path }: {host: string, path: string}): Promise { return new Promise((resolve, reject) => { const request = http.request({ family: 4, @@ -23,7 +23,7 @@ export default function getUrlContents({ host, path }: {host: string, path: stri return reject(`Expected length ${headerLength} but received ${totalLength}`); } const fileContents = Buffer.concat(chunkList, totalLength); - resolve(fileContents); + resolve(fileContents.buffer as ArrayBuffer); }); }); diff --git a/src/installPatch.ts b/src/installPatch.ts index 1dfb1ac..d540e71 100644 --- a/src/installPatch.ts +++ b/src/installPatch.ts @@ -10,19 +10,19 @@ const Decoder = new TextDecoder('utf-8'); //----- PATCHMANIFEST ----- //.patchmanifest files contain a single XML file called "manifest.xml" const patchmanifestBuffer = await getPatchmanifest('assets_swtor_de_de'); - console.log(patchmanifestBuffer.length, patchmanifestBuffer); + console.log(patchmanifestBuffer.byteLength, patchmanifestBuffer); const patchmanifestFiles = readSsnFile(patchmanifestBuffer); 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); console.log(patchmanifestXml); //----- SOLIDPKG ----- //.solidpkg files contain a single Bencode file called "metafile.solid" const solidpkgBuffer = await getSolidpkg('assets_swtor_de_de', -1, 0); - console.log(solidpkgBuffer.length, solidpkgBuffer); + console.log(solidpkgBuffer.byteLength, solidpkgBuffer); const solidPkgFiles = readSsnFile(solidpkgBuffer); console.log(solidPkgFiles); diff --git a/src/ssn/getPatchmanifest.ts b/src/ssn/getPatchmanifest.ts index c3276a2..0635ee8 100644 --- a/src/ssn/getPatchmanifest.ts +++ b/src/ssn/getPatchmanifest.ts @@ -2,7 +2,7 @@ import getUrlContents from '../getUrlContents'; import { Product } from '../interfaces/ISettings'; import verifyProductName from '../verifyProductName'; -export default function getPatchmanifest(product: Product): Promise { +export default function getPatchmanifest(product: Product): Promise { //Verify function arguments if (!verifyProductName(product)) { throw new Error(`"${product}" is not a valid product.`); diff --git a/src/ssn/getSolidpkg.ts b/src/ssn/getSolidpkg.ts index 5dafc1b..7253b1c 100644 --- a/src/ssn/getSolidpkg.ts +++ b/src/ssn/getSolidpkg.ts @@ -2,7 +2,7 @@ import getUrlContents from '../getUrlContents'; import { Product } from '../interfaces/ISettings'; import verifyProductName from '../verifyProductName'; -export default function getSolidpkg(product: Product, from: number, to: number): Promise { +export default function getSolidpkg(product: Product, from: number, to: number): Promise { //Verify function arguments if (!verifyProductName(product)) { throw new Error(`"${product}" is not a valid product.`); diff --git a/src/ssn/readSsnFile.ts b/src/ssn/readSsnFile.ts index 7ab35d9..7d36aeb 100644 --- a/src/ssn/readSsnFile.ts +++ b/src/ssn/readSsnFile.ts @@ -14,16 +14,15 @@ const COMPRESSION_DEFLATE = 8; const Decoder = new TextDecoder('utf-8'); -export default function readSsnFile(buffer: Buffer): ISsnFileEntry[] { - const arrayBuffer = buffer.buffer; - const dv = new DataView(arrayBuffer); +export default function readSsnFile(buffer: ArrayBuffer): ISsnFileEntry[] { + const dv = new DataView(buffer); const fileEntries: ISsnFileEntry[] = []; //--------------- READ END OF CENTRAL DIR --------------- //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 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 */ const relOffset = dv.getUint32(pos, true); pos += 4; /** 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 const extraFieldEnd = pos + extraFieldLength; @@ -122,7 +121,7 @@ export default function readSsnFile(buffer: Buffer): ISsnFileEntry[] { if (fieldLength > 120) { 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; } case 0x80AE: //diff type