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:
committed by
Soam Vasani
parent
e1cb5f6e09
commit
9bdaf3c74e
@@ -0,0 +1 @@
|
|||||||
|
node_modules/
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
# A docker image for the func container.
|
# A docker image for the func container.
|
||||||
|
|
||||||
FROM node:4-onbuild
|
FROM node:7-onbuild
|
||||||
|
|
||||||
ADD server.js /usr/src/app/server.js
|
ADD server.js /usr/src/app/server.js
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +1,26 @@
|
|||||||
{
|
{
|
||||||
"name": "fission-nodejs-runtime",
|
"name": "fission-nodejs-runtime",
|
||||||
"version": "0.0.0",
|
"version": "0.1.0",
|
||||||
"author": "Soam Vasani",
|
"author": "Soam Vasani",
|
||||||
"contributors": [
|
"contributors": [
|
||||||
{
|
{
|
||||||
"name": "Soam Vasani",
|
"name": "Soam Vasani",
|
||||||
"email": "soamvasani@platform9.com"
|
"email": "soamvasani@platform9.com"
|
||||||
}
|
|
||||||
],
|
|
||||||
"description": "NodeJS run container for the fission framework",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=4.2.2"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"express": "",
|
|
||||||
"minimist": "",
|
|
||||||
"body-parser": "",
|
|
||||||
"morgan": "",
|
|
||||||
|
|
||||||
"co": "~4.6.0",
|
|
||||||
"request": "",
|
|
||||||
"request-promise": "^1.0.2",
|
|
||||||
"mz": "~2.1.0",
|
|
||||||
"underscore": ">=1.8.3"
|
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
"description": "NodeJS run container for the fission framework",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=7.6.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"body-parser": "",
|
||||||
|
"co": "~4.6.0",
|
||||||
|
"express": "",
|
||||||
|
"minimist": "",
|
||||||
|
"morgan": "",
|
||||||
|
"mz": "~2.1.0",
|
||||||
|
"request": "^2.81.0",
|
||||||
|
"request-promise-native": "^1.0.3",
|
||||||
|
"underscore": ">=1.8.3"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,11 +75,13 @@ app.all('/', function (req, res) {
|
|||||||
res.status(500).send("Generic container: no requests supported");
|
res.status(500).send("Generic container: no requests supported");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const context = {
|
const context = {
|
||||||
request: req,
|
request: req,
|
||||||
response: res
|
response: res
|
||||||
// TODO: context should also have: URL template params, query string
|
// TODO: context should also have: URL template params, query string
|
||||||
};
|
};
|
||||||
|
|
||||||
function callback(status, body, headers) {
|
function callback(status, body, headers) {
|
||||||
if (!status)
|
if (!status)
|
||||||
return;
|
return;
|
||||||
@@ -90,18 +92,32 @@ app.all('/', function (req, res) {
|
|||||||
}
|
}
|
||||||
res.status(status).send(body);
|
res.status(status).send(body);
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
//
|
//
|
||||||
// Customizing the request context
|
// Customizing the request context
|
||||||
//
|
//
|
||||||
// If you want to modify the context to add anything to it,
|
// If you want to modify the context to add anything to it,
|
||||||
// you can do that here by adding properties to the context.
|
// you can do that here by adding properties to the context.
|
||||||
//
|
//
|
||||||
userFunction(context, callback);
|
|
||||||
} catch(e) {
|
let functionProm;
|
||||||
console.log(`Function error: ${e}`);
|
if (userFunction.length === 1) { // One argument (context)
|
||||||
callback(500, "Internal server error")
|
// Make sure their function returns a promise
|
||||||
|
Promise.resolve(userFunction(context)).then(function({ status, body, headers }) {
|
||||||
|
callback(status, body, headers);
|
||||||
|
}).catch(function(err) {
|
||||||
|
console.log(`Function error: ${e}`);
|
||||||
|
callback(500, "Internal server error");
|
||||||
|
});
|
||||||
|
} else { // 2 arguments (context, callback)
|
||||||
|
try {
|
||||||
|
userFunction(context, callback);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(`Function error: ${e}`);
|
||||||
|
callback(500, "Internal server error");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(argv.port);
|
app.listen(argv.port);
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
module.exports = function (context, callback) {
|
module.exports = async function (context) {
|
||||||
console.log("headers=", JSON.stringify(context.request.headers));
|
console.log("headers=", JSON.stringify(context.request.headers));
|
||||||
console.log("body=", JSON.stringify(context.request.body));
|
console.log("body=", JSON.stringify(context.request.body));
|
||||||
|
|
||||||
callback(200, "Hello, world !\n");
|
return {
|
||||||
|
status: 200,
|
||||||
|
body: "Hello, world !\n"
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
module.exports = function(context, callback) {
|
||||||
|
callback(200, "Hello, world!\n");
|
||||||
|
}
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
|
|
||||||
module.exports = function(context, callback) {
|
module.exports = async function(context) {
|
||||||
callback(200, "Hello, world!\n");
|
return {
|
||||||
|
status: 200,
|
||||||
|
body: "Hello, world!\n"
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ function upcaseFirst(s) {
|
|||||||
return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
|
return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendSlackMessage(msg, cb) {
|
async function sendSlackMessage(msg) {
|
||||||
let postData = `{"text": "${msg}"}`;
|
let postData = `{"text": "${msg}"}`;
|
||||||
let options = {
|
let options = {
|
||||||
hostname: "hooks.slack.com",
|
hostname: "hooks.slack.com",
|
||||||
@@ -33,17 +33,20 @@ function sendSlackMessage(msg, cb) {
|
|||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let req = https.request(options, function(res) {
|
|
||||||
console.log(`slack request status = ${res.statusCode}`);
|
return new Promise(function(resolve, reject) {
|
||||||
cb();
|
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);
|
console.log(context.request.headers);
|
||||||
|
|
||||||
let obj = context.request.body;
|
let obj = context.request.body;
|
||||||
let version = obj.metadata.resourceVersion;
|
let version = obj.metadata.resourceVersion;
|
||||||
let eventType = context.request.get('X-Kubernetes-Event-Type');
|
let eventType = context.request.get('X-Kubernetes-Event-Type');
|
||||||
@@ -54,10 +57,11 @@ module.exports = function(context, callback) {
|
|||||||
|
|
||||||
if (eventType == 'DELETED' || eventType == 'ADDED') {
|
if (eventType == 'DELETED' || eventType == 'ADDED') {
|
||||||
console.log("sending event to slack")
|
console.log("sending event to slack")
|
||||||
sendSlackMessage(msg, function() {
|
await sendSlackMessage(msg);
|
||||||
callback(200, "");
|
}
|
||||||
});
|
|
||||||
} else {
|
return {
|
||||||
callback(200, "");
|
status: 200,
|
||||||
|
body: ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-23
@@ -1,31 +1,37 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var http = require('http');
|
const rp = require('request-promise-native');
|
||||||
|
|
||||||
module.exports = function (context, callback) {
|
module.exports = async function (context) {
|
||||||
let body = context.request.body;
|
const body = context.request.body;
|
||||||
console.log(`body text: ${body['text']}`);
|
const symbol = body.symbol
|
||||||
|
|
||||||
var symbol = body['text'].split(' ')[1];
|
console.log(`Got symbol: ${symbol}`);
|
||||||
|
|
||||||
http.get({
|
if (!symbol) {
|
||||||
host: 'finance.google.com',
|
return {
|
||||||
path: `/finance/info?q=NYSE:${symbol}`
|
status: 400,
|
||||||
}, function(response) {
|
body: {
|
||||||
var resp = '';
|
text: 'You must provide a stock symbol.'
|
||||||
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?)" }`);
|
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
});
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user