Comparing version 0.6.0 to 0.7.0
// lambda connection tester | ||
'use strict'; | ||
var net = require('net'); | ||
/** | ||
@@ -16,33 +16,47 @@ * Test connection to a destination host/port. | ||
*/ | ||
exports.connect = function(event, context, callback) { | ||
if (!event || !event.endpointAddress || !event.endpointPort) { | ||
return callback(new Error("Connection failed. Event data missing")); | ||
return callback(new Error("Invalid call: Event data missing")); | ||
} | ||
if (!isInt(event.endpointPort)) { | ||
return callback(new Error('Connection failed. Event data malformed: "destinationPort" should be an integer')); | ||
return callback(new Error('Invalid call: Event data malformed: "destinationPort" should be an integer')); | ||
} | ||
var socket = new require('net').Socket().setTimeout(1000); | ||
var connectionSuccessful = false; | ||
var timeoutRef = undefined; | ||
var socket = net.createConnection(event.endpointPort, event.endpointAddress); | ||
socket.on('error', function() { | ||
clearTimeout(timeoutRef); | ||
socket.end(); | ||
fail(event, context, callback); | ||
}).on('timeout', function() { | ||
socket.end(); | ||
fail(event, context, callback); | ||
fail(event, callback); | ||
}); | ||
socket.connect(event.endpointPort, event.endpointAddress, function() { | ||
socket.end(); | ||
callback(null, { | ||
result: true | ||
}); | ||
}) | ||
socket.on('connect', function() { | ||
clearTimeout(timeoutRef); | ||
try{ | ||
socket.end(); | ||
} catch (e) { | ||
socket.destroy(); | ||
} | ||
callback(null, {result: true}); | ||
}); | ||
if (event.connectionTimeout_ms) { | ||
timeoutRef = setTimeout(function() { | ||
try{ | ||
socket.end(); | ||
} catch (e) { | ||
console.log('Error ending socket'); | ||
console.log(e); | ||
} | ||
console.log('Destroying socket after timeout') | ||
socket.destroy(); | ||
callback(null, {result: false}); | ||
}, event.connectionTimeout_ms); | ||
} | ||
}; | ||
function fail(event, context, callback) { | ||
function fail(event, callback) { | ||
const util = require('util'); | ||
@@ -49,0 +63,0 @@ var msg = util.format("Connection failed to %s:%s", event.endpointAddress, event.endpointPort) |
{ | ||
"name": "beady-eye", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": ".build/src/index.js", |
@@ -52,4 +52,21 @@ import { LambdaConnectionTester } from '../src/ConnectionTesters' | ||
InvocationType: 'RequestResponse', | ||
Payload: '{"endpointAddress":"mythical.host","endpointPort":31337}' | ||
Payload: '{"endpointAddress":"mythical.host","endpointPort":31337,"connectionTimeout_ms":1234}' | ||
}; | ||
await lambdaConnectionTester.tryConnectionTo(endpointAddress, 1234) | ||
expect(mock.calledOnce).toEqual(true); | ||
expect(mock.calledWith(expectedParams)).toEqual(true); | ||
}) | ||
it("should pass through a defaulted timeout parameter if it is missing", async () => { | ||
// test that the payload is correct: | ||
// - endpoint | ||
// - invocation type | ||
// - function name | ||
const mock = sinon.spy(callbackSuccessReturning(lambdaResponseData.successResult)); | ||
AWSMock.mock('Lambda','invoke',mock) | ||
const expectedParams = { | ||
FunctionName: 'ConnectionTestFunctionName', | ||
InvocationType: 'RequestResponse', | ||
Payload: '{"endpointAddress":"mythical.host","endpointPort":31337,"connectionTimeout_ms":2000}' | ||
}; | ||
await lambdaConnectionTester.tryConnectionTo(endpointAddress) | ||
@@ -56,0 +73,0 @@ expect(mock.calledOnce).toEqual(true); |
@@ -57,15 +57,2 @@ import { Credentials } from 'aws-sdk' | ||
tryConnection(port, address, timeout_ms) { | ||
let p = new Promise((resolve, reject) => { | ||
let s = net.createConnection(port, address); | ||
s.on('connect', () => { s.destroy(); resolve(true)} ); | ||
s.on('error', () => { s.destroy(); reject(false)} ); | ||
// If there has been no response, naturally terminate the socket/promise | ||
setTimeout(() => {s.destroy(); reject(false)}, timeout_ms); | ||
}); | ||
return p; | ||
} | ||
toString() { | ||
@@ -72,0 +59,0 @@ return `${this.typeName}: ${this.name}`; |
@@ -22,3 +22,4 @@ import { Lambda as AwsLambda } from 'aws-sdk' | ||
endpointAddress: endpoint.address, | ||
endpointPort: endpoint.port | ||
endpointPort: endpoint.port, | ||
connectionTimeout_ms: timeout_ms | ||
}) | ||
@@ -25,0 +26,0 @@ } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
226922
81
1517