npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

message-events

v0.4.0

Published

(pre)formatted message events for improved logging

Downloads

6

Readme

message-events

(pre)formatted message events for improved logging

It would be great if more developers would silently emit message objects from their libraries instead of spamming logs in the console. Maybe the developer of the consuming app wants to send it to a server, store it in a database or take any other action based upon the specifics of a message. Many things are possible, and there is no need to not let the user of your lib decide what to do with your libs messages.

MessageEvents is an attempt to replace console statements in libraries with a generic and much more powerful system of showing errors and sending out data from your lib. Although there are many great event and log libraries out there, none are as lean and specific as I wished for. MessageEvents main differences compared to some popular event and logging libraries are:

- you can format and send messages before a .on handler is installed
- it is non-opinionated and extremely lightweight
- pretty robust and well tested
- sends out message objects itself

Here are some basic examples of how to use it

// my-lib.js
const MessageEvents = require( 'message-events' );

// totally minimalistic working example
let message = new MessageEvents;
message.on( 'error', console.error );
message.error( 'arguments', 'to', 'console.error' );
// arguments to console.error


// a bit more elaborate
// with the format method you can format every message you emit into the format of your liking
message.format( 'error', (method, ...text) => {
	return {
		sender: 'my-lib.js',
		type: 'error',
		method,
		text: text.join(' '),
		// appVersion: APP_VERSION,
		// userId: '...',
		// etc..
	};
});

// you can now write error messages where needed,
// but they are to be seen/received only after a .on handler is installed
message.error( 'idOfMethod', 'enter', 'the', 'void!' );

// add more types of messages when needed
message.format( 'info', (...text) => {
	return {
		sender: 'my-lib.js',
		type: 'info',
		text: text.join(' '),
	};
});
// now you can do
message.info( 'this info can go to another user defined handler' );


// make your messages available for users of your lib
class MyLib{}
// allow a user to add a handler for the messages
MyLib.on = (type: string, handler: Function) => message.on(type, handler);
module.exports = MyLib
// end of module



// in your users file
const lib = require( 'your-lib' );

// for development your user could install a handler to direct your libs messages to the console,
// while in production they can send these same errors instead to a server for monitoring
if ( process.env.NODE_ENV === 'production' ){
	lib.on( 'error', (data) => {
		axios.post( 'solib.api.domain/error-messages', data );
	});
} else lib.on( 'error', console.error );


// when you only need one quick message type, you can call .format directly via the constructor
message = new MessageEvents( 'error', (...text) => {
	return {
		sender: 'myModule',
		type: 'error',
		text	: text.join(' '),
	};
});

In a large app it might be a good idea to make the MessageEvents instance(s) "public" by putting them in a separate module:

// instance/message-events.js

const MessageEvents = require( 'message-events' );

const message = new MessageEvents;

if ( process.env.NODE_ENV !== 'production' ){
	// handle error messages coming from MessageEvents itself during development
	MessageEvents.on( 'error', console.error );
	// define output for development
	message.on( 'log', console.log );
	message.on( 'info', console.log );
	message.on( 'error', console.error );
}

// you could define a generic format for your messages
const formatMessage = (type, module, args) => {
	return {
		sender: 'my-lib.js',
		module,
		type,
		text: args,
	}
};

message.format( 'info', (module, text) => formatMessage('info', module, text) );
message.format( 'error', (module, text) => formatMessage('error', module, text) );
// etc..


// and then use it in some-module.js
const { message } = require( '@instance/message-events.js' );

message.log( 'no formatting was defined for message.log' );
message.info( 'some-module.js:', 'hello message-events!' );
message.error( 'some-module.js:', 'your error message here' );
// in development mode these three lines should output:
//
// no formatting was defined for message.log
// {
//   sender: 'my-lib.js',
//   module: 'some-module.js:',
//   type: 'info',
//   text: 'hello message-events!'
// }
// {
//   sender: 'my-lib.js',
//   module: 'some-module.js:',
//   type: 'error',
//   text: 'your error message here'
// }

api

MessageEvents emits it's own messages from an instance of itself. There are 2 types: 'info' and 'error'. Use MessageEvents.on to install a handler that listens to them.

MessageEvents.on( id: string, handler: function ) : void;
MessageEvents.off( id: string || void ) : void;	// void/undefined removes all handlers

// methods:
	.format( id: string, handler: function ) : this;
	.off( id: string || void ) : this;			// void/undefined removes all handlers
	.on( id: string, handler: function ) : this;