-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(utils): Modern implementation of getGlobalObject
#5809
feat(utils): Modern implementation of getGlobalObject
#5809
Conversation
I wonder if we can also clean up our usage of edit: The Node 8 test failures 😓 |
The node test failures were due to me assuming I knew better than the many hours that have been put into the core-js code 🤷♂️ The ember test failure look unrelated?
There might be some options there for cleanup but it will be much easier to revert this PR if it only contains this one change 😂 |
Yeah it's probably a flake
Yeah, let's do it in another change. We can even make a new task to track that! |
getGlobalObject
getGlobalObject
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few small suggestions for slightly easier reading, but otherwise LGTM!
(Oh, actually, one half-baked thought/question: Is there a reason we couldn't just do away with getGlobalObject()
entirely and just import GLOBAL
everywhere? I suppose we'd lose the typecasting we get from the generic (when we use it, which we don't always), but getGlobalObject<Widow>()
vs GLOBAL as Window
is kind of a wash on that score. Maybe it could be an issue with web workers?
Anyway, not saying it's necessarily better, just something to think about.)
Agree they're all much clearer. Also renamed the
I would be tempted to hide the types behind different exports to reduce the use of generics everywhere and then eventually move them to the individual runtime packages: export const GLOBAL_OBJ: SentryGlobal = /* get the global object here */;
// This would eventually end up in `@sentry/browser`
export const WINDOW = GLOBAL_OBJ as Window & SentryGlobal;
// This would eventually end up in `@sentry/node`
export const GLOBAL = GLOBAL_OBJ as NodeJs.Global & SentryGlobal;
// This would eventually end up in `@sentry/some-runtime`
export const GLOBAL_THIS = GLOBAL_OBJ as GlobalThis & SentryGlobal; This might go against some of the reasoning here, however the current typecasting with With the above code, we'd still have full runtime global object detection but the types would be be a lot more strict around different runtimes. |
Ah, right, thanks for linking to that comment of Kamil's. Yeah, that's what I meant about it possibly being an issue for web workers - exactly what he mentions about them not having |
My suggestion in that discussion was to just use However, If we have a global type that we want to use in both browser and web worker contexts, we probably should create a union type to reflect what is available in both with optional properties for what is only available in one context. Using the |
I'm a big fan of this idea, can we try experimenting with this as a next step?
I think we should generally assume that this is the case, and that either I'm going to go ahead and merge this because it'll help unblock the rest of the work we have to do here. |
This PR:
getGlobalObject
global object detection with a modern implementation taken fromcore-js
which is then modified/simplified a littleglobalThis
which was previously removed due to requiring a TypeScript version which was required in a no longer supported version of Angular (ERROR in node_modules/@sentry/browser/dist/transports/base.d.ts(31,19): error TS2503: Cannot find namespace 'globalThis'. #2978)isNodeEnv
🎉getGlobalObject