text: "use strict";
  
  exports.__esModule = true;
  exports.sendRequest = sendRequest;
  exports.error = error;
  exports.callResponseEventCallbackForProcessedRequest = callResponseEventCallbackForProcessedRequest;
  exports.callOnRequestEventCallback = callOnRequestEventCallback;
  exports.callOnResponseEventCallbackForFailedSameOriginCheck = callOnResponseEventCallbackForFailedSameOriginCheck;
  exports.callOnConfigureResponseEventForNonProcessedRequest = callOnConfigureResponseEventForNonProcessedRequest;
  exports.callOnResponseEventCallbackWithBodyForNonProcessedRequest = callOnResponseEventCallbackWithBodyForNonProcessedRequest;
  exports.callOnResponseEventCallbackWithoutBodyForNonProcessedResource = callOnResponseEventCallbackWithoutBodyForNonProcessedResource;
  exports.callOnResponseEventCallbackForMotModifiedResource = callOnResponseEventCallbackForMotModifiedResource;
  
  var _fileRequest = _interopRequireDefault(require("./file-request"));
  
  var _destinationRequest = _interopRequireDefault(require("./destination-request"));
  
  var _info = require("../session/events/info");
  
  var _promisifyStream = _interopRequireDefault(require("../utils/promisify-stream"));
  
  var _configureResponseEvent = _interopRequireDefault(require("../session/events/configure-response-event"));
  
  var _requestEvent = _interopRequireDefault(require("../session/events/request-event"));
  
  var _responseEvent = _interopRequireDefault(require("../session/events/response-event"));
  
  var _names = _interopRequireDefault(require("../session/events/names"));
  
  var _configureResponseEventOptions = _interopRequireDefault(require("../session/events/configure-response-event-options"));
  
  var _buffer = require("../utils/buffer");
  
  var _stream = require("stream");
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  
  function sendRequest(ctx) {
    return new Promise(resolve => {
      const req = ctx.isFileProtocol ? new _fileRequest.default(ctx.reqOpts.url) : new _destinationRequest.default(ctx.reqOpts);
      ctx.goToNextStage = false;
      req.on('response', res => {
        if (ctx.isWebSocketConnectionReset) {
          res.destroy();
          resolve();
        }
  
        ctx.destRes = res;
        ctx.goToNextStage = true;
        resolve();
      });
      req.on('error', () => {
        ctx.hasDestReqErr = true;
        resolve();
      });
      req.on('fatalError', err => {
        error(ctx, err);
        resolve();
      });
      req.on('socketHangUp', () => {
        ctx.req.socket.end();
        resolve();
      });
    });
  }
  
  function error(ctx, err) {
    if (ctx.isPage && !ctx.isIframe) ctx.session.handlePageError(ctx, err);else if (ctx.isFetch || ctx.isXhr) ctx.req.destroy();else ctx.closeWithError(500, err.toString());
  }
  
  function callResponseEventCallbackForProcessedRequest(_x, _x2) {
    return _callResponseEventCallbackForProcessedRequest.apply(this, arguments);
  }
  
  function _callResponseEventCallbackForProcessedRequest() {
    _callResponseEventCallbackForProcessedRequest = _asyncToGenerator(function* (ctx, configureResponseEvent) {
      const responseInfo = new _info.ResponseInfo(ctx);
      const preparedResponseInfo = new _info.PreparedResponseInfo(responseInfo, configureResponseEvent.opts);
      const responseEvent = new _responseEvent.default(configureResponseEvent._requestFilterRule, preparedResponseInfo);
      yield ctx.session.callRequestEventCallback(_names.default.onResponse, configureResponseEvent._requestFilterRule, responseEvent);
    });
    return _callResponseEventCallbackForProcessedRequest.apply(this, arguments);
  }
  
  function callOnRequestEventCallback(_x3, _x4, _x5) {
    return _callOnRequestEventCallback.apply(this, arguments);
  }
  
  function _callOnRequestEventCallback() {
    _callOnRequestEventCallback = _asyncToGenerator(function* (ctx, rule, reqInfo) {
      const requestEvent = new _requestEvent.default(ctx, rule, reqInfo);
      yield ctx.session.callRequestEventCallback(_names.default.onRequest, rule, requestEvent);
    });
    return _callOnRequestEventCallback.apply(this, arguments);
  }
  
  function callOnResponseEventCallbackForFailedSameOriginCheck(_x6, _x7, _x8) {
    return _callOnResponseEventCallbackForFailedSameOriginCheck.apply(this, arguments);
  }
  
  function _callOnResponseEventCallbackForFailedSameOriginCheck() {
    _callOnResponseEventCallbackForFailedSameOriginCheck = _asyncToGenerator(function* (ctx, rule, configureOpts) {
      const responseInfo = new _info.ResponseInfo(ctx);
      const preparedResponseInfo = new _info.PreparedResponseInfo(responseInfo, configureOpts);
      const responseEvent = new _responseEvent.default(rule, preparedResponseInfo);
      yield ctx.session.callRequestEventCallback(_names.default.onResponse, rule, responseEvent);
    });
    return _callOnResponseEventCallbackForFailedSameOriginCheck.apply(this, arguments);
  }
  
  function callOnConfigureResponseEventForNonProcessedRequest(_x9) {
    return _callOnConfigureResponseEventForNonProcessedRequest.apply(this, arguments);
  }
  
  function _callOnConfigureResponseEventForNonProcessedRequest() {
    _callOnConfigureResponseEventForNonProcessedRequest = _asyncToGenerator(function* (ctx) {
      yield ctx.forEachRequestFilterRule(
      /*#__PURE__*/
      function () {
        var _ref = _asyncToGenerator(function* (rule) {
          const configureResponseEvent = new _configureResponseEvent.default(ctx, rule, _configureResponseEventOptions.default.DEFAULT);
          yield ctx.session.callRequestEventCallback(_names.default.onConfigureResponse, rule, configureResponseEvent);
          ctx.onResponseEventData.push({
            rule,
            opts: configureResponseEvent.opts
          });
        });
  
        return function (_x15) {
          return _ref.apply(this, arguments);
        };
      }());
    });
    return _callOnConfigureResponseEventForNonProcessedRequest.apply(this, arguments);
  }
  
  function callOnResponseEventCallbackWithBodyForNonProcessedRequest(_x10, _x11) {
    return _callOnResponseEventCallbackWithBodyForNonProcessedRequest.apply(this, arguments);
  }
  
  function _callOnResponseEventCallbackWithBodyForNonProcessedRequest() {
    _callOnResponseEventCallbackWithBodyForNonProcessedRequest = _asyncToGenerator(function* (ctx, onResponseEventDataWithBody) {
      const destResBodyCollectorStream = new _stream.PassThrough();
      ctx.destRes.pipe(destResBodyCollectorStream);
      (0, _promisifyStream.default)(destResBodyCollectorStream).then(
      /*#__PURE__*/
      function () {
        var _ref2 = _asyncToGenerator(function* (data) {
          ctx.saveNonProcessedDestResBody(data);
          const responseInfo = new _info.ResponseInfo(ctx);
          yield Promise.all(onResponseEventDataWithBody.map(
          /*#__PURE__*/
          function () {
            var _ref3 = _asyncToGenerator(function* ({
              rule,
              opts
            }) {
              const preparedResponseInfo = new _info.PreparedResponseInfo(responseInfo, opts);
              const responseEvent = new _responseEvent.default(rule, preparedResponseInfo);
              yield ctx.session.callRequestEventCallback(_names.default.onResponse, rule, responseEvent);
            });
  
            return function (_x17) {
              return _ref3.apply(this, arguments);
            };
          }()));
          (0, _buffer.toReadableStream)(data).pipe(ctx.res);
        });
  
        return function (_x16) {
          return _ref2.apply(this, arguments);
        };
      }());
    });
    return _callOnResponseEventCallbackWithBodyForNonProcessedRequest.apply(this, arguments);
  }
  
  function callOnResponseEventCallbackWithoutBodyForNonProcessedResource(_x12, _x13) {
    return _callOnResponseEventCallbackWithoutBodyForNonProcessedResource.apply(this, arguments);
  }
  
  function _callOnResponseEventCallbackWithoutBodyForNonProcessedResource() {
    _callOnResponseEventCallbackWithoutBodyForNonProcessedResource = _asyncToGenerator(function* (ctx, onResponseEventDataWithoutBody) {
      const responseInfo = new _info.ResponseInfo(ctx);
      yield Promise.all(onResponseEventDataWithoutBody.map(
      /*#__PURE__*/
      function () {
        var _ref4 = _asyncToGenerator(function* (item) {
          const preparedResponseInfo = new _info.PreparedResponseInfo(responseInfo, item.opts);
          const responseEvent = new _responseEvent.default(item.rule, preparedResponseInfo);
          yield ctx.session.callRequestEventCallback(_names.default.onResponse, item.rule, responseEvent);
        });
  
        return function (_x18) {
          return _ref4.apply(this, arguments);
        };
      }()));
      ctx.destRes.pipe(ctx.res);
    });
    return _callOnResponseEventCallbackWithoutBodyForNonProcessedResource.apply(this, arguments);
  }
  
  function callOnResponseEventCallbackForMotModifiedResource(_x14) {
    return _callOnResponseEventCallbackForMotModifiedResource.apply(this, arguments);
  }
  
  function _callOnResponseEventCallbackForMotModifiedResource() {
    _callOnResponseEventCallbackForMotModifiedResource = _asyncToGenerator(function* (ctx) {
      const responseInfo = new _info.ResponseInfo(ctx);
      yield Promise.all(ctx.onResponseEventData.map(
      /*#__PURE__*/
      function () {
        var _ref5 = _asyncToGenerator(function* (item) {
          const preparedResponseInfo = new _info.PreparedResponseInfo(responseInfo, item.opts);
          const responseEvent = new _responseEvent.default(item.rule, preparedResponseInfo);
          yield ctx.session.callRequestEventCallback(_names.default.onResponse, item.rule, responseEvent);
        });
  
        return function (_x19) {
          return _ref5.apply(this, arguments);
        };
      }()));
      ctx.res.end();
    });
    return _callOnResponseEventCallbackForMotModifiedResource.apply(this, arguments);
  }