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-auditor

v0.0.6

Published

Audit express requests and responses

Downloads

28

Readme

express-auditor

Audit express requests and responses

Installation

Install this package in your NodeJS project

$ yarn add express-auditor

or

$ npm i express-auditor

Getting started

Creating a auditor instance

import express from 'express'
import { createAuditor } from 'express-auditor'

const app = express()

// create auditor and middleware instance
const { auditor, handler, errorHandler } = createAuditor(/* options */)

// setup the body/response types to auditor catch him
app.use(express.json())

// the handler object return is the express middleware
app.use(handler)

/*
  routes, middlewares, ...etc
*/

// put errorHandler after all definitions to catch uncaught exceptions in your routes
app.use(errorHandler)

app.listen(3000, () => console.log('app is running'))

Using audit in routes

The audit property is injected in all Request object

app.use('/', (request: Request, response: Response) => {
  // request.audit.doSomething

  /* do something */
})

By default two plugins are available

  • request.audit.execution: this plugin is independent, he collect request, response, start time and and time data from each HTTP call
  • request.audit.metadata: this plugin provide methods to make audit more rich
    • setUser(username): specify why user are execution request
    • setType(type): request action type, like 'CREATE_NEW_USER' to post filters
    • setDescription(description): Action description to post consume
    • addObject(object): string array, used to specify why objects the request is manipulating
    • addDetail(detail): string array, to provide details about actions, like 'Default permissions applied to new user'
    • addChange({ property, from, to }): object array, to register all changes from registered type in the object list

Call this methods are optional, but add rich data to post consume

Listening audited data

With auditor instance you can listen when response has sended to client

// Use this callback to save or show audited request
auditor.on('finish', (store) => {
  console.log(store)
  /*
    this go print in your terminal:
    {
      metadata: {
        executedAt: Date,
        objects: Array,
        details: Array,
        changes: Array
      },
      execution: {
        startAt: number,
        finishedAt: number,

        request: {
          body: object,
          method: string,
          url: string,
          params: object,
          query: object[],
          headers: object[],
          protocol: string,
          ip: string
        },

        response: {
          body: string,
          headers: object,
          statusCode: number,
          statusMessage: string
        },

        exception?: {
          name: string,
          message: string,
          stack: StackTrace[]
        }
      }
    }
  */
})

Options

In createAuditor you can pass the following options

Filter

Pass filter option to filter which requests/responses can be audited, if filter return false, audition is stopped and finish callback are not called

{
  filter: {
    request: {
      // HTTP verbs
      methods: ['GET', 'POST', 'PUT', 'DELETE']
    },

    response: {
      // 'Content-Type' header value
      contentType: ['application/json']
    }
  },
}

// OR

{
  filter: {
    request: (request: Request) => {
      /* ...some verification */
      
      return true
    },
    
    response: (response: Response) => {
      /* ...some verification */
      
      return true
    }
  }
}

Plugins

Pass plugins option you can add external/custom features do audition

{
  // Array with external/custom plugins
  plugins: [/* some plugins */],
}

Custom plugin example:

{
  plugins: [
    {
      // property name to be create in `request.audit` object
      name: 'name',

      create(req, res) {
        const store = {};

        return {
          // plugin state
          store,

          // injected actions in `request.audit.{name}` object
          plugin: {
            foobar() {
              // your can perform changes in state using plugin actions
              store.name = 'foobar'

              console.log('foobar')
            }
          },

          finish(store) {
            // store: root state of the audition

            // you can call actions using `this.plugin.foobar`
          }
        }
      }
    }
  ]
}

// now in all express route you can call `foobar()`

app.get('/', (request, response) => {
  request.audit.name.foobar() // 'foobar'

  response.send('o/')
})