<commit_before>part of monadart;

/// A set of values to configure an instance of a web server application.
class ApplicationInstanceConfiguration {
  /// The address to listen for HTTP requests on.
  ///
  /// By default, this address will default to 'any' address. If [isIpv6Only] is true,
  /// the address will be any IPv6 address, otherwise, it will be any IPv4 address.
  dynamic address;

  /// The port to listen for HTTP requests on.
  ///
  /// Defaults to 8080.
  int port = 8080;

  /// Whether or not the application should only listen for IPv6 requests.
  ///
  /// Defaults to false. This flag impacts the [address] property if it has not been set.
  bool isIpv6Only = false;

  /// Whether or not the application's request handlers should use client-side HTTPS certificates.
  ///
  /// Defaults to false. If this is false and [serverCertificateName] is null, the server will
  /// run over HTTP instead of HTTPS.
  bool isUsingClientCertificate = false;

  /// Information for securing the application over HTTPS.
  ///
  /// Defaults to null. If this is null, this application will run unsecured over HTTP. To
  /// run securely over HTTPS, this property must be set with valid security details.
  SecurityContext securityContext = null;

  /// Options for instances of ApplicationPipeline to use when in this application.
  ///
  /// Allows delivery of custom configuration parameters to ApplicationPipeline instances
  /// that are attached to this application.
  Map<dynamic, dynamic> pipelineOptions;

  bool _shared = false;

  /// The default constructor.
  ApplicationInstanceConfiguration();

  /// A copy constructor
  ApplicationInstanceConfiguration.fromConfiguration(ApplicationInstanceConfiguration config) {
    var reflectedThis = reflect(this);
    var reflectedThat = reflect(config);
    reflectedThat.type.declarations.values.where((dm) => dm is VariableMirror).forEach((VariableMirror vm) {
      reflectedThis.setField(vm.simpleName, reflectedThat.getField(vm.simpleName).reflectee);
    });
  }
}

/// A abstract class that concrete subclasses will implement to provide request handling behavior.
///
/// [Application]s set up HTTP(S) listeners, but do not do anything with them. The behavior of how an application
/// responds to requests is defined by its [ApplicationPipeline]. Instances of this class must implement the
/// [handleRequest] method from [RequestHandler] - this is the entry point of all requests into this pipeline.
abstract class ApplicationPipeline extends RequestHandler {
  /// Passed in options for this pipeline from its owning [Application].
  ///
  /// These values give an opportunity for a pipeline to have a customization point within attachTo., like running
  /// the owning [Application] in 'Development' or 'Production' mode. This property will always be set prior to invoking attachTo, but may be null
  /// if the user did not set any configuration values.
  Map<String, dynamic> options;

  /// Returns the first handler in the pipeline.
  ///
  /// This method must be implemented. When a [ResourceRequest] is delivered to the pipeline, this
  /// handler will be the first to act on it.  Typically, this is an instance of [Router].
  /// If you need to add [context] to the [ResourceRequest], override [willReceiveRequest].
  RequestHandler initialHandler();

  /// Executed prior to this being opened.
  ///
  /// Use this method to perform any initialization that requires the [options]
  /// of this pipeline to be set. Initialization that does not require the use of [options]
  /// should take place in the constructor.
  /// This method will be executed prior to the start of the [HttpServer].
  Future willOpen() {
    return null;
  }

  /// Executed after the pipeline is attached to an [HttpServer].
  ///
  /// This method is executed after the [HttpServer] is started and
  /// the [initialHandler] has been set to start receiving requests.
  /// Because requests could potentially be queued prior to this pipeline
  /// being opened, a request could be received prior to this method being executed.
  void didOpen() {}

  /// Executed for each [ResourceRequest] that will be sent to this pipeline.
  ///
  /// This method will run prior to each request being [deliver]ed to this
  /// pipeline's [initialHandler]. Use this method to provide additional
  /// context to the request prior to it being handled.
  Future willReceiveRequest(ResourceRequest request) {
    return null;
  }
}

/// A container for web server applications.
///
/// Applications are responsible for managing starting and stopping of HTTP server instances across multiple isolates.
/// Behavior specific to an application is implemented by setting the [Application]'s [configuration], and providing
/// a [PipelineType] and [RequestType].
class Application<PipelineType extends ApplicationPipeline> {
  /// A list of items identifying the Isolates running a HTTP(s) listener and response handlers.
  List<_ServerSupervisor> servers = [];

  /// The configuration for the HTTP(s) server this application is running.
  ///
  /// This must be configured prior to [start]ing the [Application].
  ApplicationInstanceConfiguration configuration = new ApplicationInstanceConfiguration();

  /// Starts the application by spawning Isolates that listen for HTTP(s) requests.
  ///
  /// Returns a [Future] that completes when all Isolates have started listening for requests.
  /// The [numberOfInstances] defines how many Isolates are spawned running this application's [configuration]
  /// and [PipelineType].
  Future start({int numberOfInstances: 1}) async {
    if (configuration.address == null) {
      if (configuration.isIpv6Only) {
        configuration.address = InternetAddress.ANY_IP_V6;
      } else {
        configuration.address = InternetAddress.ANY_IP_V4;
      }
    }

    configuration._shared = numberOfInstances > 1;

    for (int i = 0; i < numberOfInstances; i++) {
      var config = new ApplicationInstanceConfiguration.fromConfiguration(configuration);

      var serverRecord = await _spawn(config, i + 1);
      servers.add(serverRecord);
    }

    var futures = servers.map((i) {
      return i.resume();
    });

    await Future.wait(futures);
  }

  Future stop() async {
    await Future.wait(servers.map((s) => s.stop()));
    servers = [];
  }

