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

custardjs

v1.0.0

Published

A simple priority system for NodeJS

Downloads

12

Readme

     ██████╗ ██╗   ██╗ ███████╗ ████████╗  █████╗  ██████╗  ██████╗           ██╗ ███████╗
    ██╔════╝ ██║   ██║ ██╔════╝ ╚══██╔══╝ ██╔══██╗ ██╔══██╗ ██╔══██╗          ██║ ██╔════╝
    ██║      ██║   ██║ ███████╗    ██║    ███████║ ██████╔╝ ██║  ██║          ██║ ███████╗
    ██║      ██║   ██║ ╚════██║    ██║    ██╔══██║ ██╔══██╗ ██║  ██║     ██   ██║ ╚════██║
    ╚██████╗ ╚██████╔╝ ███████║    ██║    ██║  ██║ ██║  ██║ ██████╔╝ ██╗ ╚█████╔╝ ███████║
     ╚═════╝  ╚═════╝  ╚══════╝    ╚═╝    ╚═╝  ╚═╝ ╚═╝  ╚═╝ ╚═════╝  ╚═╝  ╚════╝  ╚══════╝

NPM

A simple priority system for NodeJS

Why

You know when you’re in one of those 5PM meetings after a long day of crazy stuff and the HIPPO (highest paid personal opinion) comes up with completely arbitrary solutions to complete arbitrary problems that will mean you’ll have to stay late and you start to get angry? This is because the stress causes you to sacrifice higher function like sympathy and social etiquette in order to keep lower functions like logic and speech working.

When running a node application and your boss wants you to add funky little things that add no value but bring your applications performance down you want to start sacrificing those functions when the demand gets too high in order to keep lower functions alive.

CustardJS simply keeps track of how many requests are running and starts sacrificing sub functions you specify according to a maxCalls property set by you.

Installing

To install simply NPM install it and require it in your project.

$ npm install custardjs
const Custard = require('custardjs');

Custard.run([
	{
		run: function1,
	},{
		run: function2,
		maxCalls: 900,
		fallback: () => {
			console.log(`function2 has been switched off due to high traffic: (${Custard.getRequest})`);
		},
	},{
		run: function3,
		maxCalls: 100,
		fallback: fallbackFunction3,
	}
], allGood);


function function1() {
	//Do magic here you have to do regardless

	Custard.finished(); //make sure you signal to Custard that your task is finished
}

function function2() {
	//Do magic here that is totally not something we NEED but we try to get done
	//Maybe some internal analytics reporting or whatever. Anything you can handle
	//while having 900 requests in the queue.

	Custard.finished(); //make sure you signal to Custard that your task is finished
}

function function3() {
	//Do magic here that is totally icing on the cake and can easily not happen
	//Could be some meta you want to save to disk. On a work load of 100 requests
	//currently running your I/O will probably not allow you anyhow ;)

	Custard.finished(); //make sure you signal to Custard that your task is finished
}

function fallbackFunction3() {
	//This function only runs when there are more requests than 100 pending.
	//So now save your stuff you wanted to save into memory in function3
	//(hoping this isn’t much?)
}

function allGood() {
	//Now that the dust has settled and we can breath again, take those in-memory
	//things we saved in fallbackFunction3 and put them into the file
}

Usage

run

Type: <method>
Parameters: targets <object> and done <function> (optional)

The main method to invoke all parts of your application and assigned priority. maxCalls means the method or function will only be run if there are less then maxCalls processes running at the same time that are tracked with Custard. The fallback function is optional and is run when we drop runs becasue we reached maxCalls capacity. There is also an optional done function as second parameter. This function runs when we have just gone through a request and completely cleared the queue.
(You can declare the done function without calling run by assigning your logic right to Custard.done if that’s easier)

Custard.run([
	{
		run: function1,               //run this always
	},{
		run: function2,               //the function in this task
		maxCalls: 900,                //run the function if there are less than 900 processes running
		fallback: fallbackFunction2,  //run this when we start sacrificing this based on your maxCalls
		                              //send panicked email to sys-admin
		                              //automatically scale system, send lease requests to AWS
		                              //scream and ask yourself how it’s gotten this for
		                              //question all life decisions so far
		                              //realize the demand means your stuff is liked
		                              //or at very least used
	},{
		run: function3,               //the function in this task
		maxCalls: 100,                //run the function if there are less than 100 processes running
		fallback: fallbackFunction3,  //run this when we start sacrificing this based on your maxCalls
	}
], () => {                        //run this when we have nothing in the queue anymore
	console.log('Done. No more requests to be processed. Ready for more all fresh and fit!');
});

finished

Type: <method>
Parameters: none

When all your functions completed and your promise is resloved, run this method to make sure all calls are counted correctly.
:exclamation: Remember to do that in error handlers as well in case your function faulters and skips the Custard.finished() call!

Custard.finished();

getQueue

Type: <method>
Parameters: none

If you want to know how many calls are in the queue mid-flow, run the getQueue method. It returns the number of calls in queue. This includes all tasks you may have been assigning to run.

Custard.getQueue()

getRequest

Type: <method>
Parameters: none

If you want to know how many requests have been fired off to Custard, run the getRequest method. This will return the number of times you ran run.

Custard.getRequest()

Tests

The build comes with a test server that let’s you test Custard’s functionality. The server is started at http://localhost:1337.
(I know this is pretty … simple but for now it’s enough.)

To run the current test:

node test/test-server.js

This starts an express app that listens to http://localhost:1337. When you request that URL in your browser, the app will run three delayed methods and check if Custard keeps track of them all and recognises when the queue is empty. Stress test this by requesting it several times

Contributing

Please look at the coding style and work with it, not against it ;)

Release History

  • 1.0.0 - Added getRequest and getQueue, Removed get, added individual fallbacks to each task
  • 0.2.2 - Fixed critical count bug
  • 0.2.1 - Fixed prod concat bug
  • 0.2.0 - Converted to ES6, bumped node requirement
  • 0.1.1 - Fixed prod folder ignores
  • 0.1.0 - First working example
  • 0.0.1 - initial commit

License

Copyright (c) 2016 Dominik Wilkowski. Licensed under the GNU GPL.