Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow message to be passed in directly #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions lib/snsclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ function validateRequest(opts, message, cb) {
return validateMessage(pem, message, cb);
} else {
https.get(cert, function(res) {
if (res.statusCode != 200) {
return cb(new Error('Could not load certificate'));
}

var chunks = [];
res.on('data', function(chunk) {
chunks.push(chunk);
Expand All @@ -49,6 +53,8 @@ function validateRequest(opts, message, cb) {
res.on('error', function() {
return cb(new Error('Could not download certificate.'));
});
}).on('error', function(e) {
return cb(e);
});
}
}
Expand Down Expand Up @@ -107,6 +113,25 @@ function SNSClient(opts, cb) {
cb = opts;
opts = {};
}

var handleMessage = function(message) {
validateRequest(opts, message, function(err){
if(err) {
return cb(err);
}
if(message.Type === 'SubscriptionConfirmation') {
return https.get(url.parse(message.SubscribeURL));
}
if(message.Type === 'Notification') {
return cb(null, message);
}
});
};

if (opts.message) {
return handleMessage(opts.message);
}

return function SNSClient(req, res) {
var chunks = [];
req.on('data', function(chunk) {
Expand All @@ -120,17 +145,7 @@ function SNSClient(opts, cb) {
// catch a JSON parsing error
return cb(new Error('Error parsing JSON'));
}
validateRequest(opts, message, function(err){
if(err) {
return cb(err);
}
if(message.Type === 'SubscriptionConfirmation') {
return https.get(url.parse(message.SubscribeURL));
}
if(message.Type === 'Notification') {
return cb(null, message);
}
});
handleMessage(message, cb);
});
// end the request always before doing anything with the request.
// There isn't any reason to let it hang.
Expand Down