<commit_before>import createDebug from 'debug'
import partialStream from 'partial-stream'
import {connect} from 'tls'
import {parse} from 'url'

const debug = createDebug('xo:proxy-console')

export default function proxyConsole (ws, vmConsole, sessionId) {
  const url = parse(vmConsole.location)

  let closed = false

  const socket = connect({
    host: url.host,
    port: url.port || 443,
    rejectUnauthorized: false,
  }, () => {
    // Write headers.
    socket.write([
      `CONNECT ${url.path} HTTP/1.0`,
      `Host: ${url.hostname}`,
      `Cookie: session_id=${sessionId}`,
      '', '',
    ].join('\r\n'))

    const onSend = (error) => {
      if (error) {
        debug('error sending to the XO client: %s', error.stack || error.message || error)
      }
    }

    socket.pipe(partialStream('\r\n\r\n', headers => {
      // TODO: check status code 200.
      debug('connected')
    })).on('data', data => {
      if (!closed) {
        ws.send(data, onSend)
      }
    }).on('end', () => {
      if (!closed) {
        closed = true
        debug('disconnected from the console')
      }

      ws.close()
    })

    ws
      .on('error', error => {
        closed = true
        debug('error from the XO client: %s', error.stack || error.message || error)

        socket.close()
      })
      .on('message', data => {<commit_msg>fix(proxy-console): socket.close() → socket.end() (#646)<commit_after>import createDebug from 'debug'
import partialStream from 'partial-stream'
import {connect} from 'tls'
import {parse} from 'url'

const debug = createDebug('xo:proxy-console')

export default function proxyConsole (ws, vmConsole, sessionId) {
  const url = parse(vmConsole.location)

  let closed = false

  const socket = connect({
    host: url.host,
    port: url.port || 443,
    rejectUnauthorized: false,
  }, () => {
    // Write headers.
    socket.write([
      `CONNECT ${url.path} HTTP/1.0`,
      `Host: ${url.hostname}`,
      `Cookie: session_id=${sessionId}`,
      '', '',
    ].join('\r\n'))

    const onSend = (error) => {
      if (error) {
        debug('error sending to the XO client: %s', error.stack || error.message || error)
      }
    }

    socket.pipe(partialStream('\r\n\r\n', headers => {
      // TODO: check status code 200.
      debug('connected')
    })).on('data', data => {
      if (!closed) {
        ws.send(data, onSend)
      }
    }).on('end', () => {
      if (!closed) {
        closed = true
        debug('disconnected from the console')
      }

      ws.close()
    })

    ws
      .on('error', error => {
        closed = true
        debug('error from the XO client: %s', error.stack || error.message || error)

        socket.end()
      })
      .on('message', data => {
{'fraction_non_alphanumeric': 0.16926988739556847, 'fraction_numerical': 0.006901561932437341, 'mean_word_length': 2.3503649635036497, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '2057250', 'n_tokens_mistral': 889, 'n_tokens_neox': 862, 'n_words': 222}