var http = require('http')
var cors = require('corsify')
var collect = require('stream-collector')
var pump = require('pump')
var iterate = require('random-iterate')
var limiter = require('size-limit-stream')
var eos = require('end-of-stream')

module.exports = function (opts) {
  var channels = {}
  var maxBroadcasts = (opts && opts.maxBroadcasts) || Infinity

  var get = function (channel) {
    if (channels[channel]) return channels[channel]
    channels[channel] = {name: channel, subscribers: []}
    return channels[channel]
  }

  var server = http.createServer(cors(function (req, res) {
    if (req.url === '/') {
      res.end(JSON.stringify({name: 'signalhub', version: require('./package').version}, null, 2) + '\n')
      return
    }

    if (req.url.slice(0, 4) !== '/v1/') {
      res.statusCode = 404
      res.end()
      return
    }

    var name = req.url.slice(4).split('?')[0]

    if (req.method === 'POST') {
      collect(pump(req, limiter(64 * 1024)), function (err, data) {
        if (err) return res.end()
        if (!channels[name]) return res.end()
        var channel = get(name)

        server.emit('publish', channel.name, data)
        data = Buffer.concat(data).toString()

        var ite = iterate(channel.subscribers)
        var next
        var cnt = 0

        while ((next = ite()) && cnt++ < maxBroadcasts) {
          next.write('data: ' + data + '\n\n')
        }

        res.end()
      })
      return
    }

    if (req.method === 'GET') {
      res.setHeader('Content-Type', 'text/event-stream; charset=utf-8')
      var channel = get(name)
      server.emit('subscribe', channel.name)
      channel.subscribers.push(res)
      eos(res, function () {
        var i = channel.subscribers.indexOf(res)
        if (i > -1) channel.subscribers.splice(i, 1)
        if (!channel.subscribers.length && channel === channels[channel.name]) delete channels[channel.name]
      })
      if (res.flushHeaders) res.flushHeaders()
      return
    }

    res.statusCode = 404
    res.end()
  }))

  return server
}

@@ -28,16 +28,19 @@
 var cors
+ify
  = requi
@@ -534,16 +534,265 @@
 nel%5D%0A  %7D
+%0A  %0A  var cors = corsify(%7B%0A    %22Access-Control-Allow-Origin%22: %22*%22,%0A    %22Access-Control-Allow-Methods%22: %22GET, POST, PUT, DELETE%22,%0A    %22Access-Control-Allow-Headers%22: %22X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Authorization%22%0A  %7D)
 %0A%0A  var 

use more specific cors config