Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cloudflare_workers/api/wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"ai": {
"binding": "AI"
},
"version_metadata": {
"binding": "CF_VERSION_METADATA"
},
"vars": {
"ENV_NAME": "capgo_api-prod"
},
Expand Down Expand Up @@ -77,6 +80,9 @@
"ai": {
"binding": "AI"
},
"version_metadata": {
"binding": "CF_VERSION_METADATA"
},
"vars": {
"ENV_NAME": "capgo_api-preprod"
},
Expand Down Expand Up @@ -132,6 +138,9 @@
"ai": {
"binding": "AI"
},
"version_metadata": {
"binding": "CF_VERSION_METADATA"
},
"vars": {
"ENV_NAME": "capgo_api-alpha"
},
Expand Down Expand Up @@ -187,6 +196,9 @@
"ai": {
"binding": "AI"
},
"version_metadata": {
"binding": "CF_VERSION_METADATA"
},
"vars": {
"ENV_NAME": "capgo_api-local"
}
Expand Down
3 changes: 2 additions & 1 deletion supabase/functions/_backend/utils/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AnalyticsEngineDataPoint, D1Database, Hyperdrive } from '@cloudflare/workers-types'
import type { AnalyticsEngineDataPoint, D1Database, Hyperdrive, WorkerVersionMetadata } from '@cloudflare/workers-types'
import type { Context } from 'hono'
import type { DeviceComparable } from './deviceComparison.ts'
import type { Database } from './supabase.types.ts'
Expand Down Expand Up @@ -55,6 +55,7 @@ export type Bindings = {
ATTACHMENT_UPLOAD_HANDLER: DurableObjectNamespace
ATTACHMENT_BUCKET: R2Bucket
AI?: AiBinding
CF_VERSION_METADATA?: WorkerVersionMetadata
}

const TRACK_DEVICE_USAGE_CACHE_PATH = '/.track-device-usage-cache'
Expand Down
20 changes: 18 additions & 2 deletions supabase/functions/_backend/utils/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,21 @@ export const middlewareAPISecret = honoFactory.createMiddleware(async (c, next)

export const BRES = { status: 'ok' }

export function buildWorkerSourceHeaders(functionName: string, envName: string, versionMetadata?: Partial<Bindings['CF_VERSION_METADATA']>) {
const headers: Record<string, string> = {
'X-Worker-Source': `${envName || functionName}-${CapgoVersion}`,
}

if (versionMetadata?.id)
headers['X-Worker-Version-Id'] = versionMetadata.id
if (versionMetadata?.tag)
headers['X-Worker-Version-Tag'] = versionMetadata.tag
if (versionMetadata?.timestamp)
headers['X-Worker-Version-Timestamp'] = versionMetadata.timestamp

return headers
}

export function createHono(functionName: string, _version: string) {
let appGlobal
if (getRuntimeKey() === 'deno') {
Expand All @@ -221,8 +236,9 @@ export function createHono(functionName: string, _version: string) {
}
appGlobal.use('*', (c, next): Promise<any> => {
// ADD HEADER TO IDENTIFY WORKER SOURCE
const name = `${getEnv(c, 'ENV_NAME') || functionName}-${CapgoVersion}`
c.header('X-Worker-Source', name)
const headers = buildWorkerSourceHeaders(functionName, getEnv(c, 'ENV_NAME'), c.env?.CF_VERSION_METADATA)
for (const [header, value] of Object.entries(headers))
c.header(header, value)
return next()
})

Expand Down
45 changes: 45 additions & 0 deletions tests/worker-source-header.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { afterAll, describe, expect, it, vi } from 'vitest'
import { createHono } from '../supabase/functions/_backend/utils/hono.ts'
import { version } from '../supabase/functions/_backend/utils/version.ts'

describe('worker source headers', () => {
afterAll(() => {
vi.unstubAllEnvs()
})

it.concurrent('exposes the Cloudflare Worker deployment version when metadata is bound', async () => {
vi.stubEnv('ENV_NAME', 'capgo_api-prod')
const app = createHono('api', version)
app.get('/ok', c => c.json({ status: 'ok' }))

const response = await app.fetch(
new Request('http://localhost/ok'),
{
CF_VERSION_METADATA: {
id: '02af90ed-1d5a-474c-9afd-aa2eb41a14ac',
tag: 'deploy-prod',
timestamp: '2026-05-11T13:58:16.307Z',
},
},
)

expect(response.headers.get('x-worker-source')).toBe(`capgo_api-prod-${version}`)
expect(response.headers.get('x-worker-version-id')).toBe('02af90ed-1d5a-474c-9afd-aa2eb41a14ac')
expect(response.headers.get('x-worker-version-tag')).toBe('deploy-prod')
expect(response.headers.get('x-worker-version-timestamp')).toBe('2026-05-11T13:58:16.307Z')
})

it.concurrent('keeps the existing source header when version metadata is not available', async () => {
vi.stubEnv('ENV_NAME', 'capgo_api-prod')
const app = createHono('api', version)
app.get('/ok', c => c.json({ status: 'ok' }))

const response = await app.fetch(
new Request('http://localhost/ok'),
{},
)

expect(response.headers.get('x-worker-source')).toBe(`capgo_api-prod-${version}`)
expect(response.headers.get('x-worker-version-id')).toBeNull()
})
})
Loading