All files / src koaMiddleware.coffee

46.67% Statements 35/75
100% Branches 0/0
0% Functions 0/5
46.67% Lines 35/75
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 1661x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x   1x 1x   1x   1x                         1x                                                                                       1x                                                         1x                                               1x                                          
koa = require 'koa'
router = require './middleware/router'
messageStore = require './middleware/messageStore'
basicAuthentication = require './middleware/basicAuthentication'
tlsAuthentication = require "./middleware/tlsAuthentication"
rerunBypassAuthentication = require "./middleware/rerunBypassAuthentication"
rerunBypassAuthorisation = require "./middleware/rerunBypassAuthorisation"
rerunUpdateTransactionTask = require "./middleware/rerunUpdateTransactionTask"
tcpBypassAuthentication = require "./middleware/tcpBypassAuthentication"
retrieveTCPTransaction = require "./middleware/retrieveTCPTransaction"
requestMatching = require './middleware/requestMatching'
authorisation = require './middleware/authorisation'
stats = require './stats'
pollingBypassAuthorisation = require './middleware/pollingBypassAuthorisation'
pollingBypassAuthentication = require './middleware/pollingBypassAuthentication'
events = require './middleware/events'
proxy = require './middleware/proxy'
rewrite = require './middleware/rewriteUrls'
config = require './config/config'
config.authentication = config.get('authentication')
getRawBody = require 'raw-body'
tcpAdapter = require './tcpAdapter'
Q = require "q"
config.statsd = config.get 'statsd'
 
application = config.get 'application'
SDC = require 'statsd-client'
os = require 'os'
 
domain = "#{os.hostname()}.#{application.name}.appMetrics"
sdc = new SDC config.statsd
 
compress = require 'koa-compress'
 
rawBodyReader = (next) ->
  startTime = new Date() if config.statsd.enabled
  body = yield getRawBody this.req,
    length: this.length,
    encoding: this.charset
 
  this.body = body if body
  sdc.timing "#{domain}.rawBodyReaderMiddleware", startTime if config.statsd.enabled
  yield next
 
 
# Primary app
 
exports.setupApp = (done) ->
  app = koa()
 
  # Basic authentication middleware
  if config.authentication.enableBasicAuthentication
    app.use basicAuthentication.koaMiddleware
 
  # TLS authentication middleware
  if config.authentication.enableMutualTLSAuthentication
    app.use tlsAuthentication.koaMiddleware
 
  app.use rawBodyReader
 
  # Request Matching middleware
  app.use requestMatching.koaMiddleware
 
  # Authorisation middleware
  app.use authorisation.koaMiddleware
 
  # Compress response on exit
  app.use compress(
    threshold: 8
    flush: require("zlib").Z_SYNC_FLUSH
  )
 
  # Proxy
  app.use proxy.koaMiddleware
 
  # Persist message middleware
  app.use messageStore.koaMiddleware
 
  # URL rewriting middleware
  app.use rewrite.koaMiddleware
 
  # Events
  app.use events.koaMiddleware
 
  # Call router
  app.use router.koaMiddleware
 
  done(app)
 
 
# Rerun app that bypasses auth
exports.rerunApp = (done) ->
  app = koa()
 
  app.use rawBodyReader
 
  # Rerun bypass authentication middlware
  app.use rerunBypassAuthentication.koaMiddleware
 
  # Rerun bypass authorisation middlware
  app.use rerunBypassAuthorisation.koaMiddleware
 
  # Update original transaction with rerunned transaction ID
  app.use rerunUpdateTransactionTask.koaMiddleware
 
  # Persist message middleware
  app.use messageStore.koaMiddleware
 
  # Authorisation middleware
  app.use authorisation.koaMiddleware
 
  # Events
  app.use events.koaMiddleware
 
  # Call router
  app.use router.koaMiddleware
 
  done(app)
 
# App for TCP/TLS sockets
exports.tcpApp = (done) ->
  app = koa()
 
  app.use rawBodyReader
  app.use retrieveTCPTransaction.koaMiddleware
 
  # TCP bypass authentication middlware
  app.use tcpBypassAuthentication.koaMiddleware
 
  # Proxy
  app.use proxy.koaMiddleware
 
  # Persist message middleware
  app.use messageStore.koaMiddleware
 
  # Events
  app.use events.koaMiddleware
 
  # Call router
  app.use router.koaMiddleware
 
  done(app)
 
# App used by scheduled polling
exports.pollingApp = (done) ->
  app = koa()
 
  app.use rawBodyReader
 
  # Polling bypass authentication middlware
  app.use pollingBypassAuthentication.koaMiddleware
 
  # Polling bypass authorisation middleware
  app.use pollingBypassAuthorisation.koaMiddleware
 
  # Persist message middleware
  app.use messageStore.koaMiddleware
 
  # Events
  app.use events.koaMiddleware
 
  # Call router
  app.use router.koaMiddleware
 
  done(app)