Gathering detailed insights and metrics for express-intercept
Gathering detailed insights and metrics for express-intercept
Gathering detailed insights and metrics for express-intercept
Gathering detailed insights and metrics for express-intercept
express-interceptor
A tiny interceptor for Express responses
@metis-data/sequelize-express-interceptor
Intercept sequelize queries and express requests using OpenTelemetry, enrich spans and send them to metis platform
@metis-data/prisma-express-interceptor
Metis package for prisma and express interception
mock-responses
espress-kind http request middleware
Build Express middleware to intercept / replace / inspect / transform response
npm install express-intercept
Typescript
Module System
Node Version
NPM Version
TypeScript (97.65%)
Makefile (2.35%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
9 Stars
82 Commits
1 Forks
2 Watchers
7 Branches
1 Contributors
Updated on Nov 24, 2024
Latest Version
1.1.1
Package Id
express-intercept@1.1.1
Unpacked Size
41.73 kB
Size
6.75 kB
File Count
9
NPM Version
10.9.0
Node Version
22.11.0
Published on
Nov 24, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
Build Express middleware to intercept / replace / inspect / transform response
1import express from "express";
2import {requestHandler, responseHandler} from "express-intercept";
3
4const app = express();
5
6// replace response string if response Content-Type is html.
7
8app.use(responseHandler().if(res => /html/i.test(res.getHeader("content-type"))).replaceString(body => body.replace(/MacBook/g, "Surface")));
9
10// log access_token for request path: /login
11
12app.use(responseHandler().for(req => (req.path === "/login")).getString(body => console.warn(JSON.parse(body).access_token)));
13
14// dump response body to file if statusCode is Internal Server Error
15
16app.use(responseHandler().if(res => (+res.statusCode === 500)).getBuffer(body => fs.promises.writeFile("debug", body)));
17
18// log cookie sent in request
19
20app.use(requestHandler().getRequest(req => console.warn(req.getHeader("cookie"))));
21
22// log cookie sending in response
23
24app.use(responseHandler().getResponse(res => console.warn(res.getHeader("set-cookie"))));
25
26// transform response as a Readable stream
27
28app.use(responseHandler().interceptStream(upstream => upstream.pipe(new stream.Transform({...}))));
29
30// compress response if response Content-Type is a text type.
31
32app.use(responseHandler().if(res => /text/.test(String(res.getHeader("content-type")))).compressResponse());
33
34// decompress response if statusCode is OK
35
36app.use(responseHandler().if(res => (+res.statusCode === 200)).decompressResponse());
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.
The MIT License (MIT)
Copyright (c) 2020-2024 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.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/6 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More