Upgrade node environment to Node.js 7.6.0+ (#151)

Upgrades the node environment to NodeJS 7.6.0.  Functions can now use async/await and promises; they can return a promise instead of using a callback.  The change preserves compatibility with the callback style.  

* Upgrade node environment to Node.js 7.6.0+

* Bump package version

* Preserve compatibility for callbacks

* Cleanup

* Re-add hello callback example

* Make sure not returning won't blow everything up

* Update stock example with request promise
This commit is contained in:
Robert Herhold
2017-03-14 16:00:26 -07:00
committed by Soam Vasani
parent e1cb5f6e09
commit 9bdaf3c74e
9 changed files with 112 additions and 76 deletions
+17 -13
View File
@@ -23,7 +23,7 @@ function upcaseFirst(s) {
return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
}
function sendSlackMessage(msg, cb) {
async function sendSlackMessage(msg) {
let postData = `{"text": "${msg}"}`;
let options = {
hostname: "hooks.slack.com",
@@ -33,17 +33,20 @@ function sendSlackMessage(msg, cb) {
"Content-Type": "application/json"
}
};
let req = https.request(options, function(res) {
console.log(`slack request status = ${res.statusCode}`);
cb();
return new Promise(function(resolve, reject) {
let req = https.request(options, function(res) {
console.log(`slack request status = ${res.statusCode}`);
return resolve();
});
req.write(postData);
req.end();
});
req.write(postData);
req.end();
}
module.exports = function(context, callback) {
module.exports = async function(context) {
console.log(context.request.headers);
let obj = context.request.body;
let version = obj.metadata.resourceVersion;
let eventType = context.request.get('X-Kubernetes-Event-Type');
@@ -54,10 +57,11 @@ module.exports = function(context, callback) {
if (eventType == 'DELETED' || eventType == 'ADDED') {
console.log("sending event to slack")
sendSlackMessage(msg, function() {
callback(200, "");
});
} else {
callback(200, "");
await sendSlackMessage(msg);
}
return {
status: 200,
body: ""
}
}