metascraper-audio 5.49.24 → 5.49.25

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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/src/index.js +61 -44
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "metascraper-audio",
3
3
  "description": "Get audio property from HTML markup",
4
4
  "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-audio",
5
- "version": "5.49.24",
5
+ "version": "5.49.25",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "author": {
@@ -23,7 +23,7 @@
23
23
  "metascraper"
24
24
  ],
25
25
  "dependencies": {
26
- "@metascraper/helpers": "5.49.24",
26
+ "@metascraper/helpers": "5.49.25",
27
27
  "lodash": "~4.17.23",
28
28
  "p-reflect": "~2.1.0"
29
29
  },
@@ -32,7 +32,7 @@
32
32
  "ava": "5"
33
33
  },
34
34
  "engines": {
35
- "node": ">= 16"
35
+ "node": ">= 22"
36
36
  },
37
37
  "files": [
38
38
  "src"
@@ -47,5 +47,5 @@
47
47
  "!test/helpers.js"
48
48
  ]
49
49
  },
50
- "gitHead": "4498fc7634a83a772379ab474f66abf4de1d1fa7"
50
+ "gitHead": "b656312fda1e8c1cd65b4e92cdf9713a16cfdd3b"
51
51
  }
package/src/index.js CHANGED
@@ -11,36 +11,29 @@ const {
11
11
  toRule
12
12
  } = require('@metascraper/helpers')
13
13
 
14
- const { find, chain, isEqual } = require('lodash')
15
- const pReflect = require('p-reflect')
16
-
17
14
  const toAudio = toRule(audio)
18
15
 
16
+ const getMediaType = domNode => {
17
+ const type = domNode?.attribs.type
18
+ if (!type) return
19
+ const mediaType = type.split(';')[0]?.split('/')[1]
20
+ if (!mediaType) return
21
+ if (mediaType === 'mpeg' || mediaType === 'mp4') return 'mp3'
22
+ return mediaType
23
+ }
24
+
19
25
  const toAudioFromDom = toRule((domNodes, opts) => {
20
- const values = chain(domNodes)
21
- .map(domNode => ({
22
- src: domNode?.attribs.src,
23
- type: chain(domNode)
24
- .get('attribs.type')
25
- .split(';')
26
- .get(0)
27
- .split('/')
28
- .get(1)
29
- // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
30
- .replace('mpeg', 'mp3')
31
- /* mp4 is commonly used for video */
32
- .replace('mp4', 'mp3')
33
- .value()
34
- }))
35
- .uniqWith(isEqual)
36
- .value()
37
-
38
- let result
39
- find(
40
- values,
41
- ({ src, type }) => (result = audio(src, Object.assign({ type }, opts)))
42
- )
43
- return result
26
+ const seen = new Set()
27
+ for (const domNode of domNodes) {
28
+ const src = domNode?.attribs.src
29
+ const type = getMediaType(domNode)
30
+ const key = `${src}::${type}`
31
+ if (seen.has(key)) continue
32
+ seen.add(key)
33
+
34
+ const result = audio(src, { type, ...opts })
35
+ if (result !== undefined) return result
36
+ }
44
37
  })
45
38
 
46
39
  const audioRules = [
@@ -77,33 +70,57 @@ const audioRules = [
77
70
  const _getIframe = (url, $, { src }) =>
78
71
  loadIframe(url, $.load(`<iframe src="${src}"></iframe>`))
79
72
 
73
+ const createGetIframeCached = getIframe => {
74
+ const cacheByHtmlDom = new WeakMap()
75
+
76
+ return async (url, $, src) => {
77
+ let cacheBySrc = cacheByHtmlDom.get($)
78
+ if (!cacheBySrc) {
79
+ cacheBySrc = new Map()
80
+ cacheByHtmlDom.set($, cacheBySrc)
81
+ }
82
+
83
+ const cachedHtmlDom = cacheBySrc.get(src)
84
+ if (cachedHtmlDom) return cachedHtmlDom
85
+
86
+ const pendingHtmlDom = getIframe(url, $, { src }).catch(error => {
87
+ cacheBySrc.delete(src)
88
+ throw error
89
+ })
90
+
91
+ cacheBySrc.set(src, pendingHtmlDom)
92
+ return pendingHtmlDom
93
+ }
94
+ }
95
+
80
96
  module.exports = ({ getIframe = _getIframe } = {}) => {
97
+ const getIframeCached = createGetIframeCached(getIframe)
81
98
  const rules = {
82
99
  audio: audioRules.concat(
83
100
  async ({ htmlDom: $, url }) => {
84
- const srcs = [
85
- ...new $('iframe[src^="http"], iframe[src^="/"]')
86
- .map((_, element) => $(element).attr('src'))
87
- .get()
88
- .map(src => normalizeUrl(url, src))
89
- ]
101
+ const srcs = $('iframe[src^="http"], iframe[src^="/"]')
102
+ .map((_, element) => $(element).attr('src'))
103
+ .get()
90
104
  if (srcs.length === 0) return
91
- return pReflect(
92
- Promise.any(
93
- srcs.map(async src => {
94
- const htmlDom = await getIframe(url, $, { src })
95
- const result = await findRule(audioRules, { htmlDom, url })
96
- if (!has(result)) throw TypeError('no result')
97
- return result
98
- })
99
- )
100
- ).then(({ value }) => value)
105
+ const seenSrcs = new Set()
106
+ for (const src of srcs) {
107
+ try {
108
+ const normalizedSrc = normalizeUrl(url, src)
109
+ if (!normalizedSrc) continue
110
+ if (seenSrcs.has(normalizedSrc)) continue
111
+ seenSrcs.add(normalizedSrc)
112
+
113
+ const htmlDom = await getIframeCached(url, $, normalizedSrc)
114
+ const result = await findRule(audioRules, { htmlDom, url })
115
+ if (has(result)) return result
116
+ } catch (_) {}
117
+ }
101
118
  },
102
119
  async ({ htmlDom: $, url }) => {
103
120
  const src = $('meta[name="twitter:player"]').attr('content')
104
121
  return src
105
122
  ? findRule(audioRules, {
106
- htmlDom: await getIframe(url, $, { src }),
123
+ htmlDom: await getIframeCached(url, $, src),
107
124
  url
108
125
  })
109
126
  : undefined