pact-foundation/pact-js

View on GitHub
src/dsl/verifier/proxy/tracer.ts

Summary

Maintainability
A
0 mins
Test Coverage
import express from 'express';
import { pickBy, identity, reduce, Dictionary } from 'lodash';

import logger from '../../../common/logger';

const removeEmptyResponseProperties = (body: string, res: express.Response) =>
  pickBy(
    {
      body,
      headers: reduce(
        res.getHeaders(),
        (
          acc: Dictionary<string | number | string[] | undefined>,
          val,
          index
        ) => {
          acc[index] = val;
          return acc;
        },
        {}
      ),
      status: res.statusCode,
    },
    identity
  );

const removeEmptyRequestProperties = (req: express.Request) =>
  pickBy(
    {
      body: req.body,
      headers: req.headers,
      method: req.method,
      path: req.path,
    },
    identity
  );

export const createResponseTracer =
  (): express.RequestHandler => (_, res, next) => {
    const [oldWrite, oldEnd] = [res.write, res.end];
    const chunks: Buffer[] = [];

    res.write = (chunk: Parameters<typeof res.write>[0]) => {
      chunks.push(Buffer.from(chunk));
      return oldWrite.apply(res, [chunk]);
    };

    res.end = (chunk: Parameters<typeof res.write>[0]) => {
      if (chunk) {
        chunks.push(Buffer.from(chunk));
      }
      const body = Buffer.concat(chunks).toString('utf8');
      logger.debug(
        `outgoing response: ${JSON.stringify(
          removeEmptyResponseProperties(body, res)
        )}`
      );
      return oldEnd.apply(res, [chunk]);
    };
    if (typeof next === 'function') {
      next();
    }
  };

export const createRequestTracer =
  (): express.RequestHandler => (req, _, next) => {
    logger.debug(
      `incoming request: ${JSON.stringify(removeEmptyRequestProperties(req))}`
    );
    next();
  };