Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Lazily evaluate arguments to debug calls #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
22 changes: 13 additions & 9 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ var debug = (debugLevel & 0x4) ?
function() { sys.error.apply(this, arguments); } :
function() { };

var debugLazy = (debugLevel & 0x4) ?
function(func) { debug(func()); } :
function(func) { };

// Generate a Sec-WebSocket-* value
var createSecretKey = function() {
// How many spaces will we be inserting?
Expand Down Expand Up @@ -223,7 +227,7 @@ var WebSocket = function(url, proto, opts) {

// FRAME_LO
function(buf, off) {
debug('frame_lo(' + sys.inspect(buf) + ', ' + off + ')');
debugLazy(function(){return 'frame_lo(' + sys.inspect(buf) + ', ' + off + ')';});

// Find the first instance of 0xff, our terminating byte
for (var i = off; i < buf.length && buf[i] != 0xff; i++)
Expand Down Expand Up @@ -283,7 +287,7 @@ var WebSocket = function(url, proto, opts) {

// FRAME_HI
function(buf, off) {
debug('frame_hi(' + sys.inspect(buf) + ', ' + off + ')');
debugLazy(function(){return 'frame_hi(' + sys.inspect(buf) + ', ' + off + ')';});

if (buf[off] !== 0) {
throw new Error('High-byte framing not supported.');
Expand All @@ -300,7 +304,7 @@ var WebSocket = function(url, proto, opts) {
return;
}

debug('dataListener(' + sys.inspect(buf) + ')');
debugLazy(function(){return 'dataListener(' + sys.inspect(buf) + ')';});

var off = 0;
var consumed = 0;
Expand Down Expand Up @@ -441,11 +445,11 @@ var WebSocket = function(url, proto, opts) {
var key2 = createSecretKey();
var challenge = createChallenge();

debug(
debugLazy(function(){return
'key1=\'' + str2hex(key1) + '\'; ' +
'key2=\'' + str2hex(key2) + '\'; ' +
'challenge=\'' + str2hex(challenge) + '\''
);
'challenge=\'' + str2hex(challenge) + '\'';
});

var httpHeaders = {
'Connection' : 'Upgrade',
Expand Down Expand Up @@ -516,10 +520,10 @@ var WebSocket = function(url, proto, opts) {

// Handshaking fails; we're donezo
if (actual != expected) {
debug(
debugLazy(function(){return
'expected=\'' + str2hex(expected) + '\'; ' +
'actual=\'' + str2hex(actual) + '\''
);
'actual=\'' + str2hex(actual) + '\'';
});

process.nextTick(function() {
// N.B. Emit 'wserror' here, as 'error' is a reserved word in the
Expand Down