  Future<_ServerSupervisor> _spawn(ApplicationInstanceConfiguration config, int identifier) async {
    var receivePort = new ReceivePort();

    var pipelineTypeMirror = reflectType(PipelineType);
    var pipelineLibraryURI = (pipelineTypeMirror.owner as LibraryMirror).uri;
    var pipelineTypeName = MirrorSystem.getName(pipelineTypeMirror.simpleName);<commit_msg>Removed copy per isolate mechanism for app config<commit_after>part of monadart;

/// A set of values to configure an instance of a web server application.
class ApplicationInstanceConfiguration {
  /// The address to listen for HTTP requests on.
  ///
  /// By default, this address will default to 'any' address. If [isIpv6Only] is true,
  /// the address will be any IPv6 address, otherwise, it will be any IPv4 address.
  dynamic address;

  /// The port to listen for HTTP requests on.
  ///
  /// Defaults to 8080.
  int port = 8080;

  /// Whether or not the application should only listen for IPv6 requests.
  ///
  /// Defaults to false. This flag impacts the [address] property if it has not been set.
  bool isIpv6Only = false;

  /// Whether or not the application's request handlers should use client-side HTTPS certificates.
  ///
  /// Defaults to false. If this is false and [serverCertificateName] is null, the server will
  /// run over HTTP instead of HTTPS.
  bool isUsingClientCertificate = false;

  /// Information for securing the application over HTTPS.
  ///
  /// Defaults to null. If this is null, this application will run unsecured over HTTP. To
  /// run securely over HTTPS, this property must be set with valid security details.
  SecurityContext securityContext = null;

  /// Options for instances of ApplicationPipeline to use when in this application.
  ///
  /// Allows delivery of custom configuration parameters to ApplicationPipeline instances
  /// that are attached to this application.
  Map<dynamic, dynamic> pipelineOptions;

  bool _shared = false;
}

/// A abstract class that concrete subclasses will implement to provide request handling behavior.
///
/// [Application]s set up HTTP(S) listeners, but do not do anything with them. The behavior of how an application
/// responds to requests is defined by its [ApplicationPipeline]. Instances of this class must implement the
/// [handleRequest] method from [RequestHandler] - this is the entry point of all requests into this pipeline.
abstract class ApplicationPipeline extends RequestHandler {
  /// Passed in options for this pipeline from its owning [Application].
  ///
  /// These values give an opportunity for a pipeline to have a customization point within attachTo., like running
  /// the owning [Application] in 'Development' or 'Production' mode. This property will always be set prior to invoking attachTo, but may be null
  /// if the user did not set any configuration values.
  Map<String, dynamic> options;

  /// Returns the first handler in the pipeline.
  ///
  /// This method must be implemented. When a [ResourceRequest] is delivered to the pipeline, this
  /// handler will be the first to act on it.  Typically, this is an instance of [Router].
  /// If you need to add [context] to the [ResourceRequest], override [willReceiveRequest].
  RequestHandler initialHandler();

  /// Executed prior to this being opened.
  ///
  /// Use this method to perform any initialization that requires the [options]
  /// of this pipeline to be set. Initialization that does not require the use of [options]
  /// should take place in the constructor.
  /// This method will be executed prior to the start of the [HttpServer].
  Future willOpen() {
    return null;
  }

  /// Executed after the pipeline is attached to an [HttpServer].
  ///
  /// This method is executed after the [HttpServer] is started and
  /// the [initialHandler] has been set to start receiving requests.
  /// Because requests could potentially be queued prior to this pipeline
  /// being opened, a request could be received prior to this method being executed.
  void didOpen() {}

  /// Executed for each [ResourceRequest] that will be sent to this pipeline.
  ///
  /// This method will run prior to each request being [deliver]ed to this
  /// pipeline's [initialHandler]. Use this method to provide additional
  /// context to the request prior to it being handled.
  Future willReceiveRequest(ResourceRequest request) {
    return null;
  }
}

/// A container for web server applications.
///
/// Applications are responsible for managing starting and stopping of HTTP server instances across multiple isolates.
/// Behavior specific to an application is implemented by setting the [Application]'s [configuration], and providing
/// a [PipelineType] and [RequestType].
class Application<PipelineType extends ApplicationPipeline> {
  /// A list of items identifying the Isolates running a HTTP(s) listener and response handlers.
  List<_ServerSupervisor> servers = [];

  /// The configuration for the HTTP(s) server this application is running.
  ///
  /// This must be configured prior to [start]ing the [Application].
  ApplicationInstanceConfiguration configuration = new ApplicationInstanceConfiguration();

  /// Starts the application by spawning Isolates that listen for HTTP(s) requests.
  ///
  /// Returns a [Future] that completes when all Isolates have started listening for requests.
  /// The [numberOfInstances] defines how many Isolates are spawned running this application's [configuration]
  /// and [PipelineType].
  Future start({int numberOfInstances: 1}) async {
    if (configuration.address == null) {
      if (configuration.isIpv6Only) {
        configuration.address = InternetAddress.ANY_IP_V6;
      } else {
        configuration.address = InternetAddress.ANY_IP_V4;
      }
    }

    configuration._shared = numberOfInstances > 1;

    for (int i = 0; i < numberOfInstances; i++) {
      var serverRecord = await _spawn(configuration, i + 1);
      servers.add(serverRecord);
    }

    var futures = servers.map((i) {
      return i.resume();
    });

    await Future.wait(futures);
  }

  Future stop() async {
    await Future.wait(servers.map((s) => s.stop()));
    servers = [];
  }

  Future<_ServerSupervisor> _spawn(ApplicationInstanceConfiguration config, int identifier) async {
    var receivePort = new ReceivePort();

    var pipelineTypeMirror = reflectType(PipelineType);
    var pipelineLibraryURI = (pipelineTypeMirror.owner as LibraryMirror).uri;
    var pipelineTypeName = MirrorSystem.getName(pipelineTypeMirror.simpleName);
{'fraction_non_alphanumeric': 0.08719621088593835, 'fraction_numerical': 0.0031058311980743845, 'mean_word_length': 3.398907103825137, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '325497', 'n_tokens_mistral': 3259, 'n_tokens_neox': 3140, 'n_words': 1470}