favicon-url
Get the actual favicon url of a given host domain in nodejs
Project Goal
This utility was extracted out of REDPILL. The goal was to embed into the application the icon of a given host domain: ex..: google.com.
Posible solutions :
-
Every host domain url will most likely have a favicon.ico somewhere in the root so we might get away with guessing it: ex... for google.com -> http://google.com/favicon.ico -
Use the content-length property of the headers' response in a network request to the icon directly. To validate the above point. -
We could parse the host domain html structure and look for the link tag with the icon rel... etc.
The following scenarios are handled:
- 200 status code
- Other then 200
- Icon type or invalid icon type
- No data buffer present but status 200
- No content-length in the request
- Error
- Timeout of the request to abort
- Minimun Data buffer length
For all these you will either get the url or null. There will be no error handling(null if). The function only does 1 thing. Url or nothing.
Install
yarn add favicon-url
Run tests with mocha
$ yarn run test
API
const faviconUrl = require('favicon-url')
faviconUrl('google.com', {}, (favicon, dataBufferLength) => {
// console.log(favicon)
// console.log(dataBufferLength)
})
The options object
faviconUrl('google.com', {timeout: 2000, minBufferLength: 400, securedOnly: true}, (favicon, dataBufferLength) => {
// Only favicons served over https.
// console.log(favicon)
// console.log(dataBufferLength)
})
The following properties are supported:
-
timeout
(default: 8000) – The timeout of the HTTP/HTTPS request in milliseconds. -
minBufferLength
(default: 200) – sets the minimum length of the data buffer. Any favicon who's data buffer length is below will return null. -
securedOnly
(default: false) – sets theagent
option per protocol, since HTTP and HTTPS use different agents. Example value:{ securedOnly: true }
will only return favicon served over HTTPS.
Dependencies
MIT. Copyright (c) Wilkin Novo.