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 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-intercept

v1.1.0

Published

Build Express middleware to intercept / replace / inspect / transform response

Downloads

49,443

Readme

express-intercept

Build Express middleware to intercept / replace / inspect / transform response

Node.js CI npm version

SYNOPSIS

const express = require("express");
const {requestHandler, responseHandler} = require("express-intercept");
const app = express();

// replace response string if response Content-Type is html.

app.use(responseHandler().if(res => /html/i.test(res.getHeader("content-type"))).replaceString(body => body.replace(/MacBook/g, "Surface")));

// log access_token for request path: /login

app.use(responseHandler().for(req => (req.path === "/login")).getString(body => console.warn(JSON.parse(body).access_token)));

// dump response body to file if statusCode is Internal Server Error

app.use(responseHandler().if(res => (+res.statusCode === 500)).getBuffer(body => fs.promises.writeFile("debug", body)));

// log cookie sent in request

app.use(requestHandler().getRequest(req => console.warn(req.getHeader("cookie"))));

// log cookie sending in response

app.use(responseHandler().getResponse(res => console.warn(res.getHeader("set-cookie"))));

// transform response as a Readable stream

app.use(responseHandler().interceptStream(upstream => upstream.pipe(new stream.Transform({...}))));

// compress response if response Content-Type is a text type.

app.use(responseHandler().if(res => /text/.test(String(res.getHeader("content-type")))).compressResponse());

// decompress response if statusCode is OK

app.use(responseHandler().if(res => (+res.statusCode === 200)).decompressResponse());

METHODS

See TypeScript declaration express-intercept.d.ts for more detail.

for(condition: (req: Request) => boolean)

It appends a test condition to perform the RequestHandler. Call this for multiple times to add multiple tests in AND condition. Those tests could avoid unnecessary work later.

if(condition: (res: Response) => boolean)

It appends a test condition to perform the RequestHandler. Call this for multiple times to add multiple tests in AND condition. Those tests could avoid unnecessary response interception work including additional buffering.

replaceString(replacer: (body: string, req: Request, res: Response) => string)

It returns a RequestHandler to replace the response content body as a string. It manages the response stream even when chunked or compressed.

replaceBuffer(replacer: (body: Buffer, req: Request, res: Response) => Buffer)

It returns a RequestHandler to replace the response content body as a Buffer. It manages the response stream even when chunked or compressed.

interceptStream(interceptor: (upstream: Readable, req: Request, res: Response) => Readable)

It returns a RequestHandler to replace the response content body as a stream.Readable. It passes raw response as a stream.Readable whether compressed or not. Interceptor should return yet another stream.Readable to perform transform the stream. Interceptor would use stream.Transform for most cases as it is a Readable. Interceptor could return null or the upstream itself as given if transformation not happened.

getString(receiver: (body: string, req: Request, res: Response) => void)

It returns a RequestHandler to retrieve the response content body as a string. It manages the response stream even when chunked or compressed.

getBuffer(receiver: (body: Buffer, req: Request, res: Response) => void)

It returns a RequestHandler to retrieve the response content body as a Buffer. It manages the response stream even when chunked or compressed.

getRequest(receiver: (req: Request) => void)

It returns a RequestHandler to inspect express Request object (aka req). With requestHandler(), it works at request phase as normal RequestHandler works. With responseHandler(), it works at response returning phase after res.send() fired.

getResponse(receiver: (res: Response) => void)

It returns a RequestHandler to inspect express Response object (aka res). It works at response returning phase after res.send() fired.

use(handler: RequestHandler, ...more)

It returns a RequestHandler which connects multiple RequestHandlers. Use this after requestHandler() method but not after responseHandler().

compressResponse()

It returns a RequestHandler to compress the response content.

decompressResponse()

It returns a RequestHandler to decompress the response content.

SEE ALSO

  • https://github.com/kawanet/async-request-handler
  • https://github.com/kawanet/express-compress
  • https://github.com/kawanet/express-intercept
  • https://github.com/kawanet/express-sed
  • https://github.com/kawanet/express-tee
  • https://github.com/kawanet/middleware-supertest
  • https://github.com/kawanet/weboverlay

LICENSE

The MIT License (MIT)

Copyright (c) 2020-2023 Yusuke Kawasaki

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.