Skip to content

Commit

Permalink
Merge pull request #127 from apigee/for302
Browse files Browse the repository at this point in the history
For302
  • Loading branch information
keyurkarnik authored Jul 2, 2019
2 parents 4b5eae0 + c5d5f72 commit 3a53a76
Show file tree
Hide file tree
Showing 18 changed files with 2,694 additions and 834 deletions.
16 changes: 13 additions & 3 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
{
"esversion": 6,
"node": true
}
"asi" : true,
"esversion" : 6,
"node" : true,
"sub" : true,
"strict": true,
"white": true,
"unused": true,
"eqeqeq": true,
"maxparams": 10,
"maxdepth": 8,
"maxstatements": 25,
"maxcomplexity": 10
}
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
language: node_js
node_js:
- '6.14'
- '8'
- '10'
- '12'
notifications:
slack:
secure: F5C36kAtfo0X4upvC0IkRDycsh7vSuRJrPSUm95j7w5LPVClqrIM6Lr4Zn4RkvptGnGTZNGSdPHtNz07bI5LosCjzvkJRpJkOHkPMqM0VydIr1hfIcWnrs9koHtMOMpn2NM1OLgoef5ettRDVDBO6DZFkHhdk2+vlgmCxOK0LcQJ6dMVfG99dhhoewlfCd1OG6yk1U+hW+BMlEwuAxUUxazKfR9NI+KOwxXEmtAM9lTI6nXh83XEbrVSzOlMFz2gPQzZxOLBo7zagPo49q150obG16hI0yvgkw/yH4Fv7Oh/TCedFctrwVp+zwmtJZckxCQ3UxgbV89ZYfK2Fy7RbzPIk3CjsP5w0LtULn5pURyCDiwCkQ0YJYlIDo8bUWGKOwnjUcU2cy2k4GKnNkP4obTzvK3RXWBnVEq57rMh1A6Dwvzu1xImA27+cj0Jx1CjgdoWYw1qQeQ7k/EV4q/vBhvVhIYm4oK7tYMNMevPdfGWmcaJjgT5ZVQ+5xvGhvtI9IJqXfLb9ab+ZiJw2esY9YuDEYZcYiMcFnN3P0SM2syNhfUDrInSx8SrID6ldixS9Ohfiq8KlxINAxm+MlfjL/N9CP4IMTqcUmdcXBOY+sBah3XSbLxlfjGLmrpvSt7/QQGS36AM7/edbe9MtqNkvz1ipQurtn9z0kUMyH4p7Ek=
Expand Down
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';
var debug = require('debug')('gateway:index');
var util = require('util');
var gateway = require('./lib/gateway');
var assert = require('assert');
var logging = require('./lib/logging');
var stats = require('./lib/stats');


