var getUserFunction = function()

in cmd/nodejs/legacy_worker/converter/worker/worker.js [406:460]


var getUserFunction = function(functionFileRelativePath) {
  if (!fs.existsSync(getCodeAbsolutePath(functionFileRelativePath))) {
    processUserCodeError(
        'File ' + functionFileRelativePath + ' that is ' +
        'expected to define function doesn\'t exist.');
    return null;
  }

  try {
    var functionCode = require(getCodeAbsolutePath(functionFileRelativePath));
    var userFunction =
        ENTRY_POINT.split('.').reduce(function(code, entryPointPart) {
          if (typeof code === 'undefined') {
            return undefined;
          } else {
            return code[entryPointPart];
          }
        }, functionCode);

    if (typeof userFunction === 'undefined') {
      if (functionCode.hasOwnProperty('function')) {
        userFunction = functionCode['function'];
      } else {
        processUserCodeError(
            'Node.js module defined by file ' + functionFileRelativePath +
            ' is expected to export function named ' + ENTRY_POINT);
        return null;
      }
    }

    if (typeof userFunction != 'function') {
      processUserCodeError(
          'The function exported from file ' + functionFileRelativePath +
          ' as ' + ENTRY_POINT +
          ' needs to be of type function. Got: ' + typeof userFunction);
      return null;
    }

    return userFunction;
  } catch (ex) {
    var errorDetails = getErrorDetails(ex);
    var additionalHint = '';
    if (errorDetails.includes('Cannot find module')) {
      additionalHint =
          'Did you list all required modules in the package.json ' +
          'dependencies?\n';
    } else {
      additionalHint = 'Is there a syntax error in your code?\n';
    }
    processUserCodeError(
        'Code in file ' + functionFileRelativePath + ' can\'t be loaded.\n' +
        additionalHint + 'Detailed stack trace: ' + errorDetails);
    return null;
  }
};