Skip to content

Commit

Permalink
feat(server): set gzip encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveVanOpstal committed Sep 11, 2017
1 parent 45119e8 commit 84115ef
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 227 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/app.js",
"program": "${workspaceRoot}/dist/server/static-server.js",
"cwd": "${workspaceRoot}"
},
{
Expand Down
36 changes: 17 additions & 19 deletions src/client/services/lolapi.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {Headers, Http} from '@angular/http';
import {Router} from '@angular/router';
import {Observable} from 'rxjs';
import {Observable} from 'rxjs/Rx';

import {settings} from '../../../config/settings';
import {environment} from '../../environments/environment';

export enum Endpoint {
static,
Expand All @@ -17,17 +18,12 @@ export class LolApiService {
constructor(private http: Http, private router: Router) {}

public getRegions(): Observable<any> {
let observables: Array<Observable<any>> = [];
for (let region of settings.api.regions) {
observables.push(
this.cache(this.getEndpoint(Endpoint.static) + region + '/status/shard-data'));
}
return Observable.forkJoin(observables);
return this.cache(this.getEndpoint(Endpoint.static) + 'all/status/shard-data');
}

public getRealm(): Observable<any> {
return this.get(Endpoint.static, 'static-data/realms').map(res => {
// both http and https are supported, realm data will return http
// both http and https are supported but realm data will return http as cdn
if (res.cdn) {
res.cdn = res.cdn.replace('http://', 'https://');
}
Expand All @@ -45,8 +41,7 @@ export class LolApiService {

public getChampion(championKey: string): Observable<any> {
return this.get(
Endpoint.static,
'static-data/champions/' + championKey +
Endpoint.static, 'static-data/champions/' + championKey +
'?tags=allytips&tags=image&tags=passive&tags=spells&tags=stats&tags=tags');
}

Expand All @@ -65,8 +60,7 @@ export class LolApiService {
public getMatchData(summonerName: string, championKey: string, gameTime: number, samples: number):
Observable<any> {
return this.get(
Endpoint.match,
'match/' + summonerName + '/' + championKey + '?gameTime=' + gameTime +
Endpoint.match, 'match/' + summonerName + '/' + championKey + '?gameTime=' + gameTime +
'&samples=' + samples);
}

Expand Down Expand Up @@ -121,9 +115,9 @@ export class LolApiService {

private abort(name: string, error, statusCode: number) {
if (error.status && error.status === statusCode) {
let text = name + ', not attempting a retry. (' + error + ')';
const text = name + ', not attempting a retry. (' + error + ')';
console.error(text);
throw text;
throw Error(text);
}
}

Expand All @@ -134,9 +128,13 @@ export class LolApiService {
private getEndpoint(endpoint: Endpoint): string {
switch (endpoint) {
case Endpoint.static:
return 'https://' + settings.domain + '/staticapi/';
return environment.production ?
'https://' + settings.domain + '/staticapi/' :
'https://' + settings.host + ':' + settings.static.port + '/';
default:
return 'https://' + settings.domain + '/matchapi/';
return environment.production ?
'https://' + settings.domain + '/matchapi/' :
'https://' + settings.host + ':' + settings.match.port + '/';
}
}

Expand All @@ -151,8 +149,8 @@ export class LolApiService {
}

private checkRegion(region: string): Observable<string> {
return this.getRegions().map((regions: Array<string>) => {
let foundRegion = regions.find((r: any) => {
return this.getRegions().map((regions: Array<any>) => {
const foundRegion = regions.find((r: any) => {
return r.slug === region;
});
if (foundRegion) {
Expand Down
12 changes: 6 additions & 6 deletions src/server/console.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let chalk = require('chalk');
let dateFormat = require('dateformat');
const chalk = require('chalk');
const dateFormat = require('dateformat');

import {tim} from '../client/shared/tim';

Expand All @@ -8,7 +8,7 @@ export class ColorConsole {

public logHttp(method: string, path: string, statusCode: number, extra?: any) {
method = this.pad(method, 6);
let time = this.pad(this.totalTime() + 'ms', 13);
const time = this.pad(this.totalTime() + 'ms', 13);
if (statusCode !== 200) {
this.error('%s %s (%d) %s [%s]', method, path, statusCode, time, extra);
} else {
Expand Down Expand Up @@ -47,7 +47,7 @@ export class ColorConsole {
}

private format(message: any, type: string): string {
let timestamp = dateFormat(new Date(), 'HH:MM:ss.L');
const timestamp = dateFormat(new Date(), 'HH:MM:ss.L');
return tim(
'{{timestamp}} {{type}}: {{message}}',
{timestamp: timestamp, type: type, message: message});
Expand All @@ -58,8 +58,8 @@ export class ColorConsole {
}

private totalTime() {
let diff = process.hrtime(this.timeStart);
let diffMs = (diff[0] * 1e9 + diff[1]) / 1000000;
const diff = process.hrtime(this.timeStart);
const diffMs = (diff[0] * 1e9 + diff[1]) / 1000000;
return diffMs;
}
}
Expand Down
56 changes: 56 additions & 0 deletions src/server/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as fs from 'fs';
import {ServerResponse} from 'https';
import * as zlib from 'zlib';

import {ColorConsole} from './console';

export class Helpers {
static readFile(filename: string): string {
try {
return fs.readFileSync(filename).toString();
} catch (e) {
const console = new ColorConsole();
console.error('`' + filename + '` missing or inaccesible');
console.error(e);
return undefined;
}
}

static watchFile(filename: string, listener: FunctionStringCallback): void {
console.log('watching: ' + filename);
fs.watch(filename, () => {
const result = this.readFile(filename);
if (result) {
listener(result);
}
});
}

static jsonParse(data: string): any {
let json = data;
try {
json = JSON.parse(data);
} catch (e) {
console.log('json parse failed: ' + e);
}
return json;
}

static jsonStringify(json: any): string {
let data = json;
try {
data = JSON.stringify(json);
} catch (e) {
console.log('json stringify failed: ' + e);
}
return data;
}

static gzip(data: any): Buffer {
return zlib.gzipSync(data);
}

static jsonGzip(data: any): Buffer {
return this.gzip(this.jsonStringify(data));
}
}
12 changes: 6 additions & 6 deletions src/server/match/match.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ describe('Match', () => {
let server: MockServer;
let match: Match;

let responseSummoners = {
const responseSummoners = {
url: 'summoners',
message: new MockHostResponseSuccess(JSON.stringify({'accountId': 123}))
};

let responseMatchLists = {
const responseMatchLists = {
url: 'matchlists/by-account',
message: new MockHostResponseSuccess(JSON.stringify({
'matches': [{'gameId': 2701428538}, {'gameId': 2698839638}, {'gameId': 2695882481}],
'totalGames': 3
}))
};

let responseMatch = {
const responseMatch = {
url: 'matches',
message: new MockHostResponseSuccess(JSON.stringify({
'participantIdentities': [{
Expand All @@ -32,7 +32,7 @@ describe('Match', () => {
}))
};

let responseTimelines = {
const responseTimelines = {
url: 'timelines',
message: new MockHostResponseSuccess(JSON.stringify({
'frameInterval': 60000,
Expand All @@ -55,8 +55,8 @@ describe('Match', () => {

it('should get frames', () => {
server.responses = [responseSummoners, responseMatchLists, responseMatch, responseTimelines];
let incomingMessage: any = {url: 'test'};
let serverResponse: any = new MockServerResponse();
const incomingMessage: any = {url: 'test'};
const serverResponse: any = new MockServerResponse();

match.get(
'euw', 'DinosHaveNoLife', '123', settings.gameTime, settings.match.sampleSize,
Expand Down
Loading

0 comments on commit 84115ef

Please sign in to comment.