forked from brianc/node-packet-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (49 loc) · 1.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var assert = require('assert')
var Reader = module.exports = function(options) {
//TODO - remove for version 1.0
if(typeof options == 'number') {
options = { headerSize: options }
}
options = options || {}
this.offset = 0
this.lastChunk = false
this.chunk = null
this.headerSize = options.headerSize || 0
this.lengthPadding = options.lengthPadding || 0
this.header = null
assert(this.headerSize < 2, 'pre-length header of more than 1 byte length not currently supported')
}
Reader.prototype.addChunk = function(chunk) {
this.offset = 0
this.chunk = chunk
if(this.lastChunk) {
this.chunk = Buffer.concat([this.lastChunk, this.chunk])
this.lastChunk = false
}
}
Reader.prototype._save = function() {
//save any unread chunks for next read
if(this.offset < this.chunk.length) {
this.lastChunk = this.chunk.slice(this.offset)
}
return false
}
Reader.prototype.read = function() {
if(this.chunk.length < (this.headerSize + 4 + this.offset)) {
return this._save()
}
if(this.headerSize) {
this.header = this.chunk[this.offset]
}
//read length of next item
var length = this.chunk.readUInt32BE(this.offset + this.headerSize) + this.lengthPadding
//next item spans more chunks than we have
var remaining = this.chunk.length - (this.offset + 4 + this.headerSize)
if(length > remaining) {
return this._save()
}
this.offset += (this.headerSize + 4)
var result = this.chunk.slice(this.offset, this.offset + length)
this.offset += length
return result
}