diff --git a/src/config.ts b/src/config.ts index e982af0..e0ae782 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,12 +1,6 @@ -interface ISettings { - [key: string]: any; - product?: 'assets_swtor_de_de' | 'assets_swtor_en_us' | 'assets_swtor_fr_fr' | 'assets_swtor_main' | 'assets_swtor_test_de_de' | 'assets_swtor_test_en_us' | 'assets_swtor_test_fr_fr' | 'assets_swtor_test_main' | 'eualas' | 'movies_de_de' | 'movies_en_us' | 'movies_fr_fr' | 'patcher2014' | 'patcher2017' | 'retailclient_betatest' | 'retailclient_cstraining' | 'retailclient_liveeptest' | 'retailclient_publictest' | 'retailclient_squadron157' | 'retailclient_swtor'; - release?: number; //TODO: allow 'current', but how will we know what current version is? - from?: number; - outputType?: 'info' | 'file'; //whether to just show JSON information, or actually write files into a directory -} +import { ISettings, Product } from './interfaces/ISettings'; -const allowedProducts: Array = ['assets_swtor_de_de', 'assets_swtor_en_us', 'assets_swtor_fr_fr', 'assets_swtor_main', 'assets_swtor_test_de_de', 'assets_swtor_test_en_us', 'assets_swtor_test_fr_fr', 'assets_swtor_test_main', 'eualas', 'movies_de_de', 'movies_en_us', 'movies_fr_fr', 'patcher2014', 'patcher2017', 'retailclient_betatest', 'retailclient_cstraining', 'retailclient_liveeptest', 'retailclient_publictest', 'retailclient_squadron157', 'retailclient_swtor']; +const allowedProducts: Product[] = ['assets_swtor_de_de', 'assets_swtor_en_us', 'assets_swtor_fr_fr', 'assets_swtor_main', 'assets_swtor_test_de_de', 'assets_swtor_test_en_us', 'assets_swtor_test_fr_fr', 'assets_swtor_test_main', 'eualas', 'movies_de_de', 'movies_en_us', 'movies_fr_fr', 'patcher2014', 'patcher2017', 'retailclient_betatest', 'retailclient_cstraining', 'retailclient_liveeptest', 'retailclient_publictest', 'retailclient_squadron157', 'retailclient_swtor']; const settings: ISettings = {}; @@ -16,8 +10,8 @@ export const set = (key: string, value: any) => { case 'product': //TODO: need to verify input (one of allowed products) if (typeof value !== 'string') { throw new Error(`product must be a string but it's a "${typeof value}".`); } - if (!allowedProducts.includes(value as ISettings['product'])) { throw new Error(`"${value}" is not an allowed product.`); } - settings.product = value as ISettings['product']; + if (!allowedProducts.includes(value as Product)) { throw new Error(`"${value}" is not an allowed product.`); } + settings.product = value as Product; break; case 'release': //verify input (must be a number >=0, and >settings.from) diff --git a/src/enums/releases.ts b/src/enums/releases.ts deleted file mode 100644 index 16e545f..0000000 --- a/src/enums/releases.ts +++ /dev/null @@ -1,22 +0,0 @@ -export const enum Releases { - 'assets_swtor_de_de', - 'assets_swtor_en_us', - 'assets_swtor_fr_fr', - 'assets_swtor_main', - 'assets_swtor_test_de_de', - 'assets_swtor_test_en_us', - 'assets_swtor_test_fr_fr', - 'assets_swtor_test_main', - 'eualas', - 'movies_de_de', - 'movies_en_us', - 'movies_fr_fr', - 'patcher2014', - 'patcher2017', - 'retailclient_betatest', - 'retailclient_cstraining', - 'retailclient_liveeptest', - 'retailclient_publictest', - 'retailclient_squadron157', - 'retailclient_swtor', -}; diff --git a/src/getFileContents.ts b/src/getFileContents.ts index 39ef6c0..5b10f56 100644 --- a/src/getFileContents.ts +++ b/src/getFileContents.ts @@ -1,40 +1,39 @@ import * as http from 'http'; +import { Product } from './interfaces/ISettings'; -export const getFileContents: (product?: string) => Promise = (product: string = 'assets_swtor_de_de') => new Promise((resolve, reject) => { - //Generate URL - const url = `http://cdn-patch.swtor.com/patch/${product}/${product}_-1to0.solidpkg`; - const path = `/patch/${product}/${product}_-1to0.solidpkg`; - const fileName = url.substr(url.lastIndexOf('/') + 1); +export default function getFileContents(product: string = 'assets_swtor_de_de', from: number, to: number): Promise { + return new Promise((resolve, reject) => { + //Generate URL + const path = `/patch/${product}/${product}_${from}to${to}.solidpkg`; - const request = http.request({ - family: 4, - host: 'cdn-patch.swtor.com', - path, - }, (response) => { - if (response.statusCode !== 200) { - return reject(`Expected status code 200 but received ${response.statusCode}`); - } - const headerLength = Number(response.headers['content-length']); - - const chunkList: Buffer[] = []; - let totalLength = 0; - response.on('data', (chunk: Buffer) => { - chunkList.push(chunk); - totalLength += chunk.length; - }); - response.on('end', () => { - if (totalLength !== headerLength) { - return reject(`Expected length ${headerLength} but received ${totalLength}`); + const request = http.request({ + family: 4, + host: 'cdn-patch.swtor.com', + path, + }, (response) => { + if (response.statusCode !== 200) { + return reject(`Expected status code 200 but received ${response.statusCode}`); } - const fileContents = Buffer.concat(chunkList, totalLength); - resolve(fileContents); + const headerLength = Number(response.headers['content-length']); + + const chunkList: Buffer[] = []; + let totalLength = 0; + response.on('data', (chunk: Buffer) => { + chunkList.push(chunk); + totalLength += chunk.length; + }); + response.on('end', () => { + if (totalLength !== headerLength) { + return reject(`Expected length ${headerLength} but received ${totalLength}`); + } + const fileContents = Buffer.concat(chunkList, totalLength); + resolve(fileContents); + }); }); - }); - request.on('error', (e) => { - reject(e); + request.on('error', (e) => { + reject(e); + }); + request.end(); }); - request.end(); -}); - -getFileContents(); +} diff --git a/src/installPatch.ts b/src/installPatch.ts new file mode 100644 index 0000000..6304fb3 --- /dev/null +++ b/src/installPatch.ts @@ -0,0 +1,6 @@ +import getFileContents from './getFileContents'; + +(async () => { + const buffer = await getFileContents('assets_swtor_de_de', -1, 0); + console.log(buffer.length, buffer); +})(); diff --git a/src/interfaces/ISettings.ts b/src/interfaces/ISettings.ts new file mode 100644 index 0000000..b77750b --- /dev/null +++ b/src/interfaces/ISettings.ts @@ -0,0 +1,11 @@ +type Product = 'assets_swtor_de_de' | 'assets_swtor_en_us' | 'assets_swtor_fr_fr' | 'assets_swtor_main' | 'assets_swtor_test_de_de' | 'assets_swtor_test_en_us' | 'assets_swtor_test_fr_fr' | 'assets_swtor_test_main' | 'eualas' | 'movies_de_de' | 'movies_en_us' | 'movies_fr_fr' | 'patcher2014' | 'patcher2017' | 'retailclient_betatest' | 'retailclient_cstraining' | 'retailclient_liveeptest' | 'retailclient_publictest' | 'retailclient_squadron157' | 'retailclient_swtor'; + +interface ISettings { + [key: string]: any; + product?: Product; + release?: number; //TODO: allow 'current', but how will we know what current version is? + from?: number; + outputType?: 'info' | 'file'; //whether to just show JSON information, or actually write files into a directory +} + +export { ISettings, Product };