Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Commit

Permalink
- use resolveUrl to avoid duplicate of '/'
Browse files Browse the repository at this point in the history
  • Loading branch information
GylSky authored and ryanoolala committed Nov 16, 2018
1 parent aa095c2 commit c480920
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Expand Up @@ -13,6 +13,7 @@ services:
#USE_PROXY_AGENT: true
#TO_PROXY: true
# First Gateway
GATEWAY_IS_SINGLE: "true"
GATEWAY_1_HOST: <yourproject>.api.xxx.sg
GATEWAY_1_SIGNING_HOST: <yourproject>.e.api.xxx.sg
GATEWAY_1_PORT: 443
Expand Down
8 changes: 5 additions & 3 deletions src/handler.ts
Expand Up @@ -7,7 +7,7 @@ import {
MODE,
} from './constants';
import {IConfig} from './types';
import {printOpts} from './utils';
import {printOpts, resolveUrl} from './utils';

export const sign = ({
type,
Expand Down Expand Up @@ -69,7 +69,9 @@ export const firstGateSignature = (req: http.IncomingMessage, config: IConfig):
gateway1Passphrase,
} = config;

const urlPath = `https://${gateway1SigningHost}:${gateway1Port}/${gateway1UrlPrefix}/${gateway2UrlPrefix}${url}`;
const urlPath = resolveUrl(`https://${gateway1SigningHost}:${gateway1Port}`,
gateway1UrlPrefix,
gateway2UrlPrefix, url);
return sign({
type: gateway1Type,
secret: gateway1Secret,
Expand Down Expand Up @@ -99,7 +101,7 @@ export const secondGateSignature = (req: http.IncomingMessage, config: IConfig):
gateway2Passphrase,
} = config;

const urlPath = `https://${gateway2SigningHost}:${gateway2Port}/${gateway2UrlPrefix}${url}`;
const urlPath = resolveUrl(`https://${gateway2SigningHost}:${gateway2Port}`, gateway2UrlPrefix, url);
return sign({
type: gateway2Type,
secret: gateway2Secret,
Expand Down
19 changes: 18 additions & 1 deletion src/utils/index.ts
@@ -1,4 +1,6 @@
import { createHash } from "crypto" ;
import { createHash } from "crypto";
import { join } from 'path';
import { parse, resolve } from 'url';

export const generateSHA512Hash = (data: string): string =>
createHash('sha512').update(data || '').digest('hex');
Expand Down Expand Up @@ -29,3 +31,18 @@ export const printOpts = (opts: any): void =>{
console.log("Printing opts:", tempOpts);
};

export const resolveUrl = (urlStub: string, ...urlPathToAdd: string[]): string => {
const urlStubObject = parse(urlStub);
const {protocol, hostname, port} = urlStubObject;
switch (protocol) {
case 'http:': // tslint:disable-line no-http-string
case 'https:':
case 'ftp:':
break;
default:
throw new Error('Protocol was not specified. Prefix :urlStub with "http://" or "https://"');
}
const hostStub = `${protocol}//${hostname}${port ? `:${port}` : ''}/`;
const urlStubPath = urlStubObject.pathname || '/';
return resolve(hostStub, join(urlStubPath, ...urlPathToAdd));
};

0 comments on commit c480920

Please sign in to comment.