through2 本质上是一种transform的流
被封装更好地操纵流
var Transform = require('readable-stream/transform')
, inherits = require('util').inherits
, xtend = require('xtend')
function DestroyableTransform(opts) {
// 继续Transform
Transform.call(this, opts)
this._destroyed = false
}
inherits(DestroyableTransform, Transform)
// 原型接口destory 用来封闭流
DestroyableTransform.prototype.destroy = function(err) {
if (this._destroyed) return
this._destroyed = true
var self = this
process.nextTick(function() {
if (err)
self.emit('error', err)
self.emit('close')
//events的运用
})
}
// a noop _transform function
// 空操纵
function noop (chunk, enc, callback) {
callback(null, chunk)
}
// create a new export function, used by both the main export and
// the .ctor export, contains common logic for dealing with arguments
// 返回一个导出的函数接口
function through2 (construct) {
// 返回运用的匿名函数
return function (options, transform, flush) {
if (typeof options == 'function') {
flush = transform
transform = options
options = {}
}
// 这类匿名函数我们平常能够用来做二次推断触发
if (typeof transform != 'function')
transform = noop
if (typeof flush != 'function')
flush = null
return construct(options, transform, flush)
}
}
// main export, just make me a transform stream!
// 重要出口,运用through2返回一个DestroyTransform实例
module.exports = through2(function (options, transform, flush) {
var t2 = new DestroyableTransform(options)
t2._transform = transform
if (flush)
t2._flush = flush
return t2
})
// make me a reusable prototype that I can `new`, or implicitly `new`
// with a constructor call
// 对外暴露一个能够直接 new (或许不加 new)来建立实例的的组织函数
module.exports.ctor = through2(function (options, transform, flush) {
function Through2 (override) {
if (!(this instanceof Through2))
// 这里就是直接自动new
return new Through2(override)
this.options = xtend(options, override)
// 增加设置
DestroyableTransform.call(this, this.options)
}
inherits(Through2, DestroyableTransform)
Through2.prototype._transform = transform
if (flush)
Through2.prototype._flush = flush
return Through2
})
// Object形式的简朴封装
module.exports.obj = through2(function (options, transform, flush) {
var t2 = new DestroyableTransform(xtend({ objectMode: true, highWaterMark: 16 }, options))
t2._transform = transform
if (flush)
t2._flush = flush
return t2
})
xtend lib //简朴的继续 两种,一种可变 一种不可变
module.exports = extend
var hasOwnProperty = Object.prototype.hasOwnProperty;
// 缓存老套路。优化机能
function extend(target) {
// 这里有个有意思的处所。target第一个拿进来。在多个参数的情况下。下面从arguments[1]最先取,而下面能够直接运用target,而不必再声明一下变量
for (var i = 1; i < arguments.length//这块实在能够缓存长度的; i++) {
var source = arguments[i]
for (var key in source) {
if (hasOwnProperty.call(source, key)) {
// 推断当前实例是不是存在属性
// 有则增加到target上。无则跳过,支撑掩盖
target[key] = source[key]
}
}
}
return target
}
不可变。
module.exports = extend
var hasOwnProperty = Object.prototype.hasOwnProperty;
function extend() {
// 与本来差别的是,这里运用了一份初始化的对象援用来作为容器承载
// 其他没有差别
var target = {}
for (var i = 0; i < arguments.length; i++) {
var source = arguments[i]
for (var key in source) {
if (hasOwnProperty.call(source, key)) {
target[key] = source[key]
}
}
}
return target
}