{"size": 7675, "ext": "js", "max_stars_repo_path": "node_modules/serverless-offline/dist/lambda/handler-runner/HandlerRunner.js", "max_stars_repo_name": "danilocecilia/serverless-http", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "node_modules/serverless-offline/dist/lambda/handler-runner/HandlerRunner.js", "max_issues_repo_name": "danilocecilia/serverless-http", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "node_modules/serverless-offline/dist/lambda/handler-runner/HandlerRunner.js", "max_forks_repo_name": "danilocecilia/serverless-http", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 42.1703296703, "max_line_length": 743, "alphanum_fraction": 0.6969381107}
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _debugLog = _interopRequireDefault(require("../../debugLog.js"));

var _serverlessLog = require("../../serverlessLog.js");

var _index = require("../../config/index.js");

var _index2 = require("../../utils/index.js");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _classPrivateFieldLooseBase(receiver, privateKey) { if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) { throw new TypeError("attempted to use private field on non-instance"); } return receiver; }

var id = 0;

function _classPrivateFieldLooseKey(name) { return "__private_" + id++ + "_" + name; }

var _env = _classPrivateFieldLooseKey("env");

var _funOptions = _classPrivateFieldLooseKey("funOptions");

var _options = _classPrivateFieldLooseKey("options");

var _runner = _classPrivateFieldLooseKey("runner");

class HandlerRunner {
  constructor(funOptions, options, env) {
    Object.defineProperty(this, _env, {
      writable: true,
      value: null
    });
    Object.defineProperty(this, _funOptions, {
      writable: true,
      value: null
    });
    Object.defineProperty(this, _options, {
      writable: true,
      value: null
    });
    Object.defineProperty(this, _runner, {
      writable: true,
      value: null
    });
    _classPrivateFieldLooseBase(this, _env)[_env] = env;
    _classPrivateFieldLooseBase(this, _funOptions)[_funOptions] = funOptions;
    _classPrivateFieldLooseBase(this, _options)[_options] = options;
  }

  async _loadRunner() {
    const {
      useDocker,
      useChildProcesses,
      useWorkerThreads,
      allowCache
    } = _classPrivateFieldLooseBase(this, _options)[_options];

    const {
      functionKey,
      handlerName,
      handlerPath,
      runtime,
      timeout
    } = _classPrivateFieldLooseBase(this, _funOptions)[_funOptions];

    (0, _debugLog.default)(`Loading handler... (${handlerPath})`);

    if (useDocker) {
      // https://github.com/lambci/docker-lambda/issues/329
      if (runtime === 'nodejs14.x') {
        (0, _serverlessLog.logWarning)('"nodejs14.x" runtime is not supported with docker. See https://github.com/lambci/docker-lambda/issues/329');
        throw new Error('Unsupported runtime');
      }

      const dockerOptions = {
        host: _classPrivateFieldLooseBase(this, _options)[_options].dockerHost,
        hostServicePath: _classPrivateFieldLooseBase(this, _options)[_options].dockerHostServicePath,
        layersDir: _classPrivateFieldLooseBase(this, _options)[_options].layersDir,
        network: _classPrivateFieldLooseBase(this, _options)[_options].dockerNetwork,
        readOnly: _classPrivateFieldLooseBase(this, _options)[_options].dockerReadOnly
      };
      const {
        default: DockerRunner
      } = await Promise.resolve().then(() => _interopRequireWildcard(require('./docker-runner/index.js')));
      return new DockerRunner(_classPrivateFieldLooseBase(this, _funOptions)[_funOptions], _classPrivateFieldLooseBase(this, _env)[_env], dockerOptions);
    }

    if (_index.supportedNodejs.has(runtime)) {
      if (useChildProcesses) {
        const {
          default: ChildProcessRunner
        } = await Promise.resolve().then(() => _interopRequireWildcard(require('./child-process-runner/index.js')));
        return new ChildProcessRunner(_classPrivateFieldLooseBase(this, _funOptions)[_funOptions], _classPrivateFieldLooseBase(this, _env)[_env], allowCache);
      }

      if (useWorkerThreads) {
        // worker threads
        this._verifyWorkerThreadCompatibility();

        const {
          default: WorkerThreadRunner
        } = await Promise.resolve().then(() => _interopRequireWildcard(require('./worker-thread-runner/index.js')));
        return new WorkerThreadRunner(_classPrivateFieldLooseBase(this, _funOptions)[_funOptions], _classPrivateFieldLooseBase(this, _env)[_env], allowCache);
      }

      const {
        default: InProcessRunner
      } = await Promise.resolve().then(() => _interopRequireWildcard(require('./in-process-runner/index.js')));
      return new InProcessRunner(functionKey, handlerPath, handlerName, _classPrivateFieldLooseBase(this, _env)[_env], timeout, allowCache);
    }

    if (_index.supportedPython.has(runtime)) {
      const {
        default: PythonRunner
      } = await Promise.resolve().then(() => _interopRequireWildcard(require('./python-runner/index.js')));
      return new PythonRunner(_classPrivateFieldLooseBase(this, _funOptions)[_funOptions], _classPrivateFieldLooseBase(this, _env)[_env], allowCache);
    }

    if (_index.supportedRuby.has(runtime)) {
      const {
        default: RubyRunner
      } = await Promise.resolve().then(() => _interopRequireWildcard(require('./ruby-runner/index.js')));
      return new RubyRunner(_classPrivateFieldLooseBase(this, _funOptions)[_funOptions], _classPrivateFieldLooseBase(this, _env)[_env], allowCache);
    }

    if (_index.supportedJava.has(runtime)) {
      const {
        default: JavaRunner
      } = await Promise.resolve().then(() => _interopRequireWildcard(require('./java-runner/index.js')));
      return new JavaRunner(_classPrivateFieldLooseBase(this, _funOptions)[_funOptions], _classPrivateFieldLooseBase(this, _env)[_env], allowCache);
    } // TODO FIXME


    throw new Error('Unsupported runtime');
  }

  _verifyWorkerThreadCompatibility() {
    const {
      node: currentVersion
    } = process.versions;
    const requiredVersionRange = '>=11.7.0';
    const versionIsSatisfied = (0, _index2.satisfiesVersionRange)(currentVersion, requiredVersionRange); // we're happy

    if (!versionIsSatisfied) {
      (0, _serverlessLog.logWarning)(`"worker threads" require node.js version ${requiredVersionRange}, but found version ${currentVersion}.
         To use this feature you have to update node.js to a later version.
        `);
      throw new Error('"worker threads" are not supported with this node.js version');
    }
  } // TEMP TODO FIXME


  isDockerRunner() {
    return _classPrivateFieldLooseBase(this, _runner)[_runner] && _classPrivateFieldLooseBase(this, _runner)[_runner].constructor.name === 'DockerRunner';
  } // () => Promise<void>


  cleanup() {
    // TODO console.log('handler runner cleanup')
    return _classPrivateFieldLooseBase(this, _runner)[_runner].cleanup();
  }

  async run(event, context) {
    if (_classPrivateFieldLooseBase(this, _runner)[_runner] == null) {
      _classPrivateFieldLooseBase(this, _runner)[_runner] = await this._loadRunner();
    }

    return _classPrivateFieldLooseBase(this, _runner)[_runner].run(event, context);
  }

}

exports.default = HandlerRunner;