Skip to content

Commit

Permalink
Merge pull request #1354 from nylen/debug-no-circular-dep
Browse files Browse the repository at this point in the history
Remove circular dependency from debugging code
  • Loading branch information
seanstrom committed Jan 17, 2015
2 parents a1dc16e + e7c7784 commit f0b7f75
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
12 changes: 11 additions & 1 deletion index.js
Expand Up @@ -172,5 +172,15 @@ request.forever = function (agentOptions, optionsArg) {

module.exports = request
request.Request = require('./request')
request.debug = process.env.NODE_DEBUG && /\brequest\b/.test(process.env.NODE_DEBUG)
request.initParams = initParams

// Backwards compatibility for request.debug
Object.defineProperty(request, 'debug', {
enumerable : true,
get : function() {
return request.Request.debug
},
set : function(debug) {
request.Request.debug = debug
}
})
11 changes: 0 additions & 11 deletions lib/debug.js

This file was deleted.

9 changes: 8 additions & 1 deletion request.js
Expand Up @@ -23,7 +23,6 @@ var http = require('http')
, FormData = require('form-data')
, cookies = require('./lib/cookies')
, copy = require('./lib/copy')
, debug = require('./lib/debug')
, net = require('net')
, CombinedStream = require('combined-stream')
, isstream = require('isstream')
Expand Down Expand Up @@ -240,6 +239,14 @@ function Request (options) {

util.inherits(Request, stream.Stream)

// Debugging
Request.debug = process.env.NODE_DEBUG && /\brequest\b/.test(process.env.NODE_DEBUG)
function debug() {
if (Request.debug) {
console.error('REQUEST %s', util.format.apply(util, arguments))
}
}

Request.prototype.setupTunnel = function () {
// Set up the tunneling agent if necessary
// Only send the proxy whitelisted header names.
Expand Down
16 changes: 12 additions & 4 deletions tests/test-node-debug.js
Expand Up @@ -12,18 +12,21 @@ var s = http.createServer(function(req, res) {
var stderr = []
, prevStderrLen = 0

process.stderr.write = function(string, encoding, fd) {
stderr.push(string)
}

tape('setup', function(t) {
process.stderr._oldWrite = process.stderr.write
process.stderr.write = function(string, encoding, fd) {
stderr.push(string)
}

s.listen(6767, function() {
t.end()
})
})

tape('a simple request should not fail with debugging enabled', function(t) {
request.debug = true
t.equal(request.Request.debug, true, 'request.debug sets request.Request.debug')
t.equal(request.debug, true, 'request.debug gets request.Request.debug')
stderr = []

request('http://localhost:6767', function(err, res, body) {
Expand Down Expand Up @@ -68,6 +71,8 @@ tape('there should be no further lookups on process.env', function(t) {

tape('it should be possible to disable debugging at runtime', function(t) {
request.debug = false
t.equal(request.Request.debug, false, 'request.debug sets request.Request.debug')
t.equal(request.debug, false, 'request.debug gets request.Request.debug')
stderr = []

request('http://localhost:6767', function(err, res, body) {
Expand All @@ -79,6 +84,9 @@ tape('it should be possible to disable debugging at runtime', function(t) {
})

tape('cleanup', function(t) {
process.stderr.write = process.stderr._oldWrite
delete process.stderr._oldWrite

s.close(function() {
t.end()
})
Expand Down

0 comments on commit f0b7f75

Please sign in to comment.