format changes and adding back initialized as its used by knative (#251)
* format changes and adding back initialized as its used by knative
* Add eslint automation
* first pass of eslint
* first pass of eslint
diff --git a/knative-build/runtimes/javascript/app.js b/knative-build/runtimes/javascript/app.js
index 7658fe5..6532fd8 100644
--- a/knative-build/runtimes/javascript/app.js
+++ b/knative-build/runtimes/javascript/app.js
@@ -54,12 +54,12 @@
// default to "openwhisk" platform initialization if not defined
// TODO export isvalid() from platform, if undefined this is OK to default, but if not valid value then error out
-if(typeof targetPlatform === "undefined") {
+if (typeof targetPlatform === "undefined") {
targetPlatform = platformFactory.PLATFORM_OPENWHISK;
// console.log("__OW_RUNTIME_PLATFORM is undefined; defaulting to 'openwhisk' ...");
}
-if(!platformFactory.isSupportedPlatform(targetPlatform)){
+if (!platformFactory.isSupportedPlatform(targetPlatform)) {
console.error("__OW_RUNTIME_PLATFORM ("+targetPlatform+") is not supported by the runtime.");
process.exit(9);
}
@@ -72,7 +72,7 @@
const platformImpl = factory.createPlatformImpl(targetPlatform);
-if(typeof platformImpl !== "undefined"){
+if (typeof platformImpl !== "undefined") {
platformImpl.registerHandlers(app, platformImpl);
diff --git a/knative-build/runtimes/javascript/platform/knative.js b/knative-build/runtimes/javascript/platform/knative.js
index 1fe3d2c..a537aa5 100644
--- a/knative-build/runtimes/javascript/platform/knative.js
+++ b/knative-build/runtimes/javascript/platform/knative.js
@@ -138,9 +138,9 @@
// param. data (as they both appear within "body.value") so we must save it to its final location
// as the default Action name as part of the activation data
// NOTE: if action name is not present in the action data, we will set it regardless even if an empty string
- if( typeof(activationdata) !== "undefined" ) {
- if ( typeof(activationdata.action_name) === "undefined" ||
- (typeof(activationdata.action_name) === "string" && activationdata.action_name.length == 0 )){
+ if (typeof(activationdata) !== "undefined" ) {
+ if (typeof(activationdata.action_name) === "undefined" ||
+ (typeof(activationdata.action_name) === "string" && activationdata.action_name.length == 0)){
activationdata.action_name = actionName;
}
}
diff --git a/knative-build/runtimes/javascript/platform/openwhisk.js b/knative-build/runtimes/javascript/platform/openwhisk.js
index b72a35f..6be4308 100644
--- a/knative-build/runtimes/javascript/platform/openwhisk.js
+++ b/knative-build/runtimes/javascript/platform/openwhisk.js
@@ -20,7 +20,7 @@
function PlatformOpenWhiskImpl(platformFactory) {
DEBUG.functionStart();
- DEBUG.dumpObject(platformFactory, "platformFactory");
+ DEBUG.dumpObject(platformFactory, 'platformFactory');
// Provide access to common runtime services
var service = platformFactory.service;
diff --git a/knative-build/runtimes/javascript/src/service.js b/knative-build/runtimes/javascript/src/service.js
index 8adfe83..813cb77 100644
--- a/knative-build/runtimes/javascript/src/service.js
+++ b/knative-build/runtimes/javascript/src/service.js
@@ -26,23 +26,23 @@
ready: 'ready',
starting: 'starting',
running: 'running',
- stopped: 'stopped'
+ stopped: 'stopped',
};
// TODO: save the entire configuration for use by any of the route handlers
var status = Status.ready;
- var ignoreRunStatus = config.allowConcurrent === undefined ? false : config.allowConcurrent.toLowerCase() === "true";
+ var ignoreRunStatus = config.allowConcurrent === undefined ? false : config.allowConcurrent.toLowerCase() === 'true';
var server = undefined;
var userCodeRunner = undefined;
- DEBUG.trace("Initialize: status=" + status);
- DEBUG.trace("Initialize: ignoreRunStatus=" + ignoreRunStatus);
+ DEBUG.trace('Initialize: status=' + status);
+ DEBUG.trace('Initialize: ignoreRunStatus=' + ignoreRunStatus);
function setStatus(newStatus) {
- DEBUG.functionStart("newStatus=" + newStatus + " (oldStatus=" + status + ")");
+ DEBUG.functionStart('newStatus=' + newStatus + ' (oldStatus=' + status + ')');
if (status !== Status.stopped) {
status = newStatus;
}
- DEBUG.functionEnd("status=" + status);
+ DEBUG.functionEnd('status=' + status);
}
/**
@@ -53,15 +53,24 @@
* { code: int, response: object }
*
*/
- function responseMessage (code, response) {
+ function responseMessage(code, response) {
return { code: code, response: response };
}
- function errorMessage (code, errorMsg) {
+ function errorMessage(code, errorMsg) {
return responseMessage(code, { error: errorMsg });
}
/**
+ * Indicates if we have been initialized which is determined by if we have
+ * created a NodeActionRunner.
+ * @returns {boolean}
+ */
+ this.initialized = function isInitialized(){
+ return (typeof userCodeRunner !== 'undefined');
+ };
+
+ /**
* Starts the server.
*
* @param app express app
@@ -71,12 +80,12 @@
server = app.listen(config.port, function() {
var host = server.address().address;
var port = server.address().port;
- DEBUG.trace("listening: host: [" + host + "], port: [" + port + "]", "Express (callback)");
+ DEBUG.trace('listening: host: [' + host + '], port: [' + port + ']', 'Express (callback)');
});
// This is required as http server will auto disconnect in 2 minutes, this to not auto disconnect at all
server.timeout = 0;
- DEBUG.dumpObject(server, "server");
+ DEBUG.dumpObject(server, 'server');
DEBUG.functionEnd();
};
@@ -85,7 +94,7 @@
* req.body = { main: String, code: String, binary: Boolean }
*/
this.initCode = function initCode(req) {
- DEBUG.functionStart("status=" + status);
+ DEBUG.functionStart('status=' + status);
if (status === Status.ready && userCodeRunner === undefined) {
@@ -93,36 +102,36 @@
var body = req.body || {};
var message = body.value || {};
- DEBUG.dumpObject(body,"body");
- DEBUG.dumpObject(message,"message");
+ DEBUG.dumpObject(body, 'body');
+ DEBUG.dumpObject(message, 'message');
if (message.main && message.code && typeof message.main === 'string' && typeof message.code === 'string') {
- return doInit(message).then(function (result) {
+ return doInit(message).then(function(result) {
setStatus(Status.ready);
- DEBUG.dumpObject(result, "result","initCode");
- DEBUG.functionEndSuccess("[200] { OK: true }", "initCode");
+ DEBUG.dumpObject(result, 'result', 'initCode');
+ DEBUG.functionEndSuccess('[200] { OK: true }', 'initCode');
return responseMessage(200, { OK: true });
- }).catch(function (error) {
+ }).catch(function(error) {
setStatus(Status.stopped);
- var errStr = "Initialization has failed due to: " + error.stack ? String(error.stack) : error;
- DEBUG.functionEndError("[502] " + errStr, "initCode");
+ var errStr = 'Initialization has failed due to: ' + error.stack ? String(error.stack) : error;
+ DEBUG.functionEndError('[502] ' + errStr, 'initCode');
return Promise.reject(errorMessage(502, errStr));
});
} else {
setStatus(Status.ready);
- var msg = "Missing main/no code to execute.";
- DEBUG.functionEndError("[403] " + msg);
+ var msg = 'Missing main/no code to execute.';
+ DEBUG.functionEndError('[403] ' + msg);
return Promise.reject(errorMessage(403, msg));
}
} else if (userCodeRunner !== undefined) {
- var msg = "Cannot initialize the action more than once.";
- console.error("Internal system error:", msg);
- DEBUG.functionEndError("[403] " + msg);
+ var msg = 'Cannot initialize the action more than once.';
+ console.error('Internal system error:', msg);
+ DEBUG.functionEndError('[403] ' + msg);
return Promise.reject(errorMessage(403, msg));
} else {
- var msg = "System not ready, status is " + status + ".";
- console.error("Internal system error:", msg);
- DEBUG.functionEndError("[403] " + msg);
+ var msg = 'System not ready, status is ' + status + '.';
+ console.error('Internal system error:', msg);
+ DEBUG.functionEndError('[403] ' + msg);
return Promise.reject(errorMessage(403, msg));
}
};
@@ -136,35 +145,35 @@
* req.body = { value: Object, meta { activationId : int } }
*/
this.runCode = function runCode(req) {
- DEBUG.functionStart("status=" + status);
- DEBUG.dumpObject(req, "request");
+ DEBUG.functionStart('status=' + status);
+ DEBUG.dumpObject(req, 'request');
if (status === Status.ready) {
if (!ignoreRunStatus) {
setStatus(Status.running);
}
- return doRun(req).then(function (result) {
+ return doRun(req).then(function(result) {
if (!ignoreRunStatus) {
setStatus(Status.ready);
}
- DEBUG.dumpObject(result, "result", "runCode");
- if (typeof result !== "object") {
- DEBUG.functionEndError("[502] The action did not return a dictionary.","runCode");
- return errorMessage(502, "The action did not return a dictionary.");
+ DEBUG.dumpObject(result, 'result', 'runCode');
+ if (typeof result !== 'object') {
+ DEBUG.functionEndError('[502] The action did not return a dictionary.', 'runCode');
+ return errorMessage(502, 'The action did not return a dictionary.');
} else {
- DEBUG.functionEndSuccess("[200] result: " + result, "runCode");
+ DEBUG.functionEndSuccess('[200] result: ' + result, 'runCode');
return responseMessage(200, result);
}
- }).catch(function (error) {
- var msg = "An error has occurred: " + error;
+ }).catch(function(error) {
+ var msg = 'An error has occurred: ' + error;
setStatus(Status.stopped);
- DEBUG.functionEndError("[502]: " + msg);
+ DEBUG.functionEndError('[502]: ' + msg);
return Promise.reject(errorMessage(502, msg));
});
} else {
- var msg = "System not ready, status is " + status + ".";
- console.error("Internal system error:", msg);
- DEBUG.functionEndError("[403] " + msg);
+ var msg = 'System not ready, status is ' + status + '.';
+ console.error('Internal system error:', msg);
+ DEBUG.functionEndError('[403] ' + msg);
return Promise.reject(errorMessage(403, msg));
}
};
@@ -173,51 +182,51 @@
userCodeRunner = new NodeActionRunner();
DEBUG.functionStart();
- DEBUG.dumpObject(message,"message")
- return userCodeRunner.init(message).then(function (result) {
+ DEBUG.dumpObject(message, 'message');
+ return userCodeRunner.init(message).then(function(result) {
// 'true' has no particular meaning here. The fact that the promise
// is resolved successfully in itself carries the intended message
// that initialization succeeded.
- DEBUG.functionEndSuccess("userCodeRunner.init() Success");
+ DEBUG.functionEndSuccess('userCodeRunner.init() Success');
return true;
- }).catch(function (error) {
+ }).catch(function(error) {
// emit error to activation log then flush the logs as this
// is the end of the activation
console.error('Error during initialization:', error);
writeMarkers();
- DEBUG.functionEndError("userCodeRunner.init() Error: " + error);
+ DEBUG.functionEndError('userCodeRunner.init() Error: ' + error);
return Promise.reject(error);
});
}
function doRun(req) {
DEBUG.functionStart();
- DEBUG.dumpObject(req,"request");
+ DEBUG.dumpObject(req, 'request');
var msg = req && req.body || {};
- DEBUG.dumpObject(msg,"msg");
- DEBUG.trace("Adding process environment variables:");
- // Move per-activation keys to process env. vars with __OW_ (reserved) prefix)
+ DEBUG.dumpObject(msg, 'msg');
+ DEBUG.trace('Adding process environment variables:');
+ // Move per-activation keys to process env. vars with __OW_ (reserved) prefix
Object.keys(msg).forEach(
- function (k) {
- if(typeof msg[k] === 'string' && k !== 'value'){
+ function(k) {
+ if (typeof msg[k] === 'string' && k !== 'value'){
var envVariable = '__OW_' + k.toUpperCase();
process.env['__OW_' + k.toUpperCase()] = msg[k];
- DEBUG.dumpObject(process.env[envVariable], envVariable, "doRun");
+ DEBUG.dumpObject(process.env[envVariable], envVariable, 'doRun');
}
}
);
return userCodeRunner.run(msg.value).then(function(result) {
- if (typeof result !== "object") {
+ if (typeof result !== 'object') {
console.error('Result must be of type object but has type "' + typeof result + '":', result);
}
writeMarkers();
- DEBUG.functionEndSuccess("userCodeRunner.run(): Success (" + result + ")", "doRun");
+ DEBUG.functionEndSuccess('userCodeRunner.run(): Success (' + result + ')', 'doRun');
return result;
- }).catch(function (error) {
+ }).catch(function(error) {
console.error(error);
writeMarkers();
- DEBUG.functionEndError("userCodeRunner.run(): Error:" + error, "doRun");
+ DEBUG.functionEndError('userCodeRunner.run(): Error:' + error, 'doRun');
return Promise.reject(error);
});
}