♻️ Extract solidpkg from general file download
This commit is contained in:
parent
7a81a67132
commit
1a37702b24
3 changed files with 26 additions and 14 deletions
22
src/getSolidpkg.ts
Normal file
22
src/getSolidpkg.ts
Normal file
|
@ -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<Buffer> {
|
||||
//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` });
|
||||
}
|
|
@ -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<Buffer> {
|
||||
export default function getUrlContents({ host, path }: {host: string, path: string}): Promise<Buffer> {
|
||||
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) {
|
|
@ -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);
|
||||
})();
|
||||
|
|
Loading…
Reference in a new issue