Skip to content
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

Fix in order to make images compatible with web workers #1354

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ghaccenta
Copy link

@ghaccenta ghaccenta commented Sep 27, 2024

The code meant to detect nodejs in gen-media.ts and pptxgen.ts is mistaking web workers for nodejs and crashes.

export function encodeSlideMediaRels (layout: PresSlide | SlideLayout): Array<Promise<string>> {
    const fs = typeof require !== 'undefined' && typeof window === 'undefined' ? require('fs') : null // NodeJS
    const https = typeof require !== 'undefined' && typeof window === 'undefined' ? require('https') : null // NodeJS

.....

require('fs') returns a Proxy in the context of a web worker which happens to be true-ish in the tests that are following the code snippet above. The code then tries to use fs, which obviously fails and shouldn't be used in the context of a web browser.

This pull request adds a new check to differentiate web workers from the rest of the execution :

let isWorker = false
if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
	isWorker = true
}

I have also added the "webworker" lib in tsconfig.json.

Unfortunately only PNG images work, due to how SVGs are rendered (similar to node).
Function writeFile() does not work either with web workers, users should use the write() function instead.

@gitbrent gitbrent self-assigned this Oct 27, 2024
@gitbrent gitbrent added this to the 3.13.0 milestone Oct 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants