-
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(readability): use worker threads
happy-dom throw unhandled exceptions based on the target URL markup, so there is no way to don't pullate the main Node.js process.
- Loading branch information
Showing
4 changed files
with
53 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict' | ||
|
||
const { workerData, parentPort } = require('node:worker_threads') | ||
const { Readability } = require('@mozilla/readability') | ||
|
||
const parseReader = reader => { | ||
try { | ||
return reader.parse() | ||
} catch (_) { | ||
return {} | ||
} | ||
} | ||
|
||
const getDocument = ({ url, html }) => { | ||
const { Window } = require('happy-dom') | ||
const window = new Window({ url }) | ||
const document = window.document | ||
document.documentElement.innerHTML = 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))) |