Skip to content

Commit 32ad76c

Browse files
committed
Do not throw on captcha protected, moved debug to helpers
1 parent 921f766 commit 32ad76c

File tree

2 files changed

+67
-67
lines changed

2 files changed

+67
-67
lines changed

package/src/helpers.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import STATIC_ICONS from "./icons"
2-
import { toLog } from "./index"
3-
import type { Icon } from "./parsers"
2+
import type { Head, Icon } from "./parsers"
43

54
export function fullpath(url: string, query: string): string {
65
if (url.startsWith("data:image/")) {
@@ -61,3 +60,43 @@ export function getIconFromList(query: string): string | undefined {
6160
}
6261
}
6362
}
63+
64+
// Loggers & debuggers
65+
66+
let canLog = false
67+
let canDebug = false
68+
let debugList: Debug = {}
69+
70+
export function initLog(val: boolean): void {
71+
canLog = val
72+
}
73+
74+
export function initDebug(val: boolean): void {
75+
debugList = {}
76+
canDebug = val
77+
}
78+
79+
export function toDebug(key: keyof Debug, value: any) {
80+
if (canDebug) {
81+
debugList[key] = value
82+
}
83+
}
84+
85+
export function toLog(...logs: string[]) {
86+
if (canLog) {
87+
logs.forEach(console.error)
88+
}
89+
}
90+
91+
export function getDebug(): Debug {
92+
return debugList
93+
}
94+
95+
export interface Debug {
96+
html?: string
97+
head?: Head
98+
metas?: string[]
99+
links?: string[]
100+
manifest?: string[]
101+
paths?: string[]
102+
}

package/src/index.ts

Lines changed: 26 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
1-
import { fullpath, getIconFromList, sortClosestToSize } from "./helpers"
1+
import { fullpath, getDebug, getIconFromList, initDebug, initLog, sortClosestToSize, toDebug, toLog } from "./helpers"
22
import { fetchHtml, fetchIcon, fetchManifest } from "./fetchers"
33
import { parseHead, parseManifest } from "./parsers"
44
import STATIC_ICONS from "./icons"
55

6-
import type { Head, Icon } from "./parsers"
6+
import type { Icon } from "./parsers"
7+
import type { Debug } from "./helpers"
78

8-
interface Options {
9+
interface MainOptions {
910
log?: true
1011
fast?: true
1112
debug?: true
1213
}
1314

14-
interface Debug {
15-
html?: string
16-
head?: Head
17-
metas?: string[]
18-
links?: string[]
19-
manifest?: string[]
20-
paths?: string[]
21-
}
22-
2315
/**
2416
* Find the best favicon for the specified query.
2517
*
@@ -85,9 +77,8 @@ export async function listAvailableFavicons(query: string): Promise<string[]> {
8577
* @returns A collection of data parsed by favicon fetcher
8678
*/
8779
export async function debugFavicon(query: string): Promise<Debug> {
88-
debugList = {}
8980
await main(query, "text", { debug: true, fast: true })
90-
return debugList
81+
return getDebug()
9182
}
9283

9384
/**
@@ -121,17 +112,17 @@ export async function faviconAsFetch(request: Request): Promise<Response> {
121112
query = url.pathname.slice(url.pathname.indexOf(`/${type}/`) + type.length + 2)
122113

123114
switch (type) {
115+
case "text": {
116+
const text = await faviconAsText(query)
117+
return new Response(text, { headers })
118+
}
119+
124120
case "blob": {
125-
const blob = await main(query, "blob", {})
121+
const blob = await faviconAsBlob(query)
126122
headers.set("Content-Type", blob.type)
127123
return new Response(blob, { headers })
128124
}
129125

130-
case "text": {
131-
const text = await main(query, "text", {})
132-
return new Response(text, { headers })
133-
}
134-
135126
case "list": {
136127
const list = await listAvailableFavicons(query)
137128
headers.set("Content-Type", "application/json")
@@ -145,8 +136,8 @@ export async function faviconAsFetch(request: Request): Promise<Response> {
145136
}
146137

147138
case "": {
148-
return new Response('No valid type: must be "blob", "text" or "list"', {
149-
status: 400,
139+
return new Response('Type must be "blob", "text", "list", or "debug"', {
140+
status: 404,
150141
})
151142
}
152143

@@ -162,11 +153,11 @@ export async function faviconAsFetch(request: Request): Promise<Response> {
162153
//
163154
//
164155

165-
async function main(query: string, as: "blob", options: Options): Promise<Blob>
166-
async function main(query: string, as: "text", options: Options): Promise<string>
167-
async function main(query: string, as: "blob" | "text", options: Options) {
168-
canLog = !!options.log
169-
canDebug = !!options.debug
156+
async function main(query: string, as: "blob", options: MainOptions): Promise<Blob>
157+
async function main(query: string, as: "text", options: MainOptions): Promise<string>
158+
async function main(query: string, as: "blob" | "text", options: MainOptions) {
159+
initLog(!!options.log)
160+
initDebug(!!options.debug)
170161

171162
const found = await createFaviconList(query)
172163
const hasOneIcon = found.length === 1
@@ -188,8 +179,6 @@ async function main(query: string, as: "blob" | "text", options: Options) {
188179
throw new Error("Fast mode. Could not find valid favicon")
189180
}
190181
}
191-
192-
throw new Error("Static icon could not load. Wrong host url ?")
193182
}
194183

195184
for (const url of found) {
@@ -231,12 +220,6 @@ async function createFaviconList(query: string): Promise<string[]> {
231220
query = redirected
232221
}
233222

234-
if (captchaProtected) {
235-
const host = new URL(query).host
236-
const ddg = `https://icons.duckduckgo.com/ip3/${host}.ico`
237-
icons.push({ href: ddg, size: 64 })
238-
}
239-
240223
if (html) {
241224
const head = parseHead(html)
242225
icons.push(...sortClosestToSize(head.icons, 144))
@@ -253,18 +236,14 @@ async function createFaviconList(query: string): Promise<string[]> {
253236
}
254237

255238
if (icons.length === 0) {
256-
icons.push(
257-
{
258-
href: `/favicon.ico`,
259-
size: -1024,
260-
touch: false,
261-
},
262-
{
263-
href: `${STATIC_ICONS.HOST}notfound.svg`,
264-
size: -2048,
265-
touch: false,
266-
},
267-
)
239+
const notfound = `${STATIC_ICONS.HOST}notfound.svg`
240+
icons.push({ href: notfound, size: -2048 })
241+
}
242+
243+
if (captchaProtected) {
244+
const host = new URL(query).host
245+
const ddg = `https://icons.duckduckgo.com/ip3/${host}.ico`
246+
icons.push({ href: ddg, size: 64 })
268247
}
269248

270249
// Step 4: Return list of href
@@ -275,21 +254,3 @@ async function createFaviconList(query: string): Promise<string[]> {
275254

276255
return fullpathIcons
277256
}
278-
279-
// Helpers
280-
281-
let canLog = false
282-
let canDebug = false
283-
let debugList: Debug = {}
284-
285-
export function toDebug(key: keyof Debug, value: any) {
286-
if (canDebug) {
287-
debugList[key] = value
288-
}
289-
}
290-
291-
export function toLog(...logs: string[]) {
292-
if (canLog) {
293-
logs.forEach(console.error)
294-
}
295-
}

0 commit comments

Comments
 (0)