Skip to content

Commit c57eee8

Browse files
committed
normalize whitespace, add comments, and do a little house cleaning
1 parent 37bb13f commit c57eee8

File tree

10 files changed

+105
-80
lines changed

10 files changed

+105
-80
lines changed

lib/client.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ var Client = function(config) {
2020
this.host = this.connectionParameters.host;
2121
this.password = this.connectionParameters.password;
2222

23-
config = config || {};
23+
var c = config || {};
2424

25-
this.connection = config.connection || new Connection({
26-
stream: config.stream,
27-
ssl: config.ssl
25+
this.connection = c.connection || new Connection({
26+
stream: c.stream,
27+
ssl: c.ssl
2828
});
2929
this.queryQueue = [];
30-
this.binary = config.binary || defaults.binary;
30+
this.binary = c.binary || defaults.binary;
3131
this.encoding = 'utf8';
3232
this.processID = null;
3333
this.secretKey = null;
34-
this.ssl = config.ssl || false;
34+
this.ssl = c.ssl || false;
3535
};
3636

3737
util.inherits(Client, EventEmitter);
@@ -48,7 +48,7 @@ Client.prototype.connect = function(callback) {
4848

4949
//once connection is established send startup message
5050
con.on('connect', function() {
51-
if (self.ssl) {
51+
if(self.ssl) {
5252
con.requestSsl();
5353
} else {
5454
con.startup({
@@ -57,6 +57,7 @@ Client.prototype.connect = function(callback) {
5757
});
5858
}
5959
});
60+
6061
con.on('sslconnect', function() {
6162
con.startup({
6263
user: self.user,
@@ -89,10 +90,12 @@ Client.prototype.connect = function(callback) {
8990
con.on('rowDescription', function(msg) {
9091
self.activeQuery.handleRowDescription(msg);
9192
});
93+
9294
//delegate datarow to active query
9395
con.on('dataRow', function(msg) {
9496
self.activeQuery.handleDataRow(msg);
9597
});
98+
9699
//TODO should query gain access to connection?
97100
con.on('portalSuspended', function(msg) {
98101
self.activeQuery.getRows(con);
@@ -106,11 +109,13 @@ Client.prototype.connect = function(callback) {
106109
con.sync();
107110
}
108111
});
112+
109113
con.on('copyInResponse', function(msg) {
110114
self.activeQuery.streamData(self.connection);
111115
});
116+
112117
con.on('copyOutResponse', function(msg) {
113-
if (self.activeQuery.stream === undefined) {
118+
if(self.activeQuery.stream === undefined) {
114119
self.activeQuery._canceledDueToError =
115120
new Error('No destination stream defined');
116121
//canceling query requires creation of new connection
@@ -119,9 +124,11 @@ Client.prototype.connect = function(callback) {
119124
.cancel(self, self.activeQuery);
120125
}
121126
});
127+
122128
con.on('copyData', function (msg) {
123129
self.activeQuery.handleCopyFromChunk(msg.chunk);
124130
});
131+
125132
if (!callback) {
126133
self.emit('connect');
127134
} else {
@@ -169,7 +176,7 @@ Client.prototype.connect = function(callback) {
169176
};
170177

171178
Client.prototype.cancel = function(client, query) {
172-
if (client.activeQuery == query) {
179+
if(client.activeQuery == query) {
173180
var con = this.connection;
174181

175182
if(this.host && this.host.indexOf('/') === 0) {
@@ -182,8 +189,7 @@ Client.prototype.cancel = function(client, query) {
182189
con.on('connect', function() {
183190
con.cancel(client.processID, client.secretKey);
184191
});
185-
}
186-
else if (client.queryQueue.indexOf(query) != -1) {
192+
} else if(client.queryQueue.indexOf(query) != -1) {
187193
client.queryQueue.splice(client.queryQueue.indexOf(query), 1);
188194
}
189195
};
@@ -197,25 +203,29 @@ Client.prototype._pulseQueryQueue = function() {
197203
this.activeQuery.submit(this.connection);
198204
} else if(this.hasExecuted) {
199205
this.activeQuery = null;
200-
if(this._drainPaused > 0) { this._drainPaused++; }
201-
else { this.emit('drain'); }
206+
//TODO remove pauseDrain for v1.0
207+
if(this._drainPaused > 0) {
208+
this._drainPaused++;
209+
}
210+
else {
211+
this.emit('drain');
212+
}
202213
}
203214
}
204215
};
205216

206217
Client.prototype._copy = function (text, stream) {
207-
var config = {},
208-
query;
218+
var config = {};
209219
config.text = text;
210220
config.stream = stream;
211221
config.callback = function (error) {
212-
if (error) {
222+
if(error) {
213223
config.stream.error(error);
214224
} else {
215225
config.stream.close();
216226
}
217227
};
218-
query = new Query(config);
228+
var query = new Query(config);
219229
this.queryQueue.push(query);
220230
this._pulseQueryQueue();
221231
return config.stream;
@@ -234,7 +244,7 @@ Client.prototype.query = function(config, values, callback) {
234244
//can take in strings, config object or query object
235245
var query = (config instanceof Query) ? config :
236246
new Query(config, values, callback);
237-
if (this.binary && !query.binary) {
247+
if(this.binary && !query.binary) {
238248
query.binary = true;
239249
}
240250

lib/connection.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ util.inherits(Connection, EventEmitter);
2424

2525
Connection.prototype.connect = function(port, host) {
2626

27-
if (this.stream.readyState === 'closed') {
27+
if(this.stream.readyState === 'closed') {
2828
this.stream.connect(port, host);
29-
} else if (this.stream.readyState == 'open') {
29+
} else if(this.stream.readyState == 'open') {
3030
this.emit('connect');
3131
}
3232

@@ -48,7 +48,7 @@ Connection.prototype.connect = function(port, host) {
4848
self.emit(msg.name, msg);
4949
});
5050
this.once('sslresponse', function(msg) {
51-
if (msg.text == 0x53) {
51+
if(msg.text == 0x53) {
5252
var tls = require('tls');
5353
self.stream.removeAllListeners();
5454
self.stream = tls.connect({
@@ -214,7 +214,7 @@ Connection.prototype.bind = function(config, more) {
214214
}
215215
}
216216

217-
if (config.binary) {
217+
if(config.binary) {
218218
buffer.addInt16(1); // format codes to use binary
219219
buffer.addInt16(1);
220220
}
@@ -301,7 +301,10 @@ Connection.prototype.readSslResponse = function() {
301301
this.lastOffset = this.offset;
302302
return false;
303303
}
304-
return { name: 'sslresponse', text: this.buffer[this.offset++] };
304+
return {
305+
name: 'sslresponse',
306+
text: this.buffer[this.offset++]
307+
};
305308
};
306309

307310
Connection.prototype.parseMessage = function() {
@@ -500,12 +503,12 @@ Connection.prototype.parseE = function(input) {
500503
fields[fieldType] = this.parseCString();
501504
fieldType = this.readString(1);
502505
}
503-
if (input.name === 'error') {
506+
if(input.name === 'error') {
504507
// the msg is an Error instance
505508
msg = new Error(fields.M);
506509
for (item in input) {
507510
// copy input properties to the error
508-
if (input.hasOwnProperty(item)) {
511+
if(input.hasOwnProperty(item)) {
509512
msg[item] = input[item];
510513
}
511514
}

lib/copystream.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ CopyFromStream.prototype._writable = function () {
1919
};
2020

2121
CopyFromStream.prototype.startStreamingToConnection = function (connection) {
22-
if (this._error) {
22+
if(this._error) {
2323
return;
2424
}
2525
this._connection = connection;
@@ -30,13 +30,13 @@ CopyFromStream.prototype.startStreamingToConnection = function (connection) {
3030
CopyFromStream.prototype._handleChunk = function (string, encoding) {
3131
var dataChunk,
3232
tmpBuffer;
33-
if (string !== undefined) {
34-
if (string instanceof Buffer) {
33+
if(string !== undefined) {
34+
if(string instanceof Buffer) {
3535
dataChunk = string;
3636
} else {
3737
dataChunk = new Buffer(string, encoding);
3838
}
39-
if (this._buffer.length) {
39+
if(this._buffer.length) {
4040
//Buffer.concat is better, but it's missing
4141
//in node v0.6.x
4242
tmpBuffer = new Buffer(this._buffer.length + dataChunk.length);
@@ -53,10 +53,10 @@ CopyFromStream.prototype._handleChunk = function (string, encoding) {
5353

5454
CopyFromStream.prototype._sendIfConnectionReady = function () {
5555
var dataSent = false;
56-
if (this._connection) {
56+
if(this._connection) {
5757
dataSent = this._connection.sendCopyFromChunk(this._buffer);
5858
this._buffer = new Buffer(0);
59-
if (this._dataBuffered) {
59+
if(this._dataBuffered) {
6060
this.emit('drain');
6161
}
6262
this._dataBuffered = false;
@@ -67,43 +67,43 @@ CopyFromStream.prototype._sendIfConnectionReady = function () {
6767
};
6868

6969
CopyFromStream.prototype._endIfNeedAndPossible = function () {
70-
if (this._connection && this._finished && !this._finishedSent) {
70+
if(this._connection && this._finished && !this._finishedSent) {
7171
this._finishedSent = true;
7272
this._connection.endCopyFrom();
7373
}
7474
};
7575

7676
CopyFromStream.prototype.write = function (string, encoding) {
77-
if (this._error || this._finished) {
77+
if(this._error || this._finished) {
7878
return false;
7979
}
8080
return this._handleChunk.apply(this, arguments);
8181
};
8282

8383
CopyFromStream.prototype.end = function (string, encondig) {
84-
if (this._error || this._finished) {
84+
if(this._error || this._finished) {
8585
return false;
8686
}
8787
this._finished = true;
88-
if (string !== undefined) {
88+
if(string !== undefined) {
8989
this._handleChunk.apply(this, arguments);
9090
}
9191
this._endIfNeedAndPossible();
9292
};
9393

9494
CopyFromStream.prototype.error = function (error) {
95-
if (this._error || this._closed) {
95+
if(this._error || this._closed) {
9696
return false;
9797
}
9898
this._error = true;
9999
this.emit('error', error);
100100
};
101101

102102
CopyFromStream.prototype.close = function () {
103-
if (this._error || this._closed) {
103+
if(this._error || this._closed) {
104104
return false;
105105
}
106-
if (!this._finishedSent) {
106+
if(!this._finishedSent) {
107107
throw new Error("seems to be error in code that uses CopyFromStream");
108108
}
109109
this.emit("close");
@@ -122,11 +122,11 @@ var CopyToStream = function () {
122122
util.inherits(CopyToStream, Stream);
123123

124124
CopyToStream.prototype._outputDataChunk = function () {
125-
if (this._paused) {
125+
if(this._paused) {
126126
return;
127127
}
128-
if (this.buffer.length) {
129-
if (this._encoding) {
128+
if(this.buffer.length) {
129+
if(this._encoding) {
130130
this.emit('data', this.buffer.toString(this._encoding));
131131
} else {
132132
this.emit('data', this.buffer);
@@ -140,31 +140,31 @@ CopyToStream.prototype._readable = function () {
140140
};
141141

142142
CopyToStream.prototype.error = function (error) {
143-
if (!this.readable) {
143+
if(!this.readable) {
144144
return false;
145145
}
146146
this._error = error;
147-
if (!this._paused) {
147+
if(!this._paused) {
148148
this.emit('error', error);
149149
}
150150
};
151151

152152
CopyToStream.prototype.close = function () {
153-
if (!this.readable) {
153+
if(!this.readable) {
154154
return false;
155155
}
156156
this._finished = true;
157-
if (!this._paused) {
157+
if(!this._paused) {
158158
this.emit("end");
159159
}
160160
};
161161

162162
CopyToStream.prototype.handleChunk = function (chunk) {
163163
var tmpBuffer;
164-
if (!this.readable) {
164+
if(!this.readable) {
165165
return;
166166
}
167-
if (!this.buffer.length) {
167+
if(!this.buffer.length) {
168168
this.buffer = chunk;
169169
} else {
170170
tmpBuffer = new Buffer(this.buffer.length + chunk.length);
@@ -176,22 +176,22 @@ CopyToStream.prototype.handleChunk = function (chunk) {
176176
};
177177

178178
CopyToStream.prototype.pause = function () {
179-
if (!this.readable) {
179+
if(!this.readable) {
180180
return false;
181181
}
182182
this._paused = true;
183183
};
184184

185185
CopyToStream.prototype.resume = function () {
186-
if (!this._paused) {
186+
if(!this._paused) {
187187
return false;
188188
}
189189
this._paused = false;
190190
this._outputDataChunk();
191-
if (this._error) {
191+
if(this._error) {
192192
return this.emit('error', this._error);
193193
}
194-
if (this._finished) {
194+
if(this._finished) {
195195
return this.emit('end');
196196
}
197197
};

0 commit comments

Comments
 (0)