-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Web Worker Support #242
base: master
Are you sure you want to change the base?
Web Worker Support #242
Conversation
Thank you very much @wSedlacek for such a thoughtful analysis of solutions and choosing the best one for your PR! |
@wSedlacek I'm having hard times importing isomorphic-dompurify from inside of a Web Worker script. I don't have much experience with web workers. Could you please instruct me how to do it? I'm using Vite vanilla template and importing this way In my package.json:
|
I’ll create an example today for testing and get back to you. |
So Update: import createDOMPurify from "isomorphic-dompurify";
import { parseHTML } from "linkedom";
const { window } = parseHTML("<!DOCTYPE html><body>");
const DOMPurify = createDOMPurify(window);
self.onmessage = (event) => {
const purified = DOMPurify.sanitize(event.data);
console.log(`On Worker Thread: ${purified}`); // Should be <img src="x”> but is <img src=x onerror=alert(1)//>
}; The only issue is that while it does run, it doesn’t actually santaize anything at all. |
Thank you @wSedlacek for your work on this issue! Really great job! However, I think we cannot release this change without |
Current Behavior
When importing into a worker environment the following runtime error occurs
Change in behavior
The runtime error does not occur and
DOMPurify
is accessible both as a exportand on
self.DOMPurify
/globalThis.DOMPurify
in workersDeveloper Notes
There are probably a dozen different ways to fix this issue, the one chosen was
picked for being the one with the fewest number of changes while being
backwards compatible all the way back to browser versions from 2012 (Firefox
back to 2004)
See: https://developer.mozilla.org/en-US/docs/Web/API/Window/self#examples
Other approaches considered
typeof window !== "unefined"
window
exists and if this code isexecuting in a worker was considered however after this check was done the
split in exuection after this was detected would have either needed to use
self
or pollyfilwindow
onself
. Sinceself
always works it seemedmore straight forward to just always use it instead of
window
Example:
globalThis
browser however it is a much newer feature and not available on all platforms
(ie, React Native, etc.)
Example:
typeof window !== "undefined"
but not assigning toself.DOMPurify
window
in worker environmentseffectively killing the polyfil nature of this in worker environments but
still allowing the import
Example:
DOMPurify
accessible onself
/globalThis
as the previous method however it would rely on a featureonly aviable in newer JavaScript environments.
Example:
fixes: #240