metascraper-logo-bimi 5.52.0 → 5.52.1

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/README.md CHANGED
@@ -49,76 +49,25 @@ The lookup is done against the registrable domain, meaning `https://blog.example
49
49
 
50
50
  ### metascraper-logo-bimi([options])
51
51
 
52
- #### options
52
+ The record is read by [bimi-url](https://github.com/kikobeats/bimi-url), and every option is passed to it: `gotOpts`, `keyvOpts`, `resolveLogoUrl`, `resolveTxt`, and `selector` are [documented there](https://github.com/kikobeats/bimi-url#api).
53
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:
54
+ Supplying your own `resolveTxt` is the common one, since `node:dns` resolves with no timeout by default:
69
55
 
70
56
  ```js
71
- const KeyvRedis = require('@keyvhq/redis')
57
+ const { Resolver } = require('dns').promises
72
58
 
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.
59
+ const resolver = new Resolver({ timeout: 2000 })
91
60
 
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
61
  const metascraper = require('metascraper')([
101
62
  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
- }
63
+ resolveTxt: hostname => resolver.resolveTxt(hostname)
112
64
  })
113
65
  ])
114
66
  ```
115
67
 
116
- ##### selector
117
-
118
- Type: `string`<br>
119
- Default: `'default'`
68
+ The same seam takes a [DNS over HTTPS](https://datatracker.ietf.org/doc/html/rfc8484) resolver, so the lookup leaves through the same egress as the rest of your traffic. [bimi-url](https://github.com/kikobeats/bimi-url#resolvetxt) has that example.
120
69
 
121
- The BIMI selector to query, used as `<selector>._bimi.<domain>`.
70
+ `createGetLogo`, `resolveLogoUrl` and `toLogoUrl` are re-exported from `bimi-url` for use outside a metascraper rule.
122
71
 
123
72
  ## License
124
73
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "metascraper-logo-bimi",
3
3
  "description": "Get logo property from the BIMI DNS record",
4
4
  "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-logo-bimi",
5
- "version": "5.52.0",
5
+ "version": "5.52.1",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "author": {
@@ -25,14 +25,11 @@
25
25
  "metascraper"
26
26
  ],
27
27
  "dependencies": {
28
- "@keyvhq/memoize": "~2.2.4",
29
28
  "@metascraper/helpers": "5.52.0",
30
- "reachable-url": "~1.8.3"
29
+ "bimi-url": "~1.0.0"
31
30
  },
32
31
  "devDependencies": {
33
- "async-listen": "latest",
34
- "ava": "8",
35
- "got": "11"
32
+ "ava": "8"
36
33
  },
37
34
  "engines": {
38
35
  "node": ">= 22"
@@ -50,5 +47,5 @@
50
47
  "!test/helpers.js"
51
48
  ]
52
49
  },
53
- "gitHead": "15039a21cdccb414f96ed9ef2687fecaac882597"
50
+ "gitHead": "e66c71fad2e6544dc276e12ca9175738cc6e0a75"
54
51
  }
package/src/index.d.ts CHANGED
@@ -1,43 +1,13 @@
1
- type ResolveLogoUrl = (logoUrl: string, gotOpts?: import('got').Options) => Promise<string | undefined>
1
+ type CreateGetLogo = typeof import('bimi-url')
2
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
- }
3
+ type Options = Parameters<CreateGetLogo>[0]
33
4
 
34
5
  declare function rules(options?: Options): import('metascraper').Rules;
35
6
 
36
7
  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;
8
+ const createGetLogo: CreateGetLogo;
9
+ const resolveLogoUrl: CreateGetLogo['resolveLogoUrl'];
10
+ const toLogoUrl: CreateGetLogo['toLogoUrl'];
41
11
  }
42
12
 
43
13
  export = rules;
package/src/index.js CHANGED
@@ -1,105 +1,10 @@
1
1
  'use strict'
2
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')
3
+ const { logo, parseUrl, toRule } = require('@metascraper/helpers')
4
+ const createGetLogo = require('bimi-url')
14
5
 
15
6
  const toLogo = toRule(logo)
16
7
 
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
8
  module.exports = options => {
104
9
  const getLogo = createGetLogo(options)
105
10
 
@@ -113,5 +18,5 @@ module.exports = options => {
113
18
  }
114
19
 
115
20
  module.exports.createGetLogo = createGetLogo
116
- module.exports.resolveLogoUrl = defaultResolveLogoUrl
117
- module.exports.toLogoUrl = toLogoUrl
21
+ module.exports.resolveLogoUrl = createGetLogo.resolveLogoUrl
22
+ module.exports.toLogoUrl = createGetLogo.toLogoUrl