-
Notifications
You must be signed in to change notification settings - Fork 64
/
StreamImplSimpleProtocol2.hx
64 lines (59 loc) · 1.47 KB
/
StreamImplSimpleProtocol2.hx
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
58
59
60
61
62
63
64
import js.node.Buffer;
import js.node.stream.Transform;
#if haxe4
import js.lib.Error;
#else
import js.Error;
#end
@:keep
class SimpleProtocol extends Transform<SimpleProtocol> {
var _inBody = false;
var _sawFirstCr = false;
var _rawHeader = [];
var header:Buffer;
override function _transform(chunk:Dynamic, encoding, callback) {
if (!this._inBody) {
// check if the chunk has a \n\n
var split = -1;
for (i in 0...chunk.length) {
if (chunk[i] == 10) { // '\n'
if (this._sawFirstCr) {
split = i;
break;
} else {
this._sawFirstCr = true;
}
} else {
this._sawFirstCr = false;
}
}
if (split == -1) {
// still waiting for the \n\n
// stash the chunk, and try again.
this._rawHeader.push(chunk);
} else {
this._inBody = true;
var h = chunk.slice(0, split);
this._rawHeader.push(h);
var header = Buffer.concat(this._rawHeader).toString();
try {
this.header = haxe.Json.parse(header);
} catch (_:Dynamic) {
this.emit('error', new Error('invalid simple protocol data'));
return;
}
// and let them know that we are done parsing the header.
this.emit('header', this.header);
// now, because we got some extra data, emit this first.
this.push(chunk.slice(split));
}
} else {
// from there on, just provide the data to our consumer as-is.
this.push(chunk);
}
callback(null, null);
}
}
class StreamImplSimpleProtocol2 {
static function main() {}
}