Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
event-target-polyfill
Advanced tools
The event-target-polyfill npm package provides a polyfill for the EventTarget interface, which is a standard interface implemented by objects that can receive events and may have listeners for them. This is particularly useful for environments that do not natively support EventTarget, such as older browsers or certain JavaScript environments.
Basic EventTarget Implementation
This code demonstrates the basic usage of the EventTarget polyfill. It shows how to create an EventTarget instance, add an event listener, dispatch an event, and remove the event listener.
const { EventTarget, Event } = require('event-target-polyfill');
const target = new EventTarget();
const listener = (event) => {
console.log('Event received:', event.type);
};
target.addEventListener('test', listener);
target.dispatchEvent(new Event('test'));
target.removeEventListener('test', listener);
Custom Event Handling
This example shows how to create and dispatch a custom event with additional data using the EventTarget polyfill. It demonstrates extending the Event class to include custom properties.
const { EventTarget, Event } = require('event-target-polyfill');
class MyCustomEvent extends Event {
constructor(type, customData) {
super(type);
this.customData = customData;
}
}
const target = new EventTarget();
target.addEventListener('custom', (event) => {
console.log('Custom event received with data:', event.customData);
});
target.dispatchEvent(new MyCustomEvent('custom', { key: 'value' }));
EventEmitter3 is a high-performance event emitter for Node.js and the browser. It provides a similar event handling mechanism but does not follow the EventTarget interface. Instead, it uses its own API for adding, removing, and emitting events.
Mitt is a tiny functional event emitter. It provides a simple and minimalistic API for event handling, similar to EventTarget but with a smaller footprint and fewer features. It is ideal for lightweight applications where a full EventTarget implementation is not necessary.
A polyfill for EventTarget
(and Event
), meant to run in older version of node or possibly IE 11, that has the most accurate set of characteristics of EventTarget
that can be provided.
If you find this implementation can be improved, please submit a PR and ping me on Twitter via DM.
NOTE: If you are using Node 14 and higher, EventTarget is available directly via experimental features MDN: EventTarget
import 'event-target-polyfill';
const et = new EventTarget();
et.addEventListener('test', () => console.log('hit!'));
et.dispatchEvent(new Event('test'));
This library has no dependencies. Even development dependencies. To test just run npm test
. It runs a script, and if it finishes without error, the tests pass.
FAQs
An EventTarget Polyfill
We found that event-target-polyfill 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.