From 6baaada419858be82e17097a5e60c2cf798644f0 Mon Sep 17 00:00:00 2001 From: C-3PO Date: Thu, 21 Jun 2018 15:44:13 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20file=20download=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/getFileContents.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/tslint.json | 6 +++--- 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/getFileContents.ts diff --git a/src/getFileContents.ts b/src/getFileContents.ts new file mode 100644 index 0000000..7270aa8 --- /dev/null +++ b/src/getFileContents.ts @@ -0,0 +1,40 @@ +import * as http from 'http'; + +export const getFileContents: () => Promise = () => 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) => { + console.log(`STATUS: ${response.statusCode}`); + console.log(`HEADERS: ${JSON.stringify(response.headers)}`); + + const chunkList: Buffer[] = []; + let totalLength = 0; + response.on('data', (chunk) => { + console.log(`BODY: ${chunk.length}`); + chunkList.push(chunk); + totalLength += chunk.length; + }); + response.on('end', () => { + console.log('No more data in response.'); + const fileContents = Buffer.concat(chunkList, totalLength); + resolve(fileContents); + }); + }); + + request.on('error', (e) => { + console.error(`problem with request: ${e.message}`); + reject(e); + }); + request.end(); +}); + +getFileContents(); diff --git a/src/tslint.json b/src/tslint.json index 53aee4b..426f4e5 100644 --- a/src/tslint.json +++ b/src/tslint.json @@ -1,7 +1,7 @@ { "defaultSeverity": "error", "extends": [ - "tslint:recommended", + "tslint:recommended" ], "jsRules": {}, "rules": { @@ -9,7 +9,7 @@ "indent": [true, "spaces", 2], "max-line-length": false, "no-bitwise": false, - "quotemark": [true, "single"], + "quotemark": [true, "single"] }, - "rulesDirectory": [], + "rulesDirectory": [] }