@@ -50,7 +50,6 @@ function AutoFetcher(init) {
50
50
if ( ! ( this instanceof AutoFetcher ) ) {
51
51
return new AutoFetcher ( init ) ;
52
52
}
53
- this . proxyMode = init . proxyMode ;
54
53
this . prefix = init . prefix ;
55
54
this . mod = init . mod ;
56
55
this . prefixMod = init . prefix + init . mod ;
@@ -88,22 +87,21 @@ AutoFetcher.prototype.fixupURL = function (url) {
88
87
} ;
89
88
90
89
AutoFetcher . prototype . safeFetch = function ( url ) {
91
- var fixedURL = this . fixupURL ( url ) ;
92
90
// check to see if we have seen this url before in order
93
91
// to lessen the load against the server content is fetched from
94
92
if ( this . seen [ url ] != null ) return ;
95
93
this . seen [ url ] = true ;
96
94
if ( this . queuing ) {
97
95
// we are currently waiting for a batch of fetches to complete
98
- return this . queue . push ( fixedURL ) ;
96
+ return this . queue . push ( url ) ;
99
97
}
100
98
// fetch this url
101
99
this . fetches . push ( fetch ( url ) ) ;
102
100
} ;
103
101
104
102
AutoFetcher . prototype . urlExtractor = function ( match , n1 , n2 , n3 , offset , string ) {
105
103
// Same function as style_replacer in wombat.rewrite_style, n2 is our URL
106
- this . safeFetch ( n2 ) ;
104
+ this . safeFetch ( this . fixupURL ( n2 ) ) ;
107
105
return n1 + n2 + n3 ;
108
106
} ;
109
107
@@ -154,27 +152,79 @@ AutoFetcher.prototype.extractMedia = function (mediaRules) {
154
152
}
155
153
} ;
156
154
157
- AutoFetcher . prototype . extractSrcset = function ( srcsets ) {
155
+ AutoFetcher . prototype . maybeFixUpRelSchemelessPrefix = function ( url ) {
156
+ // attempt to ensure rewritten relative or schemeless URLs become full URLS!
157
+ // otherwise returns null if this did not happen
158
+ if ( url . indexOf ( this . relative ) === 0 ) {
159
+ return url . replace ( this . relative , this . prefix ) ;
160
+ }
161
+ if ( url . indexOf ( this . schemeless ) === 0 ) {
162
+ return url . replace ( this . schemeless , this . prefix ) ;
163
+ }
164
+ return null ;
165
+ } ;
166
+
167
+ AutoFetcher . prototype . maybeResolveURL = function ( url , base ) {
168
+ // given a url and base url returns a resolved full URL or
169
+ // null if resolution was unsuccessful
170
+ try {
171
+ var _url = new URL ( url , base ) ;
172
+ return _url . href ;
173
+ } catch ( e ) {
174
+ return null ;
175
+ }
176
+ } ;
177
+
178
+
179
+ AutoFetcher . prototype . fixupURLSrcSet = function ( url , tagSrc , context ) {
180
+ // attempt to fix up the url and do our best to ensure we can get dat 200 OK!
181
+ if ( url . indexOf ( this . prefix ) !== 0 ) {
182
+ // first check for / (relative) or // (schemeless) rewritten urls
183
+ var maybeFixed = this . maybeFixUpRelSchemelessPrefix ( url ) ;
184
+ if ( maybeFixed != null ) {
185
+ return maybeFixed ;
186
+ }
187
+ // resolve URL against tag src
188
+ maybeFixed = this . maybeResolveURL ( url , tagSrc ) ;
189
+ if ( maybeFixed != null ) {
190
+ return this . prefix + 'im_/' + maybeFixed ;
191
+ }
192
+ // finally last attempt resolve the originating documents base URI
193
+ maybeFixed = this . maybeResolveURL ( url , context . docBaseURI ) ;
194
+ if ( maybeFixed != null ) {
195
+ return this . prefix + 'im_/' + maybeFixed ;
196
+ }
197
+ // not much to do now.....
198
+ return this . prefixMod + '/' + url ;
199
+ }
200
+ return url ;
201
+ } ;
202
+
203
+ AutoFetcher . prototype . extractSrcset = function ( srcsets , context ) {
158
204
if ( srcsets == null || srcsets . values == null ) return ;
159
205
var srcsetValues = srcsets . values ;
160
- // was srcsets from rewrite_srcset and if so no need to split
161
- var presplit = srcsets . presplit ;
206
+ if ( ! srcsets . presplit ) {
207
+ // was from extract from local doc so we need to duplicate work
208
+ return this . srcsetNotPreSplit ( srcsetValues , context ) ;
209
+ }
210
+ // was rewrite_srcset so just ensure we just
162
211
for ( var i = 0 ; i < srcsetValues . length ; i ++ ) {
163
- var srcset = srcsetValues [ i ] ;
164
- if ( presplit ) {
165
- // was rewrite_srcset so just ensure we just
212
+ // grab the URL not width/height key
213
+ this . safeFetch ( srcsetValues [ i ] . split ( ' ' ) [ 0 ] ) ;
214
+ }
215
+ } ;
216
+
217
+ AutoFetcher . prototype . srcsetNotPreSplit = function ( values , context ) {
218
+ // was from extract from local doc so we need to duplicate work
219
+ var j ;
220
+ for ( var i = 0 ; i < values . length ; i ++ ) {
221
+ var srcsetValues = values [ i ] . srcset . split ( srcsetSplit ) ;
222
+ var tagSrc = values [ i ] . tagSrc ;
223
+ for ( j = 0 ; j < srcsetValues . length ; j ++ ) {
166
224
// grab the URL not width/height key
167
- this . safeFetch ( srcset . split ( ' ' ) [ 0 ] ) ;
168
- } else {
169
- // was from extract from local doc so we need to duplicate work
170
- var values = srcset . split ( srcsetSplit ) ;
171
- for ( var j = 0 ; j < values . length ; j ++ ) {
172
- if ( Boolean ( values [ j ] ) ) {
173
- var value = values [ j ] . trim ( ) ;
174
- if ( value . length > 0 ) {
175
- this . safeFetch ( value . split ( ' ' ) [ 0 ] ) ;
176
- }
177
- }
225
+ if ( Boolean ( srcsetValues [ j ] ) ) {
226
+ var value = srcsetValues [ j ] . trim ( ) . split ( ' ' ) [ 0 ] ;
227
+ this . safeFetch ( this . fixupURLSrcSet ( value , tagSrc , context ) ) ;
178
228
}
179
229
}
180
230
}
@@ -184,7 +234,7 @@ AutoFetcher.prototype.autofetchMediaSrcset = function (data) {
184
234
// we got a message and now we autofetch!
185
235
// these calls turn into no ops if they have no work
186
236
this . extractMedia ( data . media ) ;
187
- this . extractSrcset ( data . srcset ) ;
237
+ this . extractSrcset ( data . srcset , data . context ) ;
188
238
this . fetchAll ( ) ;
189
239
} ;
190
240
0 commit comments