tamper is a Node.js middleware designed for intercepting and modifying HTTP response bodies before they are sent to the client. It provides a simple API for Connect/Express-compatible applications, allowing developers to inspect request and response headers and conditionally apply transformations to the response body. The current stable version, 1.1.0, indicates a mature but largely feature-complete codebase with infrequent updates. Its primary differentiation lies in its focused scope of response body manipulation, offering a callback-based or Promise-based mechanism for transformations. It's particularly useful for tasks like injecting scripts, performing server-side content rewrites, or debugging response payloads without directly altering original handlers. It ships with TypeScript types, enhancing developer experience for type-safe applications.
npm install tamperVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of `tamper` middleware with Express, showing how to conditionally modify HTML response bodies and leave other content types untouched.
Monitor memory usage for applications handling large responses. Limit `tamper` usage to specific routes or content types known to be small.
Ensure `tamper` is configured correctly, and test responses with client-side tools to verify integrity. In some cases, it might be necessary to manually `res.removeHeader('Content-Length')` if issues persist.Always place `tamper` middleware *before* any compression middleware (e.g., `express.compress()`, `compression`) in your middleware stack. This ensures `tamper` operates on the uncompressed text.
Continue using `const tamper = require('tamper');` for CommonJS compatibility. If strictly needing ESM, consider writing a wrapper or using dynamic `import()` within an async function, though this may complicate middleware chaining.As `tamper` is a CommonJS module, you need to use `import tamper from 'tamper'` with a bundler that handles CJS-to-ESM conversion, or switch your file to CommonJS (`.cjs` or `package.json` `"type": "commonjs"`).
Run `npm install tamper` or `yarn add tamper` to install the package. Verify the package is listed in `node_modules` and that `require('tamper')` is spelled correctly.Always ensure your `tamper` logic explicitly checks `res.getHeader('Content-Type')` to only process `text/html`, `text/plain`, or other expected string-based content types. Place `tamper` before compression middleware.