{"size": 1820, "ext": "js", "max_stars_repo_path": "packages/@pollyjs/adapter-node-http/src/index.js", "max_stars_repo_name": "geigerzaehler/pollyjs", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/@pollyjs/adapter-node-http/src/index.js", "max_issues_repo_name": "geigerzaehler/pollyjs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/@pollyjs/adapter-node-http/src/index.js", "max_forks_repo_name": "geigerzaehler/pollyjs", "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": 24.9315068493, "max_line_length": 88, "alphanum_fraction": 0.6961538462}
import Adapter from '@pollyjs/adapter';

import HttpWrapper from './-private/http-wrapper';

export default class HttpAdapter extends Adapter {
  static get name() {
    return 'node-http';
  }

  get defaultOptions() {
    return {
      transports: ['http', 'https']
    };
  }

  onConnect() {
    const { transports } = this.options;

    this.assert(
      'Invalid transports provided. At least one supported transport must be specified',
      transports.includes('http') || transports.includes('https')
    );

    this.httpWrapper = new HttpWrapper({
      adapter: this,
      options: this.options
    });
    this.httpWrapper.patch();
  }

  onDisconnect() {
    if (this.httpWrapper) {
      this.httpWrapper.restore();
      delete this.httpWrapper;
    }
  }

  async onRecord(pollyRequest) {
    await this.passthroughRequest(pollyRequest);
    await this.persister.recordRequest(pollyRequest);
    await this.respond(pollyRequest);
  }

  async onReplay(pollyRequest, { statusCode, headers, body }) {
    await pollyRequest.respond(statusCode, headers, body);

    await this.respond(pollyRequest);
  }

  async onPassthrough(pollyRequest) {
    await this.passthroughRequest(pollyRequest);
    await this.respond(pollyRequest);
  }

  async onIntercept(pollyRequest, { statusCode, headers, body }) {
    await pollyRequest.respond(statusCode, headers, body);
    await this.respond(pollyRequest);
  }

  async passthroughRequest(pollyRequest) {
    const [transportWrapper] = pollyRequest.requestArguments;
    const res = await transportWrapper.passthrough(pollyRequest);

    await pollyRequest.respond(res.statusCode, res.headers, res.body);
  }

  async respond(pollyRequest) {
    const [transportWrapper] = pollyRequest.requestArguments;

    return transportWrapper.respond(pollyRequest);
  }
}
