Skip to content

Commit

Permalink
Deprecate retries config opt in favor of retry (#422)
Browse files Browse the repository at this point in the history
Signed-off-by: Brian DeHamer <bdehamer@github.com>
  • Loading branch information
bdehamer committed Aug 16, 2023
1 parent d1d7498 commit d3b9587
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-chairs-wash.md
@@ -0,0 +1,5 @@
---
'tuf-js': minor
---

Deprecates in `fetchRetries` config option in the `UpdaterOptions` interface in favor of the new `fetchRetry` option.
2 changes: 1 addition & 1 deletion packages/client/src/__tests__/updater.test.ts
Expand Up @@ -21,7 +21,7 @@ describe('Updater', () => {
metadataBaseUrl: `${baseURL}/metadata`,
targetBaseUrl: `${baseURL}/targets`,
config: {
fetchRetries: 0,
fetchRetry: 0,
fetchTimeout: 1000,
},
};
Expand Down
23 changes: 19 additions & 4 deletions packages/client/src/config.ts
@@ -1,4 +1,20 @@
export const defaultConfig = {
import type { MakeFetchHappenOptions } from 'make-fetch-happen';

export type Config = {
maxRootRotations: number;
maxDelegations: number;
rootMaxLength: number;
timestampMaxLength: number;
snapshotMaxLength: number;
targetsMaxLength: number;
prefixTargetsWithHash: boolean;
fetchTimeout: number;
// deprecated use fetchRetry instead
fetchRetries: number | undefined;
fetchRetry: MakeFetchHappenOptions['retry'];
};

export const defaultConfig: Config = {
maxRootRotations: 32,
maxDelegations: 32,
rootMaxLength: 512000, //bytes
Expand All @@ -7,7 +23,6 @@ export const defaultConfig = {
targetsMaxLength: 5000000, // bytes
prefixTargetsWithHash: true,
fetchTimeout: 100000, // milliseconds
fetchRetries: 2,
fetchRetries: undefined,
fetchRetry: 2,
};

export type Config = typeof defaultConfig;
12 changes: 8 additions & 4 deletions packages/client/src/fetcher.ts
Expand Up @@ -6,6 +6,8 @@ import util from 'util';
import { DownloadHTTPError, DownloadLengthMismatchError } from './error';
import { withTempFile } from './utils/tmpfile';

import type { MakeFetchHappenOptions } from 'make-fetch-happen';

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

type DownloadFileHandler<T> = (file: string) => Promise<T>;
Expand Down Expand Up @@ -74,26 +76,28 @@ export abstract class BaseFetcher implements Fetcher {
}
}

type Retry = MakeFetchHappenOptions['retry'];

interface FetcherOptions {
timeout?: number;
retries?: number;
retry?: Retry;
}

export class DefaultFetcher extends BaseFetcher {
private timeout?: number;
private retries?: number;
private retry?: Retry;

constructor(options: FetcherOptions = {}) {
super();
this.timeout = options.timeout;
this.retries = options.retries;
this.retry = options.retry;
}

public override async fetch(url: string): Promise<NodeJS.ReadableStream> {
log('GET %s', url);
const response = await fetch(url, {
timeout: this.timeout,
retry: this.retries,
retry: this.retry,
});

if (!response.ok || !response?.body) {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/updater.ts
Expand Up @@ -62,7 +62,7 @@ export class Updater {
fetcher ||
new DefaultFetcher({
timeout: this.config.fetchTimeout,
retries: this.config.fetchRetries,
retry: this.config.fetchRetries ?? this.config.fetchRetry,
});
}

Expand Down

0 comments on commit d3b9587

Please sign in to comment.