-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into ns/chore/new-snippe…
…ts-for-contracts
- Loading branch information
Showing
16 changed files
with
260 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"fuels": patch | ||
--- | ||
|
||
feat: cache latest `fuels` version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,3 +168,5 @@ Forc.lock | |
/playwright-report/ | ||
/blob-report/ | ||
/playwright/.cache/ | ||
|
||
FUELS_VERSION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import fs from 'fs'; | ||
|
||
import { | ||
checkAndLoadCache, | ||
FUELS_VERSION_CACHE_FILE, | ||
FUELS_VERSION_CACHE_TTL, | ||
saveToCache, | ||
} from './fuelsVersionCache'; | ||
|
||
const mockWriteFile = () => vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {}); | ||
|
||
const mockFileAge = (createdAtMs: number) => | ||
// @ts-expect-error type mismatch for mtimeMs | ||
vi.spyOn(fs, 'statSync').mockReturnValue({ mtimeMs: createdAtMs }); | ||
|
||
const mockReadFile = (content: string) => vi.spyOn(fs, 'readFileSync').mockReturnValue(content); | ||
|
||
const mockFileExists = (exists: boolean) => vi.spyOn(fs, 'existsSync').mockReturnValue(exists); | ||
|
||
/** | ||
* @group node | ||
*/ | ||
describe('fuelsVersionCache', () => { | ||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
test('saveToCache', () => { | ||
const version = '0.1.0'; | ||
|
||
const writeFileMock = mockWriteFile(); | ||
|
||
saveToCache(version); | ||
|
||
// Assert that writeFileSync was called | ||
expect(writeFileMock).toHaveBeenCalledWith(FUELS_VERSION_CACHE_FILE, version, 'utf-8'); | ||
}); | ||
|
||
test('checkAndLoadCache - when cache exists', () => { | ||
mockFileExists(true); | ||
const version = '0.1.0'; | ||
|
||
const readFileMock = mockReadFile(version); | ||
|
||
mockFileAge(Date.now() - 120000); // 2 minutes ago | ||
|
||
const result = checkAndLoadCache(); | ||
|
||
expect(readFileMock).toHaveBeenCalledWith(FUELS_VERSION_CACHE_FILE, 'utf-8'); | ||
expect(result).toEqual(version); | ||
}); | ||
|
||
test('checkAndLoadCache - when cache file does not exist', () => { | ||
mockFileExists(false); | ||
|
||
const result = checkAndLoadCache(); | ||
|
||
expect(result).toBeNull(); | ||
}); | ||
|
||
test('checkAndLoadCache - when cache file is empty', () => { | ||
mockFileExists(true); | ||
mockReadFile(''); | ||
|
||
const result = checkAndLoadCache(); | ||
|
||
expect(result).toBeNull(); | ||
}); | ||
|
||
test('checkAndLoadCache - when cache is too old', () => { | ||
mockFileExists(true); | ||
const version = '0.1.0'; | ||
const readFileMock = mockReadFile(version); | ||
|
||
mockFileAge(Date.now() - FUELS_VERSION_CACHE_TTL - 1); | ||
|
||
const result = checkAndLoadCache(); | ||
|
||
expect(readFileMock).toHaveBeenCalledWith(FUELS_VERSION_CACHE_FILE, 'utf-8'); | ||
expect(result).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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 => { | ||
const doesVersionCacheExist = fs.existsSync(FUELS_VERSION_CACHE_FILE); | ||
|
||
if (doesVersionCacheExist) { | ||
const cachedVersion = fs.readFileSync(FUELS_VERSION_CACHE_FILE, 'utf-8').trim(); | ||
|
||
if (!cachedVersion) { | ||
return null; | ||
} | ||
|
||
const { mtimeMs: cacheTimestamp } = fs.statSync(FUELS_VERSION_CACHE_FILE); | ||
const hasCacheExpired = Date.now() - cacheTimestamp > FUELS_VERSION_CACHE_TTL; | ||
|
||
return hasCacheExpired ? null : cachedVersion; | ||
} | ||
|
||
return null; | ||
}; |
50 changes: 50 additions & 0 deletions
50
packages/fuels/src/cli/utils/getLatestFuelsVersion.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as cacheMod from './fuelsVersionCache'; | ||
import { getLatestFuelsVersion } from './getLatestFuelsVersion'; | ||
|
||
/** | ||
* @group node | ||
*/ | ||
describe('getLatestFuelsVersion', () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
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'); | ||
}); | ||
|
||
it('should throw if fetch times out', async () => { | ||
vi.spyOn(global, 'fetch').mockImplementation( | ||
() => | ||
new Promise((resolve) => { | ||
setTimeout(resolve, 5000); | ||
}) | ||
); | ||
await expect(getLatestFuelsVersion()).rejects.toThrow(); | ||
}); | ||
|
||
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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((_, reject) => { | ||
// eslint-disable-next-line prefer-promise-reject-errors | ||
setTimeout(() => reject(null), 3000); | ||
}), | ||
fetch('https://registry.npmjs.org/fuels/latest').then((response) => response.json()), | ||
]); | ||
|
||
if (!data) { | ||
throw new Error('Failed to fetch latest fuels version.'); | ||
} | ||
|
||
const version = data.version as string; | ||
|
||
saveToCache(version); | ||
|
||
return version; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.