var pluginsLib = require('./lib/plugins');
var uuid = require('uuid')
Expand Down Expand Up @@ -33,8 +32,8 @@ module.exports = function (config) {

module.exports.Logging = logging;
Gateway.prototype.start = function (cb) {
const logger =logging.getLogger();
const config = configService.get();
//const logger =logging.getLogger();
//const config = configService.get();
let plugins = this.plugins;
debug('starting edgemicro');
//debug('loaded config ' + util.inspect(config, {colors: true}));
Expand Down
3 changes: 2 additions & 1 deletion lib/build-non-tunnel-options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const url = require('url');
const qs = require('querystring');

/*
* Build options for a proxied request. This means:
Expand Down
4 changes: 3 additions & 1 deletion lib/build-tunnel-agent.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const url = require('url');
const TunnelAgent = require('tunnel-agent');

Expand All @@ -19,7 +21,7 @@ module.exports = (proxyConfig, agentOptions, proxy, proxyHeaders) => {
return tunnelAgentFunction(tunnelOpts);
}

const createTunnelAgentOptions = (parsedProxyUrl, agentOptions, proxy) => {
const createTunnelAgentOptions = (parsedProxyUrl, agentOptions) => { //, proxy) => { // not used jshint

var tunnelOpts = {
proxy: {
Expand Down
11 changes: 7 additions & 4 deletions lib/config-proxy-middleware.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict';

const path = require('path');
//const _ = require('lodash');
//const path = require('path');


const url = require('url');
const debug = require('debug')('gateway:main');
const configService = require('./config');
const double_slash_regex = /\/\/+/g;
const http = require('http');
const https = require('https');
const _ = require('lodash');
const buildTunnelAgent = require('./build-tunnel-agent')
const checkNoProxy = require('./no-proxy-parser')
const proxyPathMatcher = require('./proxy-path-matcher');
Expand All @@ -29,9 +31,10 @@ module.exports = function() {

if (config && config.proxies) {
config.proxies.forEach(function(proxy) {
var secureProxy;
if (httpProxyConfig) {
var parsedHttpProxyConfig = url.parse(httpProxyConfig);
var secureProxy = parsedHttpProxyConfig.protocol === 'https:';
secureProxy = parsedHttpProxyConfig.protocol === 'https:';
}

proxy.parsedUrl = url.parse(proxy.url);
Expand All @@ -41,7 +44,7 @@ module.exports = function() {
proxy.secureHttpProxy = secureProxy;
var agentLib = http;

if (secure || secureProxy) {
if ( secure || secureProxy ) {
agentLib = https;
}
var opts = {
Expand Down
3 changes: 1 addition & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ configService.init = (newConfig) => {
});

config.targets.forEach(function(target) {
if (typeof target.ssl !== 'object'
|| typeof target.ssl.client !== 'object') {
if ( (typeof target.ssl !== 'object') || (typeof target.ssl.client !== 'object') ) {
return;
}

Expand Down
17 changes: 11 additions & 6 deletions lib/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,21 @@ module.exports.start = function(plugins, cb) {
});

server.on('error', function(err, req, res) {
res && res.writeHead(500, {
'Content-Type': 'application/json'
})
if ( res ) {
res.writeHead(500, {
'Content-Type': 'application/json'
})
}

logger.error(err, "failed in error handler");

res && res.end({
"message": err
});
if ( res ) {
res.end({
"message": err
});
}
cb(err);

});

// place a hard limit on incoming connections (if configured)
Expand Down
36 changes: 19 additions & 17 deletions lib/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
'use strict';

const fs = require('fs');
const http = require('http');
const debug = require('debug')('gateway:logging');
const mkdir = require('mkdirp');
const os = require('os');
const path = require('path');
Expand Down Expand Up @@ -64,8 +62,8 @@ module.exports.init = function init(stubConfig) {
nextRotation = Date.now() + ((logConfig.rotate_interval || 24) * 60 * 60 * 1000); // hours -> ms
}

record && records.push(record);
if (writeInProgress || records.length === 0) {
if ( record ) records.push(record);
if ( writeInProgress || (records.length === 0) ) {
return record;
}

Expand Down Expand Up @@ -149,7 +147,7 @@ module.exports.init = function init(stubConfig) {
},
writeLogRecord: function(record,cb) {
const writeRecordToOutput = logToConsole ? writeLogRecordToConsole : writeLogRecordToFile;
record && record.msg && writeRecordToOutput(record.msg,cb);
if ( record && record.msg ) writeRecordToOutput(record.msg,cb);
return record;
}
};
Expand All @@ -162,7 +160,7 @@ module.exports.init = function init(stubConfig) {

Object.keys(cluster.workers).forEach((id) => {
cluster.workers[id].on('message', function (msg) {
msg && msg.msg && writeLogRecord(msg.msg);
if ( msg && msg.msg ) logger.writeLogRecord(msg.msg);
});
});
}
Expand All @@ -181,14 +179,18 @@ function serializeLogRecord(level, configLevel, obj, text) {
return null;
}
switch (level) {
case 'info':
case 'info': {
if (configLevel === 'warn' || configLevel === 'error') {
return null;
}
case 'warn':
if (configLevel === 'error') {
break;
}
case 'warn': {
if ( configLevel === 'error' ) {
return null;
}
break;
}
}

const record = {};
Expand All @@ -199,10 +201,10 @@ function serializeLogRecord(level, configLevel, obj, text) {
if (key === 'req') {
const req = obj[key];
if (req) {
record.m = req.method,
record.u = req.url,
record.h = req.headers ? req.headers.host : '',
record.r = req.socket ? (req.socket.remoteAddress + ':' + req.socket.remotePort) : ':0'
record.m = req.method;
record.u = req.url;
record.h = (req.headers ? req.headers.host : '');
record.r = (req.socket ? (req.socket.remoteAddress + ':' + req.socket.remotePort) : ':0');
}
} else if (key === 'res') {
const res = obj[key];
Expand All @@ -212,10 +214,10 @@ function serializeLogRecord(level, configLevel, obj, text) {
} else if (key === 'err') {
const err = obj[key];
if (err) {
record.name = err.name,
record.message = err.message,
record.code = err.code,
record.stack = err.stack
record.name = err.name;
record.message = err.message;
record.code = err.code;
record.stack = err.stack;
}
} else if (key === 'stats') {
const stats = obj[key];
Expand Down
10 changes: 6 additions & 4 deletions lib/no-proxy-parser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const url = require('url')

function canonicalizeHostname(h) {
Expand Down Expand Up @@ -34,7 +36,7 @@ foo.bar.com:4000 does not match no_proxy: foo*.com:3000
*/
function doesPortMatch(hostPort, noproxyPort, hostnamesMatch){
if(noproxyPort) {
return hostPort == noproxyPort && hostnamesMatch;
return (hostPort === noproxyPort) && hostnamesMatch;
} else {
return hostnamesMatch;
}
Expand All @@ -49,7 +51,7 @@ function matchHostAgainstNoProxy(host, noProxyVar) {
var canonicalHost = canonicalizeHostname(parsedHostToMatch.hostname);

if(!parsedHostToMatch.port) {
parsedHostToMatch.port = parsedHostToMatch.protocol == 'https:' ? '443' : '80';
parsedHostToMatch.port = ((parsedHostToMatch.protocol === 'https:') ? '443' : '80');
}

return parsedHosts.some((e) => {
Expand All @@ -59,14 +61,14 @@ function matchHostAgainstNoProxy(host, noProxyVar) {
//replace all . with \. ensures the regex pattern looks for the acutal period
var escapedPattern = hostname.replace(/\./g,'\\.');
escapedPattern = escapedPattern.replace('*','.*');
pattern = new RegExp(escapedPattern);
var pattern = new RegExp(escapedPattern);
var match = pattern.test(canonicalHost);
return doesPortMatch(parsedHostToMatch.port, e.port, match);
} else {
var matchIndex = e.host.indexOf(canonicalHost);
const match = matchIndex > -1

var perfectHostMatch = match && (matchIndex == e.host.length - canonicalHost.length);
var perfectHostMatch = match && (matchIndex === (e.host.length - canonicalHost.length));
return doesPortMatch(parsedHostToMatch.port, e.port, perfectHostMatch);
}
});
Expand Down
2 changes: 2 additions & 0 deletions lib/ondata-transform.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const async = require('async');
const debug = require('debug')('gateway:main');
const Transform = require('stream').Transform;
Expand Down
Loading

0 comments on commit 3a53a76

Please sign in to comment.