Projekt

Obecné

Profil

Stáhnout (2.09 KB) Statistiky
| Větev: | Revize:
1
var Transform = require('readable-stream').Transform
2
  , inherits  = require('util').inherits
3
  , xtend     = require('xtend')
4

    
5
function DestroyableTransform(opts) {
6
  Transform.call(this, opts)
7
  this._destroyed = false
8
}
9

    
10
inherits(DestroyableTransform, Transform)
11

    
12
DestroyableTransform.prototype.destroy = function(err) {
13
  if (this._destroyed) return
14
  this._destroyed = true
15
  
16
  var self = this
17
  process.nextTick(function() {
18
    if (err)
19
      self.emit('error', err)
20
    self.emit('close')
21
  })
22
}
23

    
24
// a noop _transform function
25
function noop (chunk, enc, callback) {
26
  callback(null, chunk)
27
}
28

    
29

    
30
// create a new export function, used by both the main export and
31
// the .ctor export, contains common logic for dealing with arguments
32
function through2 (construct) {
33
  return function (options, transform, flush) {
34
    if (typeof options == 'function') {
35
      flush     = transform
36
      transform = options
37
      options   = {}
38
    }
39

    
40
    if (typeof transform != 'function')
41
      transform = noop
42

    
43
    if (typeof flush != 'function')
44
      flush = null
45

    
46
    return construct(options, transform, flush)
47
  }
48
}
49

    
50

    
51
// main export, just make me a transform stream!
52
module.exports = through2(function (options, transform, flush) {
53
  var t2 = new DestroyableTransform(options)
54

    
55
  t2._transform = transform
56

    
57
  if (flush)
58
    t2._flush = flush
59

    
60
  return t2
61
})
62

    
63

    
64
// make me a reusable prototype that I can `new`, or implicitly `new`
65
// with a constructor call
66
module.exports.ctor = through2(function (options, transform, flush) {
67
  function Through2 (override) {
68
    if (!(this instanceof Through2))
69
      return new Through2(override)
70

    
71
    this.options = xtend(options, override)
72

    
73
    DestroyableTransform.call(this, this.options)
74
  }
75

    
76
  inherits(Through2, DestroyableTransform)
77

    
78
  Through2.prototype._transform = transform
79

    
80
  if (flush)
81
    Through2.prototype._flush = flush
82

    
83
  return Through2
84
})
85

    
86

    
87
module.exports.obj = through2(function (options, transform, flush) {
88
  var t2 = new DestroyableTransform(xtend({ objectMode: true, highWaterMark: 16 }, options))
89

    
90
  t2._transform = transform
91

    
92
  if (flush)
93
    t2._flush = flush
94

    
95
  return t2
96
})
(4-4/4)