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

@usageflow/core

v0.5.0

Published

Core functionality for UsageFlow integrations

Readme

@usageflow/core

Core functionality for UsageFlow API tracking. This package provides the base implementation for tracking API usage across different Node.js frameworks.

npm version License: MIT

Installation

npm install @usageflow/core

Overview

@usageflow/core is the foundational package that provides core functionality for UsageFlow integrations. It includes:

  • Base UsageFlowAPI class for framework-agnostic usage tracking
  • WebSocket connection management with connection pooling
  • Route pattern matching and ledger ID generation
  • Request metadata collection and sanitization
  • Configuration management and policy fetching

Usage

This package is typically used indirectly through framework-specific implementations:

If you need to extend UsageFlow for a different framework, you can extend the UsageFlowAPI class:

import { UsageFlowAPI, UsageFlowAPIConfig } from '@usageflow/core';

class CustomFrameworkUsageFlowAPI extends UsageFlowAPI {
  constructor(config: UsageFlowAPIConfig) {
    super(config);
    this.setWebServer('custom');
  }

  // Implement framework-specific methods
}

API Reference

UsageFlowAPIConfig

interface UsageFlowAPIConfig {
  apiKey: string; // Your UsageFlow API key
  poolSize?: number; // Number of WebSocket connections (default: 5)
}

UsageFlowAPI

Base class for all UsageFlow implementations.

Constructor

constructor(config: UsageFlowAPIConfig)

Methods

setWebServer(webServer: 'express' | 'fastify' | 'nestjs'): void

Sets the web server type for route pattern extraction.

getApiKey(): string | null

Returns the configured API key.

getUsageflowUrl(): string

Returns the UsageFlow API URL.

getRoutePattern(request: UsageFlowRequest): string

Extracts the route pattern from a request object. Supports Express, Fastify, and NestJS.

guessLedgerId(request: UsageFlowRequest, overrideUrl?: string): string

Generates a ledger ID for tracking based on the request and configured policies.

createRoutesMap(routes: Route[]): RoutesMap

Converts an array of routes into a map for efficient lookup.

shouldSkipRoute(method: string, url: string, whitelistMap: RoutesMap): boolean

Checks if a route should be skipped based on the whitelist.

shouldMonitorRoute(method: string, url: string, routesMap: RoutesMap): boolean

Checks if a route should be monitored based on the routes map.

sanitizeHeaders(headers: Headers): Headers

Sanitizes sensitive information from headers (authorization tokens, API keys, etc.).

extractBearerToken(authHeader?: string): string | null

Extracts the bearer token from an Authorization header.

decodeJwtUnverified(token: string): Record<string, any> | null

Decodes a JWT token without verifying the signature.

allocationRequest(request: UsageFlowRequest, payload: RequestForAllocation, metadata: RequestMetadata): Promise<void>

Sends an allocation request via WebSocket.

useAllocationRequest(payload: RequestForAllocation): Promise<void>

Finalizes an allocation request.

destroy(): void

Cleans up resources including intervals and WebSocket connections.

UsageFlowSocketManager

Manages WebSocket connections with connection pooling.

Constructor

constructor(apiKey: string, poolSize?: number)

Methods

connect(): Promise<void>

Establishes WebSocket connections in the pool.

sendAsync<T>(payload: UsageFlowSocketMessage): Promise<UsageFlowSocketResponse<T>>

Sends a message and waits for a response.

send(payload: UsageFlowSocketMessage): void

Sends a message without waiting for a response.

isConnected(): boolean

Checks if at least one WebSocket connection is active.

close(): void

Closes all WebSocket connections.

destroy(): void

Cleans up all resources.

Types

Route

interface Route {
  method: string; // HTTP method or '*' for all methods
  url: string; // URL pattern or '*' for all URLs
}

RequestMetadata

interface RequestMetadata {
  method: string;
  url: string;
  rawUrl: string;
  clientIP: string;
  userAgent?: string;
  timestamp: string;
  headers: Record<string, string>;
  queryParams: Record<string, any> | null;
  pathParams: Record<string, any> | null;
  body?: any;
  requestDuration?: number;
}

UsageFlowConfig

interface UsageFlowConfig {
  url: string;
  method: string;
  identityFieldName?: string;
  identityFieldLocation?:
    | 'path_params'
    | 'query_params'
    | 'body'
    | 'bearer_token'
    | 'headers';
}

Requirements

  • Node.js >= 18.0.0
  • TypeScript >= 5.0.0 (for TypeScript projects)

Dependencies

  • axios: HTTP client for API requests
  • ws: WebSocket client library

Development

# Install dependencies
npm install

# Build the package
npm run build

# Run tests
npm test

License

MIT

Support

For issues, questions, or contributions, please visit our GitHub repository.