Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug logging #33

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions through2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ var Transform = require('readable-stream/transform')
, inherits = require('util').inherits
, xtend = require('xtend')

var debug = process.env['DEBUG'] || ''
var DEBUG = (debug === '*') || (debug === 'through2')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe (debug === '*') || (/through2/.test(debug)) so you can do DEBUG=through2,another-module

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, updated in max-mapper@7445786


function DestroyableTransform(opts) {
Transform.call(this, opts)
this._destroyed = false
if (DEBUG) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for perf reasons you probably want to put the content of this if inside a function to avoid these functions being hoisted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would only happen once though at the time of stream construction, do you think that would make a noticeable perf impact?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, probably not

this.on('finish', function() {
console.error(' through2 finish')
})
this.on('end', function() {
console.error(' through2 end')
})
}
}

inherits(DestroyableTransform, Transform)
Expand Down Expand Up @@ -50,8 +61,11 @@ function through2 (construct) {

// main export, just make me a transform stream!
module.exports = through2(function (options, transform, flush) {
if (DEBUG) console.error(' through2 initialize')

var t2 = new DestroyableTransform(options)

if (DEBUG) transform = debugTransform(transform, t2)
t2._transform = transform

if (flush)
Expand All @@ -64,6 +78,8 @@ module.exports = through2(function (options, transform, flush) {
// make me a reusable prototype that I can `new`, or implicitly `new`
// with a constructor call
module.exports.ctor = through2(function (options, transform, flush) {
if (DEBUG) console.error(' through2.ctor initialize')

function Through2 (override) {
if (!(this instanceof Through2))
return new Through2(override)
Expand All @@ -75,6 +91,7 @@ module.exports.ctor = through2(function (options, transform, flush) {

inherits(Through2, DestroyableTransform)

if (DEBUG) transform = debugTransform(transform)
Through2.prototype._transform = transform

if (flush)
Expand All @@ -85,12 +102,22 @@ module.exports.ctor = through2(function (options, transform, flush) {


module.exports.obj = through2(function (options, transform, flush) {
if (DEBUG) console.error(' through2.obj initialize')

var t2 = new DestroyableTransform(xtend({ objectMode: true, highWaterMark: 16 }, options))

if (DEBUG) transform = debugTransform(transform, t2)
t2._transform = transform

if (flush)
t2._flush = flush

return t2
})

function debugTransform(transform, ctx) {
return function debugTransform(chunk, enc, callback) {
console.error(' through2 transform', typeof chunk, {length: chunk.length, encoding: enc})
transform.apply(ctx || this, arguments)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should always be this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, I dont know what I was thinking

}
}