-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
#3186
base: master
Are you sure you want to change the base?
Changes from 13 commits
6d9e7a7
016d916
e130b2b
591c32a
8af937d
6a6bf8d
6e4001d
7c4d96f
75303c4
7e4ee26
a727b1a
97e464c
b1ec98f
ff3f7fe
43dbb55
6d28555
7fb032e
ed250e9
9a79d38
28bd961
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"fuels": patch | ||
--- | ||
|
||
feat: cache latest `fuels` version |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
export const FUELS_VERSION_CACHE_FILE = path.join(__dirname, 'FUELS_VERSION'); | ||
|
||
export type FuelsVersionCache = string; | ||
|
||
export const saveToCache = (cache: FuelsVersionCache) => { | ||
fs.writeFileSync(FUELS_VERSION_CACHE_FILE, cache, 'utf-8'); | ||
}; | ||
|
||
export const FUELS_VERSION_CACHE_TTL = 6 * 60 * 60 * 1000; // 6 hours in milliseconds | ||
|
||
export const checkAndLoadCache = (): FuelsVersionCache | null => { | ||
if (fs.existsSync(FUELS_VERSION_CACHE_FILE)) { | ||
const cachedVersion = fs.readFileSync(FUELS_VERSION_CACHE_FILE, 'utf-8'); | ||
const { mtimeMs: cacheTimestamp } = fs.statSync(FUELS_VERSION_CACHE_FILE); | ||
if (cachedVersion && Date.now() - cacheTimestamp < FUELS_VERSION_CACHE_TTL) { | ||
return cachedVersion; | ||
} | ||
} | ||
return null; | ||
}; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weird, they pass locally. Investigating |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as cacheMod from './fuelsVersionCache'; | ||
import { getLatestFuelsVersion } from './getLatestFuelsVersion'; | ||
|
||
/** | ||
* @group node | ||
*/ | ||
describe('getLatestFuelsVersion', () => { | ||
it('should fail if fetch fails', async () => { | ||
vi.spyOn(global, 'fetch').mockImplementation(() => | ||
Promise.reject(new Error('Failed to fetch')) | ||
); | ||
await expect(getLatestFuelsVersion()).rejects.toThrowError('Failed to fetch'); | ||
Check failure on line 12 in packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts GitHub Actions / node@18packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts > getLatestFuelsVersion > should fail if fetch fails
Check failure on line 12 in packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts GitHub Actions / node@20packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts > getLatestFuelsVersion > should fail if fetch fails
Check failure on line 12 in packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts GitHub Actions / node@22packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts > getLatestFuelsVersion > should fail if fetch fails
|
||
}); | ||
|
||
it('should throw if fetch times out', async () => { | ||
vi.spyOn(global, 'fetch').mockImplementation( | ||
() => | ||
new Promise((resolve) => { | ||
setTimeout(resolve, 5000); | ||
}) | ||
); | ||
await expect(getLatestFuelsVersion()).rejects.toThrow(); | ||
Check failure on line 22 in packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts GitHub Actions / node@18packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts > getLatestFuelsVersion > should throw if fetch times out
Check failure on line 22 in packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts GitHub Actions / node@20packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts > getLatestFuelsVersion > should throw if fetch times out
Check failure on line 22 in packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts GitHub Actions / node@22packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts > getLatestFuelsVersion > should throw if fetch times out
|
||
}); | ||
|
||
it('should return cached version if it exists', async () => { | ||
const cachedVersion = '1.0.0'; | ||
vi.spyOn(cacheMod, 'checkAndLoadCache').mockReturnValue(cachedVersion); | ||
const result = await getLatestFuelsVersion(); | ||
expect(result).toEqual('1.0.0'); | ||
}); | ||
|
||
it('should fetch if there is no cache or the cache is expired', async () => { | ||
const mockResponse = new Response(JSON.stringify({ version: '1.0.0' })); | ||
const fetchSpy = vi.spyOn(global, 'fetch').mockReturnValue(Promise.resolve(mockResponse)); | ||
const saveCacheSpy = vi.spyOn(cacheMod, 'saveToCache').mockImplementation(() => {}); | ||
vi.spyOn(cacheMod, 'checkAndLoadCache').mockReturnValue(null); | ||
const version = await getLatestFuelsVersion(); | ||
expect(fetchSpy).toHaveBeenCalled(); | ||
expect(version).toEqual('1.0.0'); | ||
expect(saveCacheSpy).toHaveBeenCalledWith('1.0.0'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { checkAndLoadCache, saveToCache } from './fuelsVersionCache'; | ||
|
||
export const getLatestFuelsVersion = async (): Promise<string | undefined> => { | ||
const cachedVersion = checkAndLoadCache(); | ||
if (cachedVersion) { | ||
return cachedVersion; | ||
} | ||
|
||
const data: { version: string } | null = await Promise.race([ | ||
new Promise((resolve) => { | ||
setTimeout(() => resolve(null), 3000); | ||
}), | ||
arboleya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fetch('https://registry.npmjs.org/fuels/latest').then((response) => response.json()), | ||
]); | ||
|
||
if (!data) { | ||
throw new Error('Failed to fetch latest fuels version.'); | ||
} | ||
arboleya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const version = data.version as string; | ||
|
||
saveToCache(version); | ||
|
||
return version; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this tests needs to be re-synced