From 1a37702b241779a20bf6862dec344d8867874ad8 Mon Sep 17 00:00:00 2001 From: C-3PO Date: Thu, 21 Jun 2018 21:52:56 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Extract=20solidpkg=20from?= =?UTF-8?q?=20general=20file=20download?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/getSolidpkg.ts | 22 +++++++++++++++++++ src/{getFileContents.ts => getUrlContents.ts} | 14 ++---------- src/installPatch.ts | 4 ++-- 3 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 src/getSolidpkg.ts rename src/{getFileContents.ts => getUrlContents.ts} (66%) diff --git a/src/getSolidpkg.ts b/src/getSolidpkg.ts new file mode 100644 index 0000000..f1f1045 --- /dev/null +++ b/src/getSolidpkg.ts @@ -0,0 +1,22 @@ +import getUrlContents from './getUrlContents'; +import { Product } from './interfaces/ISettings'; +import verifyProductName from './verifyProductName'; + +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.`); + } + + if (typeof from !== 'number' || (from | 0) !== from || from < -1) { + throw new Error(`from must be an integer at least -1 but it is "${from}"`); + } + if (typeof to !== 'number' || (to | 0) !== to || to < 0) { + throw new Error(`to must be an integer at least 0 but it is "${to}"`); + } + if (from >= to) { + throw new Error(`from must be smaller than to but "${from}" is not smaller than "${to}".`); + } + + return getUrlContents({ host: 'cdn-patch.swtor.com', path: `/patch/${product}/${product}_${from}to${to}.solidpkg` }); +} diff --git a/src/getFileContents.ts b/src/getUrlContents.ts similarity index 66% rename from src/getFileContents.ts rename to src/getUrlContents.ts index f650113..874ae9d 100644 --- a/src/getFileContents.ts +++ b/src/getUrlContents.ts @@ -1,20 +1,10 @@ import * as http from 'http'; -import { Product } from './interfaces/ISettings'; -import verifyProductName from './verifyProductName'; -export default function getFileContents(product: Product, from: number, to: number): Promise { +export default function getUrlContents({ host, path }: {host: string, path: string}): Promise { return new Promise((resolve, reject) => { - if (!verifyProductName(product)) { - return reject(`"${product}" is not a valid product.`); - } - //TODO: also verify from and to - - //Generate URL - const path = `/patch/${product}/${product}_${from}to${to}.solidpkg`; - const request = http.request({ family: 4, - host: 'cdn-patch.swtor.com', + host, path, }, (response) => { if (response.statusCode !== 200) { diff --git a/src/installPatch.ts b/src/installPatch.ts index 6304fb3..e868186 100644 --- a/src/installPatch.ts +++ b/src/installPatch.ts @@ -1,6 +1,6 @@ -import getFileContents from './getFileContents'; +import getSolidpkg from './getSolidpkg'; (async () => { - const buffer = await getFileContents('assets_swtor_de_de', -1, 0); + const buffer = await getSolidpkg('assets_swtor_de_de', -1, 0); console.log(buffer.length, buffer); })();