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

hoctane

v0.2.0

Published

High-Octane route store/retriever

Downloads

11

Readme

Hoctane

Fast and efficient route store/retriever. It uses internally a high performance Prefix Tree, supports route parametters, and complex regex patterns.

Install

npm

npm i hoctane --save

Yarn

yarn add hoctane

Usage

Hoctane is a storage library with an optimized algorithm for fast lookup, intended to be used by routers looking for performance and routes allowing flexible patterns.

const store = new Store
store.add('foo/:id')
store.build()

const {route, params} = store.find('foo/1')

// params.id === 1

Storing routes

The method add receives a path as the first parameter and returns new route object that contains all the necessary information to be stored in the underlying storage.

const route = store.add('foo/bar')

The route object looks like the following:

interface Route {
  index: number
  path: string
  regexp: RegExp
  tokens: any[]
  paramsSpec: any[]
  generateUrl: (params: any) => string
}

Building the tree

When the routes are constructed and stored through the add method, doesn't means that any of it can be finded still. Before, it's needed to build and compress the internal structure that allows to find any route in a matter of a blink.

store.add('foo')
// ...
store.build()
// ...
store.find('foo')

Storing data related to the routes

The add method does not receives any payload to be attached to the route such as handlers or any metadata. This is considered out of the scope of this library.

Though, the index property plays the role of a unique identifier of the route in each Store instance. It is equal to the index of that route inside the list of routes returned by the method getRoutes.

const route = store.add('foo/bar')
const routes = store.getRoutes()

routes[route.index] === route // true

Regarding the said above, we can keep an list of things related to the routes, example:

// list of handlers
const handlers = []

function addPathWithHandler(path, handler) {
  const route = store.add(path)
  // store the handler
  handlers[route].index = handler
}

// returns a handler
function findHandlerByPath(path) {
  const found = store.find(path)
  if (found) {
    // return the related handler
    return handlers[found.route.index]
  }
}

Now lets use what we wrote above.

const handler = (ctx) => null

addPathWithHandler('/clients', handler)

findHandlerByPath('/clients') === handler // true

What about HTTP verbs?

HTTP verbs are strings, and each route should be related to one of them (GET, POST, PUT ...) so, it is safe (and performant) to treat each verb as part of the path, example:

store.add(method + '/' + path)
store.find(method + '/' + path)

Author: Yosbel Marin

License: MIT