Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c48c28

Browse files
committed
Removed function constructors
1 parent db362f2 commit 0c48c28

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

lib/connection.js

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,28 @@ function Connection() {
3333
this.on("pipe", function(source) {
3434
that.stream = source;
3535
});
36+
37+
for (var k in protocol.types) {
38+
var v = protocol.types[k];
39+
this[v] = shim_generator.bind(this, v);
40+
}
3641
};
3742
util.inherits(Connection, Writable);
3843

44+
function shim_generator(v, opts) {
45+
var p = generate[v](opts);
46+
if (p instanceof Error) {
47+
this.emit('error', p)
48+
} else {
49+
this.stream.write(p);
50+
}
51+
}
52+
3953
Connection.prototype.setPacketEncoding = function (encoding) {
4054
this._packetEncoding = encoding;
4155
return this;
4256
};
4357

44-
for (var k in protocol.types) {
45-
var v = protocol.types[k];
46-
47-
var fun = "" +
48-
" var p = this.generate." + v +"(opts); " +
49-
" if (p instanceof Error) { " +
50-
" this.emit('error', p) " +
51-
" } else { " +
52-
" this.stream.write(p); " +
53-
" } "
54-
" } ";
55-
56-
Connection.prototype[v] = new Function("opts", fun);
57-
}
58-
5958
Connection.prototype._newPacket = function() {
6059
this.packet = {};
6160
this.tmp = { pos: 1, mul: 1, length: 0};
@@ -138,26 +137,16 @@ Connection.prototype._readPayload = function() {
138137
};
139138

140139
(function() {
141-
var v = protocol.types[k];
142-
143-
var fun = "" +
144-
" 'use strict'; \n" +
145-
" var buf = this.data.slice(this.index, this.index + this.packet.length); \n" +
146-
" var result = null; \n" +
147-
" this.index += this.packet.length; \n" +
148-
" switch(this.packet.cmd) { \n";
149-
150-
Object.keys(parse).forEach(function(key) {
151-
fun = fun +
152-
" case '" + key + "': \n" +
153-
" result = parse." + key + "(buf, this.packet, this._packetEncoding); \n" +
154-
" break; \n ";
155-
});
156140

157-
fun += "} \n";
158-
fun += "return result; \n";
159-
160-
Connection.prototype._parsePayload = new Function("parse", fun);
141+
Connection.prototype._parsePayload = function() {
142+
var buf = this.data.slice(this.index, this.index + this.packet.length);
143+
var result = null;
144+
this.index += this.packet.length;
145+
if (parse[this.packet.cmd]) {
146+
result = parse[this.packet.cmd](buf, this.packet, this._packetEncoding);
147+
}
148+
return result;
149+
}
161150
})();
162151

163152
Connection.prototype._write = function(data, encoding, done) {

0 commit comments

Comments
 (0)