<commit_before>var {AsyncResponse} = require('ringo/jsgi/connector');
var objects = require('ringo/utils/objects');

/**
 * EventSource (or Server-Sent-Events) is a server push technology utilizing
 * plain HTTP responses. The event stream format defines three types of messages:
 *
 *    * data-only
 *    * named events with data
 *    * comments
 *
 * One method for each message type is available. Data is expected to be in JSON format.
 * EventSource instances are thread-safe.
 *
 * The EventSource wraps an AsyncResponse and is used similarly:
 *
 * @example
 *
 *    var eventSource = new EventSource(request);
 *    // send headers and start heartbeat
 *    eventSource.start({
 *       "X-Additional-Header": "Foo"
 *    });
 *    setInterval(function() {
 *        eventSource.event('foo-field', 'foo-value');
 *    }, 5 * 1000);
 *
 *    // close the response. No more data can be written
 *    // and the hearbeat stops.
 *    eventSource.close();
 *
 *    // Each EventSource instance exposes the wrapped JSGI asynchronous response
 *    eventSource.response
 *
 *
 *
 * @param {JSGIRequest} request
 * @see ringo/jsgi/connector#AsyncResponse
 * @see https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
 *
 */
exports.EventSource = function(request) {
   var CRLF = String("\r\n").toByteArray('utf-8');
   var EVENT_FIELD = String("event: ").toByteArray('utf-8');
   var DATA_FIELD = String("data: ").toByteArray('utf-8');
   var COMMENT_FIELD = String(": ").toByteArray('utf-8');
   var heartBeat = null;

   /**
    * Send a named event
    * @throws {java.io.IOException}
    */
   this.event = sync(function(name, data) {
      this.response.write(EVENT_FIELD);
      this.response.write(name);
      this.response.write(CRLF);
      this.data(data);
   }, this);

   /**
    * Send data
    * @throws {java.io.IOException}
    */
   this.data = sync(function(data) {
      this.response.write(DATA_FIELD);
      this.response.write(data);
      this.response.write(CRLF);
      this.response.write(CRLF);
      this.response.flush();
   }, this);
<commit_msg>move constants outside of class and simplify<commit_after>var {AsyncResponse} = require('ringo/jsgi/connector');
var objects = require('ringo/utils/objects');

const CRLF = "\r\n".toByteArray('utf-8');
const EVENT_FIELD = "event: ".toByteArray('utf-8');
const DATA_FIELD = "data: ".toByteArray('utf-8');
const COMMENT_FIELD = ": ".toByteArray('utf-8');
/**
 * EventSource (or Server-Sent-Events) is a server push technology utilizing
 * plain HTTP responses. The event stream format defines three types of messages:
 *
 *    * data-only
 *    * named events with data
 *    * comments
 *
 * One method for each message type is available. Data is expected to be in JSON format.
 * EventSource instances are thread-safe.
 *
 * The EventSource wraps an AsyncResponse and is used similarly:
 *
 * @example
 *
 *    var eventSource = new EventSource(request);
 *    // send headers and start heartbeat
 *    eventSource.start({
 *       "X-Additional-Header": "Foo"
 *    });
 *    setInterval(function() {
 *        eventSource.event('foo-field', 'foo-value');
 *    }, 5 * 1000);
 *
 *    // close the response. No more data can be written
 *    // and the hearbeat stops.
 *    eventSource.close();
 *
 *    // Each EventSource instance exposes the wrapped JSGI asynchronous response
 *    eventSource.response
 *
 *
 *
 * @param {JSGIRequest} request
 * @see ringo/jsgi/connector#AsyncResponse
 * @see https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
 *
 */
exports.EventSource = function(request) {
   var heartBeat = null;

   /**
    * Send a named event
    * @throws {java.io.IOException}
    */
   this.event = sync(function(name, data) {
      this.response.write(EVENT_FIELD);
      this.response.write(name);
      this.response.write(CRLF);
      this.data(data);
   }, this);

   /**
    * Send data
    * @throws {java.io.IOException}
    */
   this.data = sync(function(data) {
      this.response.write(DATA_FIELD);
      this.response.write(data);
      this.response.write(CRLF);
      this.response.write(CRLF);
      this.response.flush();
   }, this);

{'fraction_non_alphanumeric': 0.1403260099220411, 'fraction_numerical': 0.004252303330970942, 'mean_word_length': 3.0829315332690452, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '4207726', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1278, 'n_words': 319}