ktor icon indicating copy to clipboard operation
ktor copied to clipboard

KTOR-4715 Fix crash in browser web workers where window is undefined

Open baconz opened this issue 3 years ago • 1 comments
trafficstars

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.

baconz avatar Aug 03 '22 04:08 baconz

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?

baconz avatar Aug 09 '22 20:08 baconz

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.

rsinukov avatar Aug 11 '22 15:08 rsinukov

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.

baconz avatar Aug 16 '22 20:08 baconz