🎨 Improve file download and typescript annotations

This commit is contained in:
C-3PO 2018-06-21 21:26:29 +02:00
parent b502075cb3
commit 8424ae8978
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
5 changed files with 53 additions and 65 deletions

View file

@ -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<ISettings['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 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)

View file

@ -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',
};

View file

@ -1,40 +1,39 @@
import * as http from 'http';
import { Product } from './interfaces/ISettings';
export const getFileContents: (product?: string) => Promise<Buffer> = (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<Buffer> {
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();
}

6
src/installPatch.ts Normal file
View file

@ -0,0 +1,6 @@
import getFileContents from './getFileContents';
(async () => {
const buffer = await getFileContents('assets_swtor_de_de', -1, 0);
console.log(buffer.length, buffer);
})();

View file

@ -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 };