Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@adonisjs/events
Advanced tools
Async event emitter with support for typed events
This module is extracted from AdonisJs. It is an Async event emitter with support for Typed events when using typescript.
Install the package from npm registry as follows:
npm i @adonisjs/events
# yarn
yarn add @adonisjs/events
And then use it as follows:
import { Emitter } from '@adonisjs/events/build/standalone'
const emitter = new Emitter()
emitter.on('signup', async (data) => {
//
})
emitter.emit('signup', { id: 1, email: 'foo@bar.com' })
The @adonisjs/core
module includes this module by default. However, here's how you can set it up manually.
const providers = [
'@adonisjs/events'
]
And then also register the typings file inside tsconfig.json
file.
{
"files": ["./node_modules/@adonisjs/events/build/adonis-typings/events.d.ts"]
}
Node.js inbuilt event emitter is synchronous and blocks the event loop while emitting events. On the other hand, this module relies on emittery to emit events asynchronously.
When using Typescript, you can attach data types to a event name and the Typescript compiler will complain, if types aren't the same.
import { Emitter } from '@adonisjs/events/build/standalone'
type EventsMap = {
'new:user': { id: number },
}
const emitter = new Emitter<EventsMap>()
emitter.emit('new:user', { id: 1 }) // works
emitter.emit('new:user', 1) // compiler error
Just like the emit
method, all other methods enforce types on typed events.
const emitter = new Emitter<EventsMap>()
emitter.on('new:user', (user) => {
})
emitter.once('new:user', (user) => {
})
When writing tests, you may want to suppress the events and instead run assertions to ensure that your code is emitting right events with correct data.
You can do this by using the FakeEmitter
instance during tests.
import { EmitterContract } from '@adonisjs/events/build/standalone'
export default class UserController {
constructor (protected emitter) {}
create () {
const user = makeSomeDbCall()
this.emitter.emit('new:user', user)
}
}
When writing tests, you can pass the FakeEmitter
, instead of the real emitter instance.
import { FakeEmitter } from '@adonisjs/events/build/standalone'
import UserController from './UserController'
const emitter = new FakeEmitter()
new UserController(emitter).create()
assert.deepEqual(emitter.events, [{
event: 'new:user',
data: user,
}])
// Clear stored events inside memory
emitter.clear()
When using with AdonisJs, you can also define listeners as a reference to the IoC container binding. For example:
// Add event listener
Event.on('new:user', 'User.signup')
// Remove event listener
Event.off('new:user', 'User.signup')
AdonisJs will make an instance of app/Listeners/User.ts
file and will call the signup method on it. This keeps your events file clean, since you can abstract the listeners code to dedicated files.
Also, you can define a custom namespace to lookup listeners from.
Event.namespace('App/MyListeners')
Now AdonisJs will look inside the MyListeners
directory vs Listeners
.
Following are the autogenerated files via Typedoc
Click here to see the latest npm audit report.
FAQs
An implementation of the event emitter built on top of emittery
The npm package @adonisjs/events receives a total of 19,274 weekly downloads. As such, @adonisjs/events popularity was classified as popular.
We found that @adonisjs/events demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers 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
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.