<commit_before>
var dgram = require('dgram')
var util = require('util')
var EventEmitter = require('events').EventEmitter
var createSocket = dgram.createSocket // raw, use newSocket instead
var SOCKET_EVENTS = ['message', 'listening', 'error', 'close']
var PROXY_EVENTS = ['listening', 'error']
var ID = 0
var SOCKETS = {
  udp4: {},
  udp6: {}
}

var UNDEF = []

if (!dgram.__sockjacked) hijack()

function hijack() {
  dgram.__sockjacked = true
  var createSocket = dgram.createSocket

  dgram.createSocket = function(options) {
    return new Socket(options)
  }
}

function newSocket(options) {
  var socket = createSocket(options)
  bindEvents(socket)
  return socket
}

function Socket(options) {
  EventEmitter.call(this)
  this._jackid = ID++
  this._type = typeof options === 'string' ? options : options.type
  this.setMaxListeners(0)
  this._msgFilters = []
  UNDEF.push(this)
}

util.inherits(Socket, EventEmitter)

Socket.prototype.bind = function(port, host, cb) {
  var self = this

  if (this._binding) throw new Error('already binding')

  this._binding = true

  var socket
  var typePorts = SOCKETS[this._type]
  if (typeof port === 'function') {
    cb = port
    port = null
    host = null
  }
  else if (typeof host === 'function') {
    cb = host
    host = null
  }

  var cached
  if (!port) {
    socket = newSocket(this._type)
    socket.bind()
  }
  else {
    cached = typePorts[port]
    if (cached) {
      socket = cached.socket
    }
    else {
      socket = newSocket(this._type)
      socket.bind(port, host)
    }

    onPortKnowable()
  }

  if (cached) {
    if (cached.listening) {
      process.nextTick(function() {
        if (!self._closing) self.emit('listening')
      })
    }
  }
  else {
    socket.once('listening', onListening)
  }

  socket.setMaxListeners(0)
  this.socket = socket
  this._handle = socket._handle

  socket.once('close', function() {
    delete typePorts[self._port]
  })

  if (cb) this.once('listening', cb)

  function onPortKnowable() {
    if (!port) port = socket.address().port

    self._port = port
    if (!typePorts[port]) {
      typePorts[port] = {
        socket: socket,
        wrappers: []
      }
    }

    var idx = UNDEF.indexOf(self)
    if (idx !== -1) UNDEF.splice(idx, 1)

    typePorts[port].wrappers.push(self)
  }

  function onListening() {
    if (!port) onPortKnowable()<commit_msg>attach early to 'listening' event<commit_after>
var dgram = require('dgram')
var util = require('util')
var EventEmitter = require('events').EventEmitter
var createSocket = dgram.createSocket // raw, use newSocket instead
var SOCKET_EVENTS = ['message', 'listening', 'error', 'close']
var PROXY_EVENTS = ['listening', 'error']
var ID = 0
var SOCKETS = {
  udp4: {},
  udp6: {}
}

var UNDEF = []

if (!dgram.__sockjacked) hijack()

function hijack() {
  dgram.__sockjacked = true
  var createSocket = dgram.createSocket

  dgram.createSocket = function(options) {
    return new Socket(options)
  }
}

function newSocket(options) {
  var socket = createSocket(options)
  bindEvents(socket)
  return socket
}

function Socket(options) {
  EventEmitter.call(this)
  this._jackid = ID++
  this._type = typeof options === 'string' ? options : options.type
  this.setMaxListeners(0)
  this._msgFilters = []
  UNDEF.push(this)
}

util.inherits(Socket, EventEmitter)

Socket.prototype.bind = function(port, host, cb) {
  var self = this

  if (this._binding) throw new Error('already binding')

  this._binding = true

  var socket
  var typePorts = SOCKETS[this._type]
  if (typeof port === 'function') {
    cb = port
    port = null
    host = null
  }
  else if (typeof host === 'function') {
    cb = host
    host = null
  }

  var cached
  if (!port) {
    socket = newSocket(this._type)
    socket.bind()
  }
  else {
    cached = typePorts[port]
    if (cached) {
      socket = cached.socket
    }
    else {
      socket = newSocket(this._type)
      socket.once('listening', onListening)
      socket.bind(port, host)
    }

    onPortKnowable()
  }

  if (cached) {
    if (cached.listening) {
      process.nextTick(function() {
        if (!self._closing) self.emit('listening')
      })
    }
  }

  socket.setMaxListeners(0)
  this.socket = socket
  this._handle = socket._handle

  socket.once('close', function() {
    delete typePorts[self._port]
  })

  if (cb) this.once('listening', cb)

  function onPortKnowable() {
    if (!port) port = socket.address().port

    self._port = port
    if (!typePorts[port]) {
      typePorts[port] = {
        socket: socket,
        wrappers: []
      }
    }

    var idx = UNDEF.indexOf(self)
    if (idx !== -1) UNDEF.splice(idx, 1)

    typePorts[port].wrappers.push(self)
  }

  function onListening() {
    if (!port) onPortKnowable()
{'fraction_non_alphanumeric': 0.13690600795478333, 'fraction_numerical': 0.002930709650408206, 'mean_word_length': 3.021885521885522, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4748055', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1628, 'n_words': 374}