@metascraper/helpers 5.46.11 → 5.46.15
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/package.json +7 -7
- package/{index.js → src/index.js} +1 -36
- package/src/load-iframe/index.js +18 -0
- package/src/load-iframe/worker.js +37 -0
package/package.json
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
"name": "@metascraper/helpers",
|
|
3
3
|
"description": "Collection of helper functions used by metascraper",
|
|
4
4
|
"homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-helpers",
|
|
5
|
-
"version": "5.46.
|
|
6
|
-
"main": "index.js",
|
|
5
|
+
"version": "5.46.15",
|
|
6
|
+
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
9
9
|
"name": "microlink.io",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"audio-extensions": "0.0.0",
|
|
26
|
-
"chrono-node": "~2.
|
|
26
|
+
"chrono-node": "~2.8.0",
|
|
27
27
|
"condense-whitespace": "~2.0.0",
|
|
28
28
|
"data-uri-utils": "~1.0.8",
|
|
29
29
|
"debug-logfmt": "~1.2.3",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"is-uri": "~1.2.7",
|
|
36
36
|
"iso-639-3": "~2.2.0",
|
|
37
37
|
"isostring": "0.0.1",
|
|
38
|
-
"jsdom": "~26.
|
|
38
|
+
"jsdom": "~26.1.0",
|
|
39
39
|
"lodash": "~4.17.21",
|
|
40
40
|
"memoize-one": "~6.0.0",
|
|
41
41
|
"microsoft-capitalize": "~1.0.5",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"normalize-url": "~6.1.0",
|
|
44
44
|
"re2": "~1.21.4",
|
|
45
45
|
"smartquotes": "~2.3.2",
|
|
46
|
-
"tldts": "~
|
|
46
|
+
"tldts": "~7.0.6",
|
|
47
47
|
"url-regex-safe": "~4.0.0",
|
|
48
48
|
"video-extensions": "1"
|
|
49
49
|
},
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"node": ">= 16"
|
|
56
56
|
},
|
|
57
57
|
"files": [
|
|
58
|
-
"
|
|
58
|
+
"src"
|
|
59
59
|
],
|
|
60
60
|
"scripts": {
|
|
61
61
|
"test": "NODE_PATH=.. TZ=UTC NODE_ENV=test ava --timeout 30s"
|
|
@@ -64,5 +64,5 @@
|
|
|
64
64
|
"publishConfig": {
|
|
65
65
|
"access": "public"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "8de0a054e65964895f6c7d0f424ec5c12fe84751"
|
|
68
68
|
}
|
|
@@ -5,7 +5,6 @@ const debug = require('debug-logfmt')('metascraper:find-rule')
|
|
|
5
5
|
const condenseWhitespace = require('condense-whitespace')
|
|
6
6
|
const { getExtension: mimeExtension } = require('mime')
|
|
7
7
|
const capitalize = require('microsoft-capitalize')
|
|
8
|
-
const { JSDOM, VirtualConsole } = require('jsdom')
|
|
9
8
|
const isRelativeUrl = require('is-relative-url')
|
|
10
9
|
const fileExtension = require('file-extension')
|
|
11
10
|
const _normalizeUrl = require('normalize-url')
|
|
@@ -466,40 +465,6 @@ const composeRule =
|
|
|
466
465
|
const has = value =>
|
|
467
466
|
value !== undefined && !Number.isNaN(value) && hasValues(value)
|
|
468
467
|
|
|
469
|
-
const loadIframe = (url, $, { timeout = 5000 } = {}) =>
|
|
470
|
-
new Promise(resolve => {
|
|
471
|
-
const dom = new JSDOM($.html(), {
|
|
472
|
-
url,
|
|
473
|
-
virtualConsole: new VirtualConsole(),
|
|
474
|
-
runScripts: 'dangerously',
|
|
475
|
-
resources: 'usable'
|
|
476
|
-
})
|
|
477
|
-
|
|
478
|
-
const done = (html = '') => resolve($.load(html))
|
|
479
|
-
|
|
480
|
-
const listen = (element, method, fn) =>
|
|
481
|
-
element[`${method}EventListener`]('load', fn, {
|
|
482
|
-
capture: true,
|
|
483
|
-
once: true,
|
|
484
|
-
passive: true
|
|
485
|
-
})
|
|
486
|
-
|
|
487
|
-
const iframe = dom.window.document.querySelector('iframe')
|
|
488
|
-
if (!iframe) return done()
|
|
489
|
-
|
|
490
|
-
const timer = setTimeout(() => {
|
|
491
|
-
listen(iframe, 'remove', load)
|
|
492
|
-
done()
|
|
493
|
-
}, timeout)
|
|
494
|
-
|
|
495
|
-
function load () {
|
|
496
|
-
clearTimeout(timer)
|
|
497
|
-
done(iframe.contentDocument.documentElement.outerHTML)
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
listen(iframe, 'add', load)
|
|
501
|
-
})
|
|
502
|
-
|
|
503
468
|
const getUrls = input => String(input).match(urlRegexForMatch) ?? []
|
|
504
469
|
|
|
505
470
|
module.exports = {
|
|
@@ -536,7 +501,7 @@ module.exports = {
|
|
|
536
501
|
iso6393,
|
|
537
502
|
jsonld,
|
|
538
503
|
lang,
|
|
539
|
-
loadIframe,
|
|
504
|
+
loadIframe: require('./load-iframe'),
|
|
540
505
|
logo,
|
|
541
506
|
memoizeOne,
|
|
542
507
|
mimeExtension,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { Worker } = require('worker_threads')
|
|
4
|
+
const path = require('path')
|
|
5
|
+
|
|
6
|
+
const SCRIPT_PATH = path.resolve(__dirname, 'worker.js')
|
|
7
|
+
|
|
8
|
+
module.exports = (url, $, { timeout = 5000 } = {}) => {
|
|
9
|
+
const worker = new Worker(SCRIPT_PATH, {
|
|
10
|
+
workerData: { url, html: $.html(), timeout },
|
|
11
|
+
stdout: true,
|
|
12
|
+
stderr: true
|
|
13
|
+
})
|
|
14
|
+
const { promise, resolve, reject } = Promise.withResolvers()
|
|
15
|
+
worker.on('message', html => resolve($.load(html || '')))
|
|
16
|
+
worker.on('error', reject)
|
|
17
|
+
return promise
|
|
18
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { workerData, parentPort } = require('node:worker_threads')
|
|
4
|
+
const { JSDOM, VirtualConsole } = require('jsdom')
|
|
5
|
+
|
|
6
|
+
async function main ({ url, html, timeout }) {
|
|
7
|
+
const dom = new JSDOM(html, {
|
|
8
|
+
url,
|
|
9
|
+
virtualConsole: new VirtualConsole(),
|
|
10
|
+
runScripts: 'dangerously',
|
|
11
|
+
resources: 'usable'
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
const iframe = dom.window.document.querySelector('iframe')
|
|
15
|
+
if (!iframe) return
|
|
16
|
+
|
|
17
|
+
let timeoutId
|
|
18
|
+
|
|
19
|
+
const waitForIframe = new Promise(resolve => {
|
|
20
|
+
iframe.addEventListener(
|
|
21
|
+
'load',
|
|
22
|
+
() => {
|
|
23
|
+
clearTimeout(timeoutId)
|
|
24
|
+
resolve(iframe.contentDocument.documentElement.outerHTML)
|
|
25
|
+
},
|
|
26
|
+
{ once: true }
|
|
27
|
+
)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
const timeoutReached = new Promise(
|
|
31
|
+
resolve => (timeoutId = setTimeout(resolve, timeout))
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
return Promise.race([waitForIframe, timeoutReached])
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
main(workerData).then(html => parentPort.postMessage(html))
|