16
16
* See the License for the specific language governing permissions and
17
17
* limitations under the License.
18
18
*/
19
-
19
+
20
20
import buf from './buf' ;
21
21
22
22
let
23
- _CHUNK_HEADER_SIZE = 2 ,
23
+ _CHUNK_HEADER_SIZE = 2 ,
24
24
_MESSAGE_BOUNDARY = 0x00 ,
25
25
_DEFAULT_BUFFER_SIZE = 1400 ; // http://stackoverflow.com/questions/2613734/maximum-packet-size-for-a-tcp-connection
26
26
29
29
* @access private
30
30
*/
31
31
class Chunker extends buf . BaseBuffer {
32
- constructor ( channel , bufferSize ) {
33
- super ( 0 ) ;
32
+ constructor ( channel , bufferSize ) {
33
+ super ( 0 ) ;
34
34
this . _bufferSize = bufferSize || _DEFAULT_BUFFER_SIZE ;
35
35
this . _ch = channel ;
36
- this . _buffer = buf . alloc ( this . _bufferSize ) ;
36
+ this . _buffer = buf . alloc ( this . _bufferSize ) ;
37
37
this . _currentChunkStart = 0 ;
38
38
this . _chunkOpen = false ;
39
39
}
40
40
41
- putUInt8 ( position , val ) {
41
+ putUInt8 ( position , val ) {
42
42
this . _ensure ( 1 ) ;
43
- this . _buffer . writeUInt8 ( val ) ;
43
+ this . _buffer . writeUInt8 ( val ) ;
44
44
}
45
45
46
- putInt8 ( position , val ) {
46
+ putInt8 ( position , val ) {
47
47
this . _ensure ( 1 ) ;
48
- this . _buffer . writeInt8 ( val ) ;
48
+ this . _buffer . writeInt8 ( val ) ;
49
49
}
50
50
51
- putFloat64 ( position , val ) {
51
+ putFloat64 ( position , val ) {
52
52
this . _ensure ( 8 ) ;
53
- this . _buffer . writeFloat64 ( val ) ;
53
+ this . _buffer . writeFloat64 ( val ) ;
54
54
}
55
55
56
- putBytes ( position , data ) {
57
- // TODO: If data is larger than our chunk size or so, we're very likely better off just passing this buffer on rather than doing the copy here
58
- // TODO: *however* note that we need some way to find out when the data has been written (and thus the buffer can be re-used) if we take that approach
59
- while ( data . remaining ( ) > 0 )
60
- {
56
+ putBytes ( position , data ) {
57
+ // TODO: If data is larger than our chunk size or so, we're very likely better off just passing this buffer on
58
+ // rather than doing the copy here TODO: *however* note that we need some way to find out when the data has been
59
+ // written (and thus the buffer can be re-used) if we take that approach
60
+ while ( data . remaining ( ) > 0 ) {
61
61
// Ensure there is an open chunk, and that it has at least one byte of space left
62
- this . _ensure ( 1 ) ;
63
- if ( this . _buffer . remaining ( ) > data . remaining ( ) ) {
64
- this . _buffer . writeBytes ( data ) ;
62
+ this . _ensure ( 1 ) ;
63
+ if ( this . _buffer . remaining ( ) > data . remaining ( ) ) {
64
+ this . _buffer . writeBytes ( data ) ;
65
65
} else {
66
- this . _buffer . writeBytes ( data . readSlice ( this . _buffer . remaining ( ) ) ) ;
66
+ this . _buffer . writeBytes ( data . readSlice ( this . _buffer . remaining ( ) ) ) ;
67
67
}
68
68
}
69
69
return this ;
70
70
}
71
71
72
- flush ( ) {
73
- if ( this . _buffer . position > 0 ) {
72
+ flush ( ) {
73
+ if ( this . _buffer . position > 0 ) {
74
74
this . _closeChunkIfOpen ( ) ;
75
75
76
76
// Local copy and clear the buffer field. This ensures that the buffer is not re-released if the flush call fails
77
77
let out = this . _buffer ;
78
78
this . _buffer = null ;
79
79
80
- this . _ch . write ( out . getSlice ( 0 , out . position ) ) ;
80
+ this . _ch . write ( out . getSlice ( 0 , out . position ) ) ;
81
81
82
82
// Alloc a new output buffer. We assume we're using NodeJS's buffer pooling under the hood here!
83
- this . _buffer = buf . alloc ( this . _bufferSize ) ;
83
+ this . _buffer = buf . alloc ( this . _bufferSize ) ;
84
84
this . _chunkOpen = false ;
85
85
}
86
86
return this ;
87
87
}
88
88
89
- /**
89
+ /**
90
90
* Bolt messages are encoded in one or more chunks, and the boundary between two messages
91
91
* is encoded as a 0-length chunk, `00 00`. This inserts such a message boundary, closing
92
- * any currently open chunk as needed
92
+ * any currently open chunk as needed
93
93
*/
94
- messageBoundary ( ) {
95
-
94
+ messageBoundary ( ) {
95
+
96
96
this . _closeChunkIfOpen ( ) ;
97
97
98
- if ( this . _buffer . remaining ( ) < _CHUNK_HEADER_SIZE ) {
98
+ if ( this . _buffer . remaining ( ) < _CHUNK_HEADER_SIZE ) {
99
99
this . flush ( ) ;
100
100
}
101
101
102
102
// Write message boundary
103
- this . _buffer . writeInt16 ( _MESSAGE_BOUNDARY ) ;
103
+ this . _buffer . writeInt16 ( _MESSAGE_BOUNDARY ) ;
104
104
}
105
105
106
106
/** Ensure at least the given size is available for writing */
107
- _ensure ( size ) {
107
+ _ensure ( size ) {
108
108
let toWriteSize = this . _chunkOpen ? size : size + _CHUNK_HEADER_SIZE ;
109
- if ( this . _buffer . remaining ( ) < toWriteSize ) {
109
+ if ( this . _buffer . remaining ( ) < toWriteSize ) {
110
110
this . flush ( ) ;
111
111
}
112
112
113
- if ( ! this . _chunkOpen ) {
113
+ if ( ! this . _chunkOpen ) {
114
114
this . _currentChunkStart = this . _buffer . position ;
115
115
this . _buffer . position = this . _buffer . position + _CHUNK_HEADER_SIZE ;
116
116
this . _chunkOpen = true ;
117
117
}
118
118
}
119
119
120
- _closeChunkIfOpen ( ) {
121
- if ( this . _chunkOpen ) {
120
+ _closeChunkIfOpen ( ) {
121
+ if ( this . _chunkOpen ) {
122
122
let chunkSize = this . _buffer . position - ( this . _currentChunkStart + _CHUNK_HEADER_SIZE ) ;
123
- this . _buffer . putUInt16 ( this . _currentChunkStart , chunkSize ) ;
123
+ this . _buffer . putUInt16 ( this . _currentChunkStart , chunkSize ) ;
124
124
this . _chunkOpen = false ;
125
125
}
126
126
}
@@ -139,67 +139,67 @@ class Dechunker {
139
139
this . _state = this . AWAITING_CHUNK ;
140
140
}
141
141
142
- AWAITING_CHUNK ( buf ) {
143
- if ( buf . remaining ( ) >= 2 ) {
142
+ AWAITING_CHUNK ( buf ) {
143
+ if ( buf . remaining ( ) >= 2 ) {
144
144
// Whole header available, read that
145
- return this . _onHeader ( buf . readUInt16 ( ) ) ;
145
+ return this . _onHeader ( buf . readUInt16 ( ) ) ;
146
146
} else {
147
147
// Only one byte available, read that and wait for the second byte
148
148
this . _partialChunkHeader = buf . readUInt8 ( ) << 8 ;
149
149
return this . IN_HEADER ;
150
150
}
151
151
}
152
152
153
- IN_HEADER ( buf ) {
153
+ IN_HEADER ( buf ) {
154
154
// First header byte read, now we read the next one
155
- return this . _onHeader ( ( this . _partialChunkHeader | buf . readUInt8 ( ) ) & 0xFFFF ) ;
155
+ return this . _onHeader ( ( this . _partialChunkHeader | buf . readUInt8 ( ) ) & 0xFFFF ) ;
156
156
}
157
157
158
- IN_CHUNK ( buf ) {
159
- if ( this . _chunkSize <= buf . remaining ( ) ) {
158
+ IN_CHUNK ( buf ) {
159
+ if ( this . _chunkSize <= buf . remaining ( ) ) {
160
160
// Current packet is larger than current chunk, or same size:
161
- this . _currentMessage . push ( buf . readSlice ( this . _chunkSize ) ) ;
161
+ this . _currentMessage . push ( buf . readSlice ( this . _chunkSize ) ) ;
162
162
return this . AWAITING_CHUNK ;
163
163
} else {
164
164
// Current packet is smaller than the chunk we're reading, split the current chunk itself up
165
165
this . _chunkSize -= buf . remaining ( ) ;
166
- this . _currentMessage . push ( buf . readSlice ( buf . remaining ( ) ) ) ;
166
+ this . _currentMessage . push ( buf . readSlice ( buf . remaining ( ) ) ) ;
167
167
return this . IN_CHUNK ;
168
168
}
169
169
}
170
170
171
- CLOSED ( buf ) {
171
+ CLOSED ( buf ) {
172
172
// no-op
173
173
}
174
174
175
175
/** Called when a complete chunk header has been recieved */
176
- _onHeader ( header ) {
177
- if ( header == 0 ) {
176
+ _onHeader ( header ) {
177
+ if ( header == 0 ) {
178
178
// Message boundary
179
179
let message ;
180
- if ( this . _currentMessage . length == 1 ) {
180
+ if ( this . _currentMessage . length == 1 ) {
181
181
message = this . _currentMessage [ 0 ] ;
182
182
} else {
183
183
message = new buf . CombinedBuffer ( this . _currentMessage ) ;
184
184
}
185
185
this . _currentMessage = [ ] ;
186
- this . onmessage ( message ) ;
186
+ this . onmessage ( message ) ;
187
187
return this . AWAITING_CHUNK ;
188
188
} else {
189
189
this . _chunkSize = header ;
190
190
return this . IN_CHUNK ;
191
191
}
192
192
}
193
193
194
- write ( buf ) {
195
- while ( buf . hasRemaining ( ) ) {
196
- this . _state = this . _state ( buf ) ;
194
+ write ( buf ) {
195
+ while ( buf . hasRemaining ( ) ) {
196
+ this . _state = this . _state ( buf ) ;
197
197
}
198
198
}
199
199
}
200
200
201
201
202
202
export default {
203
- Chunker : Chunker ,
204
- Dechunker : Dechunker
203
+ Chunker : Chunker ,
204
+ Dechunker : Dechunker
205
205
}
0 commit comments