Skip to content
Merged
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
2 changes: 1 addition & 1 deletion stats/src/fleet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function parseFleet(value: unknown, nowMs: number): PublicFleetFeed {
export async function collectFleet(env: AppEnv, nowMs = Date.now()) {
const started = new Date(nowMs).toISOString()
try {
const r=await fetch('https://cloud.ftw.energy/v1/fleet/public',{redirect:'error',headers:{accept:'application/json'},signal:AbortSignal.timeout(15000)})
const r=await fetch('https://cloud.ftw.energy/v1/fleet/public',{redirect:'manual',headers:{accept:'application/json'},signal:AbortSignal.timeout(15000)})
if (!r.ok) {await r.body?.cancel();throw new Error('fleet unavailable')}
const reader=r.body?.getReader(); if (!reader) throw new Error('empty fleet')
const parts:Uint8Array[]=[];let size=0
Expand Down
2 changes: 1 addition & 1 deletion stats/src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function parseUsage(value: unknown, nowMs: number): UsageView {
export async function collectUsage(env: AppEnv, nowMs = Date.now()): Promise<void> {
const started = new Date(nowMs).toISOString()
try {
const response = await fetch('https://cloud.ftw.energy/v1/stats', { headers: { accept: 'application/json' }, redirect: 'error', signal: AbortSignal.timeout(15_000) })
const response = await fetch('https://cloud.ftw.energy/v1/stats', { headers: { accept: 'application/json' }, redirect: 'manual', signal: AbortSignal.timeout(15_000) })
if (response.status === 503 && response.headers.get('FTW-Statistics-State') === 'disabled') {await response.body?.cancel(); await saveCollectorRun(env.DB,'cloud:usage',started,true,'awaiting policy approval');return}
if (!response.ok) { await response.body?.cancel(); throw new Error('usage source unavailable') }
const reader = response.body?.getReader()
Expand Down
19 changes: 19 additions & 0 deletions stats/test/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,3 +779,22 @@ describe('installation estimates',()=>{
await expect(collectUsage(env,now)).resolves.toBeUndefined()
})
})


describe('Cloud collector redirects in the Workers runtime', () => {
it.each([['usage', collectUsage], ['fleet', collectFleet]] as const)(
'%s uses a valid edge Request and rejects a redirect without storing it',
async (_name, collect) => {
const send = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
const request = new Request(input, init)
expect(request.redirect).toBe('manual')
return new Response(null, {status: 302, headers: {location: 'https://other.example/feed'}})
})
vi.stubGlobal('fetch', send)
await expect(collect(env)).rejects.toThrow('unavailable')
expect(send).toHaveBeenCalledTimes(1)
expect(await env.DB.prepare('SELECT COUNT(*) AS n FROM usage_snapshots').first('n')).toBe(0)
expect(await env.DB.prepare('SELECT COUNT(*) AS n FROM cloud_fleet_view').first('n')).toBe(0)
}
)
})
Loading