Skip to content

Commit e987790

Browse files
Glavin001arcanis
authored andcommitted
Add custom headers to tarball fetcher (#6756)
* Add custom headers to tarball fetcher * Remove requestUrl argument from requestHeaders method * Load custom headers for tarball request from yarn config
1 parent 5419606 commit e987790

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/fetchers/tarball-fetcher.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as constants from '../constants.js';
66
import BaseFetcher from './base-fetcher.js';
77
import * as fsUtil from '../util/fs.js';
88
import {removePrefix} from '../util/misc.js';
9+
import normalizeUrl from 'normalize-url';
910

1011
const crypto = require('crypto');
1112
const path = require('path');
@@ -228,11 +229,13 @@ export default class TarballFetcher extends BaseFetcher {
228229
const registry = this.config.registries[this.registry];
229230

230231
try {
232+
const headers = this.requestHeaders();
231233
return await registry.request(
232234
this.reference,
233235
{
234236
headers: {
235237
'Accept-Encoding': 'gzip',
238+
...headers,
236239
},
237240
buffer: true,
238241
process: (req, resolve, reject) => {
@@ -273,6 +276,24 @@ export default class TarballFetcher extends BaseFetcher {
273276
}
274277
}
275278

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+
276297
_fetch(): Promise<FetchedOverride> {
277298
const isFilePath = this.reference.startsWith('file:');
278299
this.reference = removePrefix(this.reference, 'file:');
@@ -329,3 +350,16 @@ export class LocalTarballFetcher extends TarballFetcher {
329350
return this.fetchFromLocal(this.reference);
330351
}
331352
}
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

Comments
 (0)