Files: 7350fde90f2b29ab574fbca1f113fa270799937f / pull.js
1073 bytesRaw
1 | function isFunction (fun) { |
2 | return 'function' === typeof fun |
3 | } |
4 | |
5 | //check if this stream is either a sink or a through. |
6 | function isReader (fun) { |
7 | return isFunction(fun) && (fun.length === 1) |
8 | } |
9 | |
10 | module.exports = function pull () { |
11 | var args = [].slice.call(arguments) |
12 | |
13 | if(isReader(args[0])) |
14 | return function (read) { |
15 | args.unshift(read) |
16 | return pull.apply(null, args) |
17 | } |
18 | |
19 | var read = args.shift() |
20 | |
21 | //if the first function is a duplex stream, |
22 | //pipe from the source. |
23 | if(read && isFunction(read.source)) |
24 | read = read.source |
25 | |
26 | function next () { |
27 | var s = args.shift() |
28 | |
29 | if(null == s) |
30 | return next() |
31 | |
32 | if(isFunction(s)) return s |
33 | |
34 | return function (read) { |
35 | s.sink(read) |
36 | //this supports pipeing through a duplex stream |
37 | //pull(a, b, a) "telephone style". |
38 | //if this stream is in the a (first & last position) |
39 | //s.source will have already been used, but this should never be called |
40 | //so that is okay. |
41 | return s.source |
42 | } |
43 | } |
44 | |
45 | while(args.length) |
46 | read = next() (read) |
47 | |
48 | return read |
49 | } |
50 | |
51 |
Built with git-ssb-web