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

npl-protocol-handler

v0.1.1

Published

npl-protocol-handler is a general-purpose event handler for Evernode HotPocket NPL events with support for arbitrary application-specific protocols.

Downloads

86

Readme

npl-protocol-handler

A minimal infrastructure component for developers to uniformly integrate application protocols with HotPocket NPL messaging.

npl-protocol-handler is available for installation on npm:

npm install npl-protocol-handler

How to use it

  1. Import the module:

    const nplProtocolHandler = require("npl-protocol-handler");
  2. Create and use a compatible application-specific protocol:(see also: "Protocol requirements")

    const myProtocol = require("my-awesome-protocol");
  3. await nplProtocolHandler(...) from within your HotPocket smart contract:

    const myContract = async (ctx) => {
        if (!ctx.readonly) {
            const handler = nplProtocolHandler(
                ctx,
                myProtocol,
                true, // allow autoExit aka premature handler termination
                true, // how to invoke protocol functions (true = await)
                null // custom handler timeout. defaults to roundtime / 2
            );
    
            ctx.unl.send(myProtocol.buildSomeProtocolMessage());
    
            const reason = await handler;
    
            console.log(`npl-protocol-handler exited due to: ${reason}.`);
        }
    };
    
    const hpc = new HotPocket.Contract();
    hpc.init(myContract);

    Under normal conditions awaiting nplProtocolHandler(...) will yield one of these results:

    1. "timeout"(This is not a bad thing by default. It just signals that the internal timeout control of nplProtocolHandler(...) caused termination.)

    2. "autoExit"(i.e. the protocol decided to prematurely resolve nplProtocolHandler(...). A protocol can make use of this feature to save on HotPocket smart contract roundtime.)

    Internally nplProtocolHandler(...) at launch performs basic protocol checks. If these are not met then it will throw an Error.

About protocols

Requirements

npl-protocol-handler is able to use application-specific protocols. A protocol has just 2 hard requirements to be compatible with npl-protocol-handler:

  1. Must provide an async function for message processing:

    process: async function (node, msg) {...}
  2. Must provide an async function for signaling premature handler termination (if applicable):

    isFinished: async function () {...}

    It can also be used to prematurely exit the handler with a command that originated from the outside: the application could send some agreed-upon message into the NPL which would cause the protocol to signal instant termination of the handler. This would save valuable roundtime and could provide useful for high-performance NPL communication scenarios.

Nice to know

  • A protocol can be designed to emit() asynchronous events which can be captured and processed while npl-protocol-handler is still active.
  • A protocol can also use some sort of message recorder to collect all received NPL messages. These could e.g. be evaluated after npl-protocol-handler has already reolved.
  • Both of the aforementioned concepts are shown with the demo contract and protocol in the example1 directory.
  • example2 directory contains another example that showcases some other things one can do with protocols: Json message format and optional message compression.

Prerequisites

Your system must be generally prepared for HotPocket smart contract development:

Give it a try

A basic smart contract and demo protocol implementation can be found inside of subdirecory example. To use it locally follow these steps:

  • npm link
  • cd example1 || cd example2
  • npm link npl-protocol-handler
  • cd ..
  • npm run test1 || npm run test2