<commit_before>define(module, function(exports, require) {

  // http://www.faqs.org/rfcs/rfc6455.html

  var qp = require('qp-utility');
  var log = require('qp-library/log');

  qp.make(exports, {

    ns: 'qp-library/websocket',

    socket: null,

    id: 0,
    key: '',
    session_id: '',
    ua: '',
    version: '',
    channel: '',
    protocols: null,

    json: false,

    init: function(options) {
      this.socket = options.socket;
      this.parse_headers(options.headers);
      this.parse_url(options.url);
      this.socket.on('close', () => {
        this.url = null;
        this.socket = null;
      });
    },

    parse_headers: function(headers) {
      this.key = headers['sec-websocket-key'] || '';
      this.ua = headers['user-agent'] || '';
      this.version = headers['sec-websocket-version'] || '';
      this.protocols = (headers['sec-websocket-protocol'] || '').split(', ');
      this.json = qp.contains(this.protocols, 'json-message');
    },

    parse_url: function(url) {
      if (url.parts.length === 2) this.channel = url.parts[1];
      if (url.parts.length === 3) {
        this.session_id = url.parts[1];
        this.channel = url.parts[2];
      }
    },

    send: function(data, options, done) {
      if (this.socket) {
        log.socket('SEND', '#' + this.user_id, qp.stringify(data));
        if (this.json) data = JSON.stringify(data, null, 2);
        this.socket.send(data, options, done);
      } else {
        log.socket('SEND', 'Failed. Socket closed;', this.key);
      }
    },

    close: function(code, message) {
      this.socket.close(code, message || '');
    }

  });

});<commit_msg>websocket; guard socket ref<commit_after>define(module, function(exports, require) {

  // http://www.faqs.org/rfcs/rfc6455.html

  var qp = require('qp-utility');
  var log = require('qp-library/log');

  qp.make(exports, {

    ns: 'qp-library/websocket',

    socket: null,

    id: 0,
    key: '',
    session_id: '',
    ua: '',
    version: '',
    channel: '',
    protocols: null,

    json: false,

    init: function(options) {
      this.socket = options.socket;
      this.parse_headers(options.headers);
      this.parse_url(options.url);
      this.socket.on('close', () => {
        this.url = null;
        this.socket = null;
      });
    },

    parse_headers: function(headers) {
      this.key = headers['sec-websocket-key'] || '';
      this.ua = headers['user-agent'] || '';
      this.version = headers['sec-websocket-version'] || '';
      this.protocols = (headers['sec-websocket-protocol'] || '').split(', ');
      this.json = qp.contains(this.protocols, 'json-message');
    },

    parse_url: function(url) {
      if (url.parts.length === 2) this.channel = url.parts[1];
      if (url.parts.length === 3) {
        this.session_id = url.parts[1];
        this.channel = url.parts[2];
      }
    },

    send: function(data, options, done) {
      if (this.socket) {
        log.socket('SEND', '#' + this.user_id, qp.stringify(data));
        if (this.json) data = JSON.stringify(data, null, 2);
        this.socket.send(data, options, done);
      } else {
        log.socket('SEND', 'Failed. Socket closed;', this.key);
      }
    },

    close: function(code, message) {
      if (this.socket) {
        this.socket.close(code, message || '');
      }
    }

  });

});
{'fraction_non_alphanumeric': 0.18696040488240548, 'fraction_numerical': 0.006549568323905924, 'mean_word_length': 2.3836858006042294, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '764567', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1117, 'n_words': 193}