This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 566
/
serviceclient.js
636 lines (556 loc) · 22.6 KB
/
serviceclient.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/**
* Copyright (c) Microsoft. All rights reserved.
*
* 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.
*/
// Module dependencies.
var request = require('request');
var url = require('url');
var util = require('util');
var qs = require('qs');
var crypto = require('crypto');
var xml2js = require('xml2js');
var events = require("events");
var azureutil = require('../../util/util');
var Constants = require('../../util/constants');
var HeaderConstants = Constants.HeaderConstants;
var HttpConstants = Constants.HttpConstants;
var Logger = require('../../diagnostics/logger');
// Expose 'ServiceClient'.
exports = module.exports = ServiceClient;
/*
* Used environment variables.
* @enum {string}
*/
ServiceClient.EnvironmentVariables = {
AZURE_STORAGE_ACCOUNT: 'AZURE_STORAGE_ACCOUNT',
AZURE_STORAGE_ACCESS_KEY: 'AZURE_STORAGE_ACCESS_KEY',
AZURE_SERVICEBUS_NAMESPACE: 'AZURE_SERVICEBUS_NAMESPACE',
AZURE_SERVICEBUS_ISSUER: 'AZURE_SERVICEBUS_ISSUER',
AZURE_SERVICEBUS_ACCESS_KEY: 'AZURE_SERVICEBUS_ACCESS_KEY',
AZURE_WRAP_NAMESPACE: 'AZURE_WRAP_NAMESPACE',
HTTP_PROXY: 'HTTP_PROXY',
HTTPS_PROXY: 'HTTPS_PROXY',
EMULATED: 'EMULATED',
AZURE_CERTFILE: 'AZURE_CERTFILE',
AZURE_KEYFILE: 'AZURE_KEYFILE'
};
/**
* Default credentials.
*/
ServiceClient.DEVSTORE_STORAGE_ACCOUNT = 'devstoreaccount1';
ServiceClient.DEVSTORE_STORAGE_ACCESS_KEY = 'Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==';
/**
* Development ServiceClient URLs.
*/
ServiceClient.DEVSTORE_BLOB_HOST = '127.0.0.1:10000';
ServiceClient.DEVSTORE_QUEUE_HOST = '127.0.0.1:10001';
ServiceClient.DEVSTORE_TABLE_HOST = '127.0.0.1:10002';
/**
* Live ServiceClient URLs.
*/
ServiceClient.CLOUD_BLOB_HOST = 'blob.core.windows.net';
ServiceClient.CLOUD_QUEUE_HOST = 'queue.core.windows.net';
ServiceClient.CLOUD_TABLE_HOST = 'table.core.windows.net';
ServiceClient.CLOUD_SERVICEBUS_HOST = 'servicebus.windows.net';
ServiceClient.CLOUD_ACCESS_CONTROL_HOST = 'accesscontrol.windows.net';
ServiceClient.CLOUD_SERVICE_MANAGEMENT_HOST = 'management.core.windows.net';
/**
* The default service bus issuer.
*/
ServiceClient.DEFAULT_SERVICEBUS_ISSUER = 'owner';
/**
* The default wrap namespace suffix.
*/
ServiceClient.DEFAULT_WRAP_NAMESPACE_SUFFIX = '-sb';
/**
* The default protocol.
*/
ServiceClient.DEFAULT_PROTOCOL = Constants.HTTP + '://';
/**
* The port.
*/
ServiceClient.DEFAULT_PORT = 80;
/**
* Creates a new ServiceClient object.
*
* @constructor
* @param {string} host The host for the service.
* @param {object} authenticationProvider The authentication provider object (e.g. sharedkey / sharedkeytable / sharedaccesssignature).
*/
function ServiceClient(host, authenticationProvider) {
events.EventEmitter.call(this);
this._initDefaultFilter();
if (host) {
var parsedHost = this._parseHost(host);
this.host = parsedHost.hostname;
this.port = parsedHost.port;
if (!this.protocol) {
this.protocol = parsedHost.protocol + '//';
}
}
else if (!this.protocol) {
this.protocol = ServiceClient.DEFAULT_PROTOCOL;
}
this.useProxy = false;
this.proxyUrl = '';
this.proxyPort = 80;
this.proxyProtocol = 'http:'
this.authenticationProvider = authenticationProvider;
this.logger = new Logger(Logger.LogLevels.INFO);
}
util.inherits(ServiceClient, events.EventEmitter);
/**
* Performs a REST service request through HTTP expecting an input stream.
*
* @param {WebResource} webResource The webresource on which to perform the request.
* @param {string} outputData The outgoing request data as a raw string.
* @param {object} [options] The request options.
* @param {int} [options.timeoutIntervalInMs] The timeout interval, in milliseconds, to use for the request.
* @param {function} callback The response callback function.
*/
ServiceClient.prototype.performRequest = function (webResource, outputData, options, callback) {
this._performRequest(webResource, { outputData: outputData }, options, callback);
};
/**
* Performs a REST service request through HTTP expecting an input stream.
*
* @param {WebResource} webResource The webresource on which to perform the request.
* @param {Stream} outputStream The outgoing request data as a stream.
* @param {object} [options] The request options.
* @param {int} [options.timeoutIntervalInMs] The timeout interval, in milliseconds, to use for the request.
* @param {function} callback The response callback function.
*/
ServiceClient.prototype.performRequestOutputStream = function (webResource, outputStream, options, callback) {
this._performRequest(webResource, { outputStream: outputStream }, options, callback);
};
/**
* Performs a REST service request through HTTP expecting an input stream.
*
* @param {WebResource} webResource The webresource on which to perform the request.
* @param {string} outputData The outgoing request data as a raw string.
* @param {Stream} inputStream The ingoing response data as a stream.
* @param {object} [options] The request options.
* @param {int} [options.timeoutIntervalInMs] The timeout interval, in milliseconds, to use for the request.
* @param {function} callback The response callback function.
*/
ServiceClient.prototype.performRequestInputStream = function (webResource, outputData, inputStream, options, callback) {
this._performRequest(webResource, { outputData: outputData, inputStream: inputStream }, options, callback);
};
/**
* Performs a REST service request through HTTP.
*
* @param {WebResource} webResource The webresource on which to perform the request.
* @param {object} body The request body.
* @param {string} [body.outputData] The outgoing request data as a raw string.
* @param {Stream} [body.outputStream] The outgoing request data as a stream.
* @param {Stream} [body.inputStream] The ingoing response data as a stream.
* @param {object} [options] The request options.
* @param {int} [options.timeoutIntervalInMs] The timeout interval, in milliseconds, to use for the request.
* @param {function} callback The response callback function.
*/
ServiceClient.prototype._performRequest = function (webResource, body, options, callback) {
var self = this;
self._buildRequestOptions(webResource, options, function (err, requestOptions) {
self.logger.log(Logger.LogLevels.DEBUG, "REQUEST OPTIONS:\n" + util.inspect(requestOptions));
var operation = function (finalRequestOptions, operationCallback, next) {
self.logger.log(Logger.LogLevels.DEBUG, "FINAL REQUEST OPTIONS:\n" + util.inspect(finalRequestOptions));
var processResponseCallback = function (error, response, responseBody) {
var responseObject;
if (error) {
responseObject = { error: error, response: null };
} else {
responseObject = self._processResponse(webResource, response);
}
operationCallback(responseObject, next);
};
if (body && body.outputData) {
finalRequestOptions.body = body.outputData;
}
var buildRequest = function () {
// Build request (if body was set before, request will process immediately, if not it'll wait for the piping to happen)
var requestStream = request(finalRequestOptions, processResponseCallback);
var oldEnd = requestStream.end;
requestStream.end = function () {
if (finalRequestOptions.headers['content-length']) {
requestStream.headers['content-length'] = finalRequestOptions.headers['content-length'];
} else if (requestStream.headers['content-length']) {
delete requestStream.headers['content-length'];
}
oldEnd.call(requestStream);
};
// Bubble events up
requestStream.on('response', function (response) {
self.emit('response', response);
});
return requestStream;
};
// Pipe any input / output streams
if (body && body.inputStream) {
buildRequest().pipe(body.inputStream);
} else if (body && body.outputStream) {
if (!body.outputStream.readable) {
var requestStream = buildRequest();
// This will wait until we know the readable stream is actually valid before piping
body.outputStream.on('open', function () {
body.outputStream.pipe(requestStream);
});
} else {
body.outputStream.pipe(buildRequest());
}
// This catches any errors that happen while creating the readable stream (usually invalid names)
body.outputStream.on('error', function(error) {
processResponseCallback(error);
});
} else {
buildRequest();
}
};
// The filter will do what it needs to the requestOptions and will provide a
// function to be handled after the reply
self.filter(requestOptions, function (postFiltersRequestOptions, nextPostCallback) {
// If there is a filter, flow is:
// filter -> operation -> process response -> next filter
operation(postFiltersRequestOptions, callback, nextPostCallback);
});
});
};
/**
* Process the response.
*
* @param {WebResource} webResource The web resource that made the request.
* @param {Response} response The response object.
* @return The normalized responseObject.
*/
ServiceClient.prototype._processResponse = function (webResource, response) {
var rsp;
var responseObject;
if (webResource.validResponse(response.statusCode)) {
rsp = this._buildResponse(true, response.body, response.headers, response.statusCode, response.md5);
if (webResource.rawResponse) {
responseObject = { error: null, response: rsp };
} else {
responseObject = { error: null, response: this._parseResponse(rsp) };
}
} else {
rsp = this._parseResponse(this._buildResponse(false, response.body, response.headers, response.statusCode, response.md5));
if (response.statusCode < 400 || response.statusCode >= 500) {
this.logger.log(Logger.LogLevels.DEBUG,
'ERROR code = ' + response.statusCode + ' :\n' + util.inspect(rsp.body));
}
var normalizedError = this._normalizeError(rsp.body);
responseObject = { error: normalizedError, response: rsp };
}
this.logger.log(Logger.LogLevels.DEBUG, "RESPONSE:\n" + util.inspect(responseObject));
return responseObject;
};
/**
* Associate a filtering operation with this ServiceClient. Filtering operations
* can include logging, automatically retrying, etc. Filter operations are objects
* that implement a method with the signature:
*
* "function handle (requestOptions, next)".
*
* After doing its preprocessing on the request options, the method needs to call
* "next" passing a callback with the following signature:
* signature:
*
* "function (returnObject, finalCallback, next)"
*
* In this callback, and after processing the returnObject (the response from the
* request to the server), the callback needs to either invoke next if it exists to
* continue processing other filters or simply invoke finalCallback otherwise to end
* up the service invocation.
*
* @param {Object} filter The new filter object.
* @return {ServiceClient} A new service client with the filter applied.
*/
ServiceClient.prototype.withFilter = function (newFilter) {
if (azureutil.objectIsNull(newFilter) || !newFilter.handle) {
throw new Error('Incorrect filter object.');
}
// Create a new object with the same members as the current service
var derived = {};
for (var property in this) {
derived[property] = this[property];
}
// If the current service has a filter, merge it with the new filter
// (allowing us to effectively pipeline a series of filters)
var parentFilter = this.filter;
var mergedFilter = newFilter;
if (parentFilter !== undefined) {
// The parentFilterNext is either the operation or the nextPipe function generated on a previous merge
// Ordering is [f3 pre] -> [f2 pre] -> [f1 pre] -> operation -> [f1 post] -> [f2 post] -> [f3 post]
mergedFilter = function (originalRequestOptions, parentFilterNext) {
newFilter.handle(originalRequestOptions, function (postRequestOptions, newFilterCallback) {
// handle parent filter pre and get Parent filter post
var next = function (postPostRequestOptions, parentFilterCallback) {
// The parentFilterNext is the filter next to the merged filter.
// For 2 filters, that'd be the actual operation.
parentFilterNext(postPostRequestOptions, function (responseObject, responseCallback, finalCallback) {
parentFilterCallback(responseObject, finalCallback, function (postResponseObject) {
newFilterCallback(postResponseObject, responseCallback, finalCallback);
});
});
};
parentFilter(postRequestOptions, next);
});
};
}
// Store the filter so it can be applied in performRequest
derived.filter = mergedFilter;
return derived;
};
/*
* Sets the proxy settings for the request if a proxy is defined.
*/
ServiceClient.prototype._setRequestOptionsProxy = function (requestOptions) {
this._loadEnvironmentProxy();
if (this.useProxy) {
requestOptions.proxy = url.format({
protocol: this.proxyProtocol,
hostname: this.proxyUrl,
port: this.proxyPort
});
}
return requestOptions;
};
/*
* Returns a parsed host from a full host name.
*
* @param {string} host The full host to be parsed.
* @return {object} THe parsed host as returned by the method "url.parse".
*/
ServiceClient.prototype._parseHost = function (host) {
var fullHost = host;
if (fullHost.indexOf(Constants.HTTP) === -1) {
fullHost = ServiceClient.DEFAULT_PROTOCOL + fullHost;
}
var parsedUrl = url.parse(fullHost);
if (!parsedUrl.port) {
parsedUrl.port = ServiceClient.DEFAULT_PORT;
}
return parsedUrl;
};
/*
* Loads the fields "useProxy" and respective protocol, port and url
* from the environment values HTTPS_PROXY and HTTP_PROXY
* in case those are set.
*
* @return {string} or null
*/
ServiceClient.prototype._loadEnvironmentProxyValue = function() {
var proxyUrl = null;
if (process.env[ServiceClient.EnvironmentVariables.HTTPS_PROXY]) {
proxyUrl = process.env[ServiceClient.EnvironmentVariables.HTTPS_PROXY];
} else if (process.env[ServiceClient.EnvironmentVariables.HTTPS_PROXY.toLowerCase()]) {
proxyUrl = process.env[ServiceClient.EnvironmentVariables.HTTPS_PROXY.toLowerCase()];
} else if (process.env[ServiceClient.EnvironmentVariables.HTTP_PROXY]) {
proxyUrl = process.env[ServiceClient.EnvironmentVariables.HTTP_PROXY];
} else if (process.env[ServiceClient.EnvironmentVariables.HTTP_PROXY.toLowerCase()]) {
proxyUrl = process.env[ServiceClient.EnvironmentVariables.HTTP_PROXY.toLowerCase()];
}
return proxyUrl;
};
/*
* Loads the fields "useProxy" and respective protocol, port and url
* from the environment values ALL_PROXY, HTTPS_PROXY and HTTP_PROXY
* in case those are set.
*
* @return {undefined}
*/
ServiceClient.prototype._loadEnvironmentProxy = function() {
var proxyUrl = this._loadEnvironmentProxyValue();
if (proxyUrl) {
var parsedUrl = this._parseHost(proxyUrl);
this.useProxy = true;
this.proxyProtocol = parsedUrl.protocol;
this.proxyPort = parsedUrl.port;
this.proxyUrl = parsedUrl.hostname;
}
};
/*
* Builds a response object with normalized key names.
*
* @param {Bool} isSuccessful Boolean value indicating if the request was successful
* @param {Object} body The response body.
* @param {Object} headers The response headers.
* @param {int} statusCode The response status code.
* @param {string} md5 The response's content md5 hash.
* @return {Object} A response object.
*/
ServiceClient.prototype._buildResponse = function (isSuccessful, body, headers, statusCode, md5) {
return {
isSuccessful: isSuccessful,
statusCode: statusCode,
body: body,
headers: headers,
md5: md5
};
};
/**
* Parses a server response body from XML into a JS object.
* This is done using the xml2js library.
*
* @param {object} response The response object with a property "body" with a XML string content.
* @return {object} The same response object with the body part as a JS object instead of a XML string.
*/
ServiceClient.prototype._parseResponse = function (response) {
if (!azureutil.objectIsNull(response.body) && response.body.trim() !== '') {
if (response.headers['content-type'] &&
response.headers['content-type'].indexOf('application/json') != -1) {
var parsedBody = null;
try {
parsedBody = JSON.parse(response.body);
} catch (error) {
parsedBody = { parsingError: error };
}
} else {
var parser = new xml2js.Parser({normalize: false, trim: false});
var parseError = null;
parser.on('error', function (e) { parseError = e; });
parser.parseString(response.body);
var parsedBody;
if (parser.resultObject) {
parsedBody = parser.resultObject;
} else {
parsedBody = { parsingError: parseError };
}
}
if (!azureutil.objectIsNull(parsedBody.parsingError)) {
response.isSuccessful = false;
response.parsingError = parsedBody.parsingError;
} else if (!azureutil.objectIsNull(parsedBody)) {
response.body = parsedBody;
} else {
response.isSuccessful = false;
}
}
return response;
};
/**
* Sets the webResource's requestUrl based on the service client settings.
*
* @param {WebResource} webResource The web resource where to set the request url.
* @return {undefined}
*/
ServiceClient.prototype._setRequestUrl = function (webResource) {
// Normalize the path
webResource.path = this._getPath(webResource.path);
// Get the encoded query string
var queryString = webResource.getQueryString(true);
// Build the full request url
webResource.requestUrl = this.protocol + this._getHostname() + ":" + this.port + webResource.path + queryString;
};
/**
* Initializes the default filter.
* This filter is responsible for chaining the pre filters request into the operation and, after processing the response,
* pass it to the post processing filters. This method should only be invoked by the ServiceClient constructor.
*
* @return {undefined}
*/
ServiceClient.prototype._initDefaultFilter = function () {
this.filter = function (requestOptions, nextPreCallback) {
if (nextPreCallback) {
// Handle the next pre callback and pass the function to be handled as post call back.
nextPreCallback(requestOptions, function (returnObject, finalCallback, nextPostCallback) {
if (nextPostCallback) {
nextPostCallback(returnObject);
} else if (finalCallback) {
finalCallback(returnObject);
}
});
}
};
};
/**
* Retrieves the metadata headers from the response headers.
*
* @param {object} headers The metadata headers.
* @return {object} An object with the metadata headers (without the "x-ms-" prefix).
*/
ServiceClient.prototype.parseMetadataHeaders = function (headers) {
var metadata = {};
if (!headers) {
return metadata;
}
for (var header in headers) {
if (header.indexOf(HeaderConstants.PREFIX_FOR_STORAGE_METADATA) === 0) {
var key = header.substr(HeaderConstants.PREFIX_FOR_STORAGE_METADATA.length, header.length - HeaderConstants.PREFIX_FOR_STORAGE_METADATA.length);
metadata[key] = headers[header];
}
}
return metadata;
};
/**
* Determines if the service client is running on an emulated environment.
* This will be considered to be the case if the used url matches the address for the devstore
* (See DEVSTORE_BLOB_HOST, DEVSTORE_TABLE_HOST and DEVSTORE_QUEUE_HOST) or if the environment variable
* "EMULATED" is set.
*
* @param {string} host The used host.
* @return {bool} True if the service client is running on an emulated environment; false otherwise.
*/
ServiceClient.isEmulated = function (host) {
if (host === ServiceClient.DEVSTORE_BLOB_HOST ||
host === ServiceClient.DEVSTORE_TABLE_HOST ||
host === ServiceClient.DEVSTORE_QUEUE_HOST) {
return true;
}
return (!azureutil.objectIsNull(process.env[ServiceClient.EnvironmentVariables.EMULATED]) &&
process.env[ServiceClient.EnvironmentVariables.EMULATED] !== 'false');
};
// Other functions
/**
* Processes the error body into a normalized error object with all the properties lowercased.
*
* Error information may be returned by a service call with additional debugging information:
* http://msdn.microsoft.com/en-us/library/windowsazure/dd179382.aspx
*
* Table services returns these properties lowercased, example, "code" instead of "Code". So that the user
* can always expect the same format, this method lower cases everything.
*
* @param {Object} error The error object as returned by the service and parsed to JSON by the xml2json.
* @return {Object} The normalized error object with all properties lower cased.
*/
ServiceClient.prototype._normalizeError = function (error) {
var normalizedError = new Error();
for (var property in error) {
if (property !== '@') {
if (error[property] && error[property]['#']) {
normalizedError[property.toLowerCase()] = error[property]['#'];
} else {
normalizedError[property.toLowerCase()] = error[property];
}
}
}
return normalizedError;
};
/**
* Sets the service client proxy.
*
* @param {Object} error The error object as returned by the service and parsed to JSON by the xml2json.
* @return {Object} The normalized error object with all properties lower cased.
*/
ServiceClient.prototype.setProxy = function (proxyUrl, proxyPort) {
this.proxyUrl = proxyUrl;
this.proxyPort = proxyPort;
this.useProxy = !azureutil.objectIsNull(this.proxyUrl);
};
/**
* Determines if the current protocol is https.
*
* @return {Bool} True if the protocol is https; false otherwise.
*/
ServiceClient.prototype._isHttps = function () {
return (this.protocol.substr(0, 5).toLowerCase() === Constants.HTTPS);
};