Skip to content

Commit e004596

Browse files
committed
Ensure to resolve a valid logo url
1 parent 7ad710d commit e004596

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

packages/metascraper-clearbit-logo/index.js

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

33
const { URL } = require('url')
4+
const got = require('got')
45

56
const DEFAULTS = {
67
size: '128',
@@ -13,9 +14,16 @@ module.exports = opts => {
1314
opts = Object.assign({}, DEFAULTS, opts)
1415
const { size, format } = opts
1516

16-
const clearbitLogo = ({ htmlDom, meta, url: baseUrl }) => {
17-
const { hostname } = new URL(baseUrl)
18-
return `${ENDPOINT}/${hostname}?size=${size}&format=${format}`
17+
const clearbitLogo = async ({ url }) => {
18+
const { hostname } = new URL(url)
19+
const logoUrl = `${ENDPOINT}/${hostname}?size=${size}&format=${format}`
20+
21+
try {
22+
await got.head(logoUrl)
23+
return logoUrl
24+
} catch (err) {
25+
return null
26+
}
1927
}
2028

2129
return {

packages/metascraper-clearbit-logo/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"mocha": "latest",
2626
"nyc": "latest",
2727
"should": "latest",
28-
"snap-shot": "latest",
2928
"standard": "latest"
3029
},
3130
"engines": {
@@ -48,5 +47,8 @@
4847
"env": [
4948
"mocha"
5049
]
50+
},
51+
"dependencies": {
52+
"got": "~8.3.0"
5153
}
5254
}

packages/metascraper-clearbit-logo/test/index.js

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

3-
const snapshot = require('snap-shot')
3+
const should = require('should')
44

55
const metascraper = require('metascraper').load([
66
require('metascraper-author')(),
@@ -15,10 +15,17 @@ const metascraper = require('metascraper').load([
1515
])
1616

1717
describe('metascraper-clearbit-logo', () => {
18-
it('if logo is not present, fallback to clearbit logo API', async () => {
18+
it('returns when is possible resolve logo', async () => {
1919
const url = 'https://facebook.com'
2020
const html = '<div></div>'
2121
const meta = await metascraper({ html, url })
22-
snapshot(meta)
22+
should(meta.logo.indexOf('clearbit') !== -1).be.true()
23+
})
24+
25+
it('otherwise returns null', async () => {
26+
const url = 'https://lolwerhere.com'
27+
const html = '<div></div>'
28+
const meta = await metascraper({ html, url })
29+
should(meta.logo).be.null()
2330
})
2431
})

0 commit comments

Comments
 (0)