<commit_before>path = require("path")
es = require("event-stream")
util = require("gulp-util")
http = require("http")
https = require("https")
fs = require("fs")
connect = require("connect")
liveReload = require("connect-livereload")
tiny_lr = require("tiny-lr")
lr = undefined
apps = []

http2 = undefined
try
  http2 = require('http2')

class ConnectApp
  constructor: (options) ->
    @port = options.port || "8080"
    @root = options.root || path.dirname(module.parent.id)
    @host = options.host || "localhost"
    @debug = options.debug || false
    @silent = options.silent || false
    @https = options.https || false
    @livereload = options.livereload || false
    @middleware = options.middleware || undefined
    @fallback = options.fallback || undefined
    @oldMethod("open") if options.open
    @sockets = []
    @app = undefined
    @run()

  run: ->
    @app = connect()
    @app.use connect.directory(if typeof @root == "object" then @root[0] else @root)

    @handlers().forEach (middleware) =>
      if typeof (middleware) is "object"
        @app.use middleware[0], middleware[1]
      else
        @app.use middleware

    if @https
      @server = (http2 || https).createServer
        key: @https.key || fs.readFileSync __dirname + '/certs/server.key'
        cert: @https.cert || fs.readFileSync __dirname + '/certs/server.crt'
        ca: @https.ca || fs.readFileSync __dirname + '/certs/ca.crt'
        passphrase: @https.passphrase || '<PASSWORD>'
        @app
    else
      @server = http.createServer @app

    @server.listen @port, (err) =>
      if err
        @log "Error on starting server: #{err}"
      else
        @log "Server started http#{if @https? then 's' else ''}://#{@host}:#{@port}"

        stoped = false;
        sockets = [];

        @server.on "close", =>
          if (!stoped)
            stoped = true
            @log "Server stopped"

        # Log connections and request in debug
        @server.on "connection", (socket) =>
          @logDebug "Received incoming connection from #{socket.address().address}"
          @sockets.push socket
          socket.on "close", =>
           @sockets.splice @sockets.indexOf(socket), 1

        @server.on "request", (request, response) =>
          @logDebug "Received request #{request.method} #{request.url}"

        @server.on "error", (err) =>
          @log err.toString()

        stopServer = =>
          if (!stoped)
            @sockets.forEach (socket) =>
              socket.destroy()

            @server.close()
            process.nextTick( ->
              process.exit(0);
            )

        process.on("SIGINT", stopServer);
        process.on("exit", stopServer);

        if @livereload
          tiny_lr.Server::error = ->
          if @https
            lr = tiny_lr
              key: @https.key || fs.readFileSync __dirname + '/certs/server.key'
              cert: @https.cert || fs.readFileSync __dirname + '/certs/server.crt'
          else
            lr = tiny_lr()

          lr.listen @livereload.port
          @log "LiveReload started on port #{@livereload.port}"

  handlers: ->
    steps = if @middleware then @middleware.call(this, connect, @) else []
    if @livereload
      @livereload = {}  if typeof @livereload is "boolean"
      @livereload.port = 35729  unless @livereload.port
      steps.unshift liveReload(@livereload.port)
    if typeof @root == "object"
      @root.forEach (path) ->
        steps.push connect.static(path)
    else
      steps.push connect.static(@root)
    if @fallback
      steps.push (req, res) =>
        require('fs').createReadStream(@fallback).pipe(res);
    return steps

  log: (text) ->
    if !@silent
      util.log util.colors.green(text)

  logWarning: (text) ->
    if !@silent
      util.log util.colors.yellow(text)

  logDebug: (text) ->
    if @debug
      util.log util.colors.blue(text)

  oldMethod: (type) ->
    text = 'does not work in gulp-connect v 2.*. Please read "readme" https://github.com/AveVlad/gulp-connect'
    switch type
      when "open" then @logWarning("Option open #{text}")

module.exports =
  server: (options = {}) -> 
    app = new ConnectApp(options)
    apps.push(app)
    app
  reload: ->
    es.map (file, callback) ->
      if @livereload and typeof lr == "object"
        lr.changed body:
          files: file.path
      callback null, file
  lr: lr
  serverClose: ->
    apps.forEach((app) -> do app.server.close)
    apps = []<commit_msg>Fixed problem with livereload<commit_after>path = require("path")
