this.init = function()

in knative-build/runtimes/javascript/runner.js [51:105]


    this.init = function(message) {
        DEBUG.functionStart("NodeActionRunner");
        function assertMainIsFunction() {
            DEBUG.dumpObject(thisRunner.userScriptMain,"this.Runner.userScriptMain");
            if (typeof thisRunner.userScriptMain !== 'function') {
                DEBUG.functionEndError("ERROR: Action entrypoint '" + message.main + "' is not a function.");
                throw "Action entrypoint '" + message.main + "' is not a function.";
            }
            DEBUG.functionEnd();
        }

        // Loading the user code.
        DEBUG.dumpObject(message.binary, "message.binary");
        if (message.binary) {
            // The code is a base64-encoded zip file.
            return unzipInTmpDir(message.code).then(function (moduleDir) {
                if(!fs.existsSync(path.join(moduleDir, 'package.json')) &&
                    !fs.existsSync(path.join(moduleDir, 'index.js'))) {
                    DEBUG.functionEndError("Promise.reject(): Zipped actions must contain either package.json or index.js at the root.");
                    return Promise.reject('Zipped actions must contain either package.json or index.js at the root.')
                }

                try {
                    // Set the executable directory to the project dir
                    process.chdir(moduleDir);
                    thisRunner.userScriptMain = eval('require("' + moduleDir + '").' + message.main);
                    assertMainIsFunction();
                    // The value 'true' has no special meaning here;
                    // the successful state is fully reflected in the
                    // successful resolution of the promise.
                    DEBUG.functionEndSuccess("return true;");
                    return true;
                } catch (e) {
                    DEBUG.functionEndError("Promise.reject(): " + e.message);
                    return Promise.reject(e);
                }
            }).catch(function (error) {
                DEBUG.functionEndError("Promise.reject(): " + error.message);
                return Promise.reject(error);
            });
        } else {
            // The code is a plain old JS file.
            try {
                thisRunner.userScriptMain = eval('(function(){' + message.code + '\nreturn ' + message.main + '})()');
                DEBUG.dumpObject(thisRunner.userScriptMain,"thisRunner.userScriptMain");
                assertMainIsFunction();
                // See comment above about 'true'; it has no specific meaning.
                DEBUG.functionEndSuccess("Promise.resolve(true)");
                return Promise.resolve(true);
            } catch (e) {
                DEBUG.functionEndError("Promise.reject(): " + e.message);
                return Promise.reject(e);
            }
        }
    };