<div align="center">
  <br>
 <h1> favicon-url</h1>

<h4>Get the actual favicon url of a given host domain in nodejs</h4>

[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
&
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

</div>

## Project Goal
> This utility was extracted out of [REDPILL](https://github.com/T1263/redpill).
> The goal was to embed into the application the icon of a given host domain: ex..: google.com.

>Possible solutions :

* <strike> 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 </strike>


* <strike> Use the content-length property of the headers' response in a network request to the icon directly. To validate the above point.</strike>

* <strike> We could parse the host domain html structure and look for the link tag with the icon rel... etc. </strike>



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
```javascript
const faviconUrl = require('favicon-url')

faviconUrl('google.com', {}, (favicon, dataBufferLength) => {
    // console.log(favicon)
    // console.log(dataBufferLength)
})
```
### The options object
```javascript

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 the `agent` option per protocol, since HTTP and HTTPS use different agents. Example value: `{ securedOnly: true }` will only return favicons served over HTTPS.

## Dependencies
[follow-redirects](https://github.com/olalonde/follow-redirects)

MIT. Copyright (c) [Wilkin Novo](https://github.com/T1263).