-
Notifications
You must be signed in to change notification settings - Fork 15
/
ngsi-node.js
241 lines (200 loc) · 7.13 KB
/
ngsi-node.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Copyright (c) 2017 CoNWeT Lab., Universidad Politécnica de Madrid
* Copyright (c) 2018 Future Internet Consulting and Development Solutions S.L.
*
* This file is part of ngsijs.
*
* Ngsijs is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Ngsijs is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with ngsijs. If not, see <http://www.gnu.org/licenses/>.
*
* Linking this library statically or dynamically with other modules is
* making a combined work based on this library. Thus, the terms and
* conditions of the GNU Affero General Public License cover the whole
* combination.
*
* As a special exception, the copyright holders of this library give you
* permission to link this library with independent modules to produce an
* executable, regardless of the license terms of these independent
* modules, and to copy and distribute the resulting executable under
* terms of your choice, provided that you also meet, for each linked
* independent module, the terms and conditions of the license of that
* module. An independent module is a module which is not derived from
* or based on this library. If you modify this library, you may extend
* this exception to your version of the library, but you are not
* obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
*/
"use strict";
var http = require('http'),
https = require('https');
class Response {
constructor(message, body) {
Object.defineProperties(this, {
status: {value: message.statusCode},
statusText: {value: message.statusMessage},
responseText: {value: body},
headers: {value: message.headers}
});
};
getHeader(name) {
name = String(name).toLowerCase();
if (this.headers[name] != null) {
return this.headers[name];
} else {
return null;
}
};
}
var merge = function merge(object) {
/* This is controlled in our code, so it is not needed
if (object == null || typeof object !== "object") {
throw new TypeError("object argument must be an object");
}*/
Array.prototype.slice.call(arguments, 1).forEach(function (source) {
if (source != null) {
Object.keys(source).forEach(function (key) {
if (source[key] != null) {
object[key] = source[key];
}
});
}
});
return object;
};
var toQueryString = function toQueryString(parameters) {
// PRE: this function does not support passing parameters as string
var key, query = [];
if (parameters != null) {
for (key in parameters) {
if (typeof parameters[key] === 'undefined') {
continue;
} else if (parameters[key] === null) {
query.push(encodeURIComponent(key) + '=');
} else {
query.push(encodeURIComponent(key) + '=' + encodeURIComponent(parameters[key]));
}
}
if (query.length > 0) {
return query.join('&');
}
}
return null;
};
var privates = new WeakMap();
class Request {
constructor(url, options) {
if (options == null) {
options = {};
}
var headers = merge({
'Accept': 'application/json, */*'
}, options.requestHeaders);
// Headers
if (options.postBody != null) {
if (!('Content-Type' in headers)) {
if (options.contentType != null) {
headers['Content-Type'] = options.contentType;
} else {
headers['Content-Type'] = 'application/json';
}
if (options.encoding != null) {
headers['Content-Type'] += '; charset=' + options.encoding;
}
}
headers['Content-Length'] = Buffer.byteLength(options.postBody);
}
// parameters
var querystring = toQueryString(options.parameters);
if (querystring != null) {
if (url.search.length > 0) {
url.search += '&' + querystring;
} else {
url.search += '?' + querystring;
}
}
var promise_resolve;
const handler = url.protocol === "http:" ? http : https;
var request = handler.request({
method: options.method,
hostname: url.hostname,
protocol: url.protocol,
port: url.port,
path: url.pathname + url.search,
headers: headers
}, function (response) {
response.setEncoding('utf8');
var buf = '';
response.on('data', function (chunck) { buf += chunck; });
response.on('end', function () {
promise_resolve(new Response(response, buf));
});
});
let promise = new Promise(function (resolve, reject) {
promise_resolve = resolve;
request.on('error', (e) => {
if (e != null && "message" in e) {
reject(new NGSI.ConnectionError(e.message));
} else {
reject(new NGSI.ConnectionError(String(e)));
}
});
if (options.postBody != null) {
request.write(options.postBody);
}
request.end();
});
this.url = url;
privates.set(this, {
request: request,
promise: promise,
options: options
});
}
then(onFulfilled, onRejected) {
let _private = privates.get(this);
return _private.promise.then(onFulfilled, onRejected);
}
catch(onRejected) {
let _private = privates.get(this);
return _private.promise.catch(onRejected);
}
abort() {
let _private = privates.get(this);
_private.request.abort();
return this;
}
}
/* makeRequest for Node.js */
var makeRequest = function makeRequest(url, options) {
return new Request(url, options);
};
/* NGSI */
var NGSI = require('./NGSI');
var old_Connection = NGSI.Connection;
NGSI.Connection = function Connection(url, options) {
if (options == null) {
options = {}
}
if (!('requestFunction' in options)) {
options.requestFunction = makeRequest;
}
old_Connection.call(this, url, options);
};
NGSI.Connection.prototype = old_Connection.prototype;
NGSI.Connection.V2 = old_Connection.V2;
NGSI.Connection.LD = old_Connection.LD;
/* exports */
for (var key in NGSI) {
exports[key] = NGSI[key];
}