<commit_before>import { log } from '@serverless/utils/log.js'
import { WebSocketServer as WsWebSocketServer } from 'ws'
import { createUniqueId } from '../../utils/index.js'

export default class WebSocketServer {
  #connectionIds = new Map()

  #options = null

  #webSocketClients = null

  constructor(options, webSocketClients, sharedServer) {
    this.#options = options
    this.#webSocketClients = webSocketClients

    const server = new WsWebSocketServer({
      server: sharedServer,
      verifyClient: ({ req }, cb) => {
        const connectionId = createUniqueId()
        const key = req.headers['sec-websocket-key']

        log.debug(`verifyClient:${key} ${connectionId}`)

        // Use the websocket key to correlate connection IDs
        this.#connectionIds[key] = connectionId

        this.#webSocketClients
          .verifyClient(connectionId, req)
          .then(({ verified, statusCode, message, headers }) => {
            try {
              if (!verified) {
                cb(false, statusCode, message, headers)
                return
              }
              cb(true)
            } catch (err) {
              log.debug(`Error verifying`, err)
              cb(false)
            }
          })
      },
    })

    server.on('connection', (webSocketClient, request) => {
      log.notice('received connection')

      const { headers } = request
      const key = headers['sec-websocket-key']

      const connectionId = this.#connectionIds[key]

      log.debug(`connect:${connectionId}`)

      this.#webSocketClients.addClient(webSocketClient, connectionId)
    })
  }

  async start() {
    const { host, httpsProtocol, websocketPort } = this.#options

    log.notice(
      `Offline [websocket] listening on ws${
        httpsProtocol ? 's' : ''
      }://${host}:${websocketPort}`,
    )
  }

  // no-op, we're re-using the http server
  stop() {}

  addRoute(functionKey, webSocketEvent) {
    this.#webSocketClients.addRoute(functionKey, webSocketEvent)
  }
}<commit_msg>fix: getter/setter map<commit_after>import { log } from '@serverless/utils/log.js'
import { WebSocketServer as WsWebSocketServer } from 'ws'
import { createUniqueId } from '../../utils/index.js'

export default class WebSocketServer {
  #connectionIds = new Map()

  #options = null

  #webSocketClients = null

  constructor(options, webSocketClients, sharedServer) {
    this.#options = options
    this.#webSocketClients = webSocketClients

    const server = new WsWebSocketServer({
      server: sharedServer,
      verifyClient: ({ req }, cb) => {
        const connectionId = createUniqueId()
        const key = req.headers['sec-websocket-key']

        log.debug(`verifyClient:${key} ${connectionId}`)

        // use the websocket key to correlate connection IDs
        this.#connectionIds.set(key, connectionId)

        this.#webSocketClients
          .verifyClient(connectionId, req)
          .then(({ verified, statusCode, message, headers }) => {
            try {
              if (!verified) {
                cb(false, statusCode, message, headers)
                return
              }
              cb(true)
            } catch (err) {
              log.debug(`Error verifying`, err)
              cb(false)
            }
          })
      },
    })

    server.on('connection', (webSocketClient, request) => {
      log.notice('received connection')

      const { headers } = request
      const key = headers['sec-websocket-key']

      const connectionId = this.#connectionIds.get(key)

      log.debug(`connect:${connectionId}`)

      this.#webSocketClients.addClient(webSocketClient, connectionId)
    })
  }

  async start() {
    const { host, httpsProtocol, websocketPort } = this.#options

    log.notice(
      `Offline [websocket] listening on ws${
        httpsProtocol ? 's' : ''
      }://${host}:${websocketPort}`,
    )
  }

  // no-op, we're re-using the http server
  stop() {}

  addRoute(functionKey, webSocketEvent) {
    this.#webSocketClients.addRoute(functionKey, webSocketEvent)
  }
}
{'fraction_non_alphanumeric': 0.1242603550295858, 'fraction_numerical': 0.0, 'mean_word_length': 2.406381192275399, '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': '6946464', 'n_tokens_mistral': 1200, 'n_tokens_neox': 1144, 'n_words': 254}