stream-server.js
1.35 KB
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
var Server = require('./server');
var util = require('./util');
function StreamServer(options) {
Server.apply(this, arguments);
this._streams = [];
}
StreamServer.prototype = new Server();
StreamServer.prototype.constructor = StreamServer;
StreamServer.prototype._write = function (stream, what) {
var success;
if (stream.full) {
stream.buffer.push(what);
} else {
stream.full = !stream.stream.write(what);
}
};
StreamServer.prototype.listen = function () {
var args = util.toArray(arguments);
args.forEach(function (stream) {
var streamRecord = {
stream: stream,
buffer: []
};
streamRecord.onData = this._onData.bind(this, streamRecord);
stream.on('data', streamRecord.onData);
streamRecord.onDrain = this._onDrain.bind(this, streamRecord);
stream.on('drain', streamRecord.onDrain);
this._streams.push(streamRecord);
}, this);
};
StreamServer.prototype._onData = function (stream, request) {
var that = this;
this.respond(request, function (error, response) {
if (typeof response === 'string') {
that._write(stream, response);
}
});
};
StreamServer.prototype._onDrain = function (stream, request) {
var buffer = stream.buffer.slice().reverse();
stream.full = false;
while (buffer.length > 0) {
this._write(stream, buffer.pop());
}
};
module.exports = StreamServer;