metascraper-logo-bimi 5.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2019 Microlink <hello@microlink.io> (microlink.io)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,128 @@
1
+ <div align="center">
2
+ <br>
3
+ <img style="width: 500px; margin:3rem 0 1.5rem;" src="https://metascraper.js.org/static/logo-banner.png" alt="metascraper">
4
+ <br>
5
+ <br>
6
+ <p align="center"><strong>metascraper-logo-bimi</strong>: Get logo property from the BIMI DNS record.</p>
7
+ <p align="center">See our <a href="https://metascraper.js.org" target='_blank' rel='noopener noreferrer'>website</a> for more information.</p>
8
+ <br>
9
+ </div>
10
+
11
+ ## Why
12
+
13
+ [BIMI](https://datatracker.ietf.org/doc/draft-blank-ietf-bimi/) is the standard behind the brand logo mailbox providers show next to an email. Domains publish it as a TXT record:
14
+
15
+ ```console
16
+ $ dig +short TXT default._bimi.microlink.io
17
+ "v=BIMI1; l=https://cdn.microlink.io/logo/logo.svg;"
18
+ ```
19
+
20
+ The specification constrains the logo to [SVG Tiny P/S](https://www.w3.org/TR/SVGTiny12/): vector, square, and transparent, which is exactly the shape a logo is expected to have.
21
+
22
+ The record is published in the domain's own DNS, so it is self asserted: the same level of trust as `og:logo`. Domains may also publish a Verified Mark Certificate under `a=`, where a certificate authority has attested the mark against the trademark owner, but this package does not read or validate it.
23
+
24
+ That makes it a higher quality source than a favicon, and it doesn't need the markup: a single DNS lookup, so it works even when the page is JavaScript rendered or unreachable.
25
+
26
+ Coverage is the trade-off. It's common among large brands and rare in the long tail, so pair it with [metascraper-logo](https://github.com/microlinkhq/metascraper/tree/master/packages/metascraper-logo) and [metascraper-logo-favicon](https://github.com/microlinkhq/metascraper/tree/master/packages/metascraper-logo-favicon).
27
+
28
+ ## Install
29
+
30
+ ```bash
31
+ $ npm install metascraper-logo-bimi --save
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ Rules are evaluated in the order the packages are declared, so put it first to prefer the BIMI published logo over anything found in the markup:
37
+
38
+ ```js
39
+ const metascraper = require('metascraper')([
40
+ require('metascraper-logo-bimi')(),
41
+ require('metascraper-logo')(),
42
+ require('metascraper-logo-favicon')()
43
+ ])
44
+ ```
45
+
46
+ The lookup is done against the registrable domain, meaning `https://blog.example.com/post` resolves `default._bimi.example.com`.
47
+
48
+ ## API
49
+
50
+ ### metascraper-logo-bimi([options])
51
+
52
+ #### options
53
+
54
+ ##### gotOpts
55
+
56
+ Type: `object`
57
+
58
+ Any option to be passed to [got](https://github.com/sindresorhus/got#options) when the logo URL is checked.
59
+
60
+ ##### keyvOpts
61
+
62
+ Type: `object`
63
+
64
+ Any option to be passed to [@keyvhq/memoize](https://github.com/microlinkhq/keyv/tree/master/packages/memoize#keyvoptions).
65
+
66
+ The resolution is memoized per domain and selector, including the absence of a record. A rejection from `resolveTxt` or `resolveLogoUrl` is not memoized: it means the logo is unknown rather than absent, so the next lookup retries it.
67
+
68
+ The default store is an in-memory map that never evicts, so a long running process scraping many domains should supply its own store, plus a `ttl` in milliseconds to bound how long a record is trusted:
69
+
70
+ ```js
71
+ const KeyvRedis = require('@keyvhq/redis')
72
+
73
+ const metascraper = require('metascraper')([
74
+ require('metascraper-logo-bimi')({
75
+ keyvOpts: {
76
+ store: new KeyvRedis('redis://localhost:6379'),
77
+ ttl: 24 * 60 * 60 * 1000
78
+ }
79
+ })
80
+ ])
81
+ ```
82
+
83
+ ##### resolveLogoUrl
84
+
85
+ Type: `function`<br>
86
+ Default: `require('metascraper-logo-bimi').resolveLogoUrl`
87
+
88
+ It determines if the logo URL published in the record is valid, returning the URL or `undefined`.
89
+
90
+ The default implementation discards anything not reachable, not served as `image/svg+xml`, or that redirects away from `https`. Its verdict is exposed as `require('metascraper-logo-bimi').toLogoUrl(response)`, so a custom implementation can reuse it over its own request.
91
+
92
+ ##### resolveTxt
93
+
94
+ Type: `function`<br>
95
+ Default: `require('dns').promises.resolveTxt`
96
+
97
+ The DNS resolver used to read the TXT record. Provide your own to run the lookup over [DNS over HTTPS](https://datatracker.ietf.org/doc/html/rfc8484), so it goes through the same egress as the rest of your traffic:
98
+
99
+ ```js
100
+ const metascraper = require('metascraper')([
101
+ require('metascraper-logo-bimi')({
102
+ resolveTxt: async hostname => {
103
+ const response = await fetch(
104
+ `https://cloudflare-dns.com/dns-query?name=${hostname}&type=TXT`,
105
+ { headers: { accept: 'application/dns-json' } }
106
+ )
107
+ const { Answer = [] } = await response.json()
108
+ return Answer.filter(({ type }) => type === 16).map(({ data }) =>
109
+ data.match(/"[^"]*"/g).map(chunk => chunk.slice(1, -1))
110
+ )
111
+ }
112
+ })
113
+ ])
114
+ ```
115
+
116
+ ##### selector
117
+
118
+ Type: `string`<br>
119
+ Default: `'default'`
120
+
121
+ The BIMI selector to query, used as `<selector>._bimi.<domain>`.
122
+
123
+ ## License
124
+
125
+ **metascraper-logo-bimi** © [Microlink](https://microlink.io), released under the [MIT](https://github.com/microlinkhq/metascraper/blob/master/LICENSE.md) License.<br>
126
+ Authored and maintained by [Microlink](https://microlink.io) with help from [contributors](https://github.com/microlinkhq/metascraper/contributors).
127
+
128
+ > [microlink.io](https://microlink.io) · GitHub [microlinkhq](https://github.com/microlinkhq) · X [@microlinkhq](https://x.com/microlinkhq)
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "metascraper-logo-bimi",
3
+ "description": "Get logo property from the BIMI DNS record",
4
+ "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-logo-bimi",
5
+ "version": "5.52.0",
6
+ "types": "src/index.d.ts",
7
+ "main": "src/index.js",
8
+ "author": {
9
+ "email": "hello@microlink.io",
10
+ "name": "microlink.io",
11
+ "url": "https://microlink.io"
12
+ },
13
+ "repository": {
14
+ "directory": "packages/metascraper-logo-bimi",
15
+ "type": "git",
16
+ "url": "git+https://github.com/microlinkhq/metascraper.git"
17
+ },
18
+ "bugs": {
19
+ "url": "https://github.com/microlinkhq/metascraper/issues"
20
+ },
21
+ "keywords": [
22
+ "bimi",
23
+ "dns",
24
+ "logo",
25
+ "metascraper"
26
+ ],
27
+ "dependencies": {
28
+ "@keyvhq/memoize": "~2.2.4",
29
+ "@metascraper/helpers": "5.52.0",
30
+ "reachable-url": "~1.8.3"
31
+ },
32
+ "devDependencies": {
33
+ "async-listen": "latest",
34
+ "ava": "8",
35
+ "got": "11"
36
+ },
37
+ "engines": {
38
+ "node": ">= 22"
39
+ },
40
+ "files": [
41
+ "src"
42
+ ],
43
+ "scripts": {
44
+ "test": "NODE_PATH=.. TZ=UTC ava --timeout 15s"
45
+ },
46
+ "license": "MIT",
47
+ "ava": {
48
+ "files": [
49
+ "test/**/*.js",
50
+ "!test/helpers.js"
51
+ ]
52
+ },
53
+ "gitHead": "15039a21cdccb414f96ed9ef2687fecaac882597"
54
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ type ResolveLogoUrl = (logoUrl: string, gotOpts?: import('got').Options) => Promise<string | undefined>
2
+
3
+ type Options = {
4
+ /**
5
+ * https://github.com/sindresorhus/got#options
6
+ */
7
+ gotOpts?: import('got').Options,
8
+
9
+ /**
10
+ * https://github.com/microlinkhq/keyv/tree/master/packages/memoize#keyvoptions
11
+ */
12
+ keyvOpts?: import('@keyvhq/core').Options<any>,
13
+
14
+ /**
15
+ * It will be used to determine if the logo URL published in the BIMI record
16
+ * is valid.
17
+ */
18
+ resolveLogoUrl?: ResolveLogoUrl,
19
+
20
+ /**
21
+ * The DNS resolver used to read the TXT record, matching the signature of
22
+ * `dns.promises.resolveTxt`. Provide your own to run over DNS over HTTPS.
23
+ * @default require('dns').promises.resolveTxt
24
+ */
25
+ resolveTxt?: (hostname: string) => Promise<string[][]>,
26
+
27
+ /**
28
+ * The BIMI selector to query, used as `<selector>._bimi.<domain>`.
29
+ * @default 'default'
30
+ */
31
+ selector?: string
32
+ }
33
+
34
+ declare function rules(options?: Options): import('metascraper').Rules;
35
+
36
+ declare namespace rules {
37
+ function createGetLogo(options?: Options): (domain: string) => Promise<string | undefined>;
38
+ function toLogoUrl(response: import('got').Response): string | undefined;
39
+
40
+ const resolveLogoUrl: ResolveLogoUrl;
41
+ }
42
+
43
+ export = rules;
package/src/index.js ADDED
@@ -0,0 +1,117 @@
1
+ 'use strict'
2
+
3
+ const memoize = require('@keyvhq/memoize')
4
+ const reachableUrl = require('reachable-url')
5
+ const dns = require('dns')
6
+
7
+ const {
8
+ logo,
9
+ mimeExtension,
10
+ parseUrl,
11
+ protocol,
12
+ toRule
13
+ } = require('@metascraper/helpers')
14
+
15
+ const toLogo = toRule(logo)
16
+
17
+ const isHttps = url => protocol(url) === 'https'
18
+
19
+ const parseTag = tag => {
20
+ const [name, ...rest] = tag.split('=')
21
+ return [name.trim().toLowerCase(), rest.join('=').trim()]
22
+ }
23
+
24
+ const parseTags = record => record.split(';').map(parseTag)
25
+
26
+ const isBimi = ([[name, value]]) =>
27
+ name === 'v' && value.toLowerCase() === 'bimi1'
28
+
29
+ /**
30
+ * `l` is https only, and an empty one is a declination: the domain opting out
31
+ * of publishing a logo. Both leave nothing to resolve.
32
+ *
33
+ * https://datatracker.ietf.org/doc/html/draft-blank-ietf-bimi-02#section-4.2
34
+ */
35
+ const toLocation = tags => {
36
+ const location = tags.find(([name]) => name === 'l')?.[1]
37
+ return isHttps(location) ? location : undefined
38
+ }
39
+
40
+ const toHostname = (domain, selector) => `${selector}._bimi.${domain}`
41
+
42
+ const NO_RECORD_CODES = new Set(['ENODATA', 'ENOTFOUND'])
43
+
44
+ /**
45
+ * Other TXT records can share the hostname, so they are discarded rather than
46
+ * read as an absent record. What remains has to be a single record, so a second
47
+ * one cannot override the first, including its declination.
48
+ *
49
+ * https://datatracker.ietf.org/doc/html/draft-blank-ietf-bimi-02#section-7.2
50
+ */
51
+ const getLocation = async (hostname, resolveTxt) => {
52
+ const answers = await resolveTxt(hostname).catch(error => {
53
+ if (NO_RECORD_CODES.has(error.code)) return []
54
+ throw error
55
+ })
56
+ const records = answers
57
+ .map(answer => parseTags(answer.join('')))
58
+ .filter(isBimi)
59
+ return records.length === 1 ? toLocation(records[0]) : undefined
60
+ }
61
+
62
+ const isSvg = ({ headers }) => mimeExtension(headers['content-type']) === 'svg'
63
+
64
+ /**
65
+ * The record location is verified to be https when parsed, but redirects are
66
+ * followed, so the URL that ends up being served has to be checked again.
67
+ */
68
+ const toLogoUrl = response =>
69
+ reachableUrl.isReachable(response) && isSvg(response) && isHttps(response.url)
70
+ ? response.url
71
+ : undefined
72
+
73
+ const defaultResolveLogoUrl = async (logoUrl, gotOpts) =>
74
+ toLogoUrl(await reachableUrl(logoUrl, gotOpts))
75
+
76
+ const createGetLogo = ({
77
+ gotOpts,
78
+ keyvOpts,
79
+ resolveLogoUrl = defaultResolveLogoUrl,
80
+ resolveTxt = dns.promises.resolveTxt,
81
+ selector = 'default'
82
+ } = {}) => {
83
+ const getLogo = async hostname => {
84
+ const location = await getLocation(hostname, resolveTxt)
85
+ if (location) return resolveLogoUrl(location, gotOpts)
86
+ }
87
+
88
+ const fn = memoize(getLogo, keyvOpts, {
89
+ value: value => (value === undefined ? null : value)
90
+ })
91
+
92
+ /**
93
+ * A rejection is never stored, so it is swallowed here instead: throwing
94
+ * would abort the whole `logo` rule chain, taking the markup rules declared
95
+ * after this one down with it.
96
+ */
97
+ return domain =>
98
+ fn(toHostname(domain, selector))
99
+ .then(value => (value === null ? undefined : value))
100
+ .catch(() => undefined)
101
+ }
102
+
103
+ module.exports = options => {
104
+ const getLogo = createGetLogo(options)
105
+
106
+ const rules = {
107
+ logo: [toLogo((_, url) => getLogo(parseUrl(url).domain))]
108
+ }
109
+
110
+ rules.pkgName = 'metascraper-logo-bimi'
111
+
112
+ return rules
113
+ }
114
+
115
+ module.exports.createGetLogo = createGetLogo
116
+ module.exports.resolveLogoUrl = defaultResolveLogoUrl
117
+ module.exports.toLogoUrl = toLogoUrl