This repository has been archived by the owner on Jan 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
QueuedConnection.js
183 lines (145 loc) · 5.14 KB
/
QueuedConnection.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
// ========================================================================
// Copyright 2014 Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========================================================================
var EventEmitter = require('events').EventEmitter
var https = require('https');
var QueuedConnection = (function(_super){
function QueuedConnection(requestOptions, requestHandler, responseHandler) {
// _super.call(this);
this._options = requestOptions;
this._requestHandler = requestHandler;
this._responseHandler = responseHandler;
return this;
}
QueuedConnection.prototype = Object.create(_super.prototype);
QueuedConnection.prototype.DoConnection = function() {
var req = https.request(this._options, this._responseHandler);
req.on('close', _connectionCompleteHandler(this));
this._requestHandler.call(this, req);
}
function _connectionCompleteHandler(obj) {
return function() {
obj.emit('complete', obj);
};
}
return QueuedConnection;
})(EventEmitter);
var QueuedConnectionManager = (function(){
function QueuedConnectionManager(options) {
this._connectionQueue = [];
this._activeConnection = null;
this._connectionOptions = options;
}
QueuedConnectionManager.prototype.QueueConnection = function (requestOptions, requestHandler, responseHandler) {
var connection = new QueuedConnection(requestOptions, requestHandler, responseHandler);
connection.on('complete', _connectionCompleteHandler(this));
if (this._activeConnection == null) {
// No need to queue, do this connection immediately
this._activeConnection = connection;
connection.DoConnection();
} else {
if (typeof requestOptions['connectionPriority'] != 'undefined' && requestOptions['connectionPriority'] == 'immediate' && this._connectionQueue.length > 5) {
delete requestOptions['connectionPriority'];
// this._connectionQueue.splice(0, 0, connection);
connection.DoConnection();
} else {
this._connectionQueue.push(connection);
}
}
}
QueuedConnectionManager.prototype.GetRequestOptionsForApi = function(apiName, method) {
var options = {
'host': this._connectionOptions['host'],
'port': 443,
'path': "/" + ["api", this._connectionOptions['apiVersion'], apiName].join("/"),
'method': typeof method === 'undefined' ? 'GET' : method,
'rejectUnauthorized': false,
'requestCert': true,
'agent': false,
'auth': this._connectionOptions["user"] + ":" + this._connectionOptions["password"],
};
if (typeof this._connectionOptions['options'] !== 'undefined') {
for (var o in this._connectionOptions['options']) {
options[o] = this._connectionOptions[o];
}
}
return options;
}
QueuedConnectionManager.prototype.DoNextConnection = function() {
if (this._activeConnection != null) return;
if (this._connectionQueue.length === 0) return;
this._activeConnection = this._connectionQueue.splice(0, 1)[0];
this._activeConnection.DoConnection();
}
QueuedConnectionManager.prototype.GetJSONQueryResultConnection = function(requestOptions, resultHander, requestBody) {
var output = "";
var error = null;
var responseHandler = function(res) {
if (res.statusCode < 200 || res.statusCode >= 300) {
console.error("Error: Status Code Was %d", res.statusCode, requestOptions.path);
error = true;
}
res.setEncoding("utf8");
res.on("data", function(chunk){
output += chunk;
});
}
var requestHandler = function (req) {
req.on("error", function(err){
console.error("request error: ", err, requestOptions.path)
error = err;
});
req.on("close", function(){
if (error != null) {
resultHander(error, output);
return;
}
var json = null;
try {
json = JSON.parse(output)
} catch (exc) {
error = exc;
}
resultHander(error, json || output)
});
switch (typeof requestBody) {
case 'string':
case 'number':
req.write(requestBody);
break;
case 'object':
req.write(JSON.stringify(requestBody));
break;
}
req.setTimeout(30000, function() {console.log("The request timed out")});
req.end()
}
this.QueueConnection(requestOptions, requestHandler, responseHandler);
}
function _connectionCompleteHandler(obj) {
return function(completedConnection) {
var index = obj._connectionQueue.indexOf(completedConnection);
if (index !== -1) {
delete obj._connectionQueue[index];
}
if (completedConnection === obj._activeConnection) {
obj._activeConnection = null;
obj.DoNextConnection();
}
};
};
return QueuedConnectionManager;
})();
module.exports = {
'QueuedConnection': QueuedConnection,
'QueuedConnectionManager': QueuedConnectionManager,
};