|
| 1 | +#!/usr/bin/env node |
| 2 | +// @ts-check |
| 3 | + |
| 4 | +/** |
| 5 | + * This is probably not the script you want to run. |
| 6 | + * |
| 7 | + * This script builds the directory cache from scratch, which uses a _lot_ of |
| 8 | + * memory and a noteable amount of time when ran against the dist-prod bucket. |
| 9 | + * |
| 10 | + * This script should only be ran when there isn't already a directory cache, |
| 11 | + * for other scenarios try `./update-directory-cache.mjs`. |
| 12 | + * |
| 13 | + * Usage: build-directory-cache.mjs [--purge] |
| 14 | + * --purge: Use with caution. It will delete all elements in the cache and |
| 15 | + * then rebuild it. |
| 16 | + */ |
| 17 | + |
| 18 | +import { |
| 19 | + DIRECTORY_CACHE_NAMESPACE_ID, |
| 20 | + PROD_BUCKET, |
| 21 | + RELEASE_DIR, |
| 22 | +} from './constants.mjs'; |
| 23 | +import { |
| 24 | + addSymlinksToDirectoryCache, |
| 25 | + createCloudflareClient, |
| 26 | + createS3Client, |
| 27 | + deleteKvNamespaceKeys, |
| 28 | + getLatestVersionMapping, |
| 29 | + handleReleaseDirectory, |
| 30 | + listKvNamespace, |
| 31 | + listR2DirectoryRecursive, |
| 32 | + writeKeysToKv, |
| 33 | +} from './utils.mjs'; |
| 34 | + |
| 35 | +// Ensure all necessary environment variables are set |
| 36 | +for (const envVar of [ |
| 37 | + // 'CLOUDFLARE_API_TOKEN', |
| 38 | + 'CF_ACCESS_KEY_ID', |
| 39 | + 'CF_SECRET_ACCESS_KEY', |
| 40 | +]) { |
| 41 | + if (!process.env[envVar]) { |
| 42 | + throw new TypeError(`${envVar} missing from process.env`); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +const s3Client = createS3Client( |
| 47 | + process.env.CF_ACCESS_KEY_ID, |
| 48 | + process.env.CF_SECRET_ACCESS_KEY |
| 49 | +); |
| 50 | + |
| 51 | +const cfClient = createCloudflareClient(); |
| 52 | + |
| 53 | +// List the entire R2 bucket |
| 54 | +const directories = await listR2DirectoryRecursive(s3Client, PROD_BUCKET); |
| 55 | +console.log(`Listed ${directories.size} directories from ${PROD_BUCKET}`); |
| 56 | + |
| 57 | +const latestVersions = await getLatestVersionMapping( |
| 58 | + s3Client, |
| 59 | + PROD_BUCKET, |
| 60 | + directories.get(RELEASE_DIR), |
| 61 | + directories |
| 62 | +); |
| 63 | + |
| 64 | +await handleReleaseDirectory(s3Client, directories, latestVersions); |
| 65 | + |
| 66 | +await addSymlinksToDirectoryCache( |
| 67 | + s3Client, |
| 68 | + directories, |
| 69 | + latestVersions['latest'] |
| 70 | +); |
| 71 | + |
| 72 | +// Purge directory cache if wanted |
| 73 | +if (process.argv[2] === '--purge') { |
| 74 | + const cachedDirectoryKeys = await listKvNamespace( |
| 75 | + cfClient, |
| 76 | + DIRECTORY_CACHE_NAMESPACE_ID |
| 77 | + ); |
| 78 | + console.log( |
| 79 | + `Listed ${cachedDirectoryKeys.size} keys from directory cache to purge` |
| 80 | + ); |
| 81 | + |
| 82 | + await deleteKvNamespaceKeys( |
| 83 | + cfClient, |
| 84 | + DIRECTORY_CACHE_NAMESPACE_ID, |
| 85 | + Array.from(cachedDirectoryKeys) |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +// Update keys in the directory cache |
| 90 | +console.log(`Writing ${directories.size} keys to directory cache...`); |
| 91 | +await writeKeysToKv(cfClient, DIRECTORY_CACHE_NAMESPACE_ID, directories); |
0 commit comments