Neverending Plethora of Modules
    Have ideas to improve npm?Join in the discussion! »

    stack-storage

    2.0.1 • Public • Published

    stack-storage

    NPM version build status Test coverage npm download

    an storage on async_hooks, hack callstack.

    Mandatory use through the variable process.storage

    install

    npm install stack-storage

    Usage

    const koa = require('koa')
    const _ = require('lodash')
    const uuid = require('uuid')
    const { promisify } = require('util')
    const Storage = require('stack-storage')
     
    const sleep = promisify(setTimeout)
     
    const app = new koa()
     
    function log(...args) {
      const xRequestId = process.storage.get('x-request-id')
     
      console.log(xRequestId, ...args)
    }
     
    app.use((ctx, next) => {
      process.storage = new Storage([
        ['x-request-id', _.get(ctx.req.headers, 'x-request-id', `api:${uuid.v4()}`)],
      ])
     
      return next()
    })
     
    app.use(async (ctx, next) => {
      const { method, url } = ctx.request
     
      log('Incoming message', { method, url })
     
      await next()
     
      const { status, body } = ctx
     
      log('Server response', { status, body })
    })
     
    app.use(async (ctx) => {
      // Example async
      await sleep(500)
     
      ctx.body = 'Hello World'
    })
     
    app.listen(3000)

    License

    MIT

    Install

    npm i stack-storage

    DownloadsWeekly Downloads

    29

    Version

    2.0.1

    License

    MIT

    Unpacked Size

    5.18 kB

    Total Files

    4

    Last publish

    Collaborators

    • avatar