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

Commit

Permalink
feat: debug instead of console logs
Browse files Browse the repository at this point in the history
- renaming the `debugger` module so it's not a reserved keyword
- using `debug` module instead of `console.log` so we get an idea of performance impact
- refactored some code to make it clearer
- starting to remove `JSON.stringify` from error logs
- adding a debug print after transmitting events
  • Loading branch information
Shesekino committed Oct 10, 2018
1 parent fc5b9c3 commit f8e6766
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 35 deletions.
4 changes: 3 additions & 1 deletion lib/ast.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var fs = require('fs');
var acorn = require('acorn');
var debug = require('debug')('snyk:nodejs');

function findAllVulnerableFunctionsInScriptPath(
scriptPath, vulnerableFunctionNames) {
Expand Down Expand Up @@ -58,7 +59,8 @@ function findAllVulnerableFunctionsInScriptPath(
};
});
} catch (error) {
console.log(JSON.stringify(error));
debug(`Error finding vulnerable methods ${vulnerableFunctionNames}` +
` in script path ${scriptPath}: ${error}`);
}
return decleardFunctions;
};
Expand Down
28 changes: 14 additions & 14 deletions lib/debugger.js → lib/debugger-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var debug = require('debug')('snyk:nodejs:inspector');
var inspector = require('inspector');

var vulnMgmt = require('./vuln-mgmt');
var moduleUtils = require('./moduleUtils');
var transmitter = require('./transmitter');
Expand Down Expand Up @@ -34,7 +36,7 @@ function start() {
vulnMgmt.getVulnerableMethodsLocations(moduleInfo, scriptPath);
Object.keys(vulnerableMethods).forEach((methodName) => {
var methodLocation = vulnerableMethods[methodName];
setBreakpointsOnVulnearbleMethods(
setBreakpointsOnVulnerableMethods(
script.params.url, moduleInfo, methodName, methodLocation);
});
}
Expand All @@ -43,13 +45,14 @@ function start() {
try {
handleDebuggerPausedEvent(message.params);
} catch (error) {
console.log(JSON.stringify(error));
debug('Error handling debugger paused event: ' +
`${JSON.stringify(message)}, ${error}`);
}
});
session.post('Debugger.enable');
session.post('Debugger.setBreakpointsActive', {active: true});

function setBreakpointsOnVulnearbleMethods(
function setBreakpointsOnVulnerableMethods(
moduleUrl, moduleInfo, methodName, methodLocation) {
session.post('Debugger.setBreakpointByUrl',
{
Expand All @@ -63,22 +66,19 @@ function start() {
methodName,
moduleInfo,
};
var message = {
methodName,
moduleInfo,
message: 'Added breakpoint',
};
console.log(JSON.stringify(message));
debug(`Successfully set a breakpoint on method ${methodName}` +
` in module ${moduleInfo.name}`);
} else {
var errorMessage = {
const errorEvent = {
methodName,
moduleInfo,
error,
message: 'Failed to set breakpoint',
message: 'Failed setting a breakpoint',
};
console.log(JSON.stringify(errorMessage));
transmitter.addEvent( {
error: errorMessage,
debug(`Failed setting a breakpoint on method ${methodName} ` +
` in module ${moduleInfo.name}: ${error}`);
transmitter.addEvent({
error: errorEvent,
timestamp: (new Date()).toISOString(),
});
}
Expand Down
9 changes: 6 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
var debug = require('./debugger');
var debug = require('debug')('snyk:nodejs-agent');

var transmitter = require('./transmitter');
var debuggerWrapper = require('./debugger-wrapper');


function start(config) {
try {
transmitter.start(config);
debug.start();
debuggerWrapper.start();
} catch (error) {
console.log(JSON.stringify(error));
debug('Generic error while starting snyk-nodejs-agent:', error);
};
}

Expand Down
16 changes: 7 additions & 9 deletions lib/transmitter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
var debug = require('debug')('snyk:nodejs:transmitter');
var needle = require('needle');

const DEFAULT_BEACON_BASE_URL = 'https://homebase.snyk.io';
const DEFAULT_BEACON_URL_PATH = '/api/v1/beacon';
const DEFAULT_BEACON_INTERVAL_MS = 60000; // 60 seconds
const DEFAULT_BEACON_INTERVAL_MS = 60 * 1000;

var eventsToSend = [];
function start({
projectId, // mandatory
url = DEFAULT_BEACON_BASE_URL + DEFAULT_BEACON_URL_PATH,
beaconIntervalMs = DEFAULT_BEACON_INTERVAL_MS,
projectId,
url=DEFAULT_BEACON_BASE_URL + DEFAULT_BEACON_URL_PATH,
beaconIntervalMs=DEFAULT_BEACON_INTERVAL_MS,
}) {
if (!projectId) {
throw new Error('No projectId defined in configuration');
Expand All @@ -22,17 +23,14 @@ function start({
function transmitData() {
if (eventsToSend.length) {
needle.post(url, {projectId, eventsToSend}, {json: true});
debug(eventsToSend.length, 'events transmitted.');
}
}
}

function addEvent(event) {
eventsToSend.push(event);
var message = {
event,
message: 'Event was added',
};
console.log(JSON.stringify(message));
debug(`Event added to transmission queue: ${JSON.stringify(event)}`);
}

module.exports = {start,addEvent};
6 changes: 3 additions & 3 deletions lib/vuln-mgmt.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var fs = require('fs');
var path = require('path');
var debug = require('debug')('snyk:nodejs-agent');
var semver = require('semver');

var ast = require('./ast');

const METHODS_METATDATA_FILE = path.join(__dirname, '../methods.json');
Expand All @@ -13,9 +15,7 @@ function loadVulnerabiltiesMetadata() {
if (!exists) {
vulnerabiltiesMetadata = require(METHODS_METATDATA_FILE);
} else {
console.log(JSON.stringify(
{message: 'Using app defined method.json to load vulnerabilties'})
);
debug('Using app defined method.json to load vulnerabilties');
vulnerabiltiesMetadata = require(appMetadataFilePath);
}
return vulnerabiltiesMetadata;
Expand Down
34 changes: 30 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"dependencies": {
"acorn": "^5.7.1",
"debug": "^4.0.1",
"needle": "^2.2.1",
"semver": "^5.5.1"
},
Expand Down
2 changes: 1 addition & 1 deletion test/debugger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const test = require('tap').test;
const sinon = require('sinon');
var inspector = require('inspector');
const EventEmitter = require('events');
var dbg = require('../lib/debugger');
var dbg = require('../lib/debugger-wrapper');
var vulnMgmt = require('../lib/vuln-mgmt');
var moduleUtils = require('../lib/moduleUtils');
var transmitter = require('../lib/transmitter');
Expand Down

0 comments on commit f8e6766

Please sign in to comment.