Skip to content

Commit

Permalink
Merge pull request #60 from apigee/for302quality
Browse files Browse the repository at this point in the history
For302quality
  • Loading branch information
keyurkarnik authored Jul 2, 2019
2 parents af0ca35 + 377ff6a commit 5e06540
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 139 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": 5,
"maxstatements": 25,
"maxcomplexity": 10
}
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
language: node_js
node_js:
- '6.14'
- '8'
- '10'
- '12'
notifications:
slack:
secure: pZ5LBxdOunYr2yki24BE4dP0BpPUrEXV6RqsnwwANwa2jOksrm5ftpwR1x7oFDHUFO00J8l1fqF3gQjK16FuOLCHaNUbk/K+AyqF06oH5TU0RA6Qz3vpDZ2ZosGCTAKpE8ycinuZLBwadzheluSMpBl11EbbEd4fzqwz8/pMt+CgqZ575J3+jl3uhLWl4/HJFrIrYgrbPV5pj054lTmkkpDjObhQWa3i+HJGkplolPirlw/CMSiZIXmfxOUlUGdknuj4pKTq7hxhioOPMe1qUFK74t6LO6QyB5xBb+3uAsI7CyivJ+vEVc+raFEoewCZck8TglyAPcozirdTQtryQeZiatNwcJMnV0BW5O20I2Ts71Hwo/s5eQUl2T1bAmcP4GKjqWLQZ3LtcaAxJsg6vlTxmt/HjIDn38B+sox2usZbSw2r40oiwbVdocZ6Ug4lNCtfuf8znPyCa6HkhvCFHvER1u5ElbRTCF1E57eonYSLZtzBta50ArHlD7UW2BRQfQ5lsg4v2ek+hshBmlGiqqDc97KMTBMv9NWyJlUWOjQUiJY5a/rI6NFpB3P3gWTA/GhXNvEIw7r4zlggRndqC90vjXHkCExnWg/1uZW9yu3FYGZqEjjWN1vwUYBomQMQt2KV2X/4N3kKn0jUS0xlFQm9MG1CDp/e/GNAc3Ey30k=
on_success: never
on_failure: always
on_start: never
on_start: never

