Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions .yarn/versions/7180.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
releases:
"@yarnpkg/core": minor

declined:
- "@yarnpkg/plugin-catalog"
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-jsr"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/cli"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
24 changes: 23 additions & 1 deletion packages/yarnpkg-core/sources/httpUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {ConfigurationValueMap, Configuration} from './Configuration';
import {MessageName} from './MessageName';
import {WrapNetworkRequestInfo} from './Plugin';
import {ReportError} from './Report';
import {YarnVersion} from './YarnVersion';
import * as formatUtils from './formatUtils';
import {MapValue, MapValueToObjectValue} from './miscUtils';
import * as miscUtils from './miscUtils';
Expand Down Expand Up @@ -171,8 +172,29 @@ export type Options = {
wrapNetworkRequest?: (executor: () => Promise<Response>, extra: WrapNetworkRequestInfo) => Promise<() => Promise<Response>>;
};

function getDefaultUserAgent() {
const version = YarnVersion !== null
? YarnVersion
: `${miscUtils.dynamicRequire(`@yarnpkg/core/package.json`).version}-core`;

return `yarn/${version} node/${process.version}`;
}

function applyDefaultHeaders(headers: Options[`headers`]) {
const normalizedHeaders = {...headers};

const hasUserAgent = Object.entries(normalizedHeaders).some(([headerName, headerValue]) =>
headerName.toLowerCase() === `user-agent` && typeof headerValue !== `undefined`,
);

if (!hasUserAgent)
normalizedHeaders[`user-agent`] = getDefaultUserAgent();

return normalizedHeaders;
}

export async function request(target: string | URL, body: Body, {configuration, headers, jsonRequest, jsonResponse, method = Method.GET, wrapNetworkRequest}: Omit<Options, `customErrorMessage`>) {
const options = {target, body, configuration, headers, jsonRequest, jsonResponse, method};
const options = {target, body, configuration, headers: applyDefaultHeaders(headers), jsonRequest, jsonResponse, method};

const realRequest = async () => await requestImpl(target, body, options);

Expand Down
46 changes: 45 additions & 1 deletion packages/yarnpkg-core/tests/httpUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,55 @@ describe(`httpUtils`, () => {
expect(hookArgumentResult.target).toBe(target);
expect(hookArgumentResult.body).toBe(body);
expect(hookArgumentResult.configuration).toBe(configuration);
expect(hookArgumentResult.headers).toBe(headers);
expect(hookArgumentResult.headers).toEqual({
...headers,
[`user-agent`]: expect.stringMatching(new RegExp(`^yarn/.+ node/${process.version}$`)),
});
expect(hookArgumentResult.jsonRequest).toBe(jsonRequest);
expect(hookArgumentResult.jsonResponse).toBe(jsonResponse);
expect(hookArgumentResult.method).toBe(method);
});

it(`sets a Yarn User-Agent header by default`, async () => {
// Arrange
const target = `https://my/fake/target`;

const {plugins, mockWrapNetworkRequest} = getPluginsWithMockWrapNetworkRequestPlugin();
const configuration = Configuration.create(npath.toPortablePath(`.`), plugins);
mockWrapNetworkRequest.mockReturnValue(() => {});

// Act
await httpUtils.request(target, null, {configuration});

// Assert
const hookArgumentResult = mockWrapNetworkRequest.mock.calls[0][1];
expect(hookArgumentResult.headers).toEqual({
[`user-agent`]: expect.stringMatching(new RegExp(`^yarn/.+ node/${process.version}$`)),
});
});

it(`preserves a custom User-Agent header`, async () => {
// Arrange
const target = `https://my/fake/target`;

const {plugins, mockWrapNetworkRequest} = getPluginsWithMockWrapNetworkRequestPlugin();
const configuration = Configuration.create(npath.toPortablePath(`.`), plugins);
mockWrapNetworkRequest.mockReturnValue(() => {});

// Act
await httpUtils.request(target, null, {
configuration,
headers: {
[`User-Agent`]: `custom-user-agent`,
},
});

// Assert
const hookArgumentResult = mockWrapNetworkRequest.mock.calls[0][1];
expect(hookArgumentResult.headers).toEqual({
[`User-Agent`]: `custom-user-agent`,
});
});
});

function getPluginsWithMockWrapNetworkRequestPlugin() {
Expand Down
Loading