Skip to content

Commit ce4536f

Browse files
Kikobeatsclaude
andauthored
fix: keep the favicon body under reachable-url v2 (#865)
v2 cancels the download once the headers answer reachability, so `body` comes back undefined whenever a server ignores `Range`. The resolver drops every favicon it probes without one, and nine tests here fail on the bump alone. `maxBody: 1` keeps the byte the check reads, and it is the byte the range request already asks for, so nothing extra is downloaded. Reading one byte narrows what an absent body means. It used to cover a transfer that stopped part way, because the whole entity was downloaded anyway; now it covers a server that announces an image and sends none of it. Keeping the wider meaning would have meant either buffering whatever a guessed URL answers with, or a size bound standing in for completion, and a favicon whose transfer wobbled is not worth either: the consumer refetches the URL regardless. `undefined if body is not present` asserts the narrower guarantee, and still fails without the check it covers. Claude-Session: https://claude.ai/code/session_018JDsM3McLV5LtR3rFd97Sb Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
1 parent 440ebd1 commit ce4536f

4 files changed

Lines changed: 12 additions & 9 deletions

File tree

‎packages/metascraper-logo-favicon/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@keyvhq/memoize": "~2.2.4",
2828
"@metascraper/helpers": "workspace:*",
2929
"lodash": "~4.18.1",
30-
"reachable-url": "~1.8.3"
30+
"reachable-url": "~2.1.0"
3131
},
3232
"devDependencies": {
3333
"async-listen": "latest",

‎packages/metascraper-logo-favicon/src/index.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ pickBiggerSize.sortBySize = collection =>
137137
orderBy(collection, ['size.priority'], ['desc'])
138138

139139
const defaultResolveFaviconUrl = async (faviconUrl, contentTypes, gotOpts) => {
140-
const response = await reachableUrl(faviconUrl, gotOpts)
140+
// One byte is what the check below reads, and it is the byte the range
141+
// request already asks for, so nothing else has to be downloaded.
142+
const response = await reachableUrl(faviconUrl, { maxBody: 1, ...gotOpts })
141143
if (!reachableUrl.isReachable(response)) return undefined
142144

143145
const contentType = response.headers['content-type']
@@ -148,7 +150,9 @@ const defaultResolveFaviconUrl = async (faviconUrl, contentTypes, gotOpts) => {
148150
return undefined
149151
}
150152

151-
// 60 is the ASCII code for '<'
153+
// An absent body is a server that announced an image and sent none of it, and
154+
// 60 is the ASCII code for '<', so the route answered with markup rather than
155+
// the image its content type claimed.
152156
if (contentTypes && (!response.body || response.body[0] === 60)) {
153157
return undefined
154158
}

‎packages/metascraper-logo-favicon/test/resolve-favicon-url.js‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const test = require('ava').default
4-
const got = require('got')
54

65
const { resolveFaviconUrl } = require('..')
76
const { runServer } = require('./helpers')
@@ -25,12 +24,12 @@ test('undefined if content type is not expected', async t => {
2524
t.is(await resolveFaviconUrl(toUrl(url, 'favicon.ico'), []), undefined)
2625
})
2726

27+
// Only the first byte is read, so what disqualifies a favicon is announcing an
28+
// image and sending none of it, rather than a transfer that stopped part way.
2829
test('undefined if body is not present', async t => {
2930
const url = await runServer(t, async ({ res }) => {
30-
const stream = got.stream(
31-
'https://cdn.microlink.io/file-examples/sample-big.jpg'
32-
)
33-
stream.pipe(res)
31+
res.writeHead(200, { 'content-type': 'image/jpeg' })
32+
res.flushHeaders()
3433
setTimeout(() => res.destroy(), 100)
3534
})
3635

‎packages/metascraper-youtube/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@metascraper/helpers": "workspace:*",
2828
"get-video-id": "3",
2929
"p-locate": "~7.0.0",
30-
"reachable-url": "~1.8.3"
30+
"reachable-url": "~2.1.0"
3131
},
3232
"devDependencies": {
3333
"ava": "8"

0 commit comments

Comments
 (0)