Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-interceptor

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-interceptor

A tiny interceptor for Express responses

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
42K
decreased by-9.62%
Maintainers
1
Weekly downloads
 
Created
Source

raison d'etre

Born out of the need for a reliable, customized and maintenable middleware we need for Sesame CMS.

Some use cases include:

  • conditionally transform any kind of response
  • have access to responses after they are sent

API

npm i --save express-interceptor


var interceptor = require('express-interceptor');

app.use(interceptor(function(req,res){
  return {
    // define your custom condition to intercept this response
    //   returning `true` cause to buffer this request, and activate methods below
    //   otherwise we ignore it completely
    initerceptPredicate: function(){

    },
    // can transform the body, it's a properly encoded String, 
    //   done(err, string) param[1] will become the new response
    //   may omit secrets, erase words, append stuff, etc.
    send: function(body, done) {

    },
    // useful for caching, keeping stats, etc.
    afterSend: function(oldBody, newBody) {

    }
  };
}));

You can find many other examples at /examples folder - which also happen to be the tests.

technicalities

Express extends Node.js functionalities, that by default:

  • allow setting and modifying of headers until the moment there is a write to the socket, this will submit headers.
  • when initerceptPredicate returns true, we will buffer the contents, preventing .write to be called
  • the whole buffer is presented to send function that can modify response headers right there, as well as return a new body that will be sent right away.
  • afterSend is optional and happens on next tick.
  • this package tries to be minimally obtrusive on the way Node or Express works, original write and end methods hijacked but then at the end.

similar to

words of advice

This module is new, tests are appreciated. There might be edge cases that need fix.

Not recommended to intercept and transform big responses.

Activate debug with DEBUG=express-interceptor npm test

Keywords

FAQs

Package last updated on 06 Mar 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc