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

safeprompt-middleware

v0.1.0

Published

Vendor-neutral AI security middleware for Express and Next.js. Works with any ai-security-gateway-spec compliant provider.

Readme

safeprompt-middleware

Vendor-neutral AI security middleware for Express and Next.js.

Works with any ai-security-gateway-spec compliant provider. Defaults to SafePrompt.

npm install safeprompt-middleware

Quick Start

Express

import express from 'express';
import { createGuard } from 'safeprompt-middleware';

const app = express();
app.use(express.json());

app.use('/api/chat', createGuard({
  apiKey: process.env.GUARD_API_KEY,
}));

app.post('/api/chat', (req, res) => {
  // req.body.prompt is safe — attach your LLM call here
  res.json({ reply: 'response from LLM' });
});

Next.js (Pages Router)

// pages/api/chat.ts
import { withGuard } from 'safeprompt-middleware/next';
import type { NextApiRequest, NextApiResponse } from 'next';

async function handler(req: NextApiRequest, res: NextApiResponse) {
  // req.body.prompt is safe
  res.json({ reply: 'response from LLM' });
}

export default withGuard(handler, {
  apiKey: process.env.GUARD_API_KEY!,
  fieldName: 'message', // the field to validate
});

Next.js (App Router)

// app/api/chat/route.ts
import { withGuardRoute } from 'safeprompt-middleware/next';

async function POST(req: Request) {
  const { message } = await req.json();
  return Response.json({ reply: 'response from LLM' });
}

export { withGuardRoute(POST, {
  apiKey: process.env.GUARD_API_KEY!,
  fieldName: 'message',
}) as POST };

Configuration

interface GuardConfig {
  provider?: string;    // Default: 'https://api.safeprompt.dev'
  apiKey: string;       // Required
  mode?: 'fast' | 'balanced' | 'strict';  // Default: 'balanced'
  fieldName?: string;   // req.body field to validate. Default: 'prompt'
  failOpen?: boolean;   // Allow through on provider error. Default: false
  onBlock?: (req, res, result) => void;
  onError?: (req, res, error) => void;
}

| Option | Default | Description | |--------|---------|-------------| | provider | https://api.safeprompt.dev | Any ai-security-gateway-spec URL | | apiKey | — | Required. API key for the provider | | mode | balanced | fast (<5ms, patterns only), balanced, strict (always AI) | | fieldName | prompt | Which req.body field to validate | | failOpen | false | Fail-closed by default — blocks on provider error | | onBlock | 400 + threats | Custom handler when a prompt is blocked | | onError | 500 or pass-through | Custom handler when provider is unreachable |


Changing Providers

The default provider is SafePrompt. To use any other conformant provider:

createGuard({
  provider: 'https://your-provider.com',
  apiKey: process.env.YOUR_PROVIDER_KEY,
})

The middleware spec is defined in ai-security-gateway-spec.


What Gets Attached

On a safe request, req.guardResult is set:

req.guardResult = {
  safe: true,
  threats: [],
  confidence: 0.99,
  processingTimeMs: 4,
  passesUsed: 1,
  request_id: 'uuid',
  timestamp: '2026-03-19T...',
}

On a provider error with failOpen: true, req.guardError is set with the error.


License

MIT