Projekt

Obecné

Profil

Stáhnout (1.09 KB) Statistiky
| Větev: | Revize:
1
/*!
2
 * unpipe
3
 * Copyright(c) 2015 Douglas Christopher Wilson
4
 * MIT Licensed
5
 */
6

    
7
'use strict'
8

    
9
/**
10
 * Module exports.
11
 * @public
12
 */
13

    
14
module.exports = unpipe
15

    
16
/**
17
 * Determine if there are Node.js pipe-like data listeners.
18
 * @private
19
 */
20

    
21
function hasPipeDataListeners(stream) {
22
  var listeners = stream.listeners('data')
23

    
24
  for (var i = 0; i < listeners.length; i++) {
25
    if (listeners[i].name === 'ondata') {
26
      return true
27
    }
28
  }
29

    
30
  return false
31
}
32

    
33
/**
34
 * Unpipe a stream from all destinations.
35
 *
36
 * @param {object} stream
37
 * @public
38
 */
39

    
40
function unpipe(stream) {
41
  if (!stream) {
42
    throw new TypeError('argument stream is required')
43
  }
44

    
45
  if (typeof stream.unpipe === 'function') {
46
    // new-style
47
    stream.unpipe()
48
    return
49
  }
50

    
51
  // Node.js 0.8 hack
52
  if (!hasPipeDataListeners(stream)) {
53
    return
54
  }
55

    
56
  var listener
57
  var listeners = stream.listeners('close')
58

    
59
  for (var i = 0; i < listeners.length; i++) {
60
    listener = listeners[i]
61

    
62
    if (listener.name !== 'cleanup' && listener.name !== 'onclose') {
63
      continue
64
    }
65

    
66
    // invoke the listener
67
    listener.call(stream)
68
  }
69
}
(4-4/5)