Skip to content

Commit d4c8b84

Browse files
authored
some minor renaming/reorganizing (#106)
1 parent 1ed48bb commit d4c8b84

File tree

7 files changed

+34
-59
lines changed

7 files changed

+34
-59
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/fetcher.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import nock from 'nock';
2-
import { Fetcher } from '../requestsFetcher';
2+
import { Fetcher } from '../fetcher';
33

44
describe('Fetcher Test', () => {
55
const baseURL = 'http://localhost:8080';

src/fetcher.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { DownloadLengthMismatchError } from './error';
1+
import fetch from 'make-fetch-happen';
2+
import { DownloadHTTPError, DownloadLengthMismatchError } from './error';
23

3-
export abstract class FetcherInterface {
4+
export abstract class BaseFetcher {
45
protected timeout?: number;
56

67
constructor(timeout?: number) {
@@ -30,3 +31,19 @@ export abstract class FetcherInterface {
3031
return Buffer.concat(chunks);
3132
}
3233
}
34+
35+
export class Fetcher extends BaseFetcher {
36+
constructor(timeout?: number) {
37+
super(timeout);
38+
}
39+
40+
public override async fetch(url: string): Promise<NodeJS.ReadableStream> {
41+
const response = await fetch(url, { timeout: this.timeout });
42+
43+
if (!response.ok || !response?.body) {
44+
throw new DownloadHTTPError('Failed to download', response.status);
45+
}
46+
47+
return response.body;
48+
}
49+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { FetcherInterface } from './fetcher';
1+
export { BaseFetcher } from './fetcher';
22
export { Updater } from './updater';

src/requestsFetcher.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/trusted_metadata_set.ts renamed to src/store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type TrustedSet = {
1515
targets?: Metadata<Targets>;
1616
} & { [key: string]: Metadata<Targets> };
1717

18-
export class TrustedMetadataSet {
18+
export class TrustedMetadataStore {
1919
private trustedSet: TrustedSet = {};
2020
private referenceTime: Date;
2121

src/updater.ts

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33
import { EqualVersionError, ValueError } from './error';
4-
import { FetcherInterface } from './fetcher';
4+
import { BaseFetcher, Fetcher } from './fetcher';
55
import { Metadata, Targets } from './models';
66
import { TargetFile } from './models/file';
7-
import { Fetcher } from './requestsFetcher';
8-
import { TrustedMetadataSet } from './trusted_metadata_set';
7+
import { TrustedMetadataStore } from './store';
98
import { updaterConfig } from './utils/config';
109
import { MetadataKind } from './utils/types';
1110

@@ -14,7 +13,7 @@ interface UodaterOptions {
1413
metadataBaseUrl: string;
1514
targetDir?: string;
1615
targetBaseUrl?: string;
17-
fetcher?: FetcherInterface;
16+
fetcher?: BaseFetcher;
1817
}
1918

2019
interface Delegation {
@@ -27,9 +26,9 @@ export class Updater {
2726
private metadataBaseUrl: string;
2827
private targetDir?: string;
2928
private targetBaseUrl?: string;
30-
private trustedSet: TrustedMetadataSet;
29+
private trustedSet: TrustedMetadataStore;
3130
private config: typeof updaterConfig;
32-
private fetcher: FetcherInterface;
31+
private fetcher: BaseFetcher;
3332

3433
constructor(options: UodaterOptions) {
3534
const { metadataDir, metadataBaseUrl, targetDir, targetBaseUrl, fetcher } =
@@ -42,7 +41,7 @@ export class Updater {
4241
this.targetBaseUrl = targetBaseUrl;
4342

4443
const data = this.loadLocalMetadata(MetadataKind.Root);
45-
this.trustedSet = new TrustedMetadataSet(data);
44+
this.trustedSet = new TrustedMetadataStore(data);
4645
this.config = updaterConfig;
4746

4847
this.fetcher = fetcher || new Fetcher(this.config.fetchTimeout);
@@ -208,34 +207,12 @@ export class Updater {
208207
return this.trustedSet.getRole(role);
209208
}
210209

210+
// Returns the TargetFile instance with information for the given target path.
211+
//
212+
// Implicitly calls refresh if it hasn't already been called.
211213
public async getTargetInfo(
212214
targetPath: string
213215
): Promise<TargetFile | undefined> {
214-
/***
215-
* Returns ``TargetFile`` instance with information for ``target_path``.
216-
217-
The return value can be used as an argument to
218-
``download_target()`` and ``find_cached_target()``.
219-
220-
If ``refresh()`` has not been called before calling
221-
``get_targetinfo()``, the refresh will be done implicitly.
222-
223-
As a side-effect this method downloads all the additional (delegated
224-
targets) metadata it needs to return the target information.
225-
226-
Args:
227-
target_path: `path-relative-URL string
228-
<https://url.spec.whatwg.org/#path-relative-url-string>`_
229-
that uniquely identifies the target within the repository.
230-
231-
Raises:
232-
OSError: New metadata could not be written to disk
233-
RepositoryError: Metadata failed to verify in some way
234-
DownloadError: Download of a metadata file failed in some way
235-
236-
Returns:
237-
``TargetFile`` instance or ``None``.
238-
***/
239216
if (!this.trustedSet.targets) {
240217
this.refresh();
241218
}

0 commit comments

Comments
 (0)