source: trip-planner-front/node_modules/pump/index.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[6a3a178]1var once = require('once')
2var eos = require('end-of-stream')
3var fs = require('fs') // we only need fs to get the ReadStream and WriteStream prototypes
4
5var noop = function () {}
6var ancient = /^v?\.0/.test(process.version)
7
8var isFn = function (fn) {
9 return typeof fn === 'function'
10}
11
12var isFS = function (stream) {
13 if (!ancient) return false // newer node version do not need to care about fs is a special way
14 if (!fs) return false // browser
15 return (stream instanceof (fs.ReadStream || noop) || stream instanceof (fs.WriteStream || noop)) && isFn(stream.close)
16}
17
18var isRequest = function (stream) {
19 return stream.setHeader && isFn(stream.abort)
20}
21
22var destroyer = function (stream, reading, writing, callback) {
23 callback = once(callback)
24
25 var closed = false
26 stream.on('close', function () {
27 closed = true
28 })
29
30 eos(stream, {readable: reading, writable: writing}, function (err) {
31 if (err) return callback(err)
32 closed = true
33 callback()
34 })
35
36 var destroyed = false
37 return function (err) {
38 if (closed) return
39 if (destroyed) return
40 destroyed = true
41
42 if (isFS(stream)) return stream.close(noop) // use close for fs streams to avoid fd leaks
43 if (isRequest(stream)) return stream.abort() // request.destroy just do .end - .abort is what we want
44
45 if (isFn(stream.destroy)) return stream.destroy()
46
47 callback(err || new Error('stream was destroyed'))
48 }
49}
50
51var call = function (fn) {
52 fn()
53}
54
55var pipe = function (from, to) {
56 return from.pipe(to)
57}
58
59var pump = function () {
60 var streams = Array.prototype.slice.call(arguments)
61 var callback = isFn(streams[streams.length - 1] || noop) && streams.pop() || noop
62
63 if (Array.isArray(streams[0])) streams = streams[0]
64 if (streams.length < 2) throw new Error('pump requires two streams per minimum')
65
66 var error
67 var destroys = streams.map(function (stream, i) {
68 var reading = i < streams.length - 1
69 var writing = i > 0
70 return destroyer(stream, reading, writing, function (err) {
71 if (!error) error = err
72 if (err) destroys.forEach(call)
73 if (reading) return
74 destroys.forEach(call)
75 callback(error)
76 })
77 })
78
79 return streams.reduce(pipe)
80}
81
82module.exports = pump
Note: See TracBrowser for help on using the repository browser.