ktor
ktor copied to clipboard
KTOR-4715 Fix crash in browser web workers where window is undefined
Subsystem Clients
Motivation
In web workers window will be undefined, and window === undefined will crash because window is not dynamic.
Solution
I'm sure there is a more elegant way to accomplish this, but it seems like the best solution for now might be to just execute raw JS to check for undefined.
It seems like in order to write a good test for this we'd need a way to run the test in a JS runtime. I didn't see a way to do that based on the existing tests. I could just mock js(), I suppose. WDYT?
I see that there is similar code in CryptoJS.kt file, can you update it as well, please?
Yes, creating test is not easy here, you need to mess with gradle config to use worker. I'll experiment with it, otherwise we can ignore the test for this PR since its pretty self-explanatory.
It looks like the code in CryptoJs.kt is already doing the right thing:
js("(window ? (window.crypto ? window.crypto : window.msCrypto) : self.crypto)")
In a worker self.crypto should exist, and behave the same way as window.crypto. The issue would only arise if that was kotlin code.