Security News
The Dark Side of Open Source
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
globalthis
Advanced tools
Package description
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.
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
core-js is a modular standard library for JavaScript, which includes polyfills for ECMAScript up to the latest standards. It provides more comprehensive polyfills than globalthis, including polyfills for `globalThis`. It is more feature-rich but also larger in size compared to the focused functionality of globalthis.
es6-shim is a shim that provides compatibility shims so that legacy JavaScript engines behave as closely as possible to ECMAScript 6 (Harmony). It includes a polyfill for `globalThis` among many other features. Like core-js, it is more extensive than globalthis but also more heavyweight.
Changelog
v1.0.3 - 2022-05-07
65891e4
node/install
instead of node/run
; use codecov
action 82f8481
npmignore
to autogenerate an npmignore file 53afc39
eslint
, @ljharb/eslint-config
, aud
, auto-changelog
, tape
03169d4
eslint
, @ljharb/eslint-config
, safe-publish-latest
, tape
4986e3e
15c4b06
eslint
, @ljharb/eslint-config
, auto-changelog
, tape
8b04a74
globalThis
should be writable 8759985
0263f0d
aud
, eslint
, tape
e88d296
prepublishOnly
script for npm 7+ c81fde6
dist
bde0c0d
79f73f8
Readme
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
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());
Simply clone the repo, npm install
, and run npm test
FAQs
ECMAScript spec-compliant polyfill/shim for `globalThis`
The npm package globalthis receives a total of 23,624,654 weekly downloads. As such, globalthis popularity was classified as popular.
We found that globalthis demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.
Security News
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.