<commit_before>import * as debugLib from "debug";
import { EventEmitter } from "events";
import * as WebTorrent from "webtorrent";
import { formatBytes } from "./util";

const debug = debugLib("FileSend:WebTorrent");

/*
* A client for sending and receiving files, which internally
* uses a WebTorrent client.
*
* Events emitted:
*   - **error**
*		- err: string | Error
*	- **uploadProgress** (Emitted at some time intervals, for showing upload stats)
*       - progress: number (between 0 and 1)
*       - progressFiles: number[]
*       - timeRemaining: number (time remaining in milliseconds)
*		- uploaded: string (formatted as '20 MB' for example)
*		- uploadSpeed: string (formatted as '4 MB/s' for example)
*	- **downloadProgress** (Emitted at a time interval, for showing
  download stats) (Note: these have same format as above)
*		- downloaded: string
*		- downloadSpeed: string
*		- progress: number (between 0 and 1)
*       - progressFiles: number[]
*       - timeRemaining: number (time remaining in ms)
*   - **fileDownloadComplete** (Download of a file now complete)
*       - index: number (index of file whose download is complete)
*       - url: string (blobURL of the file downloaded)
*   - **seeding** (Begun seeding the file(s))
*   - **downloading** (Begun downloading the file(s))
*	- **downloadComplete**
*   - **torrentDestroyed** (the torrent that was downloading/seeding was destroyed)
*   - **clientDestroyed** (WebTorrent client was destroyed, possibly due to some error)
*/
export default class Client extends EventEmitter {
  public filesInfo: {
    name: string;
    size: string;
  }[] = [];
  private readonly socket: SocketIOClient.Socket;
  private readonly TRACKER_URLS: string[];
  private readonly client: WebTorrent.Instance;
  private readonly TIME_INTERVAL = 500; // Interval to report progress (ms)

  /*
    * Assuming use of SocketIO for comm. to server.
    * socket is the socket object of client connected.
    *
    * Uses STUN_URL and TRACKER_URL environment variables
    * for setting. If not set, falls back to third party
    * defaults.
    */
  public constructor(socket: SocketIOClient.Socket){
    super();
    let STUN_URL: string;
    let TRACKER_URL: string;

    if (process.env.STUN_URL) {
      STUN_URL = `stun:${process.env.STUN_URL}:3478`;
    } else {
      STUN_URL = "stun:stun.l.google.com:19302";
      debug(`STUN_URL env variable not set`);
    }
    debug(`Using ${STUN_URL} as STUN server address`);

    if (process.env.TRACKER_URL) {
      TRACKER_URL = `ws://${process.env.TRACKER_URL}`;
    } else {
      TRACKER_URL = `wss://tracker.btorrent.xyz`;
      debug("TRACKER_URL env variable not set");
    }
    debug(`Using ${TRACKER_URL} as TRACKER address`);

    this.client = new WebTorrent({
      tracker: {
        iceServers: [{ urls: STUN_URL }],
      },
    });

    this.TRACKER_URLS = [ TRACKER_URL ];

    this.socket = socket;<commit_msg>Modified ws:// back to wss:// for deployment<commit_after>import * as debugLib from "debug";
import { EventEmitter } from "events";
import * as WebTorrent from "webtorrent";
import { formatBytes } from "./util";

const debug = debugLib("FileSend:WebTorrent");

/*
* A client for sending and receiving files, which internally
* uses a WebTorrent client.
*
* Events emitted:
*   - **error**
*		- err: string | Error
*	- **uploadProgress** (Emitted at some time intervals, for showing upload stats)
*       - progress: number (between 0 and 1)
*       - progressFiles: number[]
*       - timeRemaining: number (time remaining in milliseconds)
*		- uploaded: string (formatted as '20 MB' for example)
*		- uploadSpeed: string (formatted as '4 MB/s' for example)
*	- **downloadProgress** (Emitted at a time interval, for showing
  download stats) (Note: these have same format as above)
*		- downloaded: string
*		- downloadSpeed: string
*		- progress: number (between 0 and 1)
*       - progressFiles: number[]
*       - timeRemaining: number (time remaining in ms)
*   - **fileDownloadComplete** (Download of a file now complete)
*       - index: number (index of file whose download is complete)
*       - url: string (blobURL of the file downloaded)
*   - **seeding** (Begun seeding the file(s))
*   - **downloading** (Begun downloading the file(s))
*	- **downloadComplete**
*   - **torrentDestroyed** (the torrent that was downloading/seeding was destroyed)
*   - **clientDestroyed** (WebTorrent client was destroyed, possibly due to some error)
*/
export default class Client extends EventEmitter {
  public filesInfo: {
    name: string;
    size: string;
  }[] = [];
  private readonly socket: SocketIOClient.Socket;
  private readonly TRACKER_URLS: string[];
  private readonly client: WebTorrent.Instance;
  private readonly TIME_INTERVAL = 500; // Interval to report progress (ms)

  /*
    * Assuming use of SocketIO for comm. to server.
    * socket is the socket object of client connected.
    *
    * Uses STUN_URL and TRACKER_URL environment variables
    * for setting. If not set, falls back to third party
    * defaults.
    */
  public constructor(socket: SocketIOClient.Socket){
    super();
    let STUN_URL: string;
    let TRACKER_URL: string;

    if (process.env.STUN_URL) {
      STUN_URL = `stun:${process.env.STUN_URL}:3478`;
    } else {
      STUN_URL = "stun:stun.l.google.com:19302";
      debug(`STUN_URL env variable not set`);
    }
    debug(`Using ${STUN_URL} as STUN server address`);

    if (process.env.TRACKER_URL) {
      TRACKER_URL = `wss://${process.env.TRACKER_URL}`;
    } else {
      TRACKER_URL = `wss://tracker.btorrent.xyz`;
      debug("TRACKER_URL env variable not set");
    }
    debug(`Using ${TRACKER_URL} as TRACKER address`);

    this.client = new WebTorrent({
      tracker: {
        iceServers: [{ urls: STUN_URL }],
      },
    });

    this.TRACKER_URLS = [ TRACKER_URL ];

    this.socket = socket;
{'fraction_non_alphanumeric': 0.12037978975924042, 'fraction_numerical': 0.006442861987114276, 'mean_word_length': 3.2901818181818183, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '460002', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1843, 'n_words': 583}