2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var io = require('./lib/io');
var network = require('./lib/network');
var path = require('path');
Expand Down
17 changes: 9 additions & 8 deletions lib/io.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'
const path = require('path');
const os = require('os');
const fs = require('fs-extra');
const yaml = require('js-yaml');
const assert = require('assert');
Expand Down Expand Up @@ -32,14 +31,16 @@ IO.prototype.initConfig = function (options, cb) {
const configPath = path.join(configDir, fileName);

fs.ensureDir(configDir, function (err) {
err && console.error(err);
fs.stat(configPath, function (err, stats) {
//
if ( err ) console.error(err);
//
fs.stat(configPath, function (err /*, stats */) {
if (err) {
console.log("file doesn't exist, setting up");
fs.ensureFile(configPath, function (err) {
err && console.error(err);
if ( err ) console.error(err);
fs.copy(source, configPath, function (err) {
err && console.error(err);
if ( err ) console.error(err);
return cb(err, configPath);
}); // copy from default config
});
Expand Down Expand Up @@ -123,11 +124,11 @@ IO.prototype.save = function (config, options, cb) {
const dump = yaml.safeDump(save, {skipInvalid: true});
if (options.sync) {
fs.writeFileSync(target, dump);
_.isFunction(cb) && cb();
if ( _.isFunction(cb) ) cb();
} else {
fs.writeFile(target, dump, function (err) {
err && console.error('error saving config to', target, err);
if (cb && _.isFunction(cb)) {
if ( err ) console.error('error saving config to', target, err);
if ( cb && _.isFunction(cb) ) {
cb(err);
}
});
Expand Down
47 changes: 32 additions & 15 deletions lib/network.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
'use strict';

const assert = require('assert');
const crypto = require('crypto');
const path = require('path');
const util = require('util');
const url = require('url');
const async = require('async');
const debug_ = require('debug');
const request = require('request');
const yaml = require('js-yaml');
const _ = require('lodash');
const default_config_validator = require('./default-validator');
const proxy_validator = require('./proxy-validator');
const debug = debug_('agent:config');
const ioLib = require('./io');
const fs = require('fs');


const Loader = function(io) {
this.io = io || ioLib();
};

/*
const yaml = require('js-yaml');
const crypto = require('crypto');
const util = require('util');
*/

module.exports = function() {
return new Loader();
};
Expand All @@ -36,7 +41,7 @@ Loader.prototype.get = function(options, callback) {


//EDGEMICRO_LOCAL - allows microgateway to function without any connection to Apigee Edge
if (process.env.EDGEMICRO_LOCAL == "1") {
if (process.env.EDGEMICRO_LOCAL === "1") {
debug("running microgateway in local mode");
const config = this.io.loadSync({
source: options.source
Expand Down Expand Up @@ -69,18 +74,18 @@ Loader.prototype.get = function(options, callback) {

const keys = options.keys;
const source = options.source;
const io = this.io;
//const io = this.io;
const configurl = options.configurl;

if (typeof configurl != 'undefined' && configurl) {
if ( (typeof configurl !== 'undefined') && configurl) {
request.get(configurl, this, function(error, response, body) {
if (!error && response.statusCode == 200) {
if (!error && response.statusCode === 200) {
console.log('downloading configuration from: ' + configurl);
debug(body);
writeConfig(source, body);
writeConfig(sourceBackup, body)
//writeConfig(sourceBackup, body) // sourceBackup not defined
} else {
console.error('DETECTED PRODUCT MISCONFIGURATION ERROR',err);
//console.error('DETECTED PRODUCT MISCONFIGURATION ERROR',err); // err is not defined
console.warn('using old cached configuration');
}
this.config = this.io.loadSync({
Expand Down Expand Up @@ -111,7 +116,7 @@ function writeConfig(source, body) {
encoding: 'utf8'
});
} catch (err) {
console.error('Error: ' + error);
console.error('Error: ' + err);
}
}

Expand Down Expand Up @@ -154,7 +159,7 @@ function loadConfiguration(thisconfig, keys, callback) {
it ignores or overrides the basepath set in the proxy to slash. another
important node, it is expected that decorator will have only one proxy.
*/
if (process.env.EDGEMICRO_DECORATOR && proxies.length == 1) {
if (process.env.EDGEMICRO_DECORATOR && proxies.length === 1) {
debug("running as microgateway decorator");
proxies[0].proxyEndpoint.basePath = '/';
debug(proxies);
Expand All @@ -173,12 +178,23 @@ function loadConfiguration(thisconfig, keys, callback) {
_mergeKeys(mergedConfig, keys); // merge keys before sending to edge micro
callback(null, mergedConfig);
} else {

// THIS BRANCH OF CODE HAS LIKELY NEVER BEEN RUN

// check if we have a retry_interval specified
// any value less than 5 seconds is assumed invalid and ignored
// start with the cached copy while we retry updates in the background
var io = ioLib(); // this seems to be what was intended... not absolutely sure.

var source = ''; // this has not been defined

const mergedConfig = io.loadSync({
source: source
});


var target = ''; // this has not been defined

if (mergedConfig) {
console.info('loaded cached config from', target);
_mergeKeys(mergedConfig, keys); // merge keys before sending to edge micro
Expand Down Expand Up @@ -248,7 +264,7 @@ const _load = function(config, keys, callback) {
sendImmediately: true
};
//if defined, proxy params are passed in the start cmd.
if (process.env.EDGEMICRO_LOCAL_PROXY == "1") {
if (process.env.EDGEMICRO_LOCAL_PROXY === "1") {
const proxyInfo = "{\"apiProxies\": [{\"apiProxyName\":\"" + config.proxies[0].name + "\"," +
"\"revision\":\"" + config.proxies[0].revision + "\"," +
"\"proxyEndpoint\": {" +
Expand Down Expand Up @@ -297,7 +313,7 @@ const _load = function(config, keys, callback) {
opts['url'] = config.edge_config.jwk_public_keys || null;
opts = enableTLS(config, opts);
request.get(opts, function(err, response, body) {
if (response && response.statusCode == 200) {
if (response && response.statusCode === 200) {
_loadStatus('jwk_public_keys', opts['url'],
err, response, body, cb);
} else {
Expand Down Expand Up @@ -341,7 +357,7 @@ const _load = function(config, keys, callback) {

//cleanup null targets
for (counter = 0; counter < proxiesLen; counter++) {
if (apiProxies[counter] && apiProxies[counter].targetEndpoint.url == "null") {
if (apiProxies[counter] && apiProxies[counter].targetEndpoint.url === "null") {
debug("ignoring " + apiProxies[counter].apiProxyName + " proxy since it has a null target");
delete apiProxies[counter];
}
Expand Down Expand Up @@ -426,6 +442,7 @@ const _load = function(config, keys, callback) {
const _loadStatus = function(message, url, err, response, body, cb) {
const failed = err || (response && response.statusCode !== 200);
if (url) {
// should the program keep running if it can't load products.?
if ( failed ) {
console.warn(message, 'download from', url,'returned',
(response ? (response.statusCode + ' ' + response.statusMessage) : '', err ? err : ''));
Expand Down Expand Up @@ -656,7 +673,7 @@ const _validateUrls = function(config) {

function getDefaultProxy(config, options) {
//create default proxy if params were supplied.
if (proxies == null && options.localproxy) {
if (proxies === null && options.localproxy) {
proxies = [{
max_connections: config.edgemicro.max_connections,
name: options.localproxy.apiProxyName,
Expand Down
Loading

0 comments on commit 5e06540

Please sign in to comment.