1
1
"use strict" ;
2
2
3
- const http = require ( "http" ) ;
4
- const https = require ( "https" ) ;
5
3
const { ono } = require ( "@jsdevtools/ono" ) ;
6
4
const url = require ( "../util/url" ) ;
7
5
const { ResolverError } = require ( "../util/errors" ) ;
@@ -70,12 +68,12 @@ module.exports = {
70
68
* @param {object } file - An object containing information about the referenced file
71
69
* @param {string } file.url - The full URL of the referenced file
72
70
* @param {string } file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
73
- * @returns {Promise<Buffer > }
71
+ * @returns {Promise<string > }
74
72
*/
75
73
read ( file ) {
76
74
let u = url . parse ( file . url ) ;
77
75
78
- if ( process . browser && ! u . protocol ) {
76
+ if ( typeof window !== "undefined" && ! u . protocol ) {
79
77
// Use the protocol of the current page
80
78
u . protocol = url . parse ( location . href ) . protocol ;
81
79
}
@@ -91,42 +89,40 @@ module.exports = {
91
89
* @param {object } httpOptions - The `options.resolve.http` object
92
90
* @param {number } [redirects] - The redirect URLs that have already been followed
93
91
*
94
- * @returns {Promise<Buffer > }
92
+ * @returns {Promise<string > }
95
93
* The promise resolves with the raw downloaded data, or rejects if there is an HTTP error.
96
94
*/
97
95
function 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 " ) } ` ) ) ;
107
109
}
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` ) ;
121
112
}
122
113
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 ) ;
124
117
}
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
+ } ) ;
130
126
}
131
127
132
128
/**
@@ -139,42 +135,23 @@ function download (u, httpOptions, redirects) {
139
135
* The promise resolves with the HTTP Response object.
140
136
*/
141
137
function 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
+ }
155
144
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 ) ;
158
153
}
159
154
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
+ } ) ;
180
157
}
0 commit comments