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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@streetjs/router

v1.0.0

Published

StreetJS HTTP router: a compiled-regex router with path-param extraction, a middleware pipeline, request validation, baked RBAC and per-route rate limiting, optional latency profiling, and not-found/error handlers.

Readme

@streetjs/router

The StreetJS HTTP router: a compiled-regex router with path-parameter extraction, a recursive middleware pipeline, request validation, baked RBAC and per-route rate limiting, optional latency profiling, and ready-made not-found / error handlers. ESM, strict-TypeScript.

This is the standalone home of the router that also backs the streetjs/router subpath. The streetjs framework re-exports this package, so there is a single source of truth.

Install

npm install @streetjs/router @streetjs/context @streetjs/exceptions @streetjs/ratelimit reflect-metadata

Usage

import { Router, notFoundHandler, errorHandler } from '@streetjs/router';

const router = new Router();

router.add('GET', '/users/:id', [authMiddleware], (ctx) => {
  ctx.json({ id: ctx.params.id });
});

// In your server's request handler:
async function handle(ctx) {
  try {
    const matched = await router.dispatch(ctx); // true if a route ran
    if (!matched) await notFoundHandler(ctx);
  } catch (err) {
    await errorHandler(ctx, err);
  }
}

Routing

add(method, path, middlewares, handler, validate?, handlerTarget?, handlerMethodName?) compiles path (with :param segments and * wildcards) to a regex and registers it. dispatch(ctx) matches on method + path, extracts and URL-decodes params into ctx.params, runs the pipeline, and returns whether a route matched. A method of '*' matches any verb. listRoutes() returns the registered method/pattern pairs (used for OpenAPI generation).

Pipeline order

For a matched route the pipeline is: per-route rate limiter → route middlewares → validation → handler, executed via a recursive next() chain so any middleware can wrap the rest.

Baked-in decorators

When handlerTarget/handlerMethodName are supplied, at registration time the router reads decorator metadata:

  • @Roles / @Permissions → baked onto ctx.state._requiredRoles / _requiredPermissions at dispatch, so an rbacGuard needs no prototype-chain traversal per request.
  • @RateLimit (from @streetjs/ratelimit) → a route-scoped limiter, keyed by IP (default), authenticated user (key: 'user'), or API key (key: 'apiKey').

Validation

Pass a ValidationSchema to validate body, query, and params against FieldRules (string with min/max/pattern, number, boolean, email, uuid, and required). Failures throw a BadRequestException whose details list every problem.

router.add('POST', '/users', [], createUser, {
  body: {
    name:  { type: 'string', required: true, min: 2, max: 40 },
    email: { type: 'email', required: true },
  },
});

Profiling

Pass a profiler to the constructor to record per-route latency:

const router = new Router({ profiler }); // profiler.record(method, path, latencyNs, isError)

Handlers

  • notFoundHandler(ctx) throws a NotFoundException naming the route.
  • errorHandler(ctx, err) serializes a StreetException with its status, or masks any other error as a generic 500 while reporting the real error (with the request's correlation id) via @streetjs/diagnostics — internals never leak.

Example

A complete runnable example lives in src/examples/integration.ts:

npm run example -w packages/router

License

MIT — see LICENSE.