11"use strict" ;
22
3- const http = require ( "http" ) ;
4- const https = require ( "https" ) ;
53const { ono } = require ( "@jsdevtools/ono" ) ;
64const url = require ( "../util/url" ) ;
75const { ResolverError } = require ( "../util/errors" ) ;
@@ -70,12 +68,12 @@ module.exports = {
7068 * @param {object } file - An object containing information about the referenced file
7169 * @param {string } file.url - The full URL of the referenced file
7270 * @param {string } file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
73- * @returns {Promise<Buffer > }
71+ * @returns {Promise<string > }
7472 */
7573 read ( file ) {
7674 let u = url . parse ( file . url ) ;
7775
78- if ( process . browser && ! u . protocol ) {
76+ if ( typeof window !== "undefined" && ! u . protocol ) {
7977 // Use the protocol of the current page
8078 u . protocol = url . parse ( location . href ) . protocol ;
8179 }
@@ -91,42 +89,40 @@ module.exports = {
9189 * @param {object } httpOptions - The `options.resolve.http` object
9290 * @param {number } [redirects] - The redirect URLs that have already been followed
9391 *
94- * @returns {Promise<Buffer > }
92+ * @returns {Promise<string > }
9593 * The promise resolves with the raw downloaded data, or rejects if there is an HTTP error.
9694 */
9795function download ( u , httpOptions , redirects ) {
98- return new Promise ( ( ( resolve , reject ) => {
99- u = url . parse ( u ) ;
100- redirects = redirects || [ ] ;
101- redirects . push ( u . href ) ;
102-
103- get ( u , httpOptions )
104- . then ( ( res ) => {
105- if ( res . statusCode >= 400 ) {
106- throw ono ( { status : res . statusCode } , `HTTP ERROR ${ res . statusCode } ` ) ;
96+ u = url . parse ( u ) ;
97+ redirects = redirects || [ ] ;
98+ redirects . push ( u . href ) ;
99+
100+ return get ( u , httpOptions )
101+ . then ( ( res ) => {
102+ if ( res . statusCode >= 400 ) {
103+ throw ono ( { status : res . statusCode } , `HTTP ERROR ${ res . statusCode } ` ) ;
104+ }
105+ else if ( res . statusCode >= 300 ) {
106+ if ( redirects . length > httpOptions . redirects ) {
107+ throw new ResolverError ( ono ( { status : res . statusCode } ,
108+ `Error downloading ${ redirects [ 0 ] } . \nToo many redirects: \n ${ redirects . join ( " \n " ) } ` ) ) ;
107109 }
108- else if ( res . statusCode >= 300 ) {
109- if ( redirects . length > httpOptions . redirects ) {
110- reject ( new ResolverError ( ono ( { status : res . statusCode } ,
111- `Error downloading ${ redirects [ 0 ] } . \nToo many redirects: \n ${ redirects . join ( " \n " ) } ` ) ) ) ;
112- }
113- else if ( ! res . headers . location ) {
114- throw ono ( { status : res . statusCode } , `HTTP ${ res . statusCode } redirect with no location header` ) ;
115- }
116- else {
117- // console.log('HTTP %d redirect %s -> %s', res.statusCode, u.href, res.headers.location);
118- let redirectTo = url . resolve ( u , res . headers . location ) ;
119- download ( redirectTo , httpOptions , redirects ) . then ( resolve , reject ) ;
120- }
110+ else if ( ! res . headers . location ) {
111+ throw ono ( { status : res . statusCode } , `HTTP ${ res . statusCode } redirect with no location header` ) ;
121112 }
122113 else {
123- resolve ( res . body || Buffer . alloc ( 0 ) ) ;
114+ // console.log('HTTP %d redirect %s -> %s', res.statusCode, u.href, res.headers.location);
115+ let redirectTo = url . resolve ( u , res . headers . location ) ;
116+ return download ( redirectTo , httpOptions , redirects ) ;
124117 }
125- } )
126- . catch ( ( err ) => {
127- reject ( new ResolverError ( ono ( err , `Error downloading ${ u . href } ` ) , u . href ) ) ;
128- } ) ;
129- } ) ) ;
118+ }
119+ else {
120+ return res . text ( ) ;
121+ }
122+ } )
123+ . catch ( ( err ) => {
124+ throw new ResolverError ( ono ( err , `Error downloading ${ u . href } ` ) , u . href ) ;
125+ } ) ;
130126}
131127
132128/**
@@ -139,42 +135,23 @@ function download (u, httpOptions, redirects) {
139135 * The promise resolves with the HTTP Response object.
140136 */
141137function get ( u , httpOptions ) {
142- return new Promise ( ( ( resolve , reject ) => {
143- // console.log('GET', u.href);
144-
145- let protocol = u . protocol === "https:" ? https : http ;
146- let req = protocol . get ( {
147- hostname : u . hostname ,
148- port : u . port ,
149- path : u . path ,
150- auth : u . auth ,
151- protocol : u . protocol ,
152- headers : httpOptions . headers || { } ,
153- withCredentials : httpOptions . withCredentials
154- } ) ;
138+ let controller ;
139+ let timeoutId ;
140+ if ( httpOptions . timeout ) {
141+ controller = new AbortController ( ) ;
142+ timeoutId = setTimeout ( ( ) => controller . abort ( ) , httpOptions . timeout ) ;
143+ }
155144
156- if ( typeof req . setTimeout === "function" ) {
157- req . setTimeout ( httpOptions . timeout ) ;
145+ return fetch ( u , {
146+ method : "GET" ,
147+ headers : httpOptions . headers || { } ,
148+ credentials : httpOptions . withCredentials ? "include" : "same-origin" ,
149+ signal : controller ? controller . signal : null ,
150+ } ) . then ( response => {
151+ if ( timeoutId ) {
152+ clearTimeout ( timeoutId ) ;
158153 }
159154
160- req . on ( "timeout" , ( ) => {
161- req . abort ( ) ;
162- } ) ;
163-
164- req . on ( "error" , reject ) ;
165-
166- req . once ( "response" , ( res ) => {
167- res . body = Buffer . alloc ( 0 ) ;
168-
169- res . on ( "data" , ( data ) => {
170- res . body = Buffer . concat ( [ res . body , Buffer . from ( data ) ] ) ;
171- } ) ;
172-
173- res . on ( "error" , reject ) ;
174-
175- res . on ( "end" , ( ) => {
176- resolve ( res ) ;
177- } ) ;
178- } ) ;
179- } ) ) ;
155+ return response ;
156+ } ) ;
180157}
0 commit comments