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

hndl

v2.1.2

Published

A simple node framework, you've been looking for

Downloads

10

Readme

HNDL

Are you looking for a new Node framework? Well, you've come to the right place!

HNDL was made to address my personal frustrations with existing Node frameworks, and here are the main design decisions behind HNDL:

  • No middleware
  • Strong types
  • Simple routing
  • Async by default
  • No external dependencies
  • Sane defaults

No Middleware?!

Yes! This is my main point of concern with existing Node frameworks. Let's take a typicial Express request handler:

app.get("/timestamp", (req, res) => {
  res.send({
    timestamp: new Date().valueOf();
  })
});

Let's say we send a request GET /timestamp, what would the response be?

It would be valid to say: "Well it returns a json object containing a unix millis timestamp.". However it's not correct, because you don't know that before I registered this handler, I also have:

app.use((req, res) => {
  res.sendStatus(400);
})

So to avoid confusion like this, there is no middleware support in HNDL. If your request handler gets invoked, it is guaranteed to be THE ONLY thing that will process this event. Isn't that cool? Why do I even have to sell this as a feature? How did we come to this?

Endpoint Type Definition

In HNDL any object that looks like this is a valid endpoint:

type Endpoint<T> = {
  accept: (request: Request) => Optional<T> | Promise<Optional<T>>;
  handle: (payload: T) => Response | Promise<Response>;
}

The most important thing to notice is the relationship between accept and handle: handle takes as a param whatever accept returns.

How they are related is explained in the next section about routing.

Routing

A router in HNDL is defined using the function router, it takes a variadic list of endpoints like this:

const myRouter = router(
  firstEndpoint,
  secondEndpoint,
  thirdEndpoint
)

The main job of the router is to choose one of the passed endpoints to handle the incoming request. And for this the router uses the accept function.

So when a new request comes in, the router will go in order, and call the accept function of every endpoint. The first accept that returns a truthy value, is chosen.

Whatever this truthy value is, the router will invoke the handle method of this chosen endpoint with that value.

The handle method of the chosen endpoint MUST return a Response object. In other words, if you accept the request, you must handle it entirely.

Both accept and handle can of course be async.

Finally, it's worth noting that the router function just returns another Endpoint so they can be nested, if needed, but this is discouraged.

Examples

Let's go through a few examples to illustrate how this works:

This endpoint will respond to any request with a 200 OK:

const everythingIsOK = {
  accept: () => true,
  handle: () => { status: OK }
}

This router will respond with 200 OK if the URL starts with /ok, otherwise with 404 Not Found:

const myRouter = router(
  {
    accept: request => request.url.startsWith("/ok"),
    handle: () => { status: OK }
  },
  {
    accept: () => true,
    handle: () { status: NOT_FOUND }
  }
)

The Service

When developing a web service it's necessary to perform additional tasks such as logging, error handling, etc... These do not fall into the area of responsibility of endpoints, and for this we use the service.

The service function takes the following arguments:

type ServiceProps = {
  endpoint: Endpoint<any>;
  errorHandler?: ErrorHandler;
  logger?: Logger;
}

type ErrorHandler = (error: any) => Response | Promise<Response>;

type Logger = (request: Request, response: Response) => void;

And returns a new endpoint which will log requests and handle errors correctly thrown from both the accept and handle functions of the passed endpoint.

It's a convenient function that is not strictly necessary in this package, but is something that most people will like to have.

The Listener

The last piece of the puzzle is the listener. It is meant to conform to the request handler function passed into the createServer function which comes with the default node http module.

The listener's job is to simply allow async handling of incoming requests. And can be used in the following way:

const myEndpoint: Endpoint = { /* ... */};
const server = createServer(listener(myEndpoint));