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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Support for service worker environments #203

Closed
dxh9845 opened this issue Jan 6, 2023 · 2 comments
Closed

Feature Request: Support for service worker environments #203

dxh9845 opened this issue Jan 6, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@dxh9845
Copy link

dxh9845 commented Jan 6, 2023

Description / Use Case for a Feature
Hello!
Congrats on a fantastic library! I wanted to propose adding support for another type of Javascript runtime environment - service workers. Currently, usage of this library in a non-Node or Browser environment causes reference errors when referring to window. By adding another environment, this logger could be used in serverless environments such as Cloudflare Workers.

I have compiled a version of the library locally that checks for the presence of the importScripts global to determine an additional "serviceworker" environment. I also added the WebWorker lib types to the tsconfig to prevent the Typescript reference error.

 const environment = () => {
      switch (true) {
        case ![typeof window, typeof document].includes("undefined"):
          return "browser"
        case ![typeof importScripts].includes("undefined"):
          return "serviceworker"
        case Object.prototype.toString.call(typeof process !== "undefined" ? process : 0) === "[object process]":
          return "nodejs"
        default:
          return "unknown"
      }
    }

    const env = environment()
    
    const isBrowser = env === "browser";
    const isNode = env === "nodejs"
...

My specific usecase is to use this library in a Cloudflare Worker, but I would implement a PR that attempts to be platform-agnostic.

Let me know your thoughts. Thanks!

@dxh9845 dxh9845 added the enhancement New feature or request label Jan 6, 2023
@terehov
Copy link
Contributor

terehov commented Jan 6, 2023

Great input and thank you!
PR is more than welcome 😊

@terehov
Copy link
Contributor

terehov commented Mar 2, 2023

Check out V4.8.0.

@terehov terehov closed this as completed Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants