-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (44 loc) · 993 Bytes
/
index.js
File metadata and controls
49 lines (44 loc) · 993 Bytes
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
var Reader = require('pull-reader')
var Writer = require('pull-pushable')
var cat = require('pull-cat')
var pair = require('pull-pair')
function once (cb) {
var called = 0
return function (a, b, c) {
if(called++) return
cb(a, b, c)
}
}
function isFunction (f) {
return 'function' === typeof f
}
module.exports = function (opts, _cb) {
if(isFunction(opts)) _cb = opts, opts = {}
_cb = once(_cb || function noop () {})
var reader = Reader(opts && opts.timeout || 5e3)
var writer = Writer(function (err) {
if(err) _cb(err)
})
var p = pair()
return {
handshake: {
read: reader.read,
abort: function (err) {
writer.end(err)
reader.abort(err, function (err) {
})
_cb(err)
},
write: writer.push,
rest: function () {
writer.end()
return {
source: reader.read(),
sink: p.sink
}
}
},
sink: reader,
source: cat([writer, p.source])
}
}