<commit_before>library angel_framework.http.response_context;

import 'dart:async';
import 'dart:convert';
import 'dart:convert' as c show json;
import 'dart:io' show BytesBuilder, Cookie;
import 'dart:typed_data';

import 'package:angel_route/angel_route.dart';
import 'package:file/file.dart';
import 'package:http_parser/http_parser.dart';
import 'package:mime/mime.dart';

import 'controller.dart';
import 'request_context.dart';
import 'server.dart' show Angel;

final RegExp _straySlashes = RegExp(r'(^/+)|(/+$)');

/// A convenience wrapper around an outgoing HTTP request.
abstract class ResponseContext<RawResponse>
    implements StreamConsumer<List<int>>, StreamSink<List<int>>, StringSink {
  final Map properties = {};
  final CaseInsensitiveMap<String> _headers = CaseInsensitiveMap<String>.from({
    'content-type': 'text/plain',
    'server': 'angel',
  });

  Completer _done;
  int _statusCode = 200;

  /// The [Angel] instance that is sending a response.
  Angel app;

  /// Is `Transfer-Encoding` chunked?
  bool chunked;

  /// Any and all cookies to be sent to the user.
  final List<Cookie> cookies = [];

  /// A set of [Converter] objects that can be used to encode response data.
  ///
  /// At most one encoder will ever be used to convert data.
  final Map<String, Converter<List<int>, List<int>>> encoders = {};

  /// A [Map] of data to inject when `res.render` is called.
  ///
  /// This can be used to reduce boilerplate when using templating engines.
  final Map<String, dynamic> renderParams = {};

  /// Points to the [RequestContext] corresponding to this response.
  RequestContext get correspondingRequest;

  @override
  Future get done => (_done ?? Completer()).future;

  /// Headers that will be sent to the user.
  ///
  /// Note that if you have already started writing to the underlying stream, headers will not persist.
  CaseInsensitiveMap<String> get headers => _headers;

  /// Serializes response data into a String.
  ///
  /// The default is conversion into JSON via `package:json_god`.
  ///
  /// If you are 100% sure that your response handlers will only
  /// be JSON-encodable objects (i.e. primitives, `List`s and `Map`s),
  /// then consider setting [serializer] to `JSON.encode`.
  ///
  /// To set it globally for the whole [app], use the following helper:
  /// ```dart
  /// app.injectSerializer(JSON.encode);
  /// ```
  FutureOr<String> Function(dynamic) serializer = c.json.encode;

  /// This response's status code.
  int get statusCode => _statusCode;

  set statusCode(int value) {
    if (!isOpen) {
      throw closed();
    } else {<commit_msg>Update `serialize()` docs for Angel v2<commit_after>library angel_framework.http.response_context;

import 'dart:async';
import 'dart:convert';
import 'dart:convert' as c show json;
import 'dart:io' show BytesBuilder, Cookie;
import 'dart:typed_data';

import 'package:angel_route/angel_route.dart';
import 'package:file/file.dart';
import 'package:http_parser/http_parser.dart';
import 'package:mime/mime.dart';

import 'controller.dart';
import 'request_context.dart';
import 'server.dart' show Angel;

final RegExp _straySlashes = RegExp(r'(^/+)|(/+$)');

/// A convenience wrapper around an outgoing HTTP request.
abstract class ResponseContext<RawResponse>
    implements StreamConsumer<List<int>>, StreamSink<List<int>>, StringSink {
  final Map properties = {};
  final CaseInsensitiveMap<String> _headers = CaseInsensitiveMap<String>.from({
    'content-type': 'text/plain',
    'server': 'angel',
  });

  Completer _done;
  int _statusCode = 200;

  /// The [Angel] instance that is sending a response.
  Angel app;

  /// Is `Transfer-Encoding` chunked?
  bool chunked;

  /// Any and all cookies to be sent to the user.
  final List<Cookie> cookies = [];

  /// A set of [Converter] objects that can be used to encode response data.
  ///
  /// At most one encoder will ever be used to convert data.
  final Map<String, Converter<List<int>, List<int>>> encoders = {};

  /// A [Map] of data to inject when `res.render` is called.
  ///
  /// This can be used to reduce boilerplate when using templating engines.
  final Map<String, dynamic> renderParams = {};

  /// Points to the [RequestContext] corresponding to this response.
  RequestContext get correspondingRequest;

  @override
  Future get done => (_done ?? Completer()).future;

  /// Headers that will be sent to the user.
  ///
  /// Note that if you have already started writing to the underlying stream, headers will not persist.
  CaseInsensitiveMap<String> get headers => _headers;

  /// Serializes response data into a String.
  ///
  /// The default is conversion into JSON via `json.encode`.
  ///
  /// If you are 100% sure that your response handlers will only
  /// be JSON-encodable objects (i.e. primitives, `List`s and `Map`s),
  /// then consider setting [serializer] to `JSON.encode`.
  ///
  /// To set it globally for the whole [app], use the following helper:
  /// ```dart
  /// app.injectSerializer(JSON.encode);
  /// ```
  FutureOr<String> Function(dynamic) serializer = c.json.encode;

  /// This response's status code.
  int get statusCode => _statusCode;

  set statusCode(int value) {
    if (!isOpen) {
      throw closed();
    } else {
{'fraction_non_alphanumeric': 0.13041825095057033, 'fraction_numerical': 0.00247148288973384, 'mean_word_length': 3.570807993049522, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6940561', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1558, 'n_words': 545}