2018-06-21 15:44:13 +02:00
|
|
|
import * as http from 'http';
|
|
|
|
|
|
|
|
export const getFileContents: () => Promise<Buffer> = () => new Promise((resolve, reject) => {
|
|
|
|
const product = 'assets_swtor_de_de';
|
|
|
|
|
|
|
|
//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);
|
|
|
|
|
|
|
|
const request = http.request({
|
|
|
|
family: 4,
|
|
|
|
host: 'cdn-patch.swtor.com',
|
|
|
|
path,
|
|
|
|
}, (response) => {
|
2018-06-21 20:05:57 +02:00
|
|
|
if (response.statusCode !== 200) {
|
|
|
|
return reject(`Expected status code 200 but received ${response.statusCode}`);
|
|
|
|
}
|
|
|
|
const headerLength = Number(response.headers['content-length']);
|
2018-06-21 15:44:13 +02:00
|
|
|
|
|
|
|
const chunkList: Buffer[] = [];
|
|
|
|
let totalLength = 0;
|
2018-06-21 15:50:25 +02:00
|
|
|
response.on('data', (chunk: Buffer) => {
|
2018-06-21 15:44:13 +02:00
|
|
|
chunkList.push(chunk);
|
|
|
|
totalLength += chunk.length;
|
|
|
|
});
|
|
|
|
response.on('end', () => {
|
2018-06-21 20:05:57 +02:00
|
|
|
if (totalLength !== headerLength) {
|
|
|
|
return reject(`Expected length ${headerLength} but received ${totalLength}`);
|
|
|
|
}
|
2018-06-21 15:44:13 +02:00
|
|
|
const fileContents = Buffer.concat(chunkList, totalLength);
|
|
|
|
resolve(fileContents);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request.on('error', (e) => {
|
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
request.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
getFileContents();
|