Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider Updating 'window' to 'globalThis' #218

Closed
justin0mcateer opened this issue Mar 1, 2023 · 2 comments
Closed

Consider Updating 'window' to 'globalThis' #218

justin0mcateer opened this issue Mar 1, 2023 · 2 comments

Comments

@justin0mcateer
Copy link

Hi! 馃憢

Firstly, thanks for your work on this project! 馃檪

Today I used patch-package to patch tslog@4.7.5 for the project I'm working on.

'window' does not work on WebWorkers, 'globalThis' has been available pretty much everywhere (browsers AND Node) for about four years.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis#browser_compatibility
https://caniuse.com/?search=globalThis

Here is the diff that solved my problem:

diff --git a/node_modules/tslog/dist/esm/runtime/browser/index.js b/node_modules/tslog/dist/esm/runtime/browser/index.js
index adae196..b33903d 100644
--- a/node_modules/tslog/dist/esm/runtime/browser/index.js
+++ b/node_modules/tslog/dist/esm/runtime/browser/index.js
@@ -3,7 +3,7 @@ import { formatWithOptions } from "./util.inspect.polyfil.js";
 import { jsonStringifyRecursive } from "./helper.jsonStringifyRecursive.js";
 const meta = {
     runtime: "Browser",
-    browser: window?.["navigator"].userAgent,
+    browser: globalThis?.["navigator"].userAgent,
 };
 const pathRegex = /(?:(?:file|https?|global code|[^@]+)@)?(?:file:)?((?:\/[^:/]+){2,})(?::(\d+))?(?::(\d+))?/;
 export function getMeta(logLevelId, logLevelName, stackDepthLevel, hideLogPositionForPerformance, name, parentNames) {
@@ -29,7 +29,7 @@ export function getErrorTrace(error) {
     }, []);
 }
 function stackLineToStackFrame(line) {
-    const href = window.location.origin;
+    const href = globalThis.location.origin;
     const pathResult = {
         fullFilePath: undefined,
         fileName: undefined,

This issue body was partially generated by patch-package.

@terehov
Copy link
Contributor

terehov commented Mar 2, 2023

Nice one! I must have missed this and caniuse looks promising:
https://caniuse.com/?search=globalThis

Thank you.

@terehov terehov closed this as completed in 7018919 Mar 2, 2023
@terehov
Copy link
Contributor

terehov commented Mar 2, 2023

Check out V4.8.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants