
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Implementation of the Publish/Subscribe design pattern using ES6 features. Think Event Emitter.
It is an event system that allows us to define application specific events which can serve as triggers for message passing between various parts of an application. The main idea here is to avoid dependencies and promote loose coupling. Rather than single objects calling on the methods of other objects directly, they instead subscribe to a specific topic (event) and are notified when it occurs.
'*' topicsubscribe === on, publish === emit, unsubscribe === off)// emitter that logs any publish events
import PSub from 'psub';
class PSubLogger extends PSub {
constructor() {
super();
}
publish(evt, ...args) {
console.log(`publish (${evt}): ${args}`);
super.publish(evt, ...args);
}
}
import http from 'http';
import PSub from 'psub';
const ps = new PSub();
// can also use ps.on
ps.subscribe('request', ({ method, url }) => {
console.log(`${method}: ${url}`);
});
http.createServer((req, res) => {
// can also use ps.emit
ps.publish('request', req);
res.end('Works!');
}).listen(1337, '127.0.0.1');
import PSub from 'psub';
const ps = new PSub();
ps.subscribe('email/inbox', ({
subject,
body
}) => {
new Notification(`Sent: ${subject}`, {
body
});
});
document
.querySelector('#send')
.addEventListener('click', () => {
const form = new FormData(document.getElementById('email-form'));
fetch('/login', {
method: 'POST',
body: form
}).then((response) => {
ps.publish('email/inbox', response);
});
});
// extract the default export
const { PSub } = require('psub');
const ps = new PSub();
// ...
Add the code as a script or use the unpkg cdn
<script src="https://unpkg.com/psub@latest/dist/index.umd.js"></script>
// extract the default export
const { PSub } = window.PSub;
const ps = new PSub();
// ...
yarn add psub
npm i psub
Class representing a PSub object
Kind: global class
SymbolBooleanCreate a PSub instance.
SymbolSubscribes the given handler for the given topic.
Kind: instance method of PSub
Alias: on
Returns: Symbol - Symbol that can be used to unsubscribe this subscription
| Param | Type | Description |
|---|---|---|
| topic | String | Topic name for which to subscribe the given handler |
| handler | function | Function to call when given topic is published |
Example
// subscribe for the topic "notifications"
// call onNotification when a message arrives
const subscription = psub.subscribe(
'notifications', // topic name
onNotification, // callback
);
Method to publish data to all subscribers for the given topic.
Kind: instance method of PSub
Alias: emit
| Param | Type | Description |
|---|---|---|
| topic | String | cubscription topic |
| ...args | Array | arguments to send to all subscribers for this topic |
Example
// publish an object to anybody listening
// to the topic 'message/channel'
psub.publish('message/channel', {
id: 1,
content: 'PSub is cool!'
})
BooleanCancel a subscription using the subscription symbol
Kind: instance method of PSub
Alias: off
Returns: Boolean - true if subscription was cancelled, false otherwise
| Param | Type | Description |
|---|---|---|
| symbol | Symbol | subscription Symbol obtained when subscribing |
Example
// unsubscribe using the subscription symbol
// obtained when you subscribed
const didUnsubscribe = psub.unsubscribe(subscriptionSymbol);
The PSub class internally maintains two maps.
Map<topic,subscriptionsList>Map<symbol,subscriptionLocation>Subscribing
When ps.subscribe(topic, handler) is called, PSub looks up the list of existing subscriptions from Map<topic,subscriptionsList> and appends the new subscription handler to the obtained list.
Then it creates a new Symbol to represent this subscription and creates a subscription location POJO of the form { topic: subscriptionTopic, index: positionInSubscriptionsArray }, adding them to Map<symbol,subscriptionLocation>. Finally it returns the created Symbol.
Publishing
When ps.publish(topic) is called, PSub looks up the list of existing subscriptions from Map<topic,subscriptionsList> and invokes their handlers, each in its own microtask, passing along any provided arguments.
Unsubscribing
When ps.unsubscribe(symbol) is called, PSub uses this symbol to obtain a subscription location from Map<symbol,subscriptionLocation>. It then extracts the topic and position for this subscription from the obtained subscription location and removes the subscription from Map<topic,subscriptionsList>. Finally it does some necessary cleanup and return true to signal success.
MIT
FAQs
Publish/Subscribe (Event Emitter)
We found that psub 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.