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.
tiny-typed-emitter
Advanced tools
The tiny-typed-emitter package is a lightweight, strongly-typed event emitter for TypeScript and JavaScript. It allows you to create and manage events with type safety, ensuring that event listeners and emitters are correctly typed.
Basic Event Emission
This feature allows you to define and emit events with specific types. The code sample demonstrates how to create a custom event emitter, add listeners for 'data' and 'error' events, and emit those events with the appropriate data types.
const { TypedEmitter } = require('tiny-typed-emitter');
class MyEmitter extends TypedEmitter {
// Define event types
on(event: 'data', listener: (data: string) => void): this;
on(event: 'error', listener: (error: Error) => void): this;
}
const emitter = new MyEmitter();
// Add listeners
emitter.on('data', (data) => {
console.log('Data received:', data);
});
emitter.on('error', (error) => {
console.error('Error occurred:', error);
});
// Emit events
emitter.emit('data', 'Hello, World!');
emitter.emit('error', new Error('Something went wrong!'));
Removing Event Listeners
This feature allows you to remove event listeners. The code sample demonstrates how to add a listener for the 'data' event, emit the event, remove the listener, and then emit the event again to show that the listener has been removed.
const { TypedEmitter } = require('tiny-typed-emitter');
class MyEmitter extends TypedEmitter {
on(event: 'data', listener: (data: string) => void): this;
}
const emitter = new MyEmitter();
function onData(data) {
console.log('Data received:', data);
}
// Add listener
emitter.on('data', onData);
// Emit event
emitter.emit('data', 'Hello, World!');
// Remove listener
emitter.off('data', onData);
// Emit event again (no output expected)
emitter.emit('data', 'Hello again!');
Once Event Listeners
This feature allows you to add a listener that will be called only once. The code sample demonstrates how to add a 'once' listener for the 'data' event, emit the event to trigger the listener, and then emit the event again to show that the listener is not called a second time.
const { TypedEmitter } = require('tiny-typed-emitter');
class MyEmitter extends TypedEmitter {
on(event: 'data', listener: (data: string) => void): this;
once(event: 'data', listener: (data: string) => void): this;
}
const emitter = new MyEmitter();
// Add a once listener
emitter.once('data', (data) => {
console.log('Data received:', data);
});
// Emit event (listener will be called)
emitter.emit('data', 'Hello, World!');
// Emit event again (listener will not be called)
emitter.emit('data', 'Hello again!');
EventEmitter3 is a high-performance event emitter for Node.js and the browser. It is similar to tiny-typed-emitter but does not provide built-in TypeScript support. You can use TypeScript with EventEmitter3, but you will need to define your own type definitions.
Mitt is a tiny, functional event emitter. It is similar to tiny-typed-emitter in terms of size and simplicity but does not offer built-in TypeScript support. Mitt is designed to be extremely lightweight and fast.
Node-Event-Emitter is a simple and lightweight event emitter for Node.js. It is similar to tiny-typed-emitter but does not provide type safety. It is a good choice if you need a basic event emitter without the overhead of type definitions.
Have your events and their listeners type-checked with no overhead.
Simply add the dependency using npm:
$ npm i tiny-typed-emitter
or using yarn:
$ yarn add tiny-typed-emitter
import { TypedEmitter } from 'tiny-typed-emitter';
interface MyClassEvents {
'added': (el: string, wasNew: boolean) => void;
'deleted': (deletedCount: number) => void;
}
EventEmitter
:
class MyClass extends TypedEmitter<MyClassEvents> {
constructor() {
super();
}
}
const emitter = new TypedEmitter<MyClassEvent>();
To use with generic events interface:
interface MyClassEvents<T> {
'added': (el: T, wasNew: boolean) => void;
}
class MyClass<T> extends TypedEmitter<MyClassEvents<T>> {
}
The type of eventNames()
is a superset of the actual event names to make
subclasses of a TypedEmitter
that introduce different events type
compatible. For example the following is possible:
class Animal<E extends ListenerSignature<E>=ListenerSignature<unknown>> extends TypedEmitter<{spawn: () => void} & E> {
constructor() {
super();
}
}
class Frog<E extends ListenerSignature<E>> extends Animal<{jump: () => void} & E> {
}
class Bird<E extends ListenerSignature<E>> extends Animal<{fly: () => void} & E> {
}
const animals: Animal[] = [new Frog(), new Bird()];
Library adds no overhead. All it does is it simply reexports renamed EventEmitter
with customized typings.
You can check lib/index.js to see the exported code.
FAQs
Fully type-checked EventEmitter
We found that tiny-typed-emitter 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.