metascraper-video 5.49.23 → 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 +60 -43
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "metascraper-video",
3
3
  "description": "Get video property from HTML markup",
4
4
  "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-video",
5
- "version": "5.49.23",
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
  "video"
24
24
  ],
25
25
  "dependencies": {
26
- "@metascraper/helpers": "5.49.23",
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": "74f58f4db8e6cb26e441f1e731012e3bf8be6bd2"
50
+ "gitHead": "b656312fda1e8c1cd65b4e92cdf9713a16cfdd3b"
51
51
  }
package/src/index.js CHANGED
@@ -11,35 +11,28 @@ const {
11
11
  video
12
12
  } = require('@metascraper/helpers')
13
13
 
14
- const pReflect = require('p-reflect')
15
-
16
- const { chain, find, isEqual } = require('lodash')
17
-
18
14
  const toUrl = toRule(urlFn)
19
15
 
20
16
  const toVideo = toRule(video)
21
17
 
18
+ const getMediaType = domNode => {
19
+ const type = domNode?.attribs.type
20
+ if (!type) return
21
+ return type.split(';')[0]?.split('/')[1]
22
+ }
23
+
22
24
  const toVideoFromDom = toRule((domNodes, opts) => {
23
- const values = chain(domNodes)
24
- .map(domNode => ({
25
- src: domNode?.attribs.src,
26
- type: chain(domNode)
27
- .get('attribs.type')
28
- .split(';')
29
- .get(0)
30
- .split('/')
31
- .get(1)
32
- .value()
33
- }))
34
- .uniqWith(isEqual)
35
- .value()
36
-
37
- let result
38
- find(
39
- values,
40
- ({ src, type }) => (result = video(src, Object.assign({ type }, opts)))
41
- )
42
- return result
25
+ const seen = new Set()
26
+ for (const domNode of domNodes) {
27
+ const src = domNode?.attribs.src
28
+ const type = getMediaType(domNode)
29
+ const key = `${src}::${type}`
30
+ if (seen.has(key)) continue
31
+ seen.add(key)
32
+
33
+ const result = video(src, { type, ...opts })
34
+ if (result !== undefined) return result
35
+ }
43
36
  })
44
37
 
45
38
  const videoRules = [
@@ -77,32 +70,55 @@ const imageRules = [toUrl($ => $('video').attr('poster'))]
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
  const withIframe = (rules, getIframe) =>
81
97
  rules.concat(
82
98
  async ({ htmlDom: $, url }) => {
83
- const srcs = [
84
- ...new $('iframe[src^="http"], iframe[src^="/"]')
85
- .map((_, element) => $(element).attr('src'))
86
- .get()
87
- .map(src => normalizeUrl(url, src))
88
- ]
99
+ const srcs = $('iframe[src^="http"], iframe[src^="/"]')
100
+ .map((_, element) => $(element).attr('src'))
101
+ .get()
89
102
  if (srcs.length === 0) return
90
- return pReflect(
91
- Promise.any(
92
- srcs.map(async src => {
93
- const htmlDom = await getIframe(url, $, { src })
94
- const result = await findRule(rules, { htmlDom, url })
95
- if (!has(result)) throw TypeError('no result')
96
- return result
97
- })
98
- )
99
- ).then(({ value }) => value)
103
+ const seenSrcs = new Set()
104
+ for (const src of srcs) {
105
+ try {
106
+ const normalizedSrc = normalizeUrl(url, src)
107
+ if (!normalizedSrc) continue
108
+ if (seenSrcs.has(normalizedSrc)) continue
109
+ seenSrcs.add(normalizedSrc)
110
+
111
+ const htmlDom = await getIframe(url, $, normalizedSrc)
112
+ const result = await findRule(rules, { htmlDom, url })
113
+ if (has(result)) return result
114
+ } catch (_) {}
115
+ }
100
116
  },
101
117
  async ({ htmlDom: $, url }) => {
102
118
  const src = $('meta[name="twitter:player"]').attr('content')
103
119
  return src
104
120
  ? findRule(rules, {
105
- htmlDom: await getIframe(url, $, { src }),
121
+ htmlDom: await getIframe(url, $, src),
106
122
  url
107
123
  })
108
124
  : undefined
@@ -110,9 +126,10 @@ const withIframe = (rules, getIframe) =>
110
126
  )
111
127
 
112
128
  module.exports = ({ getIframe = _getIframe } = {}) => {
129
+ const getIframeCached = createGetIframeCached(getIframe)
113
130
  const rules = {
114
- image: withIframe(imageRules, getIframe),
115
- video: withIframe(videoRules, getIframe)
131
+ image: withIframe(imageRules, getIframeCached),
132
+ video: withIframe(videoRules, getIframeCached)
116
133
  }
117
134
 
118
135
  rules.pkgName = 'metascraper-video'