rabbah commented on a change in pull request #119: An ability to run on Knative along with OpenWhisk URL: https://github.com/apache/incubator-openwhisk-runtime-nodejs/pull/119#discussion_r276424338
########## File path: core/nodejsActionBase/platform/knative.js ########## @@ -0,0 +1,487 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +const OW_ENV_PREFIX = "__OW_"; +const CONTENT_TYPE = "Content-Type"; + +/** + * Determine if runtime is a "stem" cell, i.e., can be initialized with request init. data + * @param env + * @returns {boolean} + */ +function isStemCell(env) { + let actionCode = env.__OW_ACTION_CODE; + // It is a stem cell if valid code is "built into" the runtime's process environment. + return (typeof actionCode === 'undefined' || actionCode.length === 0); +} + +/** + * Determine if the request (body) contains valid activation data. + * @param req + * @returns {boolean} + */ +function hasActivationData(req) { + // it is a valid activation if the body contains an activation and value keys with data. + if (typeof req.body !== "undefined" && + typeof req.body.activation !== "undefined" && + typeof req.body.value !== "undefined") { + return true; + } + return false; +} + +/** + * Determine if the request (body) contains valid init data. + * @param req + * @returns {boolean} + */ +function hasInitData(req) { + // it is a valid init. if the body contains an init key with data. + if (typeof req.body !== "undefined" && + typeof req.body.init !== "undefined") { + return true; + } + return false; +} + + +/** + * Remove all INIT data from the value data that will be passed to the user function. + * @param body + */ +function removeInitData(body) { + + if (typeof body !== "undefined" && + typeof body.value !== "undefined") { + delete body.value.code; + delete body.value.main; + delete body.value.binary; + delete body.value.raw; + } +} + +/** + * Pre-process the incoming + */ +function preProcessInitData(env, initdata, valuedata, activationdata) { + try { + // Set defaults to use INIT data not provided on the request + // Look first to the process (i.e., Container's) environment variables. + var main = (typeof env.__OW_ACTION_MAIN === 'undefined') ? "main" : env.__OW_ACTION_MAIN; + // TODO: Throw error if CODE is NOT defined! + var code = (typeof env.__OW_ACTION_CODE === 'undefined') ? "" : env.__OW_ACTION_CODE; + var binary = (typeof env.__OW_ACTION_BINARY === 'undefined') ? false : env.__OW_ACTION_BINARY.toLowerCase() === "true"; + // TODO: default to empty? + var actionName = (typeof env.__OW_ACTION_NAME === 'undefined') ? "" : env.__OW_ACTION_NAME; + var raw = (typeof env.__OW_ACTION_RAW === 'undefined') ? false : env.__OW_ACTION_RAW.toLowerCase() === "true"; + + + // Look for init data within the request (i.e., "stem cell" runtime, where code is injected by request) + if (typeof(initdata) !== "undefined") { + if (initdata.name && typeof initdata.name === 'string') { + actionName = initdata.name; + } + if (initdata.main && typeof initdata.main === 'string') { + main = initdata.main; + } + if (initdata.code && typeof initdata.code === 'string') { + code = initdata.code; + } + if (initdata.binary) { + if (typeof initdata.binary === 'boolean') { + binary = initdata.binary; + } else { + throw ("Invalid Init. data; expected boolean for key 'binary'."); + } + } + if (initdata.raw ) { + if (typeof initdata.raw === 'boolean') { + raw = initdata.raw; + } else { + throw ("Invalid Init. data; expected boolean for key 'raw'."); + } + } + } + + // Move the init data to the request body under the "value" key. + // This will allow us to reuse the "openwhisk" /init route handler function + valuedata.main = main; + valuedata.code = code; + valuedata.binary = binary; + valuedata.raw = raw; + + // Action name is a special case, as we have a key collision on "name" between init. data and request + // 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" || Review comment: extra space after `(`. is there a linter running to catch these? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
