Initial commit
[yaffs-website] / node_modules / multipipe / Readme.md
1 # multipipe
2
3 A better `Stream#pipe` that creates duplex streams and lets you handle errors in one place.
4
5 [![build status](https://secure.travis-ci.org/segmentio/multipipe.png)](http://travis-ci.org/segmentio/multipipe)
6
7 ## Example
8
9 ```js
10 var pipe = require('multipipe');
11
12 // pipe streams
13 var stream = pipe(streamA, streamB, streamC);
14
15 // centralized error handling
16 stream.on('error', fn);
17
18 // creates a new stream
19 source.pipe(stream).pipe(dest);
20
21 // optional callback on finish or error
22 pipe(streamA, streamB, streamC, function(err){
23   // ...
24 });
25 ```
26
27 ## Duplex streams
28
29   Write to the pipe and you'll really write to the first stream, read from the pipe and you'll read from the last stream.
30
31 ```js
32 var stream = pipe(a, b, c);
33
34 source
35   .pipe(stream)
36   .pipe(destination);
37 ```
38
39   In this example the flow of data is:
40
41   * source ->
42   * a ->
43   * b ->
44   * c ->
45   * destination
46
47 ## Error handling
48
49   Each `pipe` forwards the errors the streams it wraps emit, so you have one central place to handle errors:
50
51 ```js
52 var stream = pipe(a, b, c);
53
54 stream.on('error', function(err){
55   // called three times
56 });
57
58 a.emit('error', new Error);
59 b.emit('error', new Error);
60 c.emit('error', new Error);
61 ```
62
63 ## API
64
65 ### pipe(stream, ...)
66
67 Pass a variable number of streams and each will be piped to the next one.
68
69 A stream will be returned that wraps passed in streams in a way that errors will be forwarded and you can write to and/or read from it.
70
71 Pass a function as last argument to be called on `error` or `finish` of the last stream.
72
73 ## Installation
74
75 ```bash
76 $ npm install multipipe
77 ```
78
79 ## License
80
81 The MIT License (MIT)
82
83 Copyright (c) 2014 Segment.io Inc. <friends@segment.io>
84 Copyright (c) 2014 Julian Gruber <julian@juliangruber.com>
85
86 Permission is hereby granted, free of charge, to any person obtaining a copy
87 of this software and associated documentation files (the "Software"), to deal
88 in the Software without restriction, including without limitation the rights
89 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
90 copies of the Software, and to permit persons to whom the Software is
91 furnished to do so, subject to the following conditions:
92
93 The above copyright notice and this permission notice shall be included in
94 all copies or substantial portions of the Software.
95
96 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
97 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
98 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
99 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
100 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
101 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
102 THE SOFTWARE.