-
-
Notifications
You must be signed in to change notification settings - Fork 17
src, scripts: use kv to cache directories #756
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,30 @@ | ||
| # Your Cloudflare account tag. | ||
| # | ||
| # Needed for: | ||
| # - Directory cache scripts | ||
| CLOUDFLARE_ACCOUNT_ID= | ||
|
|
||
| # Cloudflare V4 API token. | ||
| # | ||
| # Needed for: | ||
| # - Directory cache scripts | ||
| # | ||
| # Required permissions: | ||
| # - `Workers KV Storage`: Edit | ||
| # - `Workers R2 Storage`: Read | ||
| # | ||
| # See https://developers.cloudflare.com/fundamentals/api/get-started/create-token/ | ||
| CLOUDFLARE_API_TOKEN= | ||
|
|
||
| # S3 credentials for your R2 bucket. | ||
| # | ||
| # Needed for: | ||
| # - Directory listings in the worker. | ||
| # - Directory cache scripts | ||
| # | ||
| # Required permissions: | ||
| # - `Object Read Only` | ||
| # | ||
| # See https://dash.cloudflare.com/?account=/r2/api-tokens | ||
| S3_ACCESS_KEY_ID= | ||
| S3_ACCESS_KEY_SECRET= |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ node_modules/ | |
| dist/ | ||
| .dev.vars | ||
| .sentryclirc | ||
| .env | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { env, createExecutionContext } from 'cloudflare:test'; | ||
| import { test, beforeAll, expect, vi } from 'vitest'; | ||
| import { | ||
| populateDirectoryCacheWithDevBucket, | ||
| populateR2WithDevBucket, | ||
| } from './util'; | ||
| import worker from '../src/worker'; | ||
| import type { Env } from '../src/env'; | ||
| import { CACHE_HEADERS } from '../src/constants/cache'; | ||
|
|
||
| const mockedEnv: Env = { | ||
| ...env, | ||
| ENVIRONMENT: 'e2e-tests', | ||
| CACHING: false, | ||
| LOG_ERRORS: true, | ||
| USE_KV: true, | ||
| }; | ||
|
|
||
| beforeAll(async () => { | ||
| await populateR2WithDevBucket(); | ||
| await populateDirectoryCacheWithDevBucket(); | ||
|
|
||
| vi.mock( | ||
| import('../src/constants/latestVersions.json'), | ||
| async importOriginal => { | ||
| const original = await importOriginal(); | ||
|
|
||
| // Point all `latest-` directories to one that exists in the dev bucket | ||
| Object.keys(original.default).forEach(branch => { | ||
| let updatedValue: string; | ||
| if (branch === 'node-latest.tar.gz') { | ||
| updatedValue = 'latest/node-v20.0.0.tar.gz'; | ||
| } else { | ||
| updatedValue = 'v20.0.0'; | ||
| } | ||
|
|
||
| // @ts-expect-error | ||
| original.default[branch] = updatedValue; | ||
| }); | ||
|
|
||
| return original; | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| // Ensure essential endpoints are routable | ||
| for (const path of ['/dist/', '/docs/', '/api/', '/download/', '/metrics/']) { | ||
| test(`GET \`${path}\` returns 200`, async () => { | ||
| const ctx = createExecutionContext(); | ||
|
|
||
| const res = await worker.fetch( | ||
| new Request(`https://localhost${path}`), | ||
| mockedEnv, | ||
| ctx | ||
| ); | ||
|
|
||
| // Consume body promise | ||
| await res.text(); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| }); | ||
| } | ||
|
|
||
| test('GET `/dist/unknown-directory/` returns 404', async () => { | ||
| const ctx = createExecutionContext(); | ||
|
|
||
| const res = await worker.fetch( | ||
| new Request('https://localhost/dist/unknown-directory/'), | ||
| mockedEnv, | ||
| ctx | ||
| ); | ||
|
|
||
| expect(res.status).toBe(404); | ||
| expect(res.headers.get('cache-control')).toStrictEqual(CACHE_HEADERS.failure); | ||
| expect(await res.text()).toStrictEqual('Directory not found'); | ||
| }); | ||
|
|
||
| test('GET `/dist` redirects to `/dist/`', async () => { | ||
| const ctx = createExecutionContext(); | ||
|
|
||
| const res = await worker.fetch( | ||
| new Request('https://localhost/dist'), | ||
| mockedEnv, | ||
| ctx | ||
| ); | ||
|
|
||
| expect(res.status).toBe(301); | ||
| expect(res.headers.get('location')).toStrictEqual('https://localhost/dist/'); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| import { env } from 'cloudflare:test'; | ||
| import { join } from 'node:path'; | ||
| import { inject } from 'vitest'; | ||
| import type { Env } from '../env'; | ||
| import type { Directory } from '../../vitest-setup'; | ||
| import type { Env } from '../src/env'; | ||
| import type { Directory } from '../vitest-setup'; | ||
| import { ReadDirectoryResult } from '../src/providers/provider'; | ||
|
|
||
| async function populateR2BucketDirectory(directory: Directory): Promise<void> { | ||
| const promises: Array<Promise<unknown>> = []; | ||
|
|
@@ -10,7 +12,7 @@ async function populateR2BucketDirectory(directory: Directory): Promise<void> { | |
| const file = directory.files[path]; | ||
|
|
||
| promises.push( | ||
| env.R2_BUCKET.put(path, file.contents, { | ||
| env.R2_BUCKET.put(join(directory.name, path), file.contents, { | ||
| customMetadata: { | ||
| // This is added by rclone when copying the release assets to the | ||
| // bucket. | ||
|
|
@@ -27,6 +29,40 @@ async function populateR2BucketDirectory(directory: Directory): Promise<void> { | |
| await Promise.all(promises); | ||
| } | ||
|
|
||
| async function populateDirectoryCache(directory: Directory): Promise<void> { | ||
| const cachedDirectory: ReadDirectoryResult = { | ||
| subdirectories: Object.keys(directory.subdirectories), | ||
| // @ts-expect-error this is set immediately below | ||
| files: undefined, | ||
| hasIndexHtmlFile: false, | ||
| lastModified: new Date(), | ||
| }; | ||
|
|
||
| cachedDirectory.files = Object.keys(directory.files).map(name => { | ||
| const file = directory.files[name]; | ||
|
|
||
| if (!cachedDirectory.hasIndexHtmlFile && name.match(/index.htm(?:l)$/)) { | ||
| cachedDirectory.hasIndexHtmlFile = true; | ||
| } | ||
|
|
||
| return { | ||
| name, | ||
| lastModified: new Date(file.lastModified), | ||
| size: file.size, | ||
| }; | ||
| }); | ||
|
|
||
| const promises: Array<Promise<void>> = [ | ||
| env.DIRECTORY_CACHE.put( | ||
| `${directory.name}/`, | ||
| JSON.stringify(cachedDirectory) | ||
| ), | ||
| ...Object.values(directory.subdirectories).map(populateDirectoryCache), | ||
|
Member
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. Not sure how I feel about this massive spread. Would you have alternatives perchance?
Member
Author
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. Not particularly, we could do it like const keys = Object.keys(directory.subdirectories);
const promises = new Array<Promise<void>>(keys.length + 1);
promises[0] = env.DIRECTORY_CACHE.put(
`${directory.name}/`,
JSON.stringify(cachedDirectory)
);
for (let i = 0; i < keys.length; i++) {
promises[i + 1] = populateDirectoryCache(directory.subdirectories[keys[i]]);
}but that's just avoiding a single array allocation |
||
| ]; | ||
|
|
||
| await Promise.all(promises); | ||
| } | ||
|
|
||
| /** | ||
| * Writes the contents of the dev bucket into the R2 bucket given in {@link env} | ||
| */ | ||
|
|
@@ -38,6 +74,14 @@ export async function populateR2WithDevBucket(): Promise<void> { | |
| await populateR2BucketDirectory(devBucket); | ||
| } | ||
|
|
||
| export async function populateDirectoryCacheWithDevBucket(): Promise<void> { | ||
| // Grab the contents of the dev bucket | ||
| const devBucket = inject('devBucket'); | ||
|
|
||
| // Write it to KV | ||
| await populateDirectoryCache(devBucket); | ||
| } | ||
|
|
||
| declare module 'cloudflare:test' { | ||
| // eslint-disable-next-line @typescript-eslint/no-empty-object-type | ||
| interface ProvidedEnv extends Env {} | ||
|
|
||
|
Member
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. Do we need this readme?
Member
Author
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. Imo yes since it gives context as to what the folder is there for |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # `lib/` | ||
|
|
||
| Utilities used in local scripts and in the deployed worker. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import { ListObjectsV2Command } from '@aws-sdk/client-s3'; | ||
| import { R2_RETRY_LIMIT, S3_MAX_KEYS } from './limits.mjs'; | ||
|
|
||
| /** | ||
| * List the contents of a directory in R2. | ||
| * | ||
| * @param {import('@aws-sdk/client-s3').S3Client} client | ||
| * @param {string} bucket | ||
| * @param {string | undefined} [directory=undefined] | ||
| * @param {number} retryCount | ||
| * @returns {Promise<import('../src/providers/provider.js').ReadDirectoryResult | undefined>} | ||
| */ | ||
| export async function listR2Directory( | ||
flakey5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| client, | ||
| bucket, | ||
| directory = undefined, | ||
| retryCount = R2_RETRY_LIMIT | ||
| ) { | ||
| /** | ||
| * @type {Set<string>} | ||
| */ | ||
| const subdirectories = new Set(); | ||
|
|
||
| /** | ||
| * @type {Set<import('../src/providers/provider.js').File>} | ||
| */ | ||
| const files = new Set(); | ||
|
|
||
| let hasIndexHtmlFile = false; | ||
| let directoryLastModified = new Date(0); | ||
|
|
||
| let isTruncated; | ||
| let continuationToken; | ||
| do { | ||
| /** | ||
| * @type {import('@aws-sdk/client-s3').ListObjectsV2Output | undefined} | ||
| */ | ||
| let data = undefined; | ||
|
|
||
| let retriesLeft = retryCount; | ||
| while (retriesLeft) { | ||
| try { | ||
| data = await client.send( | ||
| new ListObjectsV2Command({ | ||
| Bucket: bucket, | ||
| Delimiter: '/', | ||
| Prefix: directory, | ||
| ContinuationToken: continuationToken, | ||
| MaxKeys: S3_MAX_KEYS, | ||
| }) | ||
| ); | ||
|
|
||
| break; | ||
| } catch (err) { | ||
| retriesLeft--; | ||
|
|
||
| if (retriesLeft === 0) { | ||
| throw new Error('exhausted R2 retries', { cause: err }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!data) { | ||
| return undefined; | ||
| } | ||
|
|
||
| isTruncated = data.IsTruncated; | ||
| continuationToken = data.NextContinuationToken; | ||
|
|
||
| data.CommonPrefixes?.forEach(subdirectory => { | ||
| if (subdirectory.Prefix) { | ||
| subdirectories.add( | ||
| subdirectory.Prefix.substring(directory?.length ?? 0) | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| data.Contents?.forEach(file => { | ||
| if (!file.Key) { | ||
| return; | ||
| } | ||
|
|
||
| if (!hasIndexHtmlFile && file.Key.match(/index.htm(?:l)$/)) { | ||
| hasIndexHtmlFile = true; | ||
| } | ||
|
|
||
| files.add({ | ||
| name: file.Key.substring(directory?.length ?? 0), | ||
| lastModified: file.LastModified, | ||
| size: file.Size, | ||
| }); | ||
|
|
||
| // Set the directory's last modified date to be the same as the most | ||
| // recently updated file | ||
| if (file.LastModified > directoryLastModified) { | ||
| directoryLastModified = file.LastModified; | ||
| } | ||
| }); | ||
| } while (isTruncated); | ||
|
|
||
| if (subdirectories.size === 0 && files.size === 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return { | ||
| subdirectories: Array.from(subdirectories), | ||
| hasIndexHtmlFile, | ||
| files: Array.from(files), | ||
| lastModified: directoryLastModified, | ||
| }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.