-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.js
202 lines (156 loc) · 4.45 KB
/
client.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'use strict';
var EventEmitter = require('events').EventEmitter;
var inherits = require('inherits');
var spdy = require('spdy');
var http = require('http');
var async = require('async');
var channels = require('./channels');
var ReadChannel = channels.ReadChannel;
var WriteChannel = channels.WriteChannel;
var ByteStream = channels.ByteStream;
var encoder = require('../encoder');
function ClientSession(opts) {
if (!(this instanceof ClientSession)) {
return new ClientSession(opts);
}
opts = opts || {};
if (opts.rejectUnauthorized !== true) {
opts.rejectUnauthorized = false;
}
this.opts = opts;
this.encoder = encoder(this, channels);
opts.spdy = opts.spdy || {};
opts.spdy.maxChunk = 0;
opts.spdy.decompress = false;
opts.maxSockets = opts.maxSockets || 4096;
this.agent = spdy.createAgent(opts);
this._nextId = 1;
this._channels = {};
this._awayting = {};
var that = this;
this.agent.on('error', function(err) {
// if we force close an agent, some random errors might occur
if (that._closing || that._closed) {
return;
}
that.emit('error', err);
});
this.encoder.on('error', this.emit.bind(this, 'error'));
this.encoder.on('channel', function (chan) {
var id = chan.id;
if (that._awayting[id]) {
chan.handle(that._awayting[id].req, that._awayting[id].res);
delete that._awayting[id];
}
chan.on('close', function() {
delete that._channels[id];
});
that._channels[id] = chan;
return chan;
});
this.agent.on('push', function() {
// weird hack, do not remove
});
this.agent._spdyState.pushServer.on('request', function receive(req, res) {
var chan;
var id = req.headers['libchan-ref'];
res.useChunkedEncodingByDefault = false;
if (!id) {
res.statusCode = 500;
res.end();
that.emit('error', new Error('Unknown request coming from the server'));
} else if (that._channels[id]) {
chan = that._channels[id];
chan.handle(req, res);
} else {
that._awayting[id] = {
req: req,
res: res
};
}
});
}
inherits(ClientSession, EventEmitter);
ClientSession.prototype._createNewStream = function newStream(chan, parentId) {
var headers = {};
headers['libchan-ref'] = chan.id;
headers['libchan-parent-ref'] = parentId;
var req = http.request({
host: this.opts.host,
port: this.opts.port,
path: '/',
headers: headers,
method: 'POST',
agent: this.agent
}, function(res) {
chan.handleIn(res);
});
// needed to avoid nasty node-spdy issue
// with chunked encoding
// TODO submit issue
req.useChunkedEncodingByDefault = false;
chan.handleOut(req);
req._implicitHeader();
req._send('');
return req;
};
function createChannel(session, Klass, parent) {
var id = session._nextId;
var chan = new Klass(session, id);
session._nextId += 2;
session._channels[id] = chan;
chan.on('close', function() {
delete session._channels[id];
});
chan.on('error', session.emit.bind(session, 'error'));
session._createNewStream(chan, parent);
return chan;
}
ClientSession.prototype._createReadChannel = function(parent) {
return createChannel(this, ReadChannel, parent.id);
};
ClientSession.prototype._createWriteChannel = function(parent) {
return createChannel(this, WriteChannel, parent.id);
};
ClientSession.prototype.WriteChannel = function() {
return createChannel(this, WriteChannel, '0');
};
ClientSession.prototype._createByteStream = function() {
// duplex streams have no parents
return createChannel(this, ByteStream, '');
};
ClientSession.prototype.close = function close(done) {
if (this._closing) {
return done && this.on('close', done) || this;
} else if (this._closed) {
return done && done() || this;
}
this._closing = true;
var agent = this.agent;
var that = this;
async.eachSeries(Object.keys(this._channels), function(id, cb) {
var channel = that._channels[id];
if (!that._channels[id]) {
return cb();
}
channel.forceClose(cb);
}, function(err) {
if (err && done) {
done(err);
} else if (err) {
that.emit('error', err);
}
agent.close(function(err) {
if (done) {
done(err);
} else if (err) {
that.emit('error', err);
}
that.emit('close');
that._closed = true;
that._closing = false;
});
});
return this;
};
module.exports = ClientSession;