Skip to content

Commit

Permalink
feat(config): allow R2 bucket URLs
Browse files Browse the repository at this point in the history
- Allow config to accept URLs to R2 buckets, in addition to S3 buckets
- Force path-style S3 URLs for R2 buckets, not just for dev mode
  • Loading branch information
LoneRifle committed Feb 20, 2024
1 parent 7897bef commit ef35d7a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/app/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,17 @@ const s3BucketUrlVars = convict(s3BucketUrlSchema)
.validate({ allowed: 'strict' })
.getProperties()

const hasR2Buckets = Object.values(s3BucketUrlVars).some((url) =>
/https:\/\/\w+.r2.cloudflarestorage.com/i.test(url),

Check failure

Code scanning / CodeQL

Incomplete regular expression for hostnames High

This regular expression has an unescaped '.' before 'cloudflarestorage.com', so it might match more hosts than expected.
)

const s3 = new aws.S3({
region: basicVars.awsConfig.region,
// Unset and use default if not in development mode
// Endpoint and path style overrides are needed only in development mode for
// localstack to work.
endpoint: isDev ? s3BucketUrlVars.endPoint : undefined,
s3ForcePathStyle: isDev ? true : undefined,
// Endpoint and path style overrides are needed only in development mode
// for localstack to work, or for Cloudflare R2.
endpoint: isDev || hasR2Buckets ? s3BucketUrlVars.endPoint : undefined,
s3ForcePathStyle: isDev || hasR2Buckets ? true : undefined,
})

// using aws-sdk v3 (FRM-993)
Expand Down
7 changes: 5 additions & 2 deletions src/app/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ const validateS3BucketUrl = (
}
// Region should be specified correctly in production
const isRegionCorrect = new RegExp(`^https://s3.${region}.amazonaws.com`, 'i')
if (!isDev && !isRegionCorrect.test(val)) {
throw new Error(`region should be ${region}`)
const isR2 = new RegExp(`^https://\\w+.r2.cloudflarestorage.com`, 'i')
if (!isDev && !isRegionCorrect.test(val) && !isR2.test(val)) {
throw new Error(
`region should be ${region}, or url should be for Cloudflare R2`,
)
}
/* eslint-enable typesafe/no-throw-sync-func */
}
Expand Down

0 comments on commit ef35d7a

Please sign in to comment.