Registry / http-networking / throwback

throwback

JSON →
library4.1.0jsnpmunverified

Throwback is a lightweight JavaScript/TypeScript library that implements a simple asynchronous middleware pattern, designed to compose promise-returning functions. It is currently in its stable version 4.1.0 and maintains an active release cadence, frequently introducing breaking changes in major versions to refine its API. Key differentiators include its strict focus on a single `ctx` argument for improved TypeScript inference (since v3.0.0), its reliance solely on native Promises (since v2.0.0), and its inspiration from Koa's middleware composition, aiming for a minimalistic yet powerful approach for processing pipelines. It is commonly used in server-side HTTP applications (e.g., Servie) and client-side request libraries (e.g., Popsicle), providing development-time debugging aids that are automatically excluded from production builds.

npm install throwback
INSTALL
IMPORT
SIG · THROWBACK
T
throwback
http-networkingjavascriptv4.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

compose
import { compose } from 'throwback';
const { compose } = require('throwback');
While CommonJS `require` is shown in old examples, ESM `import` is the idiomatic way for modern TypeScript/JavaScript projects. Throwback ships with TypeScript types.
MiddlewareFunction
import type { MiddlewareFunction } from 'throwback';
Import the `MiddlewareFunction` type for strongly typing your middleware functions in TypeScript.
NextFunction
import type { NextFunction } from 'throwback';
Import the `NextFunction` type for strongly typing the `next` argument in your middleware functions in TypeScript.

Demonstrates how to compose asynchronous middleware functions and execute a stack with a context object and a final callback, showing the order of execution.

import { compose } from 'throwback'; const middlewareStack = [ async function firstMiddleware(ctx, next) { console.log('1: Before next()'); // 'next()' calls the next middleware in the stack or the final callback. await next(); console.log('4: After next()'); }, async function secondMiddleware(ctx, next) { console.log('2: Calling next()'); // You can optionally pass a new context to 'next()' which will replace it // for subsequent middleware in the stack and the final callback (since v3). // This is useful for request retries or modifying the context downstream. await next(); console.log('3: Returning from next()'); } ]; const composedApp = compose(middlewareStack); // The composed function takes a context object and an optional final callback. // The final callback runs at the end of the middleware stack, before the // execution bubbles back up through the middleware. composedApp({}, function finalCallback(ctx) { console.log('5: Final callback executed. Context:', ctx); ctx.status = 200; }); /* Expected output: 1: Before next() 2: Calling next() 5: Final callback executed. Context: { status: 200 } 3: Returning from next() 4: After next() */
Debug
Known issues
breakingThe `debugMode` configuration option was removed. Debugging behavior is now solely controlled by `NODE_ENV`. Setting `NODE_ENV !== 'production'` enables debug checks and verbose errors.
fix
Remove any explicit `debugMode` configurations. Ensure your `NODE_ENV` is correctly set for development or production environments.
affects: >=4.1.0
breakingThe ability to call `next(ctx)` directly to pass a modified context down the stack has been disabled. `next()` no longer accepts an argument. If you need to modify the context for subsequent middleware, it's recommended to create a wrapper function.
fix
Instead of `await next(modifiedCtx);`, call `await next();` and modify `ctx` directly. For deep changes, consider a wrapper like `(fn) => (ctx, next) => fn(ctx.clone(), next)` if your context supports cloning.
affects: >=4.0.0
breakingMiddleware functions now accept only a single `ctx` argument. The previous pattern of `(ctx, next)` is no longer supported for the middleware function signature itself, though the `next` function is still passed as a second argument to the wrapper.
fix
Update your middleware functions to accept a single `ctx` argument. The `next` function is still accessible within the composed context's scope or by wrapping. Note: The `next()` function *can* accept an optional `ctx` argument to replace the context downstream since v3.0.0, but this was removed in v4.0.0.
affects: >=3.0.0 <4.0.0
breakingThe library no longer uses `any-promise` and relies exclusively on the native global `Promise` constructor. This might break in environments without native Promise support or if `any-promise` was used for specific polyfills.
fix
Ensure your runtime environment (Node.js or browser) provides a native `Promise` implementation. If targeting older environments, you must provide your own Promise polyfill globally before `throwback` is loaded.
affects: >=2.0.0
gotchaIn development (`NODE_ENV !== 'production'`), `compose` will throw specific errors for unexpected behavior (e.g., calling `next` multiple times). These checks are stripped in production builds for performance.
fix
Pay attention to development-time errors to ensure correct middleware behavior. Do not rely on these error checks being present in production.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: next is not a function
Attempting to call `next()` when it's not correctly provided to the middleware or trying to pass `ctx` to `next()` after v4.0.0 which disabled this pattern.
fix
Ensure your middleware signature correctly receives `next` and that you are not passing an argument to `next()` if using version 4.0.0 or higher. For versions < 4.0.0, `next(ctx)` was allowed.
Middleware function must be a generator or return a Promise
A middleware function passed to `compose` is not an `async` function and does not explicitly return a Promise.
fix
Ensure all functions in the middleware array are `async` functions or explicitly return a `Promise`. Example: `async function myMiddleware(ctx, next) { /* ... */ await next(); }`
Error: next() called multiple times
A middleware function called `next()` more than once, which is an anti-pattern for this type of middleware flow and triggers a development-mode error.
fix
Review your middleware logic. Each middleware function should call `await next()` at most once. If you need to short-circuit, simply omit calling `next()`.
TypeError: ctx.clone is not a function (or similar context-related error)
Attempting to use `ctx.clone()` as suggested in some `throwback` examples, but your specific `ctx` object does not have a `clone` method.
fix
The `ctx` object is user-defined. If you need cloning functionality, you must implement it within your custom context object. The suggestion in the `throwback` documentation is a pattern recommendation, not an implicit feature of `throwback` itself.
Upgrade
Version history
4.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
10
OpenAI (training)
2
Resources
throwback — npm install throwback · libregistry