es = require("event-stream")
util = require("gulp-util")
http = require("http")
https = require("https")
fs = require("fs")
connect = require("connect")
liveReload = require("connect-livereload")
tiny_lr = require("tiny-lr")
lr = undefined
apps = []

http2 = undefined
try
  http2 = require('http2')

class ConnectApp
  constructor: (options) ->
    @port = options.port || "8080"
    @root = options.root || path.dirname(module.parent.id)
    @host = options.host || "localhost"
    @debug = options.debug || false
    @silent = options.silent || false
    @https = options.https || false
    @livereload = options.livereload || false
    @middleware = options.middleware || undefined
    @fallback = options.fallback || undefined
    @oldMethod("open") if options.open
    @sockets = []
    @app = undefined
    @run()

  run: ->
    @app = connect()
    @app.use connect.directory(if typeof @root == "object" then @root[0] else @root)

    @handlers().forEach (middleware) =>
      if typeof (middleware) is "object"
        @app.use middleware[0], middleware[1]
      else
        @app.use middleware

    if @https
      @server = (http2 || https).createServer
        key: @https.key || fs.readFileSync __dirname + '/certs/server.key'
        cert: @https.cert || fs.readFileSync __dirname + '/certs/server.crt'
        ca: @https.ca || fs.readFileSync __dirname + '/certs/ca.crt'
        passphrase: @https.passphrase || '<PASSWORD>'
        @app
    else
      @server = http.createServer @app

    @server.listen @port, (err) =>
      if err
        @log "Error on starting server: #{err}"
      else
        @log "Server started http#{if @https? then 's' else ''}://#{@host}:#{@port}"

        stoped = false;
        sockets = [];

        @server.on "close", =>
          if (!stoped)
            stoped = true
            @log "Server stopped"

        # Log connections and request in debug
        @server.on "connection", (socket) =>
          @logDebug "Received incoming connection from #{socket.address().address}"
          @sockets.push socket
          socket.on "close", =>
           @sockets.splice @sockets.indexOf(socket), 1

        @server.on "request", (request, response) =>
          @logDebug "Received request #{request.method} #{request.url}"

        @server.on "error", (err) =>
          @log err.toString()

        stopServer = =>
          if (!stoped)
            @sockets.forEach (socket) =>
              socket.destroy()

            @server.close()
            process.nextTick( ->
              process.exit(0);
            )

        process.on("SIGINT", stopServer);
        process.on("exit", stopServer);

        if @livereload
          tiny_lr.Server::error = ->
          if @https
            lr = tiny_lr
              key: @https.key || fs.readFileSync __dirname + '/certs/server.key'
              cert: @https.cert || fs.readFileSync __dirname + '/certs/server.crt'
          else
            lr = tiny_lr()

          lr.listen @livereload.port
          @log "LiveReload started on port #{@livereload.port}"

  handlers: ->
    steps = if @middleware then @middleware.call(this, connect, @) else []
    if @livereload
      @livereload = {}  if typeof @livereload is "boolean"
      @livereload.port = 35729  unless @livereload.port
      steps.unshift liveReload(@livereload.port)
    if typeof @root == "object"
      @root.forEach (path) ->
        steps.push connect.static(path)
    else
      steps.push connect.static(@root)
    if @fallback
      steps.push (req, res) =>
        require('fs').createReadStream(@fallback).pipe(res);
    return steps

  log: (text) ->
    if !@silent
      util.log util.colors.green(text)

  logWarning: (text) ->
    if !@silent
      util.log util.colors.yellow(text)

  logDebug: (text) ->
    if @debug
      util.log util.colors.blue(text)

  oldMethod: (type) ->
    text = 'does not work in gulp-connect v 2.*. Please read "readme" https://github.com/AveVlad/gulp-connect'
    switch type
      when "open" then @logWarning("Option open #{text}")

module.exports =
  server: (options = {}) -> 
    app = new ConnectApp(options)
    apps.push(app)
    app
  reload: ->
    es.map (file, callback) ->
      apps.forEach (app) =>
        if app.livereload and typeof lr == "object"
          lr.changed body:
            files: file.path
        callback null, file
  lr: lr
  serverClose: ->
    apps.forEach((app) -> do app.server.close)
    apps = []
{'fraction_non_alphanumeric': 0.14301589065451717, 'fraction_numerical': 0.004222691410156684, 'mean_word_length': 2.3076074972436604, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 56, 'https://': 2, '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': '183000', 'n_tokens_mistral': 2818, 'n_tokens_neox': 2728, 'n_words': 685}