<commit_before>/**
 * Server wraps the "websockets/ws" library providing JSON RPC 2.0 support on top.
 * @module Server
 */

"use strict"

import assertArgs from "assert-args"
import EventEmitter from "events"
import { Server as WebSocketServer } from "ws"
import uuid from "uuid"
import url from "url"

import * as utils from "./utils"

export default class Server extends EventEmitter
{
    /**
     * Instantiate a Server server.
     * @constructor
     * @param {Object} options - ws constructor's parameters with rpc
     * @return {Server} - returns a new Server instance
     */
    constructor(options)
    {
        super()

        this.rpc_methods = {}

        // stores all connected sockets with a universally unique identifier
        this.clients = new Map()

        // stores all events as keys and subscribed users in array as value
        this.events = {}

        this.wss = new WebSocketServer(options, () => this.emit("listening"))

        this.wss.on("connection", (socket) =>
        {
            const path = url.parse(socket.upgradeReq.url, true).pathname.split("/")
            socket._id = uuid.v1()

            // cleanup after the socket get disconnected
            socket.on("close", () =>
            {
                this.clients.delete(socket._id)

                for (const event of Object.keys(this.events))
                {
                    const index = this.events[event].indexOf(socket._id)

                    if (index === 0)
                        this.events[event].splice(index, 1)
                }<commit_msg>server: minor comment typo fix<commit_after>/**
 * Server wraps the "websockets/ws" library providing JSON RPC 2.0 support on top.
 * @module Server
 */

"use strict"

import assertArgs from "assert-args"
import EventEmitter from "events"
import { Server as WebSocketServer } from "ws"
import uuid from "uuid"
import url from "url"

import * as utils from "./utils"

export default class Server extends EventEmitter
{
    /**
     * Instantiate a Server server.
     * @constructor
     * @param {Object} options - ws constructor's parameters with rpc
     * @return {Server} - returns a new Server instance
     */
    constructor(options)
    {
        super()

        this.rpc_methods = {}

        // stores all connected sockets with a universally unique identifier
        this.clients = new Map()

        // stores all events as keys and subscribed users in array as value
        this.events = {}

        this.wss = new WebSocketServer(options, () => this.emit("listening"))

        this.wss.on("connection", (socket) =>
        {
            const path = url.parse(socket.upgradeReq.url, true).pathname.split("/")
            socket._id = uuid.v1()

            // cleanup after the socket gets disconnected
            socket.on("close", () =>
            {
                this.clients.delete(socket._id)

                for (const event of Object.keys(this.events))
                {
                    const index = this.events[event].indexOf(socket._id)

                    if (index === 0)
                        this.events[event].splice(index, 1)
                }
{'fraction_non_alphanumeric': 0.1060126582278481, 'fraction_numerical': 0.0031645569620253164, 'mean_word_length': 1.9962085308056872, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '7512222', 'n_tokens_mistral': 865, 'n_tokens_neox': 819, 'n_words': 266}