2018-06-21 15:44:13 +02:00
|
|
|
import * as http from 'http';
|
2018-06-21 21:26:29 +02:00
|
|
|
import { Product } from './interfaces/ISettings';
|
2018-06-21 21:40:27 +02:00
|
|
|
import verifyProductName from './verifyProductName';
|
2018-06-21 15:44:13 +02:00
|
|
|
|
2018-06-21 21:40:27 +02:00
|
|
|
export default function getFileContents(product: Product, from: number, to: number): Promise<Buffer> {
|
2018-06-21 21:26:29 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-06-21 21:40:27 +02:00
|
|
|
if (!verifyProductName(product)) {
|
|
|
|
return reject(`"${product}" is not a valid product.`);
|
|
|
|
}
|
|
|
|
//TODO: also verify from and to
|
|
|
|
|
2018-06-21 21:26:29 +02:00
|
|
|
//Generate URL
|
|
|
|
const path = `/patch/${product}/${product}_${from}to${to}.solidpkg`;
|
2018-06-21 15:44:13 +02:00
|
|
|
|
2018-06-21 21:26:29 +02:00
|
|
|
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}`);
|
2018-06-21 20:05:57 +02:00
|
|
|
}
|
2018-06-21 21:26:29 +02:00
|
|
|
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);
|
|
|
|
});
|
2018-06-21 15:44:13 +02:00
|
|
|
});
|
|
|
|
|
2018-06-21 21:26:29 +02:00
|
|
|
request.on('error', (e) => {
|
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
request.end();
|
2018-06-21 15:44:13 +02:00
|
|
|
});
|
2018-06-21 21:26:29 +02:00
|
|
|
}
|