<commit_before>'use strict';

const io = require('socket.io-client');

class Pubsub {
  constructor(ipfs) {
    this.ipfs = ipfs;
    this._socket = null;
    this._subscriptions = {};
    this.onConnected = null;
    this.onConnectionError = null;
  }

  connect(host, port, username, password) {
    return new Promise((resolve, reject) => {
      if(!this._socket)
        this._socket = io.connect(`http://${host}:${port}`, { 'forceNew': true });

      this._socket.on('connect', resolve);
      this._socket.on('connect_error', (err) => reject(new Error(`Connection refused to ${host}:${port}`)));
      this._socket.on('disconnect', (socket) => console.log(`Disconnected from http://${host}:${port}`));
      this._socket.on('error', (e) => console.log('Pubsub socket error:', e));
      this._socket.on('message', this._handleMessage.bind(this));
      this._socket.on('subscribed', this._handleMessage.bind(this));
    });
  }

  disconnect() {
    if(this._socket)
      this._socket.disconnect();
  }

  subscribe(hash, password, callback) {
    if(!this._subscriptions[hash]) {
      this._subscriptions[hash] = { callback: callback };
      this._socket.emit('subscribe', { channel: hash }); // calls back with 'subscribed' event
    }
  }

  unsubscribe(hash) {
    this._socket.emit('unsubscribe', { channel: hash });
    delete this._subscriptions[hash];
  }

  publish(hash, message) {
    this._socket.send({ channel: hash, message: message });
  }

  _handleMessage(hash, message) {
    if(this._subscriptions[hash] && this._subscriptions[hash].callback)
      this._subscriptions[hash].callback(hash, message);<commit_msg>Send data as string instead of object<commit_after>'use strict';

const io = require('socket.io-client');

class Pubsub {
  constructor(ipfs) {
    this.ipfs = ipfs;
    this._socket = null;
    this._subscriptions = {};
    this.onConnected = null;
    this.onConnectionError = null;
  }

  connect(host, port, username, password) {
    return new Promise((resolve, reject) => {
      if(!this._socket)
        this._socket = io.connect(`http://${host}:${port}`, { 'forceNew': true });

      this._socket.on('connect', resolve);
      this._socket.on('connect_error', (err) => reject(new Error(`Connection refused to ${host}:${port}`)));
      this._socket.on('disconnect', (socket) => console.log(`Disconnected from http://${host}:${port}`));
      this._socket.on('error', (e) => console.log('Pubsub socket error:', e));
      this._socket.on('message', this._handleMessage.bind(this));
      this._socket.on('subscribed', this._handleMessage.bind(this));
    });
  }

  disconnect() {
    if(this._socket)
      this._socket.disconnect();
  }

  subscribe(hash, password, callback) {
    if(!this._subscriptions[hash]) {
      this._subscriptions[hash] = { callback: callback };
      this._socket.emit('subscribe', { channel: hash }); // calls back with 'subscribed' event
    }
  }

  unsubscribe(hash) {
    this._socket.emit('unsubscribe', { channel: hash });
    delete this._subscriptions[hash];
  }

  publish(hash, message) {
    this._socket.send(JSON.stringify({ channel: hash, message: message }));
  }

  _handleMessage(hash, message) {
    if(this._subscriptions[hash] && this._subscriptions[hash].callback)
      this._subscriptions[hash].callback(hash, message);
{'fraction_non_alphanumeric': 0.17539109506618533, 'fraction_numerical': 0.0, 'mean_word_length': 3.7230113636363638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '5542765', 'n_tokens_mistral': 1021, 'n_tokens_neox': 1009, 'n_words': 187}