Skip to content

Commit

Permalink
add new forceCache option to Updater (#575)
Browse files Browse the repository at this point in the history
Signed-off-by: Brian DeHamer <bdehamer@github.com>
  • Loading branch information
bdehamer committed Jan 9, 2024
1 parent abe34b4 commit d7c2600
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-colts-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tuf-js": minor
---

Adds a new `forceCache` option to the `Updater`
14 changes: 14 additions & 0 deletions packages/client/src/__tests__/updater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ describe('Updater', () => {
expect(targetInfo).toBeUndefined();
});
});

describe('when the forceCache option is true', () => {
it('returns the target info', async () => {
// Clear the repo mock so we can't possibly get the target from the remote
clearMock();

const offlineUpdater = new Updater({ ...options, forceCache: true });
const targetInfo = await offlineUpdater.getTargetInfo(target.name);

expect(targetInfo).toBeDefined();
expect(targetInfo?.path).toEqual(target.name);
expect(targetInfo?.length).toBe(target.content.length);
});
});
});

describe('#getTargetPath', () => {
Expand Down
33 changes: 30 additions & 3 deletions packages/client/src/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface UpdaterOptions {
targetDir?: string;
targetBaseUrl?: string;
fetcher?: Fetcher;
forceCache?: boolean;
config?: Partial<Config>;
}

Expand All @@ -34,6 +35,7 @@ export class Updater {
private metadataBaseUrl: string;
private targetDir?: string;
private targetBaseUrl?: string;
private forceCache: boolean;
private trustedSet: TrustedMetadataStore;
private config: Config;
private fetcher: Fetcher;
Expand All @@ -53,6 +55,7 @@ export class Updater {

this.targetDir = targetDir;
this.targetBaseUrl = targetBaseUrl;
this.forceCache = options.forceCache ?? false;

const data = this.loadLocalMetadata(MetadataKind.Root);

Expand All @@ -69,8 +72,24 @@ export class Updater {
// refresh and load the metadata before downloading the target
// refresh should be called once after the client is initialized
public async refresh() {
await this.loadRoot();
await this.loadTimestamp();
// If forceCache is true, try to load the timestamp from local storage
// without fetching it from the remote. Otherwise, load the root and
// timestamp from the remote per the TUF spec.
if (this.forceCache) {
// If anything fails, load the root and timestamp from the remote. This
// should cover any situation where the local metadata is corrupted or
// expired.
try {
await this.loadTimestamp({ checkRemote: false });
} catch (error) {
await this.loadRoot();
await this.loadTimestamp();
}
} else {
await this.loadRoot();
await this.loadTimestamp();
}

await this.loadSnapshot();
await this.loadTargets(MetadataKind.Targets, MetadataKind.Root);
}
Expand Down Expand Up @@ -188,11 +207,19 @@ export class Updater {

// Load local and remote timestamp metadata.
// Client workflow 5.4: update timestamp role
private async loadTimestamp() {
private async loadTimestamp(
{ checkRemote }: { checkRemote: boolean } = { checkRemote: true }
) {
// Load local and remote timestamp metadata
try {
const data = this.loadLocalMetadata(MetadataKind.Timestamp);
this.trustedSet.updateTimestamp(data);

// If checkRemote is disabled, return here to avoid fetching the remote
// timestamp metadata.
if (!checkRemote) {
return;
}
} catch (error) {
// continue
}
Expand Down

0 comments on commit d7c2600

Please sign in to comment.