Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
strict-event-emitter-types
Advanced tools
Readme
NOTE: REQUIRES TYPESCRIPT 3.0
A TypeScript library for strongly typed event emitters in 0 kB. Declare events using a simple interface mapping event names to their payloads to get stricter versions of emit
, on
, and other common EventEmitter APIs. Works with any kind of EventEmitter.
npm i strict-event-emitter-types
import StrictEventEmitter from 'strict-event-emitter-types';
// define your events
interface Events {
request: (request: Request, response: Response) => void;
done: void;
}
// grab an event emitter
import { EventEmitter } from 'events';
// Create strict event emitter types
let ee: StrictEventEmitter<EventEmitter, Events> = new EventEmitter;
// now enjoy your strongly typed EventEmitter API!🎉
ee.on('request', (r, resp) => ... );
// r and resp are contextually typed to Request and Response
ee.on('somethingElse');
// Error: unknown event
ee.on('done', x => x);
// Error: The 'done' event does not have a payload
ee.emit('request', new Request());
// Error: missing event payload (the response)
ee.emit('request', new Request(), false);
// Error: incorrect payload type
Event records are interfaces or object types that map event names to the event's payload types. In the following example, three events are declared:
interface Events {
req: (request: Request, response: Response) => void;
done: void;
conn: Connection;
}
Each event shows one of three ways to type the event payloads:
void
: A shortcut for an event with no payload, i.e. () => void
(p: number) => void
can be written as just number
.The default export. A generic type that takes three type parameters:
The third parameter is handy when typing web sockets where client and server can listen to and emit different events. For example, if you are using socket.io:
// create types representing the server side and client
// side sockets
export type ServerSocket =
StrictEventEmitter<SocketIO.Socket, EventsFromServer, EventsFromClient>;
export type ClientSocket =
StrictEventEmitter<SocketIOClient.Socket, EventsFromClient, EventsFromServer>;
// elsewhere on server
let serverSocket: ServerSocket = new SocketIO.Socket();
serverSocket.on(/* only events that are sent from the client are allowed */, ...)
serverSocket.emit(/* only events that are emitted from the server are allowed */, ...)
// elsewhere on client
let clientSocket: ClientSocket = new SocketIOClient.Socket();
clientSocket.on(/* only events that are sent from the server are allowed */, ...)
clientSocket.emit(/* only events that are emitted from the client are allowed */, ...)
To subclass an EventEmitter you need to cast the base EventEmitter to the strict EventEmitter before extending:
type MyEmitter = StrictEventEmitter<EventEmitter, Events>;
class MyEventEmitter extends (EventEmitter as { new(): MyEmitter }) {
doEmit() {
this.emit(...); // strict
}
}
A type for a function which takes (and strictly checks) an emit event and a payload. TStrictEventEmitter is the event emitter type instantiated from StrictEventEmitter.
Useful for broadcast abstractions. It is not possible to contextually type assigments to this type, so your declarations will look something like this:
import { StrictBroadcast } from 'strict-event-emitter-types';
const broadcast: StrictBroadcast<ServerSocket> = function(
event: string,
payload?: any
) {
// ...
};
Note that the loose types for event and payload only apply inside the broadcast function (consumers will see a much stricter signature). Declaring more precise parameter types or narrowing using type guards would allow strongly-typed dispatching to emitters.
FAQs
Strictly and safely type any EventEmitter-like interface on any object
The npm package strict-event-emitter-types receives a total of 121,372 weekly downloads. As such, strict-event-emitter-types popularity was classified as popular.
We found that strict-event-emitter-types 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.