3 [![Build][build-badge]][build]
4 [![Coverage][coverage-badge]][coverage]
5 [![Downloads][downloads-badge]][downloads]
6 [![Size][size-badge]][size]
8 > **trough** /trôf/ — a channel used to convey a liquid.
10 `trough` is like [`ware`][ware] with less sugar, and middleware functions can
11 change the input of the next.
24 var fs = require('fs')
25 var path = require('path')
26 var trough = require('trough')
28 var pipeline = trough()
29 .use(function(fileName) {
30 console.log('Checking… ' + fileName)
32 .use(function(fileName) {
33 return path.join(process.cwd(), fileName)
35 .use(function(filePath, next) {
36 fs.stat(filePath, function(err, stats) {
37 next(err, {filePath, stats})
40 .use(function(ctx, next) {
41 if (ctx.stats.isFile()) {
42 fs.readFile(ctx.filePath, next)
44 next(new Error('Expected file'))
48 pipeline.run('readme.md', console.log)
49 pipeline.run('node_modules', console.log)
56 Checking… node_modules
59 at wrapped (~/node_modules/trough/index.js:93:19)
60 at next (~/node_modules/trough/index.js:56:24)
61 at done (~/node_modules/trough/index.js:124:12)
62 at ~/node_modules/example.js:14:7
63 at FSReqWrap.oncomplete (fs.js:153:5)
64 null <Buffer 23 20 74 72 6f 75 67 68 20 5b 21 5b 42 75 69 6c 64 20 53 74 61 74 75 73 5d 5b 74 72 61 76 69 73 2d 62 61 64 67 65 5d 5d 5b 74 72 61 76 69 73 5d 20 5b ... >
71 Create a new [`Trough`][trough].
73 #### `trough.wrap(middleware, callback[, …input])`
75 Call `middleware` with all input.
76 If `middleware` accepts more arguments than given in input, and extra `done`
77 function is passed in after the input when calling it.
80 The first value in `input` is called the main input value.
81 All other input values are called the rest input values.
82 The values given to `callback` are the input values, merged with every non-nully
85 * If `middleware` throws an error, returns a promise that is rejected, or
86 calls the given `done` function with an error, `callback` is invoked with
88 * If `middleware` returns a value or returns a promise that is resolved, that
89 value is the main output value
90 * If `middleware` calls `done`, all non-nully values except for the first one
91 (the error) overwrite the output values
97 #### `Trough#run([input…, ]done)`
99 Run the pipeline (all [`use()`][use]d middleware).
100 Invokes [`done`][done] on completion with either an error or the output of the
104 > as the length of input defines whether [async][] functions get a `next`
105 > function, it’s recommended to keep `input` at one value normally.
107 ##### `function done(err?, [output…])`
109 The final handler passed to [`run()`][run], invoked with an error if a
110 [middleware function][fn] rejected, passed, or threw one, or the output of the
111 last middleware function.
113 #### `Trough#use(fn)`
115 Add `fn`, a [middleware function][fn], to the pipeline.
117 ##### `function fn([input…, ][next])`
119 A middleware function invoked with the output of its predecessor.
123 If `fn` returns or throws an error, the pipeline fails and `done` is invoked
126 If `fn` returns a value (neither `null` nor `undefined`), the first `input` of
127 the next function is set to that value (all other `input` is passed through).
129 The following example shows how returning an error stops the pipeline:
132 var trough = require('trough')
136 return new Error('Got: ' + val)
138 .run('some value', console.log)
144 Error: Got: some value
149 The following example shows how throwing an error stops the pipeline:
152 var trough = require('trough')
156 throw new Error('Got: ' + val)
158 .run('more value', console.log)
164 Error: Got: more value
169 The following example shows how the first output can be modified:
172 var trough = require('trough')
178 .run('more value', 'untouched', console.log)
184 null 'even more value' 'untouched'
189 If `fn` returns a promise, and that promise rejects, the pipeline fails and
190 `done` is invoked with the rejected value.
192 If `fn` returns a promise, and that promise resolves with a value (neither
193 `null` nor `undefined`), the first `input` of the next function is set to that
194 value (all other `input` is passed through).
196 The following example shows how rejecting a promise stops the pipeline:
199 var trough = require('trough')
203 return new Promise(function(resolve, reject) {
204 reject('Got: ' + val)
207 .run('val', console.log)
216 The following example shows how the input isn’t touched by resolving to `null`.
219 var trough = require('trough')
223 return new Promise(function(resolve) {
224 setTimeout(function() {
229 .run('Input', console.log)
240 If `fn` accepts one more argument than the given `input`, a `next` function is
241 given (after the input). `next` must be called, but doesn’t have to be called
244 If `next` is given a value (neither `null` nor `undefined`) as its first
245 argument, the pipeline fails and `done` is invoked with that value.
247 If `next` is given no value (either `null` or `undefined`) as the first
248 argument, all following non-nully values change the input of the following
249 function, and all nully values default to the `input`.
251 The following example shows how passing a first argument stops the pipeline:
254 var trough = require('trough')
257 .use(function(val, next) {
258 next(new Error('Got: ' + val))
260 .run('val', console.log)
270 The following example shows how more values than the input are passed.
273 var trough = require('trough')
276 .use(function(val, next) {
277 setTimeout(function() {
278 next(null, null, 'values')
281 .run('some', console.log)
292 [MIT][license] © [Titus Wormer][author]
296 [build-badge]: https://img.shields.io/travis/wooorm/trough.svg
298 [build]: https://travis-ci.org/wooorm/trough
300 [coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/trough.svg
302 [coverage]: https://codecov.io/github/wooorm/trough
304 [downloads-badge]: https://img.shields.io/npm/dm/trough.svg
306 [downloads]: https://www.npmjs.com/package/trough
308 [size-badge]: https://img.shields.io/bundlephobia/minzip/trough.svg
310 [size]: https://bundlephobia.com/result?p=trough
312 [npm]: https://docs.npmjs.com/cli/install
316 [author]: https://wooorm.com
318 [ware]: https://github.com/segmentio/ware
324 [run]: #troughruninput-done
326 [fn]: #function-fninput-next
328 [done]: #function-doneerr-output
330 [async]: #asynchronous