-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathselect-stream.js
181 lines (166 loc) · 5.41 KB
/
select-stream.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var { Transform } = require('stream');
var sysUtil = require('util');
var util = require('./util');
function SelectStream(options) {
if (!(this instanceof SelectStream)) return new SelectStream(options);
Transform.call(this, options);
Object.assign(this, {
totalLength: 0, // current message block's total length
headerLength: 0, // current message block's header length
payloadRestLength: 0, // current message block's rest payload length
header: null, // current message block's header
chunk: Buffer.alloc(0), // the data chunk being parsed
callback: null, // current _transform function's callback
});
}
SelectStream.prototype = {
/**
* process data chunk
* concat the last chunk and current chunk
* try to parse current message block's totalLength and headerLength
* try to parse current message block's header
* try to parse current message block's payload
*/
processChunk(chunk, encoding, callback) {
Object.assign(this, {
chunk: Buffer.concat([this.chunk, chunk], this.chunk.length + chunk.length),
encoding,
callback,
});
this.parseLength();
this.parseHeader();
this.parsePayload();
},
/**
* try to parse current message block's totalLength and headerLength
*/
parseLength() {
if (!this.callback) {
return;
}
if (this.totalLength && this.headerLength) {
return;
}
if (this.chunk.length >= 12) {
this.totalLength = this.chunk.readInt32BE(0);
this.headerLength = this.chunk.readInt32BE(4);
this.payloadRestLength = this.totalLength - this.headerLength - 16;
this.chunk = this.chunk.slice(12);
} else {
this.callback();
this.callback = null;
}
},
/**
* try to parse current message block's header
* if header[':message-type'] is error, callback the error, emit error to next stream
*/
parseHeader() {
if (!this.callback) {
return;
}
if (!this.headerLength || this.header) {
return;
}
if (this.chunk.length >= this.headerLength) {
var header = {};
var offset = 0;
while (offset < this.headerLength) {
var headerNameLength = this.chunk[offset] * 1;
var headerName = this.chunk.toString('ascii', offset + 1, offset + 1 + headerNameLength);
var headerValueLength = this.chunk.readInt16BE(offset + headerNameLength + 2);
var headerValue = this.chunk.toString(
'ascii',
offset + headerNameLength + 4,
offset + headerNameLength + 4 + headerValueLength
);
header[headerName] = headerValue;
offset += headerNameLength + 4 + headerValueLength;
}
this.header = header;
this.chunk = this.chunk.slice(this.headerLength);
this.checkErrorHeader();
} else {
this.callback();
this.callback = null;
}
},
/**
* try to parse current message block's payload
*/
parsePayload() {
var self = this;
if (!this.callback) {
return;
}
if (this.chunk.length <= this.payloadRestLength) {
this.payloadRestLength -= this.chunk.length;
this.pushData(this.chunk);
this.chunk = Buffer.alloc(0);
} else if (this.chunk.length < this.payloadRestLength + 4) {
this.pushData(this.chunk.slice(0, this.payloadRestLength));
this.chunk = this.chunk.slice(this.payloadRestLength);
this.payloadRestLength = 0;
} else {
this.pushData(this.chunk.slice(0, this.payloadRestLength));
this.chunk = this.chunk.slice(this.payloadRestLength + 4);
this.totalLength = 0;
this.headerLength = 0;
this.payloadRestLength = 0;
this.header = null;
}
if (this.chunk.length && !(this.payloadRestLength === 0 && this.chunk.length < 4)) {
process.nextTick(function () {
self.processChunk(Buffer.alloc(0), self.encoding, self.callback);
});
} else {
this.callback();
this.callback = null;
}
},
/**
* if header[':event-type'] is Records, pipe payload to next stream
*/
pushData(content) {
if (this.header[':event-type'] === 'Records') {
this.push(content);
this.emit('message:records', content);
} else if (this.header[':event-type'] === 'Progress') {
var progress = util.xml2json(content.toString()).Progress;
this.emit('message:progress', progress);
} else if (this.header[':event-type'] === 'Stats') {
var stats = util.xml2json(content.toString()).Stats;
this.emit('message:stats', stats);
} else if (this.header[':event-type'] === 'error') {
var errCode = this.header[':error-code'];
var errMessage = this.header[':error-message'];
var err = new Error(errMessage);
err.message = errMessage;
err.name = err.code = errCode;
this.emit('message:error', err);
} else {
// 'Continuation', 'End'
this.emit('message:' + this.header[':event-type'].toLowerCase());
}
},
/**
* if header[':message-type'] is error, callback the error, emit error to next stream
*/
checkErrorHeader() {
if (this.header[':message-type'] === 'error') {
this.callback(this.header);
this.callback = null;
}
},
/**
* Transform Stream's implementations
*/
_transform(chunk, encoding, callback) {
this.processChunk(chunk, encoding, callback);
},
_flush(callback) {
this.processChunk(Buffer.alloc(0), this.encoding, callback);
},
};
sysUtil.inherits(SelectStream, Transform);
module.exports = SelectStream;