Skip to content

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
@gitbrent
Copy link
Owner

Other changes to the codebase for v4.0.0 have resolved this issue.

Also, there is a brand new WebWorker test under /demos/ now.

Thanks @ghaccenta for submitting this! Please test with the v4.0.0-rc if you can.

@gitbrent gitbrent closed this Apr 27, 2025
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