jsencrypt icon indicating copy to clipboard operation
jsencrypt copied to clipboard

replace window.* by self.* in order to support service worker context

Open johuder33 opened this issue 3 years ago • 3 comments

This package works perfectly well, but when you use it in a service worker context, it failed because window.* access is not support by service worker.

So in order to support this package into service worker, we should make a little change, replace any window.* usage by self.*.

So self will work for non-window contexts where this code can be executed, so with this change this package can be used into a service worker without any problem 😄 .

if window.addEventListener worry you, it is well-covered, since before adding some listener to specific events, the code is checking if the context it's actually a window one by typeof window execution.

johuder33 avatar Oct 28 '22 22:10 johuder33

A change like this really needs some automated tests around it. More specifically I would like to know if the "addEventListener" methods and things referencing the "self" object are indeed referencing the "window" object in a browser environment.

travist avatar Nov 02 '22 15:11 travist

Hello @travist , thank you for taking some time and read this PR, sure I can add some test in order to cover this change, please let me know which type of test you think is better to implement, also, the self variable will point to the main object based on the environment where the code is executed, for example, if you execute the code in a normal web environment it will point to window, but if it is executed into a Service Worker it will point to the ServiceWorkerGlobalScope, but please let me know how can I help in this.

johuder33 avatar Nov 03 '22 02:11 johuder33

I would love to have this feature as well. In principle, I see the point of automated tests, but it doesn't really seem necessary in this case. As mdn states window and self are equal in the browser context:

const w1 = window;
const w2 = self;
const w3 = window.window;
const w4 = window.self;
// w1, w2, w3, w4 all strictly equal, but only w2 will function in workers

However, I would love to help get it approved if there is anything for me to do.

MaxNoetzold avatar Feb 27 '23 10:02 MaxNoetzold