8
8
*/
9
9
10
10
var url = require ( 'url' ) ;
11
- var http = require ( 'http' ) ;
12
- var https = require ( 'https' ) ;
11
+ var http = require ( 'follow-redirects' ) . http ; // Drop-in replacement for Nodes http and https that automatically follows redirects.
12
+ var https = require ( 'follow-redirects' ) . https ;
13
+ var request = require ( 'request' ) ;
13
14
14
15
var NodeXHREventTarget = require ( './node-xhr-event-target' ) ;
15
16
17
+ function utf8StringToArray ( str ) {
18
+ var buf = new ArrayBuffer ( str . length ) ; // 2 bytes for each char
19
+ var bufView = new Uint8Array ( buf ) ;
20
+ for ( var i = 0 , strLen = str . length ; i < strLen ; i ++ ) {
21
+ bufView [ i ] = str . charCodeAt ( i ) ;
22
+ }
23
+ return buf ;
24
+ }
25
+
16
26
/**
17
27
* Currently-supported response types.
18
28
*
@@ -38,16 +48,26 @@ var supportedResponseTypes = Object.freeze({
38
48
* @private
39
49
* @param {Object } opts - Options for the request.
40
50
* @param {Function } cb - Callback for request.
41
- * @returns {ClientRequest } The request.
42
51
*/
43
52
function makeRequest ( opts , cb ) {
44
- if ( opts . protocol === 'http:' ) {
45
- return http . request ( opts , cb ) ;
46
- } else if ( opts . protocol === 'https:' ) {
47
- return https . request ( opts , cb ) ;
48
- }
53
+ opts . gzip = true ;
54
+ return request ( opts , cb )
55
+ }
49
56
50
- throw new Error ( 'Unsupported protocol "' + opts . protcol + '"' ) ;
57
+ /**
58
+ * @param {NodeRequestOptions } opts
59
+ * @param {string } reqUrl
60
+ */
61
+ function setUrl ( opts , reqUrl ) {
62
+ var urlObj = url . parse ( reqUrl ) ;
63
+ /*
64
+ ['protocol', 'hostname', 'port', 'path'].forEach(function (key) {
65
+ if (key in urlObj) {
66
+ opts[key] = urlObj[key];
67
+ }
68
+ });
69
+ */
70
+ opts . url = urlObj ;
51
71
}
52
72
53
73
/**
@@ -436,12 +456,7 @@ NodeHttpXHR.prototype.open = function (method, reqUrl, async) {
436
456
var opts = this . _reqOpts ;
437
457
opts . method = method ;
438
458
439
- var urlObj = url . parse ( reqUrl ) ;
440
- [ 'protocol' , 'hostname' , 'port' , 'path' ] . forEach ( function ( key ) {
441
- if ( key in urlObj ) {
442
- opts [ key ] = urlObj [ key ] ;
443
- }
444
- } ) ;
459
+ setUrl ( opts , reqUrl ) ;
445
460
446
461
this . _setReadyState ( this . OPENED ) ;
447
462
} ;
@@ -496,52 +511,94 @@ NodeHttpXHR.prototype.send = function (data) {
496
511
} . bind ( this ) ;
497
512
498
513
var opts = this . _reqOpts ;
499
- var req = makeRequest ( opts , function onResponse ( resp ) {
514
+
515
+ var req = makeRequest ( opts , function onResponse ( err , resp , body ) {
516
+
517
+ if ( this . readyState !== this . LOADING ) {
518
+ this . _setReadyState ( this . LOADING ) ;
519
+ }
520
+
521
+ if ( err ) {
522
+ if ( this . _listenerCount ( 'error' ) < 1 ) {
523
+ // Uncaught error; throw something more meaningful
524
+ throw err ;
525
+ }
526
+
527
+ // Dispatch an error event. The specification does not provide for any way
528
+ // to communicate the failure reason with the event object.
529
+ this . dispatchEvent ( {
530
+ type : 'error'
531
+ } ) ;
532
+
533
+ this . _setReadyState ( this . DONE ) ;
534
+ return ;
535
+ }
536
+
500
537
this . _resp = resp ;
501
538
this . _responseText = '' ;
539
+ this . _response = null ;
540
+
541
+ if ( typeof body === 'string' ) {
542
+ this . _responseText = body ;
543
+ } else if ( typeof body === 'object' ) {
544
+ if ( ! ( body instanceof Buffer ) ) {
545
+ throw new Error ( 'Assertion failed: Response-data should be of `Buffer` type' ) ;
546
+ }
547
+ this . _response = body ;
548
+ }
549
+
550
+ if ( this . _responseType === 'arraybuffer' && this . _responseText ) {
551
+ this . _response = Buffer . from ( utf8StringToArray ( this . _responseText ) ) ;
552
+ }
502
553
503
554
// var contentType = resp.headers['content-type'];
504
555
// TODO: adjust responseType from content-type header if applicable
505
556
506
557
// from Node API docs: The encoding argument is optional and only applies when chunk is a string. Defaults to 'utf8'.
558
+ /*
507
559
if (this._responseType === 'text' || this._responseType === '') {
508
560
resp.setEncoding('utf8');
509
561
}
562
+ */
510
563
511
- resp . on ( 'data' , function onData ( chunk ) {
564
+ this . _setReadyState ( this . HEADERS_RECEIVED ) ;
512
565
513
- if ( typeof chunk === 'string' ) {
514
- this . _responseText += chunk ;
515
- } else if ( typeof chunk === 'object' ) {
516
- if ( ! ( chunk instanceof Buffer ) ) {
517
- throw new Error ( 'Assertion failed: Response-data should be of `Buffer` type' ) ;
518
- }
519
- if ( this . _response ) {
520
- this . _response = Buffer . concat ( [ this . _response , chunk ] ) ;
521
- } else {
522
- this . _response = chunk ;
523
- }
524
- }
566
+ this . _setReadyState ( this . DONE ) ;
525
567
526
- if ( this . readyState !== this . LOADING ) {
527
- this . _setReadyState ( this . LOADING ) ;
528
- }
568
+ this . dispatchEvent ( {
569
+ type : 'load'
570
+ } ) ;
529
571
530
- } . bind ( this ) ) ;
531
572
532
- resp . on ( 'end' , function onEnd ( ) {
533
- this . _setReadyState ( this . DONE ) ;
534
- this . dispatchEvent ( {
535
- type : 'load'
536
- } ) ;
537
- } . bind ( this ) ) ;
573
+ } . bind ( this ) ) ;
538
574
539
- this . _setReadyState ( this . HEADERS_RECEIVED ) ;
575
+ /*
576
+ req.on('data', function onData(chunk) {
577
+
578
+ if (typeof chunk === 'string') {
579
+ this._responseText += chunk;
580
+ } else if (typeof chunk === 'object') {
581
+ if (!(chunk instanceof Buffer)) {
582
+ throw new Error('Assertion failed: Response-data should be of `Buffer` type');
583
+ }
584
+ if (this._response) {
585
+ this._response = Buffer.concat([this._response, chunk]);
586
+ } else {
587
+ this._response = chunk;
588
+ }
589
+ }
590
+
591
+ if (this.readyState !== this.LOADING) {
592
+ this._setReadyState(this.LOADING);
593
+ }
540
594
}.bind(this));
595
+ */
541
596
542
597
// Passing `opts.timeout` doesn't actually seem to set the timeout sometimes,
543
598
// so it is set manually here.
544
- req . setTimeout ( opts . timeout ) ;
599
+ //req.setTimeout(opts.timeout);
600
+
601
+ /*
545
602
546
603
req.on('abort', onAbort);
547
604
req.on('aborted', onAbort);
@@ -568,11 +625,14 @@ NodeHttpXHR.prototype.send = function (data) {
568
625
this._setReadyState(this.DONE);
569
626
}.bind(this));
570
627
628
+
571
629
if (data) {
572
630
req.write(data);
573
631
}
574
632
req.end();
575
633
634
+ */
635
+
576
636
this . _req = req ;
577
637
} ;
578
638
0 commit comments