Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom headers to tarball fetcher #6756

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/fetchers/tarball-fetcher.js
Original file line number Diff line number Diff line change
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(this.reference);
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(requestUrl: string): {[string]: string} {
Glavin001 marked this conversation as resolved.
Show resolved Hide resolved
const registry = this.config.registries[this.registry];
const config = registry.config;
const requestParts = urlParts(requestUrl);
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 = {
Copy link
Contributor Author

@Glavin001 Glavin001 Dec 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is copied from

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};
}

I would like to put it in a more central location. I ask the Yarn team where would be the most appropriate destination? 😄

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};
}