Skip to content

Commit

Permalink
Add custom headers to tarball fetcher (#6756)
Browse files Browse the repository at this point in the history
* Add custom headers to tarball fetcher

* Remove requestUrl argument from requestHeaders method

* Load custom headers for tarball request from yarn config
  • Loading branch information
Glavin001 authored and arcanis committed Dec 11, 2018
1 parent 5419606 commit e987790
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/fetchers/tarball-fetcher.js
Expand Up @@ -6,6 +6,7 @@ import * as constants from '../constants.js';
import BaseFetcher from './base-fetcher.js';
import * as fsUtil from '../util/fs.js';
import {removePrefix} from '../util/misc.js';
import normalizeUrl from 'normalize-url';

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

try {
const headers = this.requestHeaders();
return await registry.request(
this.reference,
{
headers: {
'Accept-Encoding': 'gzip',
...headers,
},
buffer: true,
process: (req, resolve, reject) => {
Expand Down Expand Up @@ -273,6 +276,24 @@ export default class TarballFetcher extends BaseFetcher {
}
}

requestHeaders(): {[string]: string} {
const registry = this.config.registries.yarn;
const config = registry.config;
const requestParts = urlParts(this.reference);
return Object.keys(config).reduce((headers, option) => {
const parts = option.split(':');
if (parts.length === 3 && parts[1] === '_header') {
const registryParts = urlParts(parts[0]);
if (requestParts.host === registryParts.host && requestParts.path.startsWith(registryParts.path)) {
const headerName = parts[2];
const headerValue = config[option];
headers[headerName] = headerValue;
}
}
return headers;
}, {});
}

_fetch(): Promise<FetchedOverride> {
const isFilePath = this.reference.startsWith('file:');
this.reference = removePrefix(this.reference, 'file:');
Expand Down Expand Up @@ -329,3 +350,16 @@ export class LocalTarballFetcher extends TarballFetcher {
return this.fetchFromLocal(this.reference);
}
}

type UrlParts = {
host: string,
path: string,
};

function urlParts(requestUrl: string): UrlParts {
const normalizedUrl = normalizeUrl(requestUrl);
const parsed = url.parse(normalizedUrl);
const host = parsed.host || '';
const path = parsed.path || '';
return {host, path};
}

0 comments on commit e987790

Please sign in to comment.