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

@laniakeajs/halley.http

v2.0.0

Published

The small, fast and easy Web Framework part of Laniakea Package Group. Inspired in Express

Downloads

30

Readme

Halley.JS ☄️

The small, fast and easy web framework

The 2.0.0 version of Halley has arrived

Fast getting started:

import { Halley } from "halley.http"

const halley = new Halley()

halley.get("/", (req, res) => {
  res.send("<h1>Hello World!</h1>")
})

halley.ready(5000)

Changes of version 2.0.0

  • Now you listen on a port describing the port on Halley.prototype.ready function instead of the constructor

  • Request.prototype.body property has now type any, giving the user more versatility to use this variable

  • You can add some initial routes in the constructor using the initialRoutes property

  • Added Halley.prototype.custom allowing the user use HTTP methods the he defines

  • Now you can change the response when the requested route would not found in the route stack using Halley.prototype.setError

    • For example:

      import { Halley } from "@laniakeajs/halley.http"
      
      const app = new Halley()
      
      app.setError = (_, res, notFoundedPath) => {
        res
          .status(404)
          .setHeader("Content-Type", "text/html")
          .write("<head><style>body {background-color: #242424;color: white}</style></head>")
        res.send("<h2>The route " + notFoundedPath + " does not exists!</h2>")
      }
      
      ...
  • Now the Route interface have an optional generic type, to indicate the HTTP Methods that are alloweds

    • For example, imagine that u want separe the "GET" and "POST" routes in differents arrays and then add them to Halley.js using Halley.prototype.use

      import { Halley, type Route } from "@laniakeajs/halley.http";
      
      const app = new Halley()
      
      // Indicating the HTTP Method, guarantees that just can use the "GET" method
      const getRoutes: Route<"GET">[] = [{
        path: "/",
        method: "GET",
        handler: (_req, res) => {
          res.send("<h1>Hello GET!</h1>")
        }
      }]
      
      // The same occurs when using POST
      const postRoutes: Route<"POST">[] = [{
        path: "/",
        method: "POST",
        handler: (_req, res) => {
          res.send("<h1>Hello POST!</h1>")
        }
      }]
      
      app.use(getRoutes).use(postRoutes)
      
      app.ready(5000)
    • If no generic is provided, the default methods passed to the generic are GET, POST, PUST and DELETE

  • URL parameters is now allowed using colons before the parameter name

    • We are simulating a web store, to use parameters in our path url we can must keep in mind the next:

      • The url parameters must begins with colon before the parameter name like this: ":parameter", look the next example:
      app.get("/products/:name", (req, res) => {
        res.send("<h1>The product: " + req.params.name + " is available</h1>")
      })

      We are requesting the product on that store that have the same name in the database that the parameter name

  • You can use regular expressions instead a string to indicate the route path

    • Lets try using a regular expresion, in this case using a literal regular expression:

      app.get(/\/ab?cd/, (_req, res) => {
        res.send("<h1>Ruta con expresion regular!</h1>")
      })
      
      // With this regex, you can access to /abcd or /acd, but not to /bcd or /cd
      • Using a constructor:
      app.get(new RegExp("/[a-zA-Z0-9]+s$"), (_req, res) => {
        res.send("<h1>Ruta con expresion regular usando un constructor!</h1>")
      })
      
      // With this regex, you can access to /products, /elements, /users
      // Or any other route that includes letter from a to z without case sensitive o any number in any position
      // As long as the route ends with 's', but it can not contains only 's' for example: /s
      • Of course, if you dont starts your route paths with a '/' it will throw an error too, an example would be this:
      // Here we are using a literal regex, in this case, you need to escape the slash character (/), like in this example
      app.get(/\/route1/, ...)
      • In another way if you doesnt escape the character JavaScript / TypeScript will conside the line a comment:
      app.get(//route1/, ...)
      • Or if you try to not escape the character and just write the literal regex, will throw an error, like this:
      app.get(/route1/, ...)
      
      // Cause an HALLEY_ROUTE_DO_NOT_START_WITH_SLASH exception

      In this case Halley.js throws an HALLEY_ROUTE_DO_NOT_START_WITH_SLASH exception

      • Would happen the same if you use the RegExp constructor using the new operator:
      app.get(new RegExp("route1"), ...)
      
      // Cause an HALLEY_ROUTE_DO_NOT_START_WITH_SLASH exception

Internal details

  • Regexs path are checked using the RegExp.prototype.source property, it returns a string containing the regex content:

    1. In the case of a literal regex the value between both slashes are returned

    2. In the case of a regex created using his constructor;

      1. If a literal regex is passed, cause the 1. step behavior

      2. If a string is passed, returns that string but scaping some characters like slashes

        • like in this example:
        new RegExp("/route1").source // Output -> \/route1 <- The escape character is added automatically to escape the slash
    • In this way Halley.js forces you to add a slash at the route path begins, checking the [1] string position that returns RegExp.prototype.source
  • Halley class now inherit from the HRouter class

  • Now path parameters validation occurs in the HRouter path using path-to-regexp package, its useful, zero dependencies and easy to use

  • The Request and Reply / Response objects are now passed as parameters instead of being a class property

  • Middlewares are now resolved using Promise class methods like Promise.resolve or Promise.all

  • The global middleware array changed his name to middlewareStack, now is called similar to the route array

Fixes

  • Fix bug that doesnt allow use global middlewares and local middleware simultaneously (in method makeSuitable validation)

By the halley.js unique author / contributor at the moment - Raxabi [email protected]