This is a temporary hack to fix https://github.com/grpc/grpc-web/issues/1153, which is caused by goog.global not properly pointing to the proper global scope (e.g. window) when grpc-web is being imported as a CommonJS module (because this points to the module scope instead of actual global scope (i.e. globalThis)).
goog.Timer.defaultTimerObject is set to point togoog.global, and that's why it throws when setTimeout is called on it.
The fix
In this fix, we'll try to use globalThis when possible, and if it's not available (pre-2020 browsers, etc.), fallback to self, which should work in both browsers and workers.
Also added an interop test to verify the fix works (the new test fails without the fix)
The issue
This is a temporary hack to fix https://github.com/grpc/grpc-web/issues/1153, which is caused by
goog.global
not properly pointing to the proper global scope (e.g.window
) when grpc-web is being imported as a CommonJS module (becausethis
points to the module scope instead of actual global scope (i.e.globalThis
)).goog.Timer.defaultTimerObject
is set to point togoog.global
, and that's why it throws whensetTimeout
is called on it.The fix
globalThis
when possible, and if it's not available (pre-2020 browsers, etc.), fallback toself
, which should work in both browsers and workers.