feat(snapshot): cover-image-HEAD-probe-modul
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
4b2c157938
commit
2af44035b8
|
|
@ -0,0 +1,23 @@
|
|||
export interface ProbeResult {
|
||||
reachable: boolean
|
||||
status: number
|
||||
}
|
||||
|
||||
export type HeadFetcher = (url: string) => Promise<{ ok: boolean; status: number }>
|
||||
|
||||
export const defaultHeadFetcher: HeadFetcher = async (url) => {
|
||||
const resp = await fetch(url, { method: 'HEAD' })
|
||||
return { ok: resp.ok, status: resp.status }
|
||||
}
|
||||
|
||||
export async function probeCover(
|
||||
url: string,
|
||||
fetcher: HeadFetcher = defaultHeadFetcher,
|
||||
): Promise<ProbeResult> {
|
||||
try {
|
||||
const r = await fetcher(url)
|
||||
return { reachable: r.ok, status: r.status }
|
||||
} catch {
|
||||
return { reachable: false, status: 0 }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
import { assertEquals } from '@std/assert'
|
||||
import { probeCover, type HeadFetcher } from '../src/core/cover-probe.ts'
|
||||
|
||||
Deno.test('probeCover: 200 -> reachable=true', async () => {
|
||||
const fetcher: HeadFetcher = async () => ({ ok: true, status: 200 })
|
||||
const r = await probeCover('https://blossom.example/abc.jpg', fetcher)
|
||||
assertEquals(r, { reachable: true, status: 200 })
|
||||
})
|
||||
|
||||
Deno.test('probeCover: 404 -> reachable=false', async () => {
|
||||
const fetcher: HeadFetcher = async () => ({ ok: false, status: 404 })
|
||||
const r = await probeCover('https://blossom.example/abc.jpg', fetcher)
|
||||
assertEquals(r, { reachable: false, status: 404 })
|
||||
})
|
||||
|
||||
Deno.test('probeCover: network error -> reachable=false', async () => {
|
||||
const fetcher: HeadFetcher = async () => {
|
||||
throw new Error('ECONNREFUSED')
|
||||
}
|
||||
const r = await probeCover('https://blossom.example/abc.jpg', fetcher)
|
||||
assertEquals(r, { reachable: false, status: 0 })
|
||||
})
|
||||
Loading…
Reference in New Issue