<commit_before>import { expect } from 'chai'
import { net, session, ClientRequest, BrowserWindow } from 'electron'
import * as http from 'http'
import * as url from 'url'
import { AddressInfo, Socket } from 'net'
import { emittedOnce } from './events-helpers'

const kOneKiloByte = 1024
const kOneMegaByte = kOneKiloByte * kOneKiloByte

function randomBuffer (size: number, start: number = 0, end: number = 255) {
  const range = 1 + end - start
  const buffer = Buffer.allocUnsafe(size)
  for (let i = 0; i < size; ++i) {
    buffer[i] = start + Math.floor(Math.random() * range)
  }
  return buffer
}

function randomString (length: number) {
  const buffer = randomBuffer(length, '0'.charCodeAt(0), 'z'.charCodeAt(0))
  return buffer.toString()
}

const cleanupTasks: (() => void)[] = []

function cleanUp () {
  cleanupTasks.forEach(t => t())
  cleanupTasks.length = 0
}

async function getResponse (urlRequest: Electron.ClientRequest) {
  return new Promise<Electron.IncomingMessage>((resolve, reject) => {
    urlRequest.on('error', reject)
    urlRequest.on('abort', reject)
    urlRequest.on('response', (response) => resolve(response))
    urlRequest.end()
  })
}

async function collectStreamBody (response: Electron.IncomingMessage | http.IncomingMessage) {
  return (await collectStreamBodyBuffer(response)).toString()
}

function collectStreamBodyBuffer (response: Electron.IncomingMessage | http.IncomingMessage) {
  return new Promise<Buffer>((resolve, reject) => {
    response.on('error', reject);
    (response as NodeJS.EventEmitter).on('aborted', reject)
    const data: Buffer[] = []
    response.on('data', (chunk) => data.push(chunk))
    response.on('end', (chunk?: Buffer) => {
      if (chunk) data.push(chunk)
      resolve(Buffer.concat(data))
    })
  })
}

function respondNTimes (fn: http.RequestListener, n: number): Promise<string> {
  return new Promise((resolve) => {
    const server = http.createServer((request, response) => {
      fn(request, response)
      // don't close if a redirect was returned
      n--
      if ((response.statusCode < 300 || response.statusCode >= 399) && n <= 0) { server.close() }
    })
    server.listen(0, '127.0.0.1', () => {
      resolve(`http://127.0.0.1:${(server.address() as AddressInfo).port}`)
    })
    const sockets: Socket[] = []
    server.on('connection', s => sockets.push(s))
    cleanupTasks.push(() => {
      server.close()
      sockets.forEach(s => s.destroy())
    })
  })
}<commit_msg>chore: fix net specs when rerunning locally (#22745)<commit_after>import { expect } from 'chai'
import { net, session, ClientRequest, BrowserWindow } from 'electron'
import * as http from 'http'
import * as url from 'url'
import { AddressInfo, Socket } from 'net'
import { emittedOnce } from './events-helpers'

const kOneKiloByte = 1024
const kOneMegaByte = kOneKiloByte * kOneKiloByte

function randomBuffer (size: number, start: number = 0, end: number = 255) {
  const range = 1 + end - start
  const buffer = Buffer.allocUnsafe(size)
  for (let i = 0; i < size; ++i) {
    buffer[i] = start + Math.floor(Math.random() * range)
  }
  return buffer
}

function randomString (length: number) {
  const buffer = randomBuffer(length, '0'.charCodeAt(0), 'z'.charCodeAt(0))
  return buffer.toString()
}

const cleanupTasks: (() => void)[] = []

function cleanUp () {
  cleanupTasks.forEach(t => t())
  cleanupTasks.length = 0
}

async function getResponse (urlRequest: Electron.ClientRequest) {
  return new Promise<Electron.IncomingMessage>((resolve, reject) => {
    urlRequest.on('error', reject)
    urlRequest.on('abort', reject)
    urlRequest.on('response', (response) => resolve(response))
    urlRequest.end()
  })
}

async function collectStreamBody (response: Electron.IncomingMessage | http.IncomingMessage) {
  return (await collectStreamBodyBuffer(response)).toString()
}

function collectStreamBodyBuffer (response: Electron.IncomingMessage | http.IncomingMessage) {
  return new Promise<Buffer>((resolve, reject) => {
    response.on('error', reject);
    (response as NodeJS.EventEmitter).on('aborted', reject)
    const data: Buffer[] = []
    response.on('data', (chunk) => data.push(chunk))
    response.on('end', (chunk?: Buffer) => {
      if (chunk) data.push(chunk)
      resolve(Buffer.concat(data))
    })
  })
}

function respondNTimes (fn: http.RequestListener, n: number): Promise<string> {
  return new Promise((resolve) => {
    const server = http.createServer((request, response) => {
      fn(request, response)
      // don't close if a redirect was returned
      if ((response.statusCode < 300 || response.statusCode >= 399) && n <= 0) {
        n--
        server.close()
      }
    })
    server.listen(0, '127.0.0.1', () => {
      resolve(`http://127.0.0.1:${(server.address() as AddressInfo).port}`)
    })
    const sockets: Socket[] = []
    server.on('connection', s => sockets.push(s))
    cleanupTasks.push(() => {
      server.close()
      sockets.forEach(s => s.destroy())
    })
  })
}
{'fraction_non_alphanumeric': 0.1567298105682951, 'fraction_numerical': 0.014556331006979064, 'mean_word_length': 3.851063829787234, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '3276328', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1559, 'n_words': 385}