@aztec/cli 0.0.1-commit.03f7ef2 → 0.0.1-commit.04852196a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/dest/cmds/aztec_node/get_current_min_fee.d.ts +3 -0
  2. package/dest/cmds/aztec_node/get_current_min_fee.d.ts.map +1 -0
  3. package/dest/cmds/aztec_node/{get_current_base_fee.js → get_current_min_fee.js} +2 -2
  4. package/dest/cmds/aztec_node/index.js +3 -3
  5. package/dest/cmds/infrastructure/sequencers.d.ts +1 -1
  6. package/dest/cmds/infrastructure/sequencers.d.ts.map +1 -1
  7. package/dest/cmds/infrastructure/sequencers.js +2 -1
  8. package/dest/cmds/infrastructure/setup_l2_contract.d.ts +1 -1
  9. package/dest/cmds/infrastructure/setup_l2_contract.d.ts.map +1 -1
  10. package/dest/cmds/infrastructure/setup_l2_contract.js +4 -3
  11. package/dest/cmds/l1/assume_proven_through.d.ts +3 -2
  12. package/dest/cmds/l1/assume_proven_through.d.ts.map +1 -1
  13. package/dest/cmds/l1/assume_proven_through.js +3 -5
  14. package/dest/cmds/l1/compute_genesis_values.d.ts +4 -0
  15. package/dest/cmds/l1/compute_genesis_values.d.ts.map +1 -0
  16. package/dest/cmds/l1/compute_genesis_values.js +17 -0
  17. package/dest/cmds/l1/index.d.ts +1 -1
  18. package/dest/cmds/l1/index.d.ts.map +1 -1
  19. package/dest/cmds/l1/index.js +7 -2
  20. package/dest/cmds/l1/update_l1_validators.d.ts +3 -3
  21. package/dest/cmds/l1/update_l1_validators.d.ts.map +1 -1
  22. package/dest/cmds/l1/update_l1_validators.js +52 -17
  23. package/dest/config/cached_fetch.d.ts +19 -10
  24. package/dest/config/cached_fetch.d.ts.map +1 -1
  25. package/dest/config/cached_fetch.js +110 -32
  26. package/dest/config/chain_l2_config.d.ts +12 -39
  27. package/dest/config/chain_l2_config.d.ts.map +1 -1
  28. package/dest/config/chain_l2_config.js +35 -505
  29. package/dest/config/generated/networks.d.ts +222 -0
  30. package/dest/config/generated/networks.d.ts.map +1 -0
  31. package/dest/config/generated/networks.js +223 -0
  32. package/dest/config/network_config.d.ts +2 -2
  33. package/dest/config/network_config.d.ts.map +1 -1
  34. package/dest/config/network_config.js +4 -3
  35. package/dest/utils/inspect.d.ts +1 -1
  36. package/dest/utils/inspect.d.ts.map +1 -1
  37. package/dest/utils/inspect.js +4 -1
  38. package/package.json +35 -30
  39. package/src/cmds/aztec_node/{get_current_base_fee.ts → get_current_min_fee.ts} +2 -2
  40. package/src/cmds/aztec_node/index.ts +3 -3
  41. package/src/cmds/infrastructure/sequencers.ts +2 -1
  42. package/src/cmds/infrastructure/setup_l2_contract.ts +5 -4
  43. package/src/cmds/l1/assume_proven_through.ts +4 -7
  44. package/src/cmds/l1/compute_genesis_values.ts +29 -0
  45. package/src/cmds/l1/index.ts +23 -4
  46. package/src/cmds/l1/update_l1_validators.ts +46 -20
  47. package/src/config/cached_fetch.ts +119 -31
  48. package/src/config/chain_l2_config.ts +35 -657
  49. package/src/config/generated/networks.ts +227 -0
  50. package/src/config/network_config.ts +4 -3
  51. package/src/utils/inspect.ts +4 -1
  52. package/dest/cmds/aztec_node/get_current_base_fee.d.ts +0 -3
  53. package/dest/cmds/aztec_node/get_current_base_fee.d.ts.map +0 -1
@@ -25,12 +25,12 @@ export function injectCommands(program: Command, log: LogFn, debugLogger: Logger
25
25
  });
26
26
 
27
27
  program
28
- .command('get-current-base-fee')
28
+ .command('get-current-min-fee')
29
29
  .description('Gets the current base fee.')
30
30
  .addOption(nodeOption)
31
31
  .action(async options => {
32
- const { getCurrentBaseFee } = await import('./get_current_base_fee.js');
33
- await getCurrentBaseFee(options.rpcUrl, debugLogger, log);
32
+ const { getCurrentMinFee } = await import('./get_current_min_fee.js');
33
+ await getCurrentMinFee(options.rpcUrl, debugLogger, log);
34
34
  });
35
35
 
36
36
  program
@@ -66,8 +66,9 @@ export async function sequencers(opts: {
66
66
 
67
67
  log(`Adding ${who} as sequencer`);
68
68
 
69
+ const stakingAssetAddress = await rollup.getStakingAsset();
69
70
  const stakingAsset = getContract({
70
- address: await rollup.getStakingAsset(),
71
+ address: stakingAssetAddress.toString(),
71
72
  abi: TestERC20Abi,
72
73
  client: walletClient,
73
74
  });
@@ -1,12 +1,13 @@
1
1
  import { getInitialTestAccountsData } from '@aztec/accounts/testing';
2
- import type { AztecAddress } from '@aztec/aztec.js/addresses';
2
+ import { AztecAddress } from '@aztec/aztec.js/addresses';
3
3
  import type { WaitOpts } from '@aztec/aztec.js/contracts';
4
4
  import { createAztecNodeClient } from '@aztec/aztec.js/node';
5
5
  import { AccountManager } from '@aztec/aztec.js/wallet';
6
6
  import { jsonStringify } from '@aztec/foundation/json-rpc';
7
7
  import type { LogFn } from '@aztec/foundation/log';
8
8
  import { ProtocolContractAddress } from '@aztec/protocol-contracts';
9
- import { TestWallet, deployFundedSchnorrAccounts } from '@aztec/test-wallet/server';
9
+ import { EmbeddedWallet } from '@aztec/wallets/embedded';
10
+ import { deployFundedSchnorrAccounts } from '@aztec/wallets/testing';
10
11
 
11
12
  export async function setupL2Contracts(nodeUrl: string, testAccounts: boolean, json: boolean, log: LogFn) {
12
13
  const waitOpts: WaitOpts = {
@@ -16,13 +17,13 @@ export async function setupL2Contracts(nodeUrl: string, testAccounts: boolean, j
16
17
  log('setupL2Contracts: Wait options' + jsonStringify(waitOpts));
17
18
  log('setupL2Contracts: Creating PXE client...');
18
19
  const node = createAztecNodeClient(nodeUrl);
19
- const wallet = await TestWallet.create(node);
20
+ const wallet = await EmbeddedWallet.create(node);
20
21
 
21
22
  let deployedAccountManagers: AccountManager[] = [];
22
23
  if (testAccounts) {
23
24
  log('setupL2Contracts: Deploying test accounts...');
24
25
  const initialAccountsData = await getInitialTestAccountsData();
25
- deployedAccountManagers = await deployFundedSchnorrAccounts(wallet, node, initialAccountsData, waitOpts);
26
+ deployedAccountManagers = await deployFundedSchnorrAccounts(wallet, initialAccountsData, waitOpts);
26
27
  }
27
28
 
28
29
  if (json) {
@@ -1,23 +1,20 @@
1
1
  import { createAztecNodeClient } from '@aztec/aztec.js/node';
2
2
  import { RollupCheatCodes } from '@aztec/ethereum/test';
3
- import { BlockNumber } from '@aztec/foundation/branded-types';
3
+ import { CheckpointNumber } from '@aztec/foundation/branded-types';
4
4
  import type { LogFn } from '@aztec/foundation/log';
5
5
  import { DateProvider } from '@aztec/foundation/timer';
6
6
 
7
7
  export async function assumeProvenThrough(
8
- blockNumberOrLatest: number | undefined,
8
+ checkpointOrLatest: CheckpointNumber | undefined,
9
9
  l1RpcUrls: string[],
10
10
  nodeUrl: string,
11
11
  log: LogFn,
12
12
  ) {
13
13
  const aztecNode = createAztecNodeClient(nodeUrl);
14
14
  const rollupAddress = await aztecNode.getNodeInfo().then(i => i.l1ContractAddresses.rollupAddress);
15
- const blockNumber: BlockNumber = blockNumberOrLatest
16
- ? BlockNumber(blockNumberOrLatest)
17
- : await aztecNode.getBlockNumber();
18
15
 
19
16
  const rollupCheatCodes = RollupCheatCodes.create(l1RpcUrls, { rollupAddress }, new DateProvider());
20
17
 
21
- await rollupCheatCodes.markAsProven(blockNumber);
22
- log(`Assumed proven through block ${blockNumber}`);
18
+ await rollupCheatCodes.markAsProven(checkpointOrLatest);
19
+ log(`Assumed proven through checkpoint ${checkpointOrLatest ?? 'latest'}`);
23
20
  }
@@ -0,0 +1,29 @@
1
+ import { getInitialTestAccountsData } from '@aztec/accounts/testing';
2
+ import type { LogFn } from '@aztec/foundation/log';
3
+ import { protocolContractsHash } from '@aztec/protocol-contracts';
4
+ import { getGenesisValues } from '@aztec/world-state/testing';
5
+
6
+ import { getSponsoredFPCAddress } from '../../utils/setup_contracts.js';
7
+
8
+ /** Computes and prints genesis values needed for L1 contract deployment. */
9
+ export async function computeGenesisValuesCmd(testAccounts: boolean, sponsoredFPC: boolean, log: LogFn) {
10
+ const initialAccounts = testAccounts ? await getInitialTestAccountsData() : [];
11
+ const sponsoredFPCAddresses = sponsoredFPC ? await getSponsoredFPCAddress() : [];
12
+ const initialFundedAccounts = initialAccounts.map(a => a.address).concat(sponsoredFPCAddresses);
13
+ const { genesisArchiveRoot } = await getGenesisValues(initialFundedAccounts);
14
+
15
+ const { getVKTreeRoot } = await import('@aztec/noir-protocol-circuits-types/vk-tree');
16
+ const vkTreeRoot = getVKTreeRoot();
17
+
18
+ log(
19
+ JSON.stringify(
20
+ {
21
+ vkTreeRoot: vkTreeRoot.toString(),
22
+ protocolContractsHash: protocolContractsHash.toString(),
23
+ genesisArchiveRoot: genesisArchiveRoot.toString(),
24
+ },
25
+ null,
26
+ 2,
27
+ ),
28
+ );
29
+ }
@@ -1,3 +1,4 @@
1
+ import { CheckpointNumber } from '@aztec/foundation/branded-types';
1
2
  import { EthAddress } from '@aztec/foundation/eth-address';
2
3
  import type { LogFn, Logger } from '@aztec/foundation/log';
3
4
 
@@ -105,6 +106,24 @@ export function injectCommands(program: Command, log: LogFn, debugLogger: Logger
105
106
  );
106
107
  });
107
108
 
109
+ program
110
+ .command('compute-genesis-values')
111
+ .description('Computes genesis values (VK tree root, protocol contracts hash, genesis archive root).')
112
+ .addOption(
113
+ new Option('--test-accounts <boolean>', 'Include initial test accounts in genesis state')
114
+ .env('TEST_ACCOUNTS')
115
+ .argParser(arg => arg === 'true'),
116
+ )
117
+ .addOption(
118
+ new Option('--sponsored-fpc <boolean>', 'Include sponsored FPC contract in genesis state')
119
+ .env('SPONSORED_FPC')
120
+ .argParser(arg => arg === 'true'),
121
+ )
122
+ .action(async options => {
123
+ const { computeGenesisValuesCmd } = await import('./compute_genesis_values.js');
124
+ await computeGenesisValuesCmd(options.testAccounts, options.sponsoredFpc, log);
125
+ });
126
+
108
127
  program
109
128
  .command('deposit-governance-tokens')
110
129
  .description('Deposits governance tokens to the governance contract.')
@@ -497,14 +516,14 @@ export function injectCommands(program: Command, log: LogFn, debugLogger: Logger
497
516
  program
498
517
  .command('set-proven-through', { hidden: true })
499
518
  .description(
500
- 'Instructs the L1 rollup contract to assume all blocks until the given number are automatically proven.',
519
+ 'Instructs the L1 rollup contract to assume all blocks until the given checkpoint are automatically proven.',
501
520
  )
502
- .argument('[blockNumber]', 'The target block number, defaults to the latest pending block number.', parseBigint)
521
+ .argument('[checkpoint]', 'The target checkpoint, defaults to the latest pending checkpoint.', parseBigint)
503
522
  .addOption(l1RpcUrlsOption)
504
523
  .addOption(nodeOption)
505
- .action(async (blockNumber, options) => {
524
+ .action(async (checkpoint, options) => {
506
525
  const { assumeProvenThrough } = await import('./assume_proven_through.js');
507
- await assumeProvenThrough(blockNumber, options.l1RpcUrls, options.nodeUrl, log);
526
+ await assumeProvenThrough(CheckpointNumber.fromBigInt(checkpoint), options.l1RpcUrls, options.nodeUrl, log);
508
527
  });
509
528
 
510
529
  program
@@ -2,19 +2,17 @@ import { createEthereumChain, isAnvilTestChain } from '@aztec/ethereum/chain';
2
2
  import { createExtendedL1Client, getPublicClient } from '@aztec/ethereum/client';
3
3
  import { getL1ContractsConfigEnvVars } from '@aztec/ethereum/config';
4
4
  import { GSEContract, RollupContract } from '@aztec/ethereum/contracts';
5
- import { createL1TxUtilsFromViemWallet } from '@aztec/ethereum/l1-tx-utils';
5
+ import { createL1TxUtils } from '@aztec/ethereum/l1-tx-utils';
6
6
  import { EthCheatCodes } from '@aztec/ethereum/test';
7
7
  import type { EthAddress } from '@aztec/foundation/eth-address';
8
8
  import type { LogFn, Logger } from '@aztec/foundation/log';
9
9
  import { DateProvider } from '@aztec/foundation/timer';
10
- import { RollupAbi, StakingAssetHandlerAbi } from '@aztec/l1-artifacts';
10
+ import { RollupAbi, StakingAssetHandlerAbi, TestERC20Abi } from '@aztec/l1-artifacts';
11
11
  import { ZkPassportProofParams } from '@aztec/stdlib/zkpassport';
12
12
 
13
- import { encodeFunctionData, formatEther, getContract } from 'viem';
13
+ import { encodeFunctionData, formatEther, getContract, maxUint256 } from 'viem';
14
14
  import { generatePrivateKey, mnemonicToAccount, privateKeyToAccount } from 'viem/accounts';
15
15
 
16
- import { addLeadingHex } from '../../utils/aztec.js';
17
-
18
16
  export interface RollupCommandArgs {
19
17
  rpcUrls: string[];
20
18
  chainId: number;
@@ -53,8 +51,8 @@ export async function addL1Validator({
53
51
  privateKey,
54
52
  mnemonic,
55
53
  attesterAddress,
54
+ withdrawerAddress,
56
55
  stakingAssetHandlerAddress,
57
- merkleProof,
58
56
  proofParams,
59
57
  blsSecretKey,
60
58
  log,
@@ -63,8 +61,8 @@ export async function addL1Validator({
63
61
  LoggerArgs & {
64
62
  blsSecretKey: bigint; // scalar field element of BN254
65
63
  attesterAddress: EthAddress;
64
+ withdrawerAddress: EthAddress;
66
65
  proofParams: Buffer;
67
- merkleProof: string[];
68
66
  }) {
69
67
  const dualLog = makeDualLog(log, debugLogger);
70
68
  const account = getAccount(privateKey, mnemonic);
@@ -87,33 +85,61 @@ export async function addL1Validator({
87
85
  });
88
86
 
89
87
  const gseAddress = await rollup.read.getGSE();
90
-
91
88
  const gse = new GSEContract(l1Client, gseAddress);
92
-
93
89
  const registrationTuple = await gse.makeRegistrationTuple(blsSecretKey);
94
90
 
95
- const l1TxUtils = createL1TxUtilsFromViemWallet(l1Client, { logger: debugLogger });
91
+ const l1TxUtils = createL1TxUtils(l1Client, { logger: debugLogger });
96
92
  const proofParamsObj = ZkPassportProofParams.fromBuffer(proofParams);
97
- const merkleProofArray = merkleProof.map(proof => addLeadingHex(proof));
98
93
 
99
- const { receipt } = await l1TxUtils.sendAndMonitorTransaction({
94
+ // Step 1: Claim STK tokens from the faucet
95
+ dualLog(`Claiming STK tokens from faucet`);
96
+ const { receipt: claimReceipt } = await l1TxUtils.sendAndMonitorTransaction({
100
97
  to: stakingAssetHandlerAddress.toString(),
101
98
  data: encodeFunctionData({
102
99
  abi: StakingAssetHandlerAbi,
103
- functionName: 'addValidator',
100
+ functionName: 'claim',
101
+ args: [proofParamsObj.toViem()],
102
+ }),
103
+ abi: StakingAssetHandlerAbi,
104
+ });
105
+ dualLog(`Claim transaction hash: ${claimReceipt.transactionHash}`);
106
+ await l1Client.waitForTransactionReceipt({ hash: claimReceipt.transactionHash });
107
+
108
+ // Step 2: Approve the rollup to spend STK tokens
109
+ const stakingAssetAddress = await stakingAssetHandler.read.STAKING_ASSET();
110
+ dualLog(`Approving rollup to spend STK tokens`);
111
+ const { receipt: approveReceipt } = await l1TxUtils.sendAndMonitorTransaction({
112
+ to: stakingAssetAddress,
113
+ data: encodeFunctionData({
114
+ abi: TestERC20Abi,
115
+ functionName: 'approve',
116
+ args: [rollupAddress, maxUint256],
117
+ }),
118
+ abi: TestERC20Abi,
119
+ });
120
+ await l1Client.waitForTransactionReceipt({ hash: approveReceipt.transactionHash });
121
+
122
+ // Step 3: Deposit into the rollup to register as a validator
123
+ dualLog(`Depositing into rollup to register validator`);
124
+ const { receipt } = await l1TxUtils.sendAndMonitorTransaction({
125
+ to: rollupAddress,
126
+ data: encodeFunctionData({
127
+ abi: RollupAbi,
128
+ functionName: 'deposit',
104
129
  args: [
105
130
  attesterAddress.toString(),
106
- merkleProofArray,
107
- proofParamsObj.toViem(),
131
+ withdrawerAddress.toString(),
108
132
  registrationTuple.publicKeyInG1,
109
133
  registrationTuple.publicKeyInG2,
110
134
  registrationTuple.proofOfPossession,
135
+ false, // moveWithLatestRollup
111
136
  ],
112
137
  }),
113
- abi: StakingAssetHandlerAbi,
138
+ abi: RollupAbi,
114
139
  });
115
- dualLog(`Transaction hash: ${receipt.transactionHash}`);
140
+ dualLog(`Deposit transaction hash: ${receipt.transactionHash}`);
116
141
  await l1Client.waitForTransactionReceipt({ hash: receipt.transactionHash });
142
+
117
143
  if (isAnvilTestChain(chainId)) {
118
144
  dualLog(`Funding validator on L1`);
119
145
  const cheatCodes = new EthCheatCodes(rpcUrls, new DateProvider(), debugLogger);
@@ -168,7 +194,7 @@ export async function addL1ValidatorViaRollup({
168
194
 
169
195
  const registrationTuple = await gse.makeRegistrationTuple(blsSecretKey);
170
196
 
171
- const l1TxUtils = createL1TxUtilsFromViemWallet(l1Client, { logger: debugLogger });
197
+ const l1TxUtils = createL1TxUtils(l1Client, { logger: debugLogger });
172
198
 
173
199
  const { receipt } = await l1TxUtils.sendAndMonitorTransaction({
174
200
  to: rollupAddress.toString(),
@@ -215,7 +241,7 @@ export async function removeL1Validator({
215
241
  const account = getAccount(privateKey, mnemonic);
216
242
  const chain = createEthereumChain(rpcUrls, chainId);
217
243
  const l1Client = createExtendedL1Client(rpcUrls, account, chain.chainInfo);
218
- const l1TxUtils = createL1TxUtilsFromViemWallet(l1Client, { logger: debugLogger });
244
+ const l1TxUtils = createL1TxUtils(l1Client, { logger: debugLogger });
219
245
 
220
246
  dualLog(`Removing validator ${validatorAddress.toString()} from rollup ${rollupAddress.toString()}`);
221
247
  const { receipt } = await l1TxUtils.sendAndMonitorTransaction({
@@ -242,7 +268,7 @@ export async function pruneRollup({
242
268
  const account = getAccount(privateKey, mnemonic);
243
269
  const chain = createEthereumChain(rpcUrls, chainId);
244
270
  const l1Client = createExtendedL1Client(rpcUrls, account, chain.chainInfo);
245
- const l1TxUtils = createL1TxUtilsFromViemWallet(l1Client, { logger: debugLogger });
271
+ const l1TxUtils = createL1TxUtils(l1Client, { logger: debugLogger });
246
272
 
247
273
  dualLog(`Trying prune`);
248
274
  const { receipt } = await l1TxUtils.sendAndMonitorTransaction({
@@ -1,24 +1,48 @@
1
1
  import { createLogger } from '@aztec/aztec.js/log';
2
2
 
3
- import { mkdir, readFile, stat, writeFile } from 'fs/promises';
3
+ import { mkdir, readFile, writeFile } from 'fs/promises';
4
4
  import { dirname } from 'path';
5
5
 
6
6
  export interface CachedFetchOptions {
7
- /** Cache duration in milliseconds */
8
- cacheDurationMs: number;
9
- /** The cache file */
7
+ /** The cache file path for storing data. If not provided, no caching is performed. */
10
8
  cacheFile?: string;
9
+ /** Fallback max-age in milliseconds when server sends no Cache-Control header. Defaults to 5 minutes. */
10
+ defaultMaxAgeMs?: number;
11
+ }
12
+
13
+ /** Cache metadata stored in a sidecar .meta file alongside the data file. */
14
+ interface CacheMeta {
15
+ etag?: string;
16
+ expiresAt: number;
17
+ }
18
+
19
+ const DEFAULT_MAX_AGE_MS = 5 * 60 * 1000; // 5 minutes
20
+
21
+ /** Extracts max-age value in milliseconds from a Response's Cache-Control header. Returns undefined if not present. */
22
+ export function parseMaxAge(response: { headers: { get(name: string): string | null } }): number | undefined {
23
+ const cacheControl = response.headers.get('cache-control');
24
+ if (!cacheControl) {
25
+ return undefined;
26
+ }
27
+ const match = cacheControl.match(/max-age=(\d+)/);
28
+ if (!match) {
29
+ return undefined;
30
+ }
31
+ return parseInt(match[1], 10) * 1000;
11
32
  }
12
33
 
13
34
  /**
14
- * Fetches data from a URL with file-based caching support.
15
- * This utility can be used by both remote config and bootnodes fetching.
35
+ * Fetches data from a URL with file-based HTTP conditional caching.
36
+ *
37
+ * Data is stored as raw JSON in the cache file (same format as the server returns).
38
+ * Caching metadata (ETag, expiry) is stored in a separate sidecar `.meta` file.
39
+ * This keeps the data file human-readable and backward-compatible with older code.
16
40
  *
17
41
  * @param url - The URL to fetch from
18
- * @param networkName - Network name for cache directory structure
19
- * @param options - Caching and error handling options
20
- * @param cacheDir - Optional cache directory (defaults to no caching)
21
- * @returns The fetched and parsed JSON data, or undefined if fetch fails and throwOnError is false
42
+ * @param options - Caching options
43
+ * @param fetch - Fetch implementation (defaults to globalThis.fetch)
44
+ * @param log - Logger instance
45
+ * @returns The fetched and parsed JSON data, or undefined if fetch fails
22
46
  */
23
47
  export async function cachedFetch<T = any>(
24
48
  url: string,
@@ -26,42 +50,106 @@ export async function cachedFetch<T = any>(
26
50
  fetch = globalThis.fetch,
27
51
  log = createLogger('cached_fetch'),
28
52
  ): Promise<T | undefined> {
29
- const { cacheDurationMs, cacheFile } = options;
53
+ const { cacheFile, defaultMaxAgeMs = DEFAULT_MAX_AGE_MS } = options;
54
+
55
+ // If no cacheFile, just fetch normally without caching
56
+ if (!cacheFile) {
57
+ return fetchAndParse<T>(url, fetch, log);
58
+ }
59
+
60
+ const metaFile = cacheFile + '.meta';
30
61
 
31
- // Try to read from cache first
62
+ // Try to read metadata
63
+ let meta: CacheMeta | undefined;
32
64
  try {
33
- if (cacheFile) {
34
- const info = await stat(cacheFile);
35
- if (info.mtimeMs + cacheDurationMs > Date.now()) {
36
- const cachedData = JSON.parse(await readFile(cacheFile, 'utf-8'));
37
- return cachedData;
38
- }
39
- }
65
+ meta = JSON.parse(await readFile(metaFile, 'utf-8'));
40
66
  } catch {
41
- log.trace('Failed to read data from cache');
67
+ log.trace('No usable cache metadata found');
42
68
  }
43
69
 
70
+ // Try to read cached data
71
+ let cachedData: T | undefined;
44
72
  try {
45
- const response = await fetch(url);
73
+ cachedData = JSON.parse(await readFile(cacheFile, 'utf-8'));
74
+ } catch {
75
+ log.trace('No usable cached data found');
76
+ }
77
+
78
+ // If metadata and data exist and cache is fresh, return directly
79
+ if (meta && cachedData !== undefined && meta.expiresAt > Date.now()) {
80
+ return cachedData;
81
+ }
82
+
83
+ // Cache is stale or missing — make a (possibly conditional) request
84
+ try {
85
+ const headers: Record<string, string> = {};
86
+ if (meta?.etag && cachedData !== undefined) {
87
+ headers['If-None-Match'] = meta.etag;
88
+ }
89
+
90
+ const response = await fetch(url, { headers });
91
+
92
+ if (response.status === 304 && cachedData !== undefined) {
93
+ // Not modified — recompute expiry from new response headers and return cached data
94
+ const maxAgeMs = parseMaxAge(response) ?? defaultMaxAgeMs;
95
+ await writeMetaFile(metaFile, { etag: meta?.etag, expiresAt: Date.now() + maxAgeMs }, log);
96
+ return cachedData;
97
+ }
98
+
46
99
  if (!response.ok) {
47
100
  log.warn(`Failed to fetch from ${url}: ${response.status} ${response.statusText}`);
48
- return undefined;
101
+ return cachedData;
49
102
  }
50
103
 
51
- const data = await response.json();
104
+ // 200 — parse new data and cache it
105
+ const data = (await response.json()) as T;
106
+ const maxAgeMs = parseMaxAge(response) ?? defaultMaxAgeMs;
107
+ const etag = response.headers.get('etag') ?? undefined;
52
108
 
53
- try {
54
- if (cacheFile) {
55
- await mkdir(dirname(cacheFile), { recursive: true });
56
- await writeFile(cacheFile, JSON.stringify(data), 'utf-8');
57
- }
58
- } catch (err) {
59
- log.warn('Failed to cache data on disk: ' + cacheFile, { cacheFile, err });
60
- }
109
+ await ensureDir(cacheFile, log);
110
+ await Promise.all([
111
+ writeFile(cacheFile, JSON.stringify(data), 'utf-8'),
112
+ writeFile(metaFile, JSON.stringify({ etag, expiresAt: Date.now() + maxAgeMs }), 'utf-8'),
113
+ ]);
61
114
 
62
115
  return data;
116
+ } catch (err) {
117
+ log.warn(`Failed to fetch from ${url}`, { err });
118
+ return cachedData;
119
+ }
120
+ }
121
+
122
+ async function fetchAndParse<T>(
123
+ url: string,
124
+ fetch: typeof globalThis.fetch,
125
+ log: ReturnType<typeof createLogger>,
126
+ ): Promise<T | undefined> {
127
+ try {
128
+ const response = await fetch(url);
129
+ if (!response.ok) {
130
+ log.warn(`Failed to fetch from ${url}: ${response.status} ${response.statusText}`);
131
+ return undefined;
132
+ }
133
+ return (await response.json()) as T;
63
134
  } catch (err) {
64
135
  log.warn(`Failed to fetch from ${url}`, { err });
65
136
  return undefined;
66
137
  }
67
138
  }
139
+
140
+ async function ensureDir(filePath: string, log: ReturnType<typeof createLogger>) {
141
+ try {
142
+ await mkdir(dirname(filePath), { recursive: true });
143
+ } catch (err) {
144
+ log.warn('Failed to create cache directory for: ' + filePath, { err });
145
+ }
146
+ }
147
+
148
+ async function writeMetaFile(metaFile: string, meta: CacheMeta, log: ReturnType<typeof createLogger>) {
149
+ try {
150
+ await mkdir(dirname(metaFile), { recursive: true });
151
+ await writeFile(metaFile, JSON.stringify(meta), 'utf-8');
152
+ } catch (err) {
153
+ log.warn('Failed to write cache metadata: ' + metaFile, { err });
154
+ }
155
+ }