@metascraper/helpers 5.46.14 → 5.46.17

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 CHANGED
@@ -2,7 +2,7 @@
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.14",
5
+ "version": "5.46.17",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -55,7 +55,7 @@
55
55
  "node": ">= 16"
56
56
  },
57
57
  "files": [
58
- "index.js"
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": "f30cf4a7a320d4a417f635b7ddd3c4eaef7943ae"
67
+ "gitHead": "2abd3a40d4c2361328ad358cd6db674eec7c324d"
68
68
  }
@@ -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))