From 92d098cadcd2a6ec10dceaa4f425d5cf21764c42 Mon Sep 17 00:00:00 2001 From: Clement Allen Date: Sat, 3 Mar 2018 21:41:24 +0000 Subject: [PATCH] Added linting and fixed errors --- .eslintrc | 32 +++++ .travis.yml | 1 + package.json | 8 +- src/sumoLogger.js | 308 +++++++++++++++++++++------------------- test/sumoLogger.test.js | 22 +-- 5 files changed, 211 insertions(+), 160 deletions(-) create mode 100644 .eslintrc diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..29dfa77 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,32 @@ +{ + "extends": [ + "airbnb" + ], + "env": { + "es6": true, + "node": true, + "mocha": true + }, + "globals": { + "expect": true, + "sinon": true + }, + "rules": { + "indent": [ + 2, + 4 + ], + "comma-dangle": [ + 2, + "never" + ], + "no-console": [ + 2, + { + "allow": [ "error" ] + } + ], + "no-unused-expressions": [0], + "strict": [0] + } +} diff --git a/.travis.yml b/.travis.yml index a7f91a3..3e96f3e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,4 +11,5 @@ install: - npm install script: + - npm run lint - npm run cover diff --git a/package.json b/package.json index 8daa3fe..75862a7 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "http-server": "grunt http-server:dev &", "test": "nyc mocha", "cover": "npm run test && nyc report --reporter=lcov", + "lint": "eslint src/sumoLogger.js test/**/*.js --fix", "test:browser": "npm run http-server && open https://127.0.0.1:8282/jasminetest/TrackerSpecRunner.html" }, "dependencies": { @@ -16,6 +17,11 @@ }, "devDependencies": { "chai": "^4.1.2", + "eslint": "^4.18.2", + "eslint-config-airbnb": "^16.1.0", + "eslint-plugin-import": "^2.9.0", + "eslint-plugin-jsx-a11y": "^6.0.3", + "eslint-plugin-react": "^7.7.0", "grunt": "^1.0.2", "grunt-contrib-jasmine": "^1.1.0", "grunt-contrib-uglify": "^3.3.0", @@ -23,7 +29,7 @@ "mocha": "^5.0.1", "np": "^2.13.1", "nyc": "^11.4.1", - "proxyquire": "^1.8.0", + "proxyquire": "^2.0.0", "sinon": "^4.4.2", "sinon-chai": "^2.14.0" }, diff --git a/src/sumoLogger.js b/src/sumoLogger.js index 2a489b6..b12e215 100644 --- a/src/sumoLogger.js +++ b/src/sumoLogger.js @@ -1,192 +1,200 @@ -var request = require('request'); -var _ = require('underscore'); +'use strict'; -var DEFAULT_INTERVAL = 0; -var NOOP = () => {}; +const request = require('request'); +const _ = require('underscore'); -var originalOpts = {}; -var currentConfig = {}; -var currentLogs = []; -var interval; +const DEFAULT_INTERVAL = 0; +const NOOP = () => {}; + +let currentConfig = {}; +let currentLogs = []; +let interval; function getUUID() { - return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { - var piece = Math.random() * 16 | 0; - var elem = c == 'x' ? piece : (piece & 0x3 | 0x8); - return elem.toString(16); - }); + // eslint gets funny about bitwise + /* eslint-disable */ + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { + const piece = Math.random() * 16 | 0; + const elem = c === 'x' ? piece : (piece & 0x3 | 0x8); + return elem.toString(16); + }); + /* eslint-enable */ } -function setConfig(opts) { - currentConfig = { - endpoint: opts.endpoint, - clientUrl: opts.clientUrl || '', - interval: opts.interval || DEFAULT_INTERVAL, - sourceName: opts.sourceName || '', - hostName: opts.hostName || '', - sourceCategory: opts.sourceCategory || '', - session: opts.sessionKey || getUUID(), - onSuccess: opts.onSuccess || NOOP, - onError: opts.onError || NOOP - }; +function setConfig(config) { + currentConfig = { + endpoint: config.endpoint, + clientUrl: config.clientUrl || '', + interval: config.interval || DEFAULT_INTERVAL, + sourceName: config.sourceName || '', + hostName: config.hostName || '', + sourceCategory: config.sourceCategory || '', + session: config.sessionKey || getUUID(), + onSuccess: config.onSuccess || NOOP, + onError: config.onError || NOOP + }; } function sendLogs() { - if (currentLogs.length === 0) { - return; - } - var tempCategory = ''; - var logsToSend; - - try { - var headers = {'Content-Type': 'application/json'}; - if (currentConfig.sourceName !== '') { - _.extend(headers, {'X-Sumo-Name': currentConfig.sourceName}); - } - if (currentConfig.sourceCategory !== '') { - _.extend(headers, {'X-Sumo-Category': currentConfig.sourceCategory}); - } - if (currentConfig.hostName !== '') { - _.extend(headers, {'X-Sumo-Host': currentConfig.hostName}); + if (currentLogs.length === 0) { + return; } - logsToSend = currentLogs; - currentLogs = []; - request({ - method: 'POST', - url: currentConfig.endpoint, - headers: headers, - body: logsToSend.join('\n') - }, function (error, response) { - var err = !!error || response.statusCode < 200 || response.statusCode >= 400; - - if (err) { + let logsToSend; + + try { + const headers = { 'Content-Type': 'application/json' }; + if (currentConfig.sourceName !== '') { + _.extend(headers, { 'X-Sumo-Name': currentConfig.sourceName }); + } + if (currentConfig.sourceCategory !== '') { + _.extend(headers, { 'X-Sumo-Category': currentConfig.sourceCategory }); + } + if (currentConfig.hostName !== '') { + _.extend(headers, { 'X-Sumo-Host': currentConfig.hostName }); + } + + logsToSend = currentLogs; + currentLogs = []; + + request({ + method: 'POST', + url: currentConfig.endpoint, + headers, + body: logsToSend.join('\n') + }, (error, response) => { + const err = !!error || response.statusCode < 200 || response.statusCode >= 400; + + if (err) { + currentLogs = logsToSend; + currentConfig.onError(); + } else { + currentConfig.onSuccess(); + } + }); + } catch (ex) { currentLogs = logsToSend; currentConfig.onError(); - } else { - currentConfig.onSuccess(); - } - }); - } catch (ex) { - currentLogs = logsToSend; - currentConfig.onError(); - } + } } function SumoLogger(opts) { - if (!opts || !opts.hasOwnProperty('endpoint') || opts.endpoint === undefined || opts.endpoint === '') { - console.error('Sumo Logic Logger requires you to set an endpoint.'); - return; - } - - originalOpts = opts; - setConfig(opts); - this.startLogSending(); + if (!opts || !Object.prototype.hasOwnProperty.call(opts, 'endpoint') || opts.endpoint === undefined || opts.endpoint === '') { + console.error('Sumo Logic Logger requires you to set an endpoint.'); + return; + } + + setConfig(opts); + this.startLogSending(); } -SumoLogger.prototype.updateConfig = function (newOpts) { - try { - if (!_.isEmpty(newOpts)) { - if (newOpts.endpoint) { - currentConfig.endpoint = newOpts.endpoint; - } - if (newOpts.interval) { - currentConfig.interval = newOpts.interval; - this.startLogSending(); - } - if (newOpts.sourceCategory) { - currentConfig.sourceCategory = newOpts.sourceCategory; - } +SumoLogger.prototype.updateConfig = (newOpts) => { + try { + if (!_.isEmpty(newOpts)) { + if (newOpts.endpoint) { + currentConfig.endpoint = newOpts.endpoint; + } + if (newOpts.interval) { + currentConfig.interval = newOpts.interval; + SumoLogger.prototype.startLogSending(); + } + if (newOpts.sourceCategory) { + currentConfig.sourceCategory = newOpts.sourceCategory; + } + } + } catch (ex) { + console.error('Could not update Sumo Logic config'); + return false; } - } catch (ex) { - console.error('Could not update Sumo Logic config'); - return false; - } - return true; + return true; }; -SumoLogger.prototype.emptyLogQueue = function () { - currentLogs = []; +SumoLogger.prototype.emptyLogQueue = () => { + currentLogs = []; }; -SumoLogger.prototype.flushLogs = function () { - sendLogs(); +SumoLogger.prototype.flushLogs = () => { + sendLogs(); }; -SumoLogger.prototype.startLogSending = function() { +SumoLogger.prototype.startLogSending = () => { if (currentConfig.interval > 0) { - interval = setInterval(function() { + interval = setInterval(() => { sendLogs(); }, currentConfig.interval); } -} +}; -SumoLogger.prototype.stopLogSending = function() { +SumoLogger.prototype.stopLogSending = () => { clearInterval(interval); -} +}; -SumoLogger.prototype.log = function(msg, optConfig) { - if (!msg) { - console.error('Sumo Logic Logger requires that you pass a value to log.'); - return; - } - - var isArray = msg instanceof Array; - var testEl = isArray ? msg[0] : msg; - var type = typeof testEl; - - if (type === 'undefined') { - console.error('Sumo Logic Logger requires that you pass a value to log.'); - return; - } else if (type === 'object') { - if (Object.keys(msg).length === 0) { - console.error('Sumo Logic Logger requires that you pass a non-empty JSON object to log.'); - return; - } - } - - if (!isArray) { - msg = [msg]; - } - - var ts = new Date(); - var sessKey = currentConfig.session; - var client = {url: currentConfig.clientUrl}; - if (optConfig) { - if (optConfig.hasOwnProperty('sessionKey')) { - sessKey = optConfig.sessionKey; +SumoLogger.prototype.log = (msg, optionalConfig) => { + let message = msg; + + if (!message) { + console.error('Sumo Logic Logger requires that you pass a value to log.'); + return; } - if (optConfig.hasOwnProperty('timestamp')) { - ts = optConfig.timestamp; + const isArray = message instanceof Array; + const testEl = isArray ? message[0] : message; + const type = typeof testEl; + + if (type === 'undefined') { + console.error('Sumo Logic Logger requires that you pass a value to log.'); + return; + } else if (type === 'object') { + if (Object.keys(message).length === 0) { + console.error('Sumo Logic Logger requires that you pass a non-empty JSON object to log.'); + return; + } } - if (optConfig.hasOwnProperty('url')) { - client.url = optConfig.url + if (!isArray) { + message = [message]; } - } - timestamp = ts.toUTCString(); - - var msgs = msg.map(function (item) { - if (typeof item === "string") { - return JSON.stringify(_.extend({ - msg: item, - sessionId: sessKey, - timestamp: timestamp - }, client)); - } else { - var curr = { - sessionId: sessKey, - timestamp: timestamp - }; - return JSON.stringify(_.extend(curr, client, item)); + + let ts = new Date(); + let sessKey = currentConfig.session; + const client = { url: currentConfig.clientUrl }; + + if (optionalConfig) { + if (Object.prototype.hasOwnProperty.call(optionalConfig, 'sessionKey')) { + sessKey = optionalConfig.sessionKey; + } + + if (Object.prototype.hasOwnProperty.call(optionalConfig, 'timestamp')) { + ts = optionalConfig.timestamp; + } + + if (Object.prototype.hasOwnProperty.call(optionalConfig, 'url')) { + client.url = optionalConfig.url; + } } - }); - currentLogs = currentLogs.concat(msgs); - if (currentConfig.interval === 0) { - sendLogs(); - } + const timestamp = ts.toUTCString(); + + const messages = message.map((item) => { + if (typeof item === 'string') { + return JSON.stringify(_.extend({ + msg: item, + sessionId: sessKey, + timestamp + }, client)); + } + const current = { + sessionId: sessKey, + timestamp + }; + return JSON.stringify(_.extend(current, client, item)); + }); + + currentLogs = currentLogs.concat(messages); + + if (currentConfig.interval === 0) { + sendLogs(); + } }; module.exports = SumoLogger; diff --git a/test/sumoLogger.test.js b/test/sumoLogger.test.js index a3f8249..346f250 100644 --- a/test/sumoLogger.test.js +++ b/test/sumoLogger.test.js @@ -7,8 +7,8 @@ const isEmptyStub = sinon.stub(); const endpoint = 'endpoint'; const message = 'message'; -const timestamp = new Date; -const sessionKey = 'abcd1234' +const timestamp = new Date(); +const sessionKey = 'abcd1234'; const SumoLogger = proxyquire('../src/sumoLogger', { request: requestStub, @@ -229,14 +229,14 @@ describe('sumoLogger', () => { const logger = new SumoLogger({ endpoint }); logger.updateConfig({ - randomProperty: 'randomValue' + randomProperty: 'randomValue' }); logger.log(message); expect(requestStub).to.have.been.calledWithMatch({ headers: { - 'Content-Type': 'application/json', + 'Content-Type': 'application/json' }, url: 'endpoint' }); @@ -252,7 +252,7 @@ describe('sumoLogger', () => { expect(requestStub).to.have.been.calledWithMatch({ headers: { - 'Content-Type': 'application/json', + 'Content-Type': 'application/json' }, url: 'endpoint' }); @@ -299,22 +299,26 @@ describe('sumoLogger', () => { describe('error logging', () => { it('no options', () => { - new SumoLogger(); + const logger = new SumoLogger(); + logger.log(); expect(console.error).to.have.been.calledWith('Sumo Logic Logger requires you to set an endpoint.'); }); it('no options endpoint property', () => { - new SumoLogger({}); + const logger = new SumoLogger({}); + logger.log(); expect(console.error).to.have.been.calledWith('Sumo Logic Logger requires you to set an endpoint.'); }); it('undefined options endpoint property', () => { - new SumoLogger({ endpoint: undefined }); + const logger = new SumoLogger({ endpoint: undefined }); + logger.log(); expect(console.error).to.have.been.calledWith('Sumo Logic Logger requires you to set an endpoint.'); }); it('empty options endpoint property', () => { - new SumoLogger({ endpoint: '' }); + const logger = new SumoLogger({ endpoint: '' }); + logger.log(); expect(console.error).to.have.been.calledWith('Sumo Logic Logger requires you to set an endpoint.'); });