Sha256: bc49d1f3305451ff1968d911d976509741e60c6878a42311dccd1d5204827ef8

Contents?: true

Size: 1.44 KB

Versions: 24

Compression:

Stored size: 1.44 KB

Contents

'use strict'

var wrap = require('./wrap.js')

module.exports = trough

trough.wrap = wrap

var slice = [].slice

// Create new middleware.
function trough() {
  var fns = []
  var middleware = {}

  middleware.run = run
  middleware.use = use

  return middleware

  // Run `fns`.  Last argument must be a completion handler.
  function run() {
    var index = -1
    var input = slice.call(arguments, 0, -1)
    var done = arguments[arguments.length - 1]

    if (typeof done !== 'function') {
      throw new Error('Expected function as last argument, not ' + done)
    }

    next.apply(null, [null].concat(input))

    // Run the next `fn`, if any.
    function next(err) {
      var fn = fns[++index]
      var params = slice.call(arguments, 0)
      var values = params.slice(1)
      var length = input.length
      var pos = -1

      if (err) {
        done(err)
        return
      }

      // Copy non-nully input into values.
      while (++pos < length) {
        if (values[pos] === null || values[pos] === undefined) {
          values[pos] = input[pos]
        }
      }

      input = values

      // Next or done.
      if (fn) {
        wrap(fn, next).apply(null, input)
      } else {
        done.apply(null, [null].concat(input))
      }
    }
  }

  // Add `fn` to the list.
  function use(fn) {
    if (typeof fn !== 'function') {
      throw new Error('Expected `fn` to be a function, not ' + fn)
    }

    fns.push(fn)

    return middleware
  }
}

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
trusty-cms-6.3.1 node_modules/trough/index.js
trusty-cms-5.0.7 node_modules/trough/index.js
trusty-cms-5.0.6 node_modules/trough/index.js
trusty-cms-5.0.5 node_modules/trough/index.js
trusty-cms-5.0.4 node_modules/trough/index.js
trusty-cms-5.0.3 node_modules/trough/index.js
trusty-cms-5.0.2 node_modules/trough/index.js
trusty-cms-5.0.1 node_modules/trough/index.js
trusty-cms-4.3.5 node_modules/trough/index.js
trusty-cms-5.0.0 node_modules/trough/index.js
trusty-cms-4.3.4 node_modules/trough/index.js
trusty-cms-4.3.3 node_modules/trough/index.js
trusty-cms-4.3.2 node_modules/trough/index.js
trusty-cms-4.3.1 node_modules/trough/index.js
trusty-cms-4.3 node_modules/trough/index.js
trusty-cms-4.2.3 node_modules/trough/index.js
trusty-cms-4.2.2 node_modules/trough/index.js
trusty-cms-4.2.1 node_modules/trough/index.js
trusty-cms-4.2 node_modules/trough/index.js
trusty-cms-4.1.9 node_modules/trough/index.js