-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathcore.js
808 lines (692 loc) · 29.2 KB
/
core.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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
/* eslint-disable jsdoc/require-param-type,jsdoc/require-param-description */
var dns = require('dns'),
constants = require('constants'),
_ = require('lodash'),
uuid = require('uuid'),
sdk = require('postman-collection'),
urlEncoder = require('postman-url-encoder'),
Socket = require('net').Socket,
requestBodyBuilders = require('./core-body-builder'),
version = require('../../package.json').version,
LOCAL_IPV6 = '::1',
LOCAL_IPV4 = '127.0.0.1',
LOCALHOST = 'localhost',
SOCKET_TIMEOUT = 500,
COLON = ':',
STRING = 'string',
HOSTS_TYPE = {
HOST_IP_MAP: 'hostIpMap'
},
HTTPS = 'https',
HTTPS_DEFAULT_PORT = 443,
HTTP_DEFAULT_PORT = 80,
S_CONNECT = 'connect',
S_ERROR = 'error',
S_TIMEOUT = 'timeout',
SSL_OP_NO = 'SSL_OP_NO_',
ERROR_ADDRESS_RESOLVE = 'NETERR: getaddrinfo ENOTFOUND ',
/**
* List of request methods without body.
*
* @private
* @type {Object}
*
* @note hash is used to reduce the lookup cost
* these methods are picked from the app, which don't support body.
* @todo move this list to SDK for parity.
*/
METHODS_WITHOUT_BODY = {
get: true,
copy: true,
head: true,
purge: true,
unlock: true
},
/**
* List of request options with their corresponding protocol profile behavior property name;
*
* @private
* @type {Object}
*/
PPB_OPTS = {
// enable or disable certificate verification
strictSSL: 'strictSSL',
// maximum number of redirects to follow (default: 10)
maxRedirects: 'maxRedirects',
// controls redirect behavior
// keeping the same convention as Newman
followRedirect: 'followRedirects',
followAllRedirects: 'followRedirects',
// Use an insecure HTTP parser that accepts invalid HTTP headers
// Refer: https://nodejs.org/api/cli.html#--insecure-http-parser
insecureHTTPParser: 'insecureHTTPParser',
// retain `authorization` header when a redirect happens to a different hostname
followAuthorizationHeader: 'followAuthorizationHeader',
// redirect with the original HTTP method (default: redirects with GET)
followOriginalHttpMethod: 'followOriginalHttpMethod',
// removes the `referer` header when a redirect happens (default: false)
// @note `referer` header set in the initial request will be preserved during redirect chain
removeRefererHeader: 'removeRefererHeaderOnRedirect',
// Select the HTTP protocol version to be used. Valid options are http1/http2/auto
protocolVersion: 'protocolVersion'
},
/**
* System headers which can be removed before sending the request if set
* in disabledSystemHeaders protocol profile behavior.
*
*
* @private
* @type {Array}
*/
ALLOWED_BLACKLIST_HEADERS = ['content-type', 'content-length', 'accept-encoding', 'connection'],
/**
* Find the enabled header with the given name.
*
* @todo Add this helper in Collection SDK.
*
* @private
* @param {HeaderList} headers
* @param {String} name
* @returns {Header|undefined}
*/
oneNormalizedHeader = function oneNormalizedHeader (headers, name) {
var i,
header;
// get all headers with `name`
headers = headers.reference[name.toLowerCase()];
if (Array.isArray(headers)) {
// traverse the headers list in reverse direction in order to find the last enabled
for (i = headers.length - 1; i >= 0; i--) {
header = headers[i];
if (header && !header.disabled) {
return header;
}
}
// bail out if no enabled header was found
return;
}
// return the single enabled header
if (headers && !headers.disabled) {
return headers;
}
},
/**
* Add static system headers if they are not disable using `disabledSystemHeaders`
* protocol profile behavior.
* Add the system headers provided as requester configuration.
*
* @note Don't traverse the user provided `disabledSystemHeaders` object
* to ensure runtime allowed headers and also for security reasons.
*
* @private
* @param {Request} request
* @param {Object} options
* @param {Object} disabledHeaders
* @param {Object} systemHeaders
*/
addSystemHeaders = function (request, options, disabledHeaders, systemHeaders) {
var key,
headers = request.headers;
[
{ key: 'User-Agent', value: `PostmanRuntime/${version}` },
{ key: 'Accept', value: '*/*' },
{ key: 'Cache-Control', value: 'no-cache' },
{ key: 'Postman-Token', value: uuid.v4() },
{ key: 'Host', value: options.url && options.url.host },
{ key: 'Accept-Encoding', value: 'gzip, deflate, br' },
{ key: 'Connection', value: 'keep-alive' }
].forEach(function (header) {
key = header.key.toLowerCase();
// add system header only if,
// 1. there's no user added header
// 2. not disabled using disabledSystemHeaders
!disabledHeaders[key] && !oneNormalizedHeader(headers, key) &&
headers.add({
key: header.key,
value: header.value,
system: true
});
});
for (key in systemHeaders) {
if (Object.hasOwn(systemHeaders, key)) {
// upsert instead of add to replace user-defined headers also
headers.upsert({
key: key,
value: systemHeaders[key],
system: true
});
}
}
},
/**
* Helper function to extract top level domain for the given hostname
*
* @private
*
* @param {String} hostname
* @returns {String}
*/
getTLD = function (hostname) {
if (!hostname) {
return '';
}
hostname = String(hostname);
return hostname.substring(hostname.lastIndexOf('.') + 1);
},
/**
* Abstracts out the logic for domain resolution
*
* @param options
* @param hostLookup
* @param hostLookup.type
* @param hostLookup.hostIpMap
* @param hostname
* @param callback
*/
_lookup = function (options, hostLookup, hostname, callback) {
var hostIpMap,
resolvedFamily = 4,
resolvedAddr;
// first we try to resolve the hostname using hosts file configuration
hostLookup && hostLookup.type === HOSTS_TYPE.HOST_IP_MAP &&
(hostIpMap = hostLookup[HOSTS_TYPE.HOST_IP_MAP]) && (resolvedAddr = hostIpMap[hostname]);
if (resolvedAddr) {
// since we only get an string for the resolved ip address, we manually find it's family (4 or 6)
// there will be at-least one `:` in an IPv6 (https://en.wikipedia.org/wiki/IPv6_address#Representation)
resolvedAddr.indexOf(COLON) !== -1 && (resolvedFamily = 6); // eslint-disable-line lodash/prefer-includes
// returning error synchronously causes uncaught error because listeners are not attached to error events
// on socket yet
return setImmediate(function () {
// when options.all is set, the callback expects an array of addresses
return options.all ?
callback(null, [{ address: resolvedAddr, family: resolvedFamily }]) :
callback(null, resolvedAddr, resolvedFamily);
});
}
// no hosts file configuration provided or no match found. Falling back to normal dns lookup
return dns.lookup(hostname, options, callback);
},
/**
* Tries to make a TCP connection to the given host and port. If successful, the connection is immediately
* destroyed.
*
* @param host
* @param port
* @param callback
*/
connect = function (host, port, callback) {
var socket = new Socket(),
called,
done = function (type) {
if (!called) {
callback(type === S_CONNECT ? null : true); // eslint-disable-line n/callback-return
called = true;
this.destroy();
}
};
socket.setTimeout(SOCKET_TIMEOUT, done.bind(socket, S_TIMEOUT));
socket.once('connect', done.bind(socket, S_CONNECT));
socket.once('error', done.bind(socket, S_ERROR));
socket.connect(port, host);
socket = null;
},
/**
* Override DNS lookups in Node, to handle localhost as a special case.
* Chrome tries connecting to IPv6 by default, so we try the same thing.
*
* @param lookupOptions
* @param lookupOptions.port
* @param lookupOptions.network
* @param lookupOptions.network.restrictedAddresses
* @param lookupOptions.network.hostLookup
* @param lookupOptions.network.hostLookup.type
* @param lookupOptions.network.hostLookup.hostIpMap
* @param hostname
* @param options
* @param callback
*/
lookup = function (lookupOptions, hostname, options, callback) {
var self = this,
lowercaseHost = hostname && hostname.toLowerCase(),
networkOpts = lookupOptions.network || {},
hostLookup = networkOpts.hostLookup;
// do dns.lookup if hostname is not one of:
// - localhost
// - *.localhost
if (getTLD(lowercaseHost) !== LOCALHOST) {
// when options.all is set, the callback expects an array of addresses
if (options.all) {
return _lookup(options, hostLookup, lowercaseHost, function (err, addresses) {
if (err) { return callback(err); }
// error out if any of the resolved addresses are restricted
if (_.some(addresses, (addr) => {
return self.isAddressRestricted(addr && addr.address, networkOpts);
})) {
return callback(new Error(ERROR_ADDRESS_RESOLVE + hostname));
}
return callback(null, addresses);
});
}
return _lookup(options, hostLookup, lowercaseHost, function (err, addr, family) {
if (err) { return callback(err); }
// error out if the resolved address is restricted
if (self.isAddressRestricted(addr, networkOpts)) {
return callback(new Error(ERROR_ADDRESS_RESOLVE + hostname));
}
return callback(null, addr, family);
});
}
// Try checking if we can connect to IPv6 localhost ('::1')
connect(LOCAL_IPV6, lookupOptions.port, function (err) {
// use IPv4 if we cannot connect to IPv6
if (err) {
return options.all ?
callback(null, [{ address: LOCAL_IPV4, family: 4 }]) :
callback(null, LOCAL_IPV4, 4);
}
return options.all ?
callback(null, [{ address: LOCAL_IPV6, family: 6 }]) :
callback(null, LOCAL_IPV6, 6);
});
},
/**
* Helper function to return postman-request compatible URL parser which
* respects the `disableUrlEncoding` protocol profile behavior.
*
* @private
* @param {Boolean} disableUrlEncoding
* @returns {Object}
*/
urlParser = function (disableUrlEncoding) {
return {
parse (urlToParse) {
return urlEncoder.toNodeUrl(urlToParse, disableUrlEncoding);
},
resolve (base, relative) {
if (typeof base === STRING) {
// @note we parse base URL here to respect `disableUrlEncoding`
// option even though resolveNodeUrl() accepts it as a string
base = urlEncoder.toNodeUrl(base, disableUrlEncoding);
}
return urlEncoder.resolveNodeUrl(base, relative);
}
};
},
/**
* Resolves given property with protocol profile behavior.
* Returns protocolProfileBehavior value if the given property is present.
* Else, returns value defined in default options.
*
* @param {String} property - Property name to look for
* @param {Object} defaultOpts - Default request options
* @param {Object} protocolProfileBehavior - Protocol profile behaviors
* @returns {*} - Resolved request option value
*/
resolveWithProtocolProfileBehavior = function (property, defaultOpts, protocolProfileBehavior) {
// bail out if property or defaultOpts is not defined
if (!(property && defaultOpts)) { return; }
if (Object.hasOwn(protocolProfileBehavior, property)) {
return protocolProfileBehavior[property];
}
return defaultOpts[property];
};
module.exports = {
/**
* Creates a node request compatible options object from a request.
*
* @param request
* @param defaultOpts
* @param defaultOpts.agents
* @param defaultOpts.network
* @param defaultOpts.keepAlive
* @param defaultOpts.timeout
* @param defaultOpts.strictSSL
* @param defaultOpts.cookieJar The cookie jar to use (if any).
* @param defaultOpts.followRedirects
* @param defaultOpts.followOriginalHttpMethod
* @param defaultOpts.maxRedirects
* @param defaultOpts.maxResponseSize
* @param defaultOpts.protocolVersion
* @param defaultOpts.implicitCacheControl
* @param defaultOpts.implicitTraceHeader
* @param defaultOpts.removeRefererHeaderOnRedirect
* @param defaultOpts.timings
* @param protocolProfileBehavior
* @returns {{}}
*/
getRequestOptions (request, defaultOpts, protocolProfileBehavior) {
!defaultOpts && (defaultOpts = {});
!protocolProfileBehavior && (protocolProfileBehavior = {});
var options = {},
networkOptions = defaultOpts.network || {},
self = this,
bodyParams,
useWhatWGUrlParser = defaultOpts.useWhatWGUrlParser,
disableUrlEncoding = protocolProfileBehavior.disableUrlEncoding,
disabledSystemHeaders = protocolProfileBehavior.disabledSystemHeaders || {},
// the system headers provided in requester configuration
systemHeaders = defaultOpts.systemHeaders || {},
url = useWhatWGUrlParser ? urlEncoder.toNodeUrl(request.url, disableUrlEncoding) :
urlEncoder.toLegacyNodeUrl(request.url.toString(true)),
isSSL = _.startsWith(url.protocol, HTTPS),
isTunnelingProxy = request.proxy && (request.proxy.tunnel || isSSL),
header,
reqOption,
portNumber,
behaviorName,
port = url && url.port,
hostname = url && url.hostname && url.hostname.toLowerCase(),
proxyHostname = request.proxy && request.proxy.host;
// resolve all *.localhost to localhost itself
// RFC: 6761 section 6.3 (https://tools.ietf.org/html/rfc6761#section-6.3)
if (getTLD(hostname) === LOCALHOST) {
// @note setting hostname to localhost ensures that we override lookup function
hostname = LOCALHOST;
}
if (getTLD(proxyHostname) === LOCALHOST) {
proxyHostname = LOCALHOST;
}
options.url = url;
options.method = request.method;
options.timeout = defaultOpts.timeout;
options.gzip = true;
options.brotli = true;
options.time = defaultOpts.timings;
options.verbose = defaultOpts.verbose;
options.agents = defaultOpts.agents;
options.extraCA = defaultOpts.extendedRootCA;
options.ignoreProxyEnvironmentVariables = defaultOpts.ignoreProxyEnvironmentVariables;
options.agentIdleTimeout = defaultOpts.agentIdleTimeout;
// Disable encoding of URL in postman-request in order to use pre-encoded URL object returned from
// toNodeUrl() function of postman-url-encoder
options.disableUrlEncoding = true;
// Ensures that "request" creates URL encoded formdata or querystring as
// foo=bar&foo=baz instead of foo[0]=bar&foo[1]=baz
options.useQuerystring = true;
// set encoding to null so that the response is a stream
options.encoding = null;
// Re-encode status message using `utf8` character encoding in postman-request.
// This is done to correctly represent status messages with characters that lie outside
// the range of `latin1` encoding (which is the default encoding in which status message is returned)
options.statusMessageEncoding = 'utf8';
// eslint-disable-next-line guard-for-in
for (reqOption in PPB_OPTS) {
behaviorName = PPB_OPTS[reqOption];
options[reqOption] = resolveWithProtocolProfileBehavior(behaviorName, defaultOpts, protocolProfileBehavior);
}
// set cookie jar if not disabled
if (!protocolProfileBehavior.disableCookies) {
options.jar = defaultOpts.cookieJar || true;
}
// use the server's cipher suite order instead of the client's during negotiation
if (protocolProfileBehavior.tlsPreferServerCiphers) {
options.honorCipherOrder = true;
}
// the SSL and TLS protocol versions to disabled during negotiation
if (Array.isArray(protocolProfileBehavior.tlsDisabledProtocols)) {
protocolProfileBehavior.tlsDisabledProtocols.forEach(function (protocol) {
// since secure options doesn't support TLSv1.3 before Node 14
// @todo remove the if condition when we drop support for Node 12
if (protocol === 'TLSv1_3' && !constants[SSL_OP_NO + protocol]) {
options.maxVersion = 'TLSv1.2';
}
else {
options.secureOptions |= constants[SSL_OP_NO + protocol];
}
});
}
// order of cipher suites that the SSL server profile uses to establish a secure connection
if (Array.isArray(protocolProfileBehavior.tlsCipherSelection)) {
options.ciphers = protocolProfileBehavior.tlsCipherSelection.join(':');
}
if (typeof defaultOpts.maxResponseSize === 'number') {
options.maxResponseSize = defaultOpts.maxResponseSize;
}
// Request body may return different options depending on the type of the body.
// @note getRequestBody may add system headers based on intent
bodyParams = self.getRequestBody(request, protocolProfileBehavior);
// Disable 'Cache-Control' and 'Postman-Token' based on global options
// @note this also make 'cache-control' and 'postman-token' part of `disabledSystemHeaders`
!defaultOpts.implicitCacheControl && (disabledSystemHeaders['cache-control'] = true);
!defaultOpts.implicitTraceHeader && (disabledSystemHeaders['postman-token'] = true);
// Add additional system headers to the request instance
addSystemHeaders(request, options, disabledSystemHeaders, systemHeaders);
// Don't add `Host` header if disabled using disabledSystemHeaders
// @note This can't be part of `blacklistHeaders` option as `setHost` is
// a Node.js http.request option to specifies whether or not to
// automatically add the Host header or not.
if (disabledSystemHeaders.host) {
header = oneNormalizedHeader(request.headers, 'host');
// only possible with AWS auth
header && header.system && (header.disabled = true);
// set `setHost` to false if there's no host header defined by the user
// or, the present host is added by the system.
(!header || header.system) && (options.setHost = false);
}
// Set `allowContentTypeOverride` if content-type header is disabled,
// this allows overriding (if invalid) the content-type for form-data
// and urlencoded request body.
if (disabledSystemHeaders['content-type']) {
options.allowContentTypeOverride = true;
}
options.blacklistHeaders = [];
ALLOWED_BLACKLIST_HEADERS.forEach(function (headerKey) {
if (!disabledSystemHeaders[headerKey]) { return; } // not disabled
header = oneNormalizedHeader(request.headers, headerKey);
// content-type added by body helper
header && header.system && (header.disabled = true);
// blacklist only if it's missing or part of system added headers
(!header || header.system) && options.blacklistHeaders.push(headerKey);
// @note for non-GET requests if no 'content-length' is set, it
// it assumes to be chucked request body and add 'transfer-encoding'
// here, we ensure blacklisting 'content-length' will also blacklist
// 'transfer-encoding' header.
if (headerKey === 'content-length') {
header = oneNormalizedHeader(request.headers, 'transfer-encoding');
(!header || header.system) && options.blacklistHeaders.push('transfer-encoding');
}
});
// Finally, get headers object
options.headers = request.getHeaders({ enabled: true, sanitizeKeys: true });
// override URL parser to WhatWG URL parser
if (useWhatWGUrlParser) {
options.urlParser = urlParser(disableUrlEncoding);
}
// override DNS lookup
if (networkOptions.restrictedAddresses || hostname === LOCALHOST ||
(!isTunnelingProxy && proxyHostname === LOCALHOST) || networkOptions.hostLookup) {
// Use proxy port for localhost resolution in case of non-tunneling proxy
// because the request will be sent to proxy server by postman-request
if (request.proxy && !isTunnelingProxy) {
portNumber = Number(request.proxy.port);
}
// Otherwise, use request's port
else {
portNumber = Number(port) || (isSSL ? HTTPS_DEFAULT_PORT : HTTP_DEFAULT_PORT);
}
_.isFinite(portNumber) && (options.lookup = lookup.bind(this, {
port: portNumber,
network: networkOptions
}));
}
_.assign(options, bodyParams, {
// @note these common agent options can be overridden by specifying
// custom http/https agents using requester option `agents`
agentOptions: {
keepAlive: defaultOpts.keepAlive
}
});
return options;
},
/**
* Processes a request body and puts it in a format compatible with
* the "request" library.
*
* @todo: Move this to the SDK.
* @param request - Request object
* @param protocolProfileBehavior - Protocol profile behaviors
*
* @returns {Object}
*/
getRequestBody (request, protocolProfileBehavior) {
if (!(request && request.body)) {
return;
}
var i,
property,
requestBody = request.body,
requestBodyType = requestBody.mode,
requestMethod = (typeof request.method === STRING) ? request.method.toLowerCase() : undefined,
bodyIsEmpty = requestBody.isEmpty(),
bodyIsDisabled = requestBody.disabled,
bodyContent = requestBody[requestBodyType],
// flag to decide body pruning for METHODS_WITHOUT_BODY
// @note this will be `true` even if protocolProfileBehavior is undefined
pruneBody = protocolProfileBehavior ? !protocolProfileBehavior.disableBodyPruning : true;
// early bailout for empty or disabled body (this area has some legacy shenanigans)
if (bodyIsEmpty || bodyIsDisabled) {
return;
}
// body is empty if all the params in urlencoded and formdata body are disabled
// @todo update Collection SDK isEmpty method to account for this
if (sdk.PropertyList.isPropertyList(bodyContent)) {
bodyIsEmpty = true;
for (i = bodyContent.members.length - 1; i >= 0; i--) {
property = bodyContent.members[i];
// bail out if a single enabled property is present
if (property && !property.disabled) {
bodyIsEmpty = false;
break;
}
}
// bail out if body is empty
if (bodyIsEmpty) {
return;
}
}
// bail out if request method doesn't support body and pruneBody is true.
if (METHODS_WITHOUT_BODY[requestMethod] && pruneBody) {
return;
}
// even if body is not empty, but the body type is not known, we do not know how to parse the same
//
// @note if you'd like to support additional body types beyond formdata, url-encoding, etc, add the same to
// the builder module
if (!Object.hasOwn(requestBodyBuilders, requestBodyType)) {
return;
}
return requestBodyBuilders[requestBodyType](bodyContent, request);
},
/**
* Returns a JSON compatible with the Node's request library. (Also contains the original request)
*
* @param rawResponse Can be an XHR response or a Node request compatible response.
* about the actual request that was sent.
* @param requestOptions Options that were used to send the request.
* @param responseBody Body as a string.
*/
jsonifyResponse (rawResponse, requestOptions, responseBody) {
if (!rawResponse) {
return;
}
var responseJSON;
if (rawResponse.toJSON) {
responseJSON = rawResponse.toJSON();
responseJSON.request && _.assign(responseJSON.request, {
data: requestOptions.form || requestOptions.formData || requestOptions.body || {},
uri: { // @todo remove this
href: requestOptions.url && requestOptions.url.href || requestOptions.url
},
url: requestOptions.url && requestOptions.url.href || requestOptions.url
});
rawResponse.rawHeaders &&
(responseJSON.headers = this.arrayPairsToObject(rawResponse.rawHeaders) || responseJSON.headers);
return responseJSON;
}
responseBody = responseBody || '';
// @todo drop support or isolate XHR requester in v8
// XHR :/
return {
statusCode: rawResponse.status,
body: responseBody,
headers: _.transform(sdk.Header.parse(rawResponse.getAllResponseHeaders()), function (acc, header) {
if (acc[header.key]) {
!Array.isArray(acc[header.key]) && (acc[header.key] = [acc[header.key]]);
acc[header.key].push(header.value);
}
else {
acc[header.key] = header.value;
}
}, {}),
request: {
method: requestOptions.method || 'GET',
headers: requestOptions.headers,
uri: { // @todo remove this
href: requestOptions.url && requestOptions.url.href || requestOptions.url
},
url: requestOptions.url && requestOptions.url.href || requestOptions.url,
data: requestOptions.form || requestOptions.formData || requestOptions.body || {}
}
};
},
/**
* ArrayBuffer to String
*
* @param {ArrayBuffer} buffer
* @returns {String}
*/
arrayBufferToString (buffer) {
var str = '',
uArrayVal = new Uint8Array(buffer),
i,
ii;
for (i = 0, ii = uArrayVal.length; i < ii; i++) {
str += String.fromCharCode(uArrayVal[i]);
}
return str;
},
/**
* Converts an array of sequential pairs to an object.
*
* @param arr
* @returns {{}}
*
* @example
* ['a', 'b', 'c', 'd'] ====> {a: 'b', c: 'd' }
*/
arrayPairsToObject (arr) {
if (!_.isArray(arr)) {
return;
}
var obj = {},
key,
val,
i,
ii;
for (i = 0, ii = arr.length; i < ii; i += 2) {
key = arr[i];
val = arr[i + 1];
if (_.has(obj, key)) {
!_.isArray(obj[key]) && (obj[key] = [obj[key]]);
obj[key].push(val);
}
else {
obj[key] = val;
}
}
return obj;
},
/**
* Checks if a given host or IP is has been restricted in the options.
*
* @param {String} host
* @param {Object} networkOptions
* @param {Array<String>} networkOptions.restrictedAddresses
*
* @returns {Boolean}
*/
isAddressRestricted (host, networkOptions) {
return networkOptions.restrictedAddresses &&
networkOptions.restrictedAddresses[(host && host.toLowerCase())];
}
};