Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
a typed event emitter that provides removal protection, filtering, and inheritance
ts-ev is a typed event emitter that provides removal protection, filtering, and inheritance.
Unlike other typed event emitters, ts-ev includes a mechanism for arbitrarily deep extensions of its Emitter class:
ts-ev has zero imports, so it should be usable in any TS environment.
Execution Order Guarantees:
.emit()
operates on the currently registered listeners.
EventEmitter
from "events".Protection:
.off()
will not remove the listener unless it is explicitly specified.Filtering:
(args: [number, string]): args is [1, "foo"] => args[0] === 1 && args[1] === "foo"
export class Emitter<
BaseEvents extends { [event: string]: (...args: any[]) => any },
DerivedEvents extends { [event: string]: (...args: any[]) => any } = {},
> { ... }
Two template parameters are provided, one for base events and another for derived events. This is to allow for extensions of an emitter-derived class that define additional events.
For example:
// allows derived classes to define additional events (but prohibits additional "foo" events)
class Foo<DerivedEvents extends Emitter.Events.Except<"foo">>
extends Emitter<{ foo: (data: "bar") => any }, DerivedEvents> {
constructor() {
this.emit("foo", "bar"); // OK
}
}
// extend the functionality of Foo and define additional events
class Bar extends Foo<{ bar: (data: "baz") => any }> {
constructor() {
this.emit("foo", "bar"); // OK
this.emit("bar", "baz"); // OK
}
}
In this example, the Emitter
BaseEvents
defined by Foo
are statically known to it and therefore can be used within the class and its descendants. It's DerivedEvents
are not statically known by it, so they may only be used by the derived class that defines them (Bar
).
public [on|prependOn|once|prependOnce]<
Ev extends keyof BaseEvents | keyof DerivedEvents,
Data extends EvData<BaseEvents, DerivedEvents, Ev> = EvData<BaseEvents, DerivedEvents, Ev>
> { ... }
Ev:
Data:
BaseEvents
or DerivedEvents
value.import { Emitter } from "ts-ev";
// standard usage, no extensions
const emitter = new Emitter<{
foo: (bar: "baz") => any
}>();
// emitter.emit("foo", "bar"); // TS error
emitter.emit("foo", "baz"); // OK
// extend Emitter
class Foo<
// use a tparam to forward derived events to Emitter (allow event extensions)
DerivedEvents extends Emitter.Events.Except<"baseEv1" | "baseEv2">
> extends Emitter<
{
baseEv1: (requires: number, these: string, args: boolean) => any;
baseEv2: () => any;
},
DerivedEvents
> {
constructor() {
super();
setTimeout(() => this.emit("baseEv1", 1, "foo", true), 100);
setTimeout(() => this.emit("baseEv2"), 1000);
// this.emit("derivedEv") // ts error
// this.emit("anythingElse") // ts error
}
}
const foo = new Foo();
// standard on/once/off functionality
await foo.once("baseEv2");
const l = () => console.log("received");
foo.on("baseEv2", l);
foo.off("baseEv2", l);
// or foo.off("baseEv2");
// or foo.off();
// protection
foo.on("baseEv2", l, { protect: true });
foo.off("baseEv2"); // does not remove the above listener
foo.off(); // does not remove the above listener
foo.off("baseEv2", l); // OK
// filtering
foo.on(
"baseEv1",
// TS Types:
// (parameter) a: 1
// (parameter) b: "foo"
// (parameter) c: boolean
(a, b, c) => console.log(a, b, c),
{
// note: must explicitly specify both the source and expected type
filter: (data: [number, string, boolean]): data is [1, "foo", boolean] =>
data[0] === 1 && data[1] === "foo",
}
);
// TS Types:
// const two: 2
// const baz: "baz"
const [two, baz] = await foo.once("baseEv1", {
filter: (data: [number, string, boolean]): data is [2, "bar", boolean] =>
data[0] === 2 && data[1] === "baz",
});
// inheritance
// extending the Foo emitter
// note: this only works because Foo passes its DerivedEvents tparam to Emitter
class Bar extends Foo<{
derivedEv: (add: "more", events: "to", the: "emitter") => any;
}> {
constructor() {
super();
// can operate on base class events
setTimeout(() => this.emit("baseEv1", 1, "foo", true), 100);
// can operate on events provided to Foo via OtherEvents
setTimeout(() => this.emit("derivedEv", "more", "to", "emitter"), 200);
}
}
const bar = new Bar();
bar.on("baseEv2", () => console.log("receives base class events!"));
bar.on("derivedEv", () => console.log("receives derived class events!"));
npm test
Uses @jpcx/testts for internal unit testing.
Additionally, this project is relied on heavily by my node-kraken-api package, so it has received plenty of integration testing.
Contribution is welcome! Please raise an issue or make a pull request.
Justin Collier - jpcx
This project is licensed under the MIT License - see the LICENSE file for details
0.4.0 (2022-03-13)
| Changes since 0.3.0 | Release Notes | README | | --- | --- | --- |
| Source Code (zip) | Source Code (tar.gz) | | --- | --- |
DataFilter
type assertion fix.<a name="0.3.0"></a>
FAQs
a typed event emitter that provides removal protection, filtering, and inheritance
The npm package ts-ev receives a total of 1,477 weekly downloads. As such, ts-ev popularity was classified as popular.
We found that ts-ev 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.