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
+4
View File
@@ -0,0 +1,4 @@
module.exports = function(context, callback) {
callback(200, "Hello, world!\n");
}
+5 -2
View File
@@ -1,4 +1,7 @@
module.exports = function(context, callback) {
callback(200, "Hello, world!\n");
module.exports = async function(context) {
return {
status: 200,
body: "Hello, world!\n"
};
}
+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: ""
}
}
+29 -23
View File
@@ -1,31 +1,37 @@
'use strict';
var http = require('http');
const rp = require('request-promise-native');
module.exports = function (context, callback) {
let body = context.request.body;
console.log(`body text: ${body['text']}`);
module.exports = async function (context) {
const body = context.request.body;
const symbol = body.symbol
var symbol = body['text'].split(' ')[1];
console.log(`Got symbol: ${symbol}`);
http.get({
host: 'finance.google.com',
path: `/finance/info?q=NYSE:${symbol}`
}, function(response) {
var resp = '';
response.on('data', function(d) {
resp += d;
});
response.on('end', function() {
try {
var parsed = JSON.parse(resp.slice(3));
var lastTrade = parsed[0]['l_cur']
callback(200, `{ "text": "${symbol} last traded at ${lastTrade}" }`);
} catch (e) {
callback(200, `{ "text": "Error (invalid NYSE symbol?)" }`);
if (!symbol) {
return {
status: 400,
body: {
text: 'You must provide a stock symbol.'
}
});
});
};
}
try {
const response = await rp(`http://finance.google.com/finance/info?q=NYSE:${symbol}`);
const parsed = JSON.parse(response.slice(3));
const lastTrade = parsed[0]['l_cur'];
return {
status: 200,
body: {
text: `${symbol} last traded at ${lastTrade}`
}
};
} catch (e) {
console.error(e);
return {
status: 500,
body: e
};
}
}