diff --git a/.github/workflows/update-links.yml b/.github/workflows/update-links.yml index 2ea4d574..1fe177ba 100644 --- a/.github/workflows/update-links.yml +++ b/.github/workflows/update-links.yml @@ -12,6 +12,11 @@ on: description: 'Node.js version (ex/ `v20.0.0`)' required: false type: string + purge-cache: + description: 'Purge all elements in the KV cache?' + type: boolean + required: false + default: false concurrency: group: update-redirect-links @@ -59,7 +64,7 @@ jobs: run: npm ci && npm update nodejs-latest-linker --save - name: Update Directory Cache - run: node scripts/handle-mainline-release.mjs && node --run format + run: node scripts/handle-mainline-release.mjs ${{ inputs.purge-cache ?? '--purge' }} && node --run format env: VERSION_INPUT: "${{ inputs.version || '' }}" CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }} diff --git a/scripts/build-directory-cache.mjs b/scripts/build-directory-cache.mjs deleted file mode 100755 index 0fd05fe0..00000000 --- a/scripts/build-directory-cache.mjs +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env node - -/** - * This is probably not the script you want to run. - * - * This script builds the directory cache from scratch, with an optional - * ability to purge the contents of the cache before doing so. - * - * This script should only be ran when there isn't already a directory cache or - * the current directory cache is corrupted in some way. For other situations, - * try `./update-directory-cache.mjs`. - * - * Usage: build-directory-cache.mjs [--purge] - * --purge: Use with caution. It will delete all elements in the cache and - * then rebuild it. - */ - -import { - DIRECTORY_CACHE_NAMESPACE_ID, - PROD_BUCKET, - RELEASE_DIR, -} from './constants.mjs'; -import { addSymlinksToDirectoryCache } from './utils/addSymlinksToDirectoryCache.mjs'; -import { getLatestVersionMapping } from './utils/getLatestVersionMapping.mjs'; -import { - createCloudflareClient, - deleteKvNamespaceKeys, - listKvNamespace, - writeKeysToKv, -} from './utils/kv.mjs'; -import { createS3Client, listR2DirectoryRecursive } from './utils/r2.mjs'; - -// Ensure all necessary environment variables are set -for (const envVar of [ - 'CLOUDFLARE_API_TOKEN', - 'S3_ACCESS_KEY_ID', - 'S3_ACCESS_KEY_SECRET', -]) { - if (!process.env[envVar]) { - throw new TypeError(`${envVar} missing from process.env`); - } -} - -// Create the clients we need to access the data -const s3Client = createS3Client( - process.env.S3_ACCESS_KEY_ID, - process.env.S3_ACCESS_KEY_SECRET -); - -const cfClient = createCloudflareClient(); - -// List the entire R2 bucket -const directories = await listR2DirectoryRecursive(s3Client, PROD_BUCKET); -console.log(`Listed ${directories.size} directories from ${PROD_BUCKET}`); - -if (!directories.get(RELEASE_DIR)) { - throw new TypeError( - `release directory ${RELEASE_DIR} missing in ${PROD_BUCKET}` - ); -} - -const latestVersions = await getLatestVersionMapping( - s3Client, - directories.get(RELEASE_DIR), - directories -); - -await addSymlinksToDirectoryCache(s3Client, directories, latestVersions); - -// Purge directory cache if wanted -if (process.argv[2] === '--purge') { - const cachedDirectoryKeys = await listKvNamespace( - cfClient, - DIRECTORY_CACHE_NAMESPACE_ID - ); - console.log(`Purging ${cachedDirectoryKeys.size} keys`); - - await deleteKvNamespaceKeys( - cfClient, - DIRECTORY_CACHE_NAMESPACE_ID, - Array.from(cachedDirectoryKeys) - ); -} - -// Update keys in the directory cache -console.log(`Writing ${directories.size} keys to directory cache...`); -await writeKeysToKv(cfClient, DIRECTORY_CACHE_NAMESPACE_ID, directories); diff --git a/scripts/handle-mainline-release.mjs b/scripts/handle-mainline-release.mjs index 4713df2f..82b7b026 100644 --- a/scripts/handle-mainline-release.mjs +++ b/scripts/handle-mainline-release.mjs @@ -4,7 +4,8 @@ * Updates the cached directory listings for paths that have symlinks for the * mainline release bucket. * - * Usage: ./handle-mainline-release.mjs + * Usage: ./handle-mainline-release.mjs [--purge] + * --purge: Delete all elements in the cache before writing the new keys. * * If no version is provided, this just updates what's in {@link ALWAYS_UPDATED_PATHS} */ @@ -18,7 +19,12 @@ import { } from './constants.mjs'; import { addSymlinksToDirectoryCache } from './utils/addSymlinksToDirectoryCache.mjs'; import { getLatestVersionMapping } from './utils/getLatestVersionMapping.mjs'; -import { createCloudflareClient, writeKeysToKv } from './utils/kv.mjs'; +import { + createCloudflareClient, + deleteKvNamespaceKeys, + listKvNamespace, + writeKeysToKv, +} from './utils/kv.mjs'; import { createS3Client, listR2DirectoryRecursive } from './utils/r2.mjs'; // Ensure all necessary environment variables are set @@ -72,6 +78,21 @@ const latestVersions = await getLatestVersionMapping( await addSymlinksToDirectoryCache(s3Client, directories, latestVersions); +// Purge directory cache if wanted +if (process.argv[2] === '--purge') { + const cachedDirectoryKeys = await listKvNamespace( + cfClient, + DIRECTORY_CACHE_NAMESPACE_ID + ); + console.log(`Purging ${cachedDirectoryKeys.size} keys`); + + await deleteKvNamespaceKeys( + cfClient, + DIRECTORY_CACHE_NAMESPACE_ID, + Array.from(cachedDirectoryKeys) + ); +} + // Update keys in the directory cache. These intentionally do not have an // expiration set since they were made with info that the worker doesn't have // when deployed.