@@ -33,29 +33,28 @@ function Connection() {
33
33
this . on ( "pipe" , function ( source ) {
34
34
that . stream = source ;
35
35
} ) ;
36
+
37
+ for ( var k in protocol . types ) {
38
+ var v = protocol . types [ k ] ;
39
+ this [ v ] = shim_generator . bind ( this , v ) ;
40
+ }
36
41
} ;
37
42
util . inherits ( Connection , Writable ) ;
38
43
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
+
39
53
Connection . prototype . setPacketEncoding = function ( encoding ) {
40
54
this . _packetEncoding = encoding ;
41
55
return this ;
42
56
} ;
43
57
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
-
59
58
Connection . prototype . _newPacket = function ( ) {
60
59
this . packet = { } ;
61
60
this . tmp = { pos : 1 , mul : 1 , length : 0 } ;
@@ -138,26 +137,16 @@ Connection.prototype._readPayload = function() {
138
137
} ;
139
138
140
139
( 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
- } ) ;
156
140
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
+ }
161
150
} ) ( ) ;
162
151
163
152
Connection . prototype . _write = function ( data , encoding , done ) {
0 commit comments