<commit_before>import express from 'express'
import getPort from 'get-port'
import http from 'http'

import Server from './Server'
import Processor from '../Processor'
import JsonRpcRequest from './JsonRpcRequest'
import JsonRpcResponse from './JsonRpcResponse'

/**
 * A `Server` using HTTP for communication.
 */
export default class HttpServer extends Server {

  /**
   * The port to listen on.
   */
  port: number

  /**
   * The address to listen on.
   *
   * Usually 127.0.0.1 or 0.0.0.0.
   */
  address: string

  /**
   * The Express application
   *
   * Used by derived classes to add routes.
   */
  app: express.Application

  /**
   * The `http` module server
   */
  server?: http.Server

  constructor (processor?: Processor, logging?: number, port: number = 2000, address: string = '127.0.0.1') {
    super(processor, logging)

    this.port = port
    this.address = address

    const app = express()

    app.disable('x-powered-by')
    app.use(express.json())

    // JSON-RPC over HTTP
    // There is no wrapping/unwrapping of the request/response or
    // special handling of errors
    app.post('/', (req, res) => {
      res.setHeader('Content-Type', 'application/json')
      res.send(this.recieve(req.body))
    })

    // JSON-RPC wrapped in HTTP
    // Unrap the HTTP request into a JSON RPC request and
    // wrap the response as HTTP
    const wrap = (method: string) => {
      return (req: express.Request, res: express.Response) => {
        const request = new JsonRpcRequest(method, req.body)
        const response = this.recieve(request, false) as JsonRpcResponse
        // For export and convert methods the content type may be some other format
        res.setHeader('Content-Type', 'application/json')
        if (response.error !== undefined) res.status((response.error.code < -32603) ? 400 : 500).send({ error: response.error })
        else res.send(response.result)
      }
    }
    app.post('/import', wrap('import'))
    app.post('/export', wrap('export'))
    app.post('/convert', wrap('convert'))
    app.post('/compile', wrap('compile'))
    app.post('/build', wrap('build'))
    app.post('/execute', wrap('execute'))

    // TODO: work out how to set this folder, if at all
    app.use('/', express.static('./tests/comms'))

    this.app = app
  }

  // Methods overriden from `Server`

  async start () {
    this.port = await getPort({ port: this.port }) // tslint:disable-line:await-promise
    return new Promise((resolve, reject) => {
      this.server = this.app.listen(this.port, this.address, () => {
        this.log({ started: `http://${this.address}:${this.port}` })
        resolve()
      })
    })
  }

  async stop () {
    if (this.server) {
      this.server.close()
      this.server = undefined
      this.log({ stopped: true })
    }
  }
}<commit_msg>fix(HttpServer): make killable (destroy all open sockets)<commit_after>import express from 'express'
import getPort from 'get-port'
import http from 'http'
// @ts-ignore
import killable from 'killable'

import Server from './Server'
import Processor from '../Processor'
import JsonRpcRequest from './JsonRpcRequest'
import JsonRpcResponse from './JsonRpcResponse'

/**
 * A `Server` using HTTP for communication.
 */
export default class HttpServer extends Server {

  /**
   * The port to listen on.
   */
  port: number

  /**
   * The address to listen on.
   *
   * Usually 127.0.0.1 or 0.0.0.0.
   */
  address: string

  /**
   * The Express application
   *
   * Used by derived classes to add routes.
   */
  app: express.Application

  /**
   * The `http` module server
   */
  server?: http.Server

  constructor (processor?: Processor, logging?: number, port: number = 2000, address: string = '127.0.0.1') {
    super(processor, logging)

    this.port = port
    this.address = address

    const app = express()

    app.disable('x-powered-by')
    app.use(express.json())

    // JSON-RPC over HTTP
    // There is no wrapping/unwrapping of the request/response or
    // special handling of errors
    app.post('/', (req, res) => {
      res.setHeader('Content-Type', 'application/json')
      res.send(this.recieve(req.body))
    })

    // JSON-RPC wrapped in HTTP
    // Unrap the HTTP request into a JSON RPC request and
    // wrap the response as HTTP
    const wrap = (method: string) => {
      return (req: express.Request, res: express.Response) => {
        const request = new JsonRpcRequest(method, req.body)
        const response = this.recieve(request, false) as JsonRpcResponse
        // For export and convert methods the content type may be some other format
        res.setHeader('Content-Type', 'application/json')
        if (response.error !== undefined) res.status((response.error.code < -32603) ? 400 : 500).send({ error: response.error })
        else res.send(response.result)
      }
    }
    app.post('/import', wrap('import'))
    app.post('/export', wrap('export'))
    app.post('/convert', wrap('convert'))
    app.post('/compile', wrap('compile'))
    app.post('/build', wrap('build'))
    app.post('/execute', wrap('execute'))

    // TODO: work out how to set this folder, if at all
    app.use('/', express.static('./tests/comms'))

    this.app = app
  }

  // Methods overriden from `Server`

  async start () {
    this.port = await getPort({ port: this.port }) // tslint:disable-line:await-promise
    return new Promise((resolve, reject) => {
      this.server = killable(this.app.listen(this.port, this.address, () => {
        this.log({ started: `http://${this.address}:${this.port}` })
        resolve()
      }))
    })
  }

  async stop () {
    if (this.server) {
      return new Promise((resolve, reject) => {
        // @ts-ignore
        this.server.kill(() => {
          this.server = undefined
          this.log({ stopped: true })
          resolve()
        })
      })
    }
  }
}
{'fraction_non_alphanumeric': 0.14885301614273577, 'fraction_numerical': 0.010535259133389974, 'mean_word_length': 2.624384236453202, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '479848', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1750, 'n_words': 505}