JSPM

stack-storage

2.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8
  • Score
    100M100P100Q34802F
  • License MIT

This is storage for callstack

Package Exports

  • stack-storage

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (stack-storage) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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