Skip to content

Commit 47e029e

Browse files
authored
add custom user agent for fetch calls (#939)
Signed-off-by: Brian DeHamer <bdehamer@github.com>
1 parent 7e3cecb commit 47e029e

5 files changed

Lines changed: 23 additions & 1 deletion

File tree

.changeset/small-trams-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tuf-js': minor
3+
---
4+
5+
Support for custom user agent string on fetch calls

packages/client/src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type Config = {
1212
// deprecated use fetchRetry instead
1313
fetchRetries: number | undefined;
1414
fetchRetry: MakeFetchHappenOptions['retry'];
15+
userAgent?: string;
1516
};
1617

1718
export const defaultConfig: Config = {
@@ -25,4 +26,5 @@ export const defaultConfig: Config = {
2526
fetchTimeout: 100000, // milliseconds
2627
fetchRetries: undefined,
2728
fetchRetry: 2,
29+
userAgent: '',
2830
};

packages/client/src/fetcher.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import type { MakeFetchHappenOptions } from 'make-fetch-happen';
1010

1111
const log = debug('tuf:fetch');
1212

13+
const USER_AGENT_HEADER = 'User-Agent';
14+
1315
type DownloadFileHandler<T> = (file: string) => Promise<T>;
1416

1517
export interface Fetcher {
@@ -79,23 +81,29 @@ export abstract class BaseFetcher implements Fetcher {
7981
type Retry = MakeFetchHappenOptions['retry'];
8082

8183
interface FetcherOptions {
84+
userAgent?: string;
8285
timeout?: number;
8386
retry?: Retry;
8487
}
8588

8689
export class DefaultFetcher extends BaseFetcher {
90+
private userAgent?: string;
8791
private timeout?: number;
8892
private retry?: Retry;
8993

9094
constructor(options: FetcherOptions = {}) {
9195
super();
96+
this.userAgent = options.userAgent;
9297
this.timeout = options.timeout;
9398
this.retry = options.retry;
9499
}
95100

96101
public override async fetch(url: string): Promise<NodeJS.ReadableStream> {
97102
log('GET %s', url);
98103
const response = await fetch(url, {
104+
headers: {
105+
[USER_AGENT_HEADER]: this.userAgent || '',
106+
},
99107
timeout: this.timeout,
100108
retry: this.retry,
101109
});

packages/client/src/updater.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Metadata, MetadataKind, TargetFile, Targets } from '@tufjs/models';
22
import debug from 'debug';
33
import * as fs from 'fs';
44
import * as path from 'path';
5+
import { version } from '../package.json';
56
import { Config, defaultConfig } from './config';
67
import {
78
DownloadHTTPError,
@@ -62,9 +63,14 @@ export class Updater {
6263

6364
this.trustedSet = new TrustedMetadataStore(data);
6465
this.config = { ...defaultConfig, ...config };
66+
const userAgent = config?.userAgent
67+
? `${config.userAgent} tuf-js/${version}`
68+
: `tuf-js/${version}`;
69+
6570
this.fetcher =
6671
fetcher ||
6772
new DefaultFetcher({
73+
userAgent,
6874
timeout: this.config.fetchTimeout,
6975
retry: this.config.fetchRetries ?? this.config.fetchRetry,
7076
});

tsconfig.base.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"noImplicitOverride": true,
77
"noImplicitReturns": true,
88
"noUnusedParameters": true,
9-
"isolatedModules": true
9+
"isolatedModules": true,
10+
"resolveJsonModule": true
1011
}
1112
}

0 commit comments

Comments
 (0)