Skip to content

Commit 0885253

Browse files
nogharttgemini-code-assist[bot]n1ru4l
authored
feat(cdn): add KV_BASE_URL for CDN worker + add compatibility layer (#7202)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Laurin Quast <[email protected]>
1 parent a4bccdf commit 0885253

File tree

9 files changed

+56
-14
lines changed

9 files changed

+56
-14
lines changed

.changeset/silly-kiwis-count.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'hive': minor
3+
---
4+
5+
Add envs for KV namespace on Cloudflare CDN worker

packages/services/cdn-worker/src/dev-polyfill.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ export const env: Env = {
1313
SENTRY_DSN: '',
1414
SENTRY_ENVIRONMENT: '',
1515
SENTRY_RELEASE: '',
16+
// eslint-disable-next-line no-process-env
17+
KV_STORAGE_BASE_URL: process.env.KV_STORAGE_BASE_URL,
1618
};

packages/services/cdn-worker/src/dev.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const artifactStorageReader = new ArtifactStorageReader(s3, null, null, null);
2727

2828
const handleRequest = createRequestHandler({
2929
isKeyValid: createIsKeyValid({
30+
kvStorageBaseUrl: env.KV_STORAGE_BASE_URL,
3031
artifactStorageReader,
3132
getCache: null,
3233
waitUntil: null,
@@ -52,6 +53,7 @@ const handleRequest = createRequestHandler({
5253

5354
const handleArtifactRequest = createArtifactRequestHandler({
5455
isKeyValid: createIsKeyValid({
56+
kvStorageBaseUrl: env.KV_STORAGE_BASE_URL,
5557
artifactStorageReader,
5658
getCache: null,
5759
waitUntil: null,

packages/services/cdn-worker/src/env.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ export type Env = {
1616
* Id of the release
1717
*/
1818
SENTRY_RELEASE: string;
19+
/**
20+
* Base URL of the KV storage, used to fetch the schema from the KV storage.
21+
* If not provided, the schema will be fetched from default KV storage value.
22+
*
23+
* @default https://key-cache.graphql-hive.com
24+
*/
25+
KV_STORAGE_BASE_URL?: string;
1926
};

packages/services/cdn-worker/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ type Env = {
4141
R2_ANALYTICS: AnalyticsEngine;
4242
S3_ANALYTICS: AnalyticsEngine;
4343
KEY_VALIDATION_ANALYTICS: AnalyticsEngine;
44+
45+
/**
46+
* Base URL of the KV storage, used to fetch the schema from the KV storage.
47+
* If not provided, the schema will be fetched from default KV storage value.
48+
*
49+
* @default https://key-cache.graphql-hive.com
50+
*/
51+
KV_STORAGE_BASE_URL?: string;
4452
};
4553

4654
const handler: ExportedHandler<Env> = {
@@ -108,6 +116,7 @@ const handler: ExportedHandler<Env> = {
108116
);
109117

110118
const isKeyValid = createIsKeyValid({
119+
kvStorageBaseUrl: env.KV_STORAGE_BASE_URL,
111120
waitUntil: p => ctx.waitUntil(p),
112121
getCache: () => caches.open('artifacts-auth'),
113122
artifactStorageReader,

packages/services/cdn-worker/src/key-validation.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type WaitUntil = (promise: Promise<void>) => void;
1111
type GetCache = () => Promise<Cache | null>;
1212

1313
type CreateKeyValidatorDeps = {
14+
kvStorageBaseUrl?: string;
1415
waitUntil: null | WaitUntil;
1516
artifactStorageReader: ArtifactStorageReader;
1617
getCache: null | GetCache;
@@ -34,6 +35,7 @@ export const createIsKeyValid =
3435
};
3536

3637
const handleLegacyCDNAccessToken = async (args: {
38+
kvStorageBaseUrl?: string;
3739
targetId: string;
3840
accessToken: string;
3941
artifactStorageReader: ArtifactStorageReader;
@@ -49,7 +51,7 @@ const handleLegacyCDNAccessToken = async (args: {
4951
if (requestCache) {
5052
const cacheKey = new Request(
5153
[
52-
'https://key-cache.graphql-hive.com',
54+
args.kvStorageBaseUrl ?? 'https://key-cache.graphql-hive.com',
5355
'legacy',
5456
args.targetId,
5557
encodeURIComponent(args.accessToken),
@@ -150,9 +152,12 @@ async function handleCDNAccessToken(
150152

151153
if (requestCache) {
152154
const cacheKey = new Request(
153-
['http://key-cache.graphql-hive.com', 'v1', targetId, encodeURIComponent(accessToken)].join(
154-
'/',
155-
),
155+
[
156+
deps.kvStorageBaseUrl ?? 'https://key-cache.graphql-hive.com',
157+
'v1',
158+
targetId,
159+
encodeURIComponent(accessToken),
160+
].join('/'),
156161
{
157162
method: 'GET',
158163
},

packages/services/server/README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ The GraphQL API for GraphQL Hive.
4545
| `S3_MIRROR_PUBLIC_URL` | No | The public URL of the S3, in case it differs from the `S3_ENDPOINT`. | `http://localhost:8083` |
4646
| `CDN_API` | No | Whether the CDN exposed via API is enabled. | `1` (enabled) or `0` (disabled) |
4747
| `CDN_API_BASE_URL` | No (Yes if `CDN_API` is set to `1`) | The public base url of the API service. | `http://localhost:8082` |
48+
| `CDN_API_KV_BASE_URL` | No (**Optional** if `CDN_API` is set to `1`) | The base URL for the KV for API Provider. Used for scenarios where we cache CDN access. | `https://key-cache.graphql-hive.com` |
4849
| `SUPERTOKENS_CONNECTION_URI` | **Yes** | The URI of the SuperTokens instance. | `http://127.0.0.1:3567` |
4950
| `SUPERTOKENS_API_KEY` | **Yes** | The API KEY of the SuperTokens instance. | `iliketurtlesandicannotlie` |
5051
| `AUTH_GITHUB` | No | Whether login via GitHub should be allowed | `1` (enabled) or `0` (disabled) |
@@ -89,12 +90,13 @@ The GraphQL API for GraphQL Hive.
8990
If you are self-hosting GraphQL Hive, you can ignore this section. It is only required for the Cloud
9091
version.
9192

92-
| Name | Required | Description | Example Value |
93-
| ------------------------- | ------------------------------------ | -------------------------------------------------------------- | --------------------------------------------------- |
94-
| `COMMERCE_ENDPOINT` | **Yes** | The endpoint of the commerce service. | `http://127.0.0.1:4012` |
95-
| `CDN_CF` | No | Whether the CDN is enabled. | `1` (enabled) or `0` (disabled) |
96-
| `CDN_CF_BASE_URL` | No (**Yes** if `CDN` is `1`) | The base URL of the cdn. | `https://cdn.graphql-hive.com` |
97-
| `HIVE_USAGE` | No | Whether usage reporting for the GraphQL API to Hive is enabled | `1` (enabled) or `0` (disabled) |
98-
| `HIVE_USAGE_TARGET` | No (**Yes** if `HIVE` is set to `1`) | The target to which the usage data should be reported | `the-guild/graphql-hive/development` |
99-
| `HIVE_USAGE_ACCESS_TOKEN` | No (**Yes** if `HIVE` is set to `1`) | The internal endpoint key. | `iliketurtles` |
100-
| `HIVE_USAGE_ENDPOINT` | No | The endpoint used for usage reporting. | `http://app.graphql-hive.com/usage` (default value) |
93+
| Name | Required | Description | Example Value |
94+
| ------------------------- | ------------------------------------ | ------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
95+
| `COMMERCE_ENDPOINT` | **Yes** | The endpoint of the commerce service. | `http://127.0.0.1:4012` |
96+
| `CDN_CF` | No | Whether the CDN is enabled. | `1` (enabled) or `0` (disabled) |
97+
| `CDN_CF_BASE_URL` | No (**Yes** if `CDN` is `1`) | The base URL of the cdn. | `https://cdn.graphql-hive.com` |
98+
| `CDN_CF_KV_BASE_URL` | No (**Optional** if `CDN` is `1`) | The base URL for the key-value store used for CDN access key validation caching when using the Cloudflare provider. | `https://key-cache.graphql-hive.com` |
99+
| `HIVE_USAGE` | No | Whether usage reporting for the GraphQL API to Hive is enabled | `1` (enabled) or `0` (disabled) |
100+
| `HIVE_USAGE_TARGET` | No (**Yes** if `HIVE` is set to `1`) | The target to which the usage data should be reported | `the-guild/graphql-hive/development` |
101+
| `HIVE_USAGE_ACCESS_TOKEN` | No (**Yes** if `HIVE` is set to `1`) | The internal endpoint key. | `iliketurtles` |
102+
| `HIVE_USAGE_ENDPOINT` | No | The endpoint used for usage reporting. | `http://app.graphql-hive.com/usage` (default value) |

packages/services/server/src/environment.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ const CdnCFModel = zod.union([
118118
zod.object({
119119
CDN_CF: zod.literal('1'),
120120
CDN_CF_BASE_URL: zod.string(),
121+
CDN_CF_KV_BASE_URL: emptyString(zod.string().url().optional()),
121122
}),
122123
]);
123124

@@ -128,6 +129,7 @@ const CdnApiModel = zod.union([
128129
zod.object({
129130
CDN_API: zod.literal('1'),
130131
CDN_API_BASE_URL: zod.string(),
132+
CDN_API_KV_BASE_URL: emptyString(zod.string().url().optional()),
131133
}),
132134
]);
133135

@@ -447,9 +449,16 @@ export const env = {
447449
cdnCf.CDN_CF === '1'
448450
? {
449451
baseUrl: cdnCf.CDN_CF_BASE_URL,
452+
kv: cdnCf.CDN_CF_KV_BASE_URL ? { baseUrl: cdnCf.CDN_CF_KV_BASE_URL } : null,
453+
}
454+
: null,
455+
api:
456+
cdnApi.CDN_API === '1'
457+
? {
458+
baseUrl: cdnApi.CDN_API_BASE_URL,
459+
kv: cdnApi.CDN_API_KV_BASE_URL ? { baseUrl: cdnApi.CDN_API_KV_BASE_URL } : null,
450460
}
451461
: null,
452-
api: cdnApi.CDN_API === '1' ? { baseUrl: cdnApi.CDN_API_BASE_URL } : null,
453462
},
454463
},
455464
s3: {

packages/services/server/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ export async function main() {
631631

632632
const artifactHandler = createArtifactRequestHandler({
633633
isKeyValid: createIsKeyValid({
634+
kvStorageBaseUrl: env.cdn.providers.api.kv?.baseUrl,
634635
artifactStorageReader,
635636
analytics: null,
636637
breadcrumb(message: string) {

0 commit comments

Comments
 (0)