Socket
Socket
Sign inDemoInstall

globalthis

Package Overview
Dependencies
10
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Protect your apps from supply chain attacks

Install

globalthis

ECMAScript spec-compliant polyfill/shim for `globalThis`

    1.0.3latest
    GitHub
    npm

Version published
Maintainers
1
Weekly downloads
24,080,168
increased by5.92%

Weekly downloads

Package description

What is globalthis?

The globalthis npm package provides a polyfill for the global `globalThis` object, which is a standard way to access the global object (e.g., `window` in browsers, `global` in Node.js) in a universal manner across different JavaScript environments. This package ensures that you can reliably use `globalThis` in environments where it might not be natively supported.

What are globalthis's main functionalities?

Polyfill for globalThis

This code snippet demonstrates how to polyfill the global `globalThis` object using the globalthis package. After requiring and invoking the package, you can reliably use `globalThis` in your code to refer to the global object.

require('globalthis')(); // This will polyfill globalThis if it doesn't exist

Other packages similar to globalthis

Changelog

Source

v1.0.3 - 2022-05-07

Commits

  • [actions] reuse common workflows 65891e4
  • [actions] use node/install instead of node/run; use codecov action 82f8481
  • [meta] use npmignore to autogenerate an npmignore file 53afc39
  • [Dev Deps] update eslint, @ljharb/eslint-config, aud, auto-changelog, tape 03169d4
  • [Dev Deps] update eslint, @ljharb/eslint-config, safe-publish-latest, tape 4986e3e
  • [actions] update codecov uploader 15c4b06
  • [Dev Deps] update eslint, @ljharb/eslint-config, auto-changelog, tape 8b04a74
  • [Fix] globalThis should be writable 8759985
  • [readme] add github actions/codecov badges 0263f0d
  • [Dev Deps] update aud, eslint, tape e88d296
  • [meta] use prepublishOnly script for npm 7+ c81fde6
  • [Tests] nycignore dist bde0c0d
  • [meta] gitignore coverage output 79f73f8

Readme

Source

globalThis Version Badge

github actions coverage dependency status dev dependency status License Downloads

npm badge

An ECMAScript spec-compliant polyfill/shim for globalThis. Invoke its "shim" method to shim globalThis if it is unavailable.

This package implements the es-shim API interface. It works in an ES3-supported environment and complies with the spec proposal.

Most common usage:

var globalThis = require('globalthis')(); // returns native globalThis if compliant
	/* or */
var globalThis = require('globalthis/polyfill')(); // returns native globalThis if compliant

Example

var assert = require('assert');

// the below function is not CSP-compliant, but reliably gets the
// global object in sloppy mode in every engine.
var getGlobal = Function('return this');

assert.equal(globalThis, getGlobal());
/* when `globalThis` is not present */
var shimmedGlobal = require('globalthis').shim();
	/* or */
var shimmedGlobal = require('globalthis/shim')();

assert.equal(shimmedGlobal, globalThis);
assert.equal(shimmedGlobal, getGlobal());
/* when `globalThis` is present */
var shimmedGlobal = require('globalthis').shim();

assert.equal(shimmedGlobal, globalThis);
assert.equal(shimmedGlobal, getGlobal());

Tests

Simply clone the repo, npm install, and run npm test

Keywords

FAQs

Last updated on 07 May 2022

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc