forked from microlinkhq/metascraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
34 lines (28 loc) · 846 Bytes
/
worker.js
File metadata and controls
34 lines (28 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict'
const { workerData, parentPort } = require('node:worker_threads')
const { Readability } = require('@mozilla/readability')
const parseReader = reader => {
try {
return reader.parse()
} catch (_) {
return {}
}
}
const errorCapture =
process.env.NODE_ENV === 'test' ? 'tryAndCatch' : 'processLevel'
const getDocument = ({ url, html }) => {
const { Window } = require('happy-dom')
const window = new Window({
url,
settings: { errorCapture }
})
const document = window.document
document.write(html)
return document
}
const main = async ({ url, html, readabilityOpts } = {}) => {
const document = getDocument({ url, html })
const reader = new Readability(document, readabilityOpts)
return parseReader(reader)
}
main(workerData).then(result => parentPort.postMessage(JSON.stringify(result)))