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

toon-middleware

v1.0.0

Published

Zero-config Express & Fastify middleware for automatic JSON ↔ TOON conversion with 30–60% LLM token savings

Downloads

156

Readme

toon-middleware

npm version license tests

Zero-config Express & Fastify middleware for automatic JSON ↔ TOON conversion — reduce LLM API token costs by 30–60% with one line of setup.


Installation

npm install toon-middleware

Quick Start — Express

import express from 'express'
import { toonMiddleware } from 'toon-middleware'

const app = express()
app.use(express.json())
app.use(toonMiddleware({ statsHeader: true }))

app.get('/users', (req, res) => {
  res.json([{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }])
  // → If client sends Accept: text/toon, response is TOON-encoded automatically
})

Quick Start — Fastify

import Fastify from 'fastify'
import toonPlugin from 'toon-middleware/fastify'

const fastify = Fastify()
await fastify.register(toonPlugin, { statsHeader: true, threshold: 0.15 })

fastify.get('/products', async () => {
  return [{ id: 1, name: 'Laptop', price: 999 }]
  // → Auto-encoded to TOON when client requests it
})

Options

| Option | Type | Default | Description | |---------------|------------|---------|-----------------------------------------------------------------------| | encode | boolean | true | Encode JSON responses to TOON format when client accepts it | | decode | boolean | true | Decode TOON request bodies to plain JS objects before route handlers | | strict | boolean | false | Throw/400 on decode failure instead of silently falling back to JSON | | statsHeader | boolean | false | Add X-TOON-Token-Savings: 42.3% header to encoded responses | | threshold | number | 0.1 | Minimum savings fraction (0–1) required before encoding (e.g. 0.1 = 10%) | | routes | string[] | undefined | Glob patterns — middleware only activates on matching paths (e.g. ['/api/*']) |


Per-Route Usage

Apply TOON to a single route instead of globally:

import { toon } from 'toon-middleware'

app.get('/heavy-data', toon({ statsHeader: true }), (req, res) => {
  res.json(bigDataset)
})

How It Works

TOON middleware intercepts traffic in two directions:

Request Decoding (TOON → JSON)

  1. Client sends Content-Type: text/toon with a TOON-encoded body
  2. Middleware reads the raw body, calls decode() from @toon-format/toon
  3. Decoded JS object is assigned to req.body before your route handler runs
  4. Your handler sees plain JSON — no changes required

Response Encoding (JSON → TOON)

  1. Client sends Accept: text/toon in the request
  2. Middleware wraps res.json() (Express) or hooks onSend (Fastify)
  3. Before encoding, it checks if token savings exceed the threshold
  4. If worthwhile: encodes to TOON string, sets Content-Type: text/toon, sends
  5. If not worthwhile or encoding fails: falls through to normal JSON response

When TOON Helps

TOON excels at compressing uniform arrays — data where the same keys repeat across many objects:

[
  { "id": 1, "name": "Alice", "role": "admin", "active": true },
  { "id": 2, "name": "Bob",   "role": "user",  "active": false },
  ...hundreds more
]

Typical savings: 30–60% fewer tokens compared to JSON when sent to GPT/Claude APIs.

When TOON Doesn't Help

  • Deeply nested, heterogeneous objects — unique keys don't compress well
  • Short single-object responses — overhead of the TOON header may exceed savings
  • Binary or non-JSON payloads — middleware passes these through untouched

The threshold option lets you tune the break-even point. At the default 0.1 (10%), responses with less than 10% token savings are sent as plain JSON.


Token Savings Header

Enable statsHeader: true to see exact savings per response:

X-TOON-Token-Savings: 47.2%

Useful for monitoring and deciding whether to lower or raise threshold.