@@ -6,6 +6,7 @@ import * as constants from '../constants.js';
6
6
import BaseFetcher from './base-fetcher.js' ;
7
7
import * as fsUtil from '../util/fs.js' ;
8
8
import { removePrefix } from '../util/misc.js' ;
9
+ import normalizeUrl from 'normalize-url' ;
9
10
10
11
const crypto = require ( 'crypto' ) ;
11
12
const path = require ( 'path' ) ;
@@ -228,11 +229,13 @@ export default class TarballFetcher extends BaseFetcher {
228
229
const registry = this . config . registries [ this . registry ] ;
229
230
230
231
try {
232
+ const headers = this . requestHeaders ( ) ;
231
233
return await registry . request (
232
234
this . reference ,
233
235
{
234
236
headers : {
235
237
'Accept-Encoding' : 'gzip' ,
238
+ ...headers ,
236
239
} ,
237
240
buffer : true ,
238
241
process : ( req , resolve , reject ) => {
@@ -273,6 +276,24 @@ export default class TarballFetcher extends BaseFetcher {
273
276
}
274
277
}
275
278
279
+ requestHeaders ( ) : { [ string ] : string } {
280
+ const registry = this . config . registries . yarn ;
281
+ const config = registry . config ;
282
+ const requestParts = urlParts ( this . reference ) ;
283
+ return Object . keys ( config ) . reduce ( ( headers , option ) => {
284
+ const parts = option . split ( ':' ) ;
285
+ if ( parts . length === 3 && parts [ 1 ] === '_header' ) {
286
+ const registryParts = urlParts ( parts [ 0 ] ) ;
287
+ if ( requestParts . host === registryParts . host && requestParts . path . startsWith ( registryParts . path ) ) {
288
+ const headerName = parts [ 2 ] ;
289
+ const headerValue = config [ option ] ;
290
+ headers [ headerName ] = headerValue ;
291
+ }
292
+ }
293
+ return headers ;
294
+ } , { } ) ;
295
+ }
296
+
276
297
_fetch ( ) : Promise < FetchedOverride > {
277
298
const isFilePath = this . reference . startsWith ( 'file:' ) ;
278
299
this . reference = removePrefix ( this . reference , 'file:' ) ;
@@ -329,3 +350,16 @@ export class LocalTarballFetcher extends TarballFetcher {
329
350
return this . fetchFromLocal ( this . reference ) ;
330
351
}
331
352
}
353
+
354
+ type UrlParts = {
355
+ host : string ,
356
+ path : string ,
357
+ } ;
358
+
359
+ function urlParts ( requestUrl : string ) : UrlParts {
360
+ const normalizedUrl = normalizeUrl ( requestUrl ) ;
361
+ const parsed = url . parse ( normalizedUrl ) ;
362
+ const host = parsed . host || '' ;
363
+ const path = parsed . path || '' ;
364
+ return { host, path} ;
365
+ }
0 commit comments