-
-
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
Open
flakey5
wants to merge
1
commit into
main
Choose a base branch
from
flakey5/159
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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= |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ node_modules/ | |
| dist/ | ||
| .dev.vars | ||
| .sentryclirc | ||
| .env | ||
This file contains hidden or 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,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/'); | ||
| }); |
File renamed without changes.
This file contains hidden or 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
flakey5 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,3 @@ | ||
| # `lib/` | ||
|
|
||
| Utilities used in local scripts and in the deployed worker. |
This file contains hidden or 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 hidden or 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,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, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blocking till node core pr passing this input in gets made + merged