Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cache latest fuels version #3350

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

Conversation

Dhaiwat10
Copy link
Member

Closes TS-654

Summary

This PR adds a basic local cache for the latest fuels version fetched by the fuels CLI.

Checklist

  • All changes are covered by tests (or not applicable)
  • All changes are documented (or not applicable)
  • I reviewed the entire PR myself (preferably, on GH UI)
  • I described all Breaking Changes (or there's none)

Copy link

vercel bot commented Oct 21, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
fuels-template ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 22, 2024 11:13am
ts-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 22, 2024 11:13am

@Dhaiwat10 Dhaiwat10 changed the title feat: cache latest fuels version feat: cache latest fuels version Oct 21, 2024
@Dhaiwat10 Dhaiwat10 marked this pull request as draft October 21, 2024 15:40
Copy link

codspeed-hq bot commented Oct 21, 2024

CodSpeed Performance Report

Merging #3350 will not alter performance

Comparing dp/fuels-version-cache (b958358) with master (28e9ed8)

Summary

✅ 18 untouched benchmarks

Copy link
Contributor

Coverage Report:

Lines Branches Functions Statements
75.99%(+0.01%) 70.14%(+0.07%) 75.09%(+0%) 76.11%(+0%)
Changed Files:
Ok File (✨=New File) Lines Branches Functions Statements
🔴 packages/account/src/account.ts 78.82%
(-2.88%)
63.51%
(-0.87%)
80%
(-4.21%)
78.61%
(-2.82%)
✨ packages/fuels/src/cli/utils/fuelsVersionCache.ts 100%
(+100%)
100%
(+100%)
100%
(+100%)
100%
(+100%)
✨ packages/fuels/src/cli/utils/getLatestFuelsVersion.ts 100%
(+100%)
100%
(+100%)
100%
(+100%)
100%
(+100%)

@Dhaiwat10 Dhaiwat10 marked this pull request as ready for review October 22, 2024 11:27
Copy link
Contributor

@Torres-ssf Torres-ssf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job 👏

Comment on lines +9 to +18
const data: { version: string } | null = await Promise.race([
new Promise((resolve) => {
setTimeout(() => resolve(null), 3000);
}),
fetch('https://registry.npmjs.org/fuels/latest').then((response) => response.json()),
]);

if (!data) {
throw new Error('Failed to fetch latest fuels version.');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since an error is being thrown anyway, we can reject the promise instead of resolving it on line 11.

Comment on lines +20 to +22
if (!cachedVersion) {
return null;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for the cache file to exist but cachedVersion to be null?

Comment on lines +24 to +27
const { mtimeMs: cacheTimestamp } = fs.statSync(FUELS_VERSION_CACHE_FILE);
const hasCacheExpired = Date.now() - cacheTimestamp > FUELS_VERSION_CACHE_TTL;

return hasCacheExpired ? null : cachedVersion;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth saving the timestamp along the cache so we don't need to call statSync ?

import fs from 'fs';
import path from 'path';

export const FUELS_VERSION_CACHE_FILE = path.join(__dirname, 'FUELS_VERSION');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should add this to the .gitignore file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement caching to check latest fuels version
2 participants