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

lisigi

v1.0.0

Published

Event based middleware alternative.

Downloads

2

Readme

Deed poll

Event based middleware alternative.

About intents

Conceptually, an intent can be seen as an event that is about to happen unless stated otherwise. Technically, it is an event which gives control to the outside world.

What can you do with intents

Since intents are quite similar to a traditional middleware, you can achieve pretty much anything that a middleware can do.

extend and augment funcitonality

Imagine something like a simple call to an external API and let's assume that this API did not have any authentication during your development process. As time goes by that particular API gets more and more popular and someone decides that some form of authentication becomes necessary.

Since nothing about the API itself changes, your code has effectively no reason to change.

// src/api.js

module.exports = app => {

    // the original function does not provide an "authorization" header
    
    const headers = { 'content-type' : 'application/json' }

    
    const sayHello = url => 
        app // give control to the outside world
            .signal('AboutToSayHello', { headers }) 
            .toPromise()
            .then(apiCallHello(url))

    const sayGoodBye = url => 
        app // give control to the outside world
            .signal('AboutToSayBye', { headers }) 
            .toPromise()
            .then(apiCallGoodBye(url))

            
    return { sayHello, sayGoodBye }
}

Having an intent in place will save your bacon.


// src/events.js 

const { fromPromise }  = require('lisigi/lib/intent')

/**
 * fake storage for demonstration purposes
 */
const storage = ({
    find: key => 
        key == 'hello-auth' 
            ? Promise.resolve('1e54b332-b123-4187-a85c-6d082236bff6') 
            : Promise.reject(`invalid key ${key}`)
})

/**
 * Transforms the storage promise into an Intent
 * 
 * @summary authenticate :: String a -> Intent(b)
 */
const authenticate = key => fromPromise(storage.find(key))

/**
 * Adds (or overwrites) the authorization string to the headers key
 * 
 * @summary addAuthHeaderTo :: Object a -> String b -> Object c 
 */
const addAuthHeaderTo = ({headers, ...rest}) => authorization => ({
    headers: { ...headers, authorization }, ...rest
})

/**
 * Tries to authenticate using 'hello-auth'.
 * This will always resolve and adds the auth header to the given context.
 * 
 * @summary addAuthenticationHeader :: Object a -> Intent(a)
 */
const addAuthenticationHeader = context => 
    authenticate('hello-auth').map(addAuthHeaderTo(context))


module.exports = app => {
    app.on('AboutToSayHello', intent => intent.chain(addAuthenticationHeader))
}

Prevent something from happening

Going back to the previous example, the sayGoodBye call will also be rejected due to the authentication. But, as of yet, the API call will be done anyways, although it is sure to fail.

We could use the same trick as before but let's further assume that the authorization for this call is different to the first one.


//src/events.js 

// ....

/**
 * Tries to authenticate using 'bye-auth'.
 * This will always be rejected and therefore prevent the acutal API call.
 * @summary addInvalidAuthenticationHeader :: Object a -> Intent(a)
 */    
const addInvalidAuthenticationHeader = context  => 
    authenticate('bye-auth').map(addAuthHeaderTo(context))

module.exports = app => {

    app.on('AboutToSayHello', intent => intent.chain(addAuthenticationHeader))
    app.on('AboutToSayBye',   intent => intent.chain(addInvalidAuthenticationHeader))
    
}

Since retrieving a viable authentication for 'bye-auth' is impossible, we are chaining a rejected intent. This, in turn, will prevent the actual API call (unless you catch it beforehand) and provide a (probably) more informative error message.