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

@beecode/msh-node-session

v3.0.8

Published

[![Build Status](https://beecode.semaphoreci.com/badges/msh-node-session/branches/main.svg?style=shields)](https://beecode.semaphoreci.com/projects/msh-node-session) [![codecov](https://codecov.io/gh/beecode-rs/msh-node-session/branch/main/graph/badge.svg

Downloads

195

Readme

Build Status codecov GitHub license
NPM

msh-node-session

Micro-service helper: node error

This project is intended to be used in typescript project.

Install

npm i @beecode/msh-node-session

Diagram

vision-diagram

Usage

Util implementation example

Class extension

// src/util/session-util.ts

import { NodeSessionUtil } from '@beecode/msh-node-session/dist/node-session-util'
import { FastAlsStrategy } from '@beecode/msh-node-session/dist/session-strategy/fast-als-strategy'
// import { ClsHookedStrategy } from '@beecode/msh-node-session/dist/session-strategy/cls-hooked-strategy'
import { SessionStrategy } from '@beecode/msh-node-session/dist/session-strategy/session-strategy'
import { cacheUtil } from '@beecode/msh-node-util/lib/cache-util.js'

export enum SessionData {
  TYPEORM_ENTITY_MANAGER = 'typeorm-entity-manager',
  AUTH_USER = 'auth-user',
}

export class SessionUtil extends NodeSessionUtil {

  public constructor(params?: { sessionStrategy?: SessionStrategy }) {
    const { sessionStrategy } = params ?? {}
    super({ sessionStrategy })
  }

  public getTransactionManager(): EntityManager | undefined {
    try {
      return this._strategy.get<EntityManager>(SessionData.TYPEORM_ENTITY_MANAGER)
    } catch (_err) {
      return undefined
    }
  }

  protected _setTransactionManager(entityManager: EntityManager): void {
    return this._strategy.set<EntityManager>(SessionData.TYPEORM_ENTITY_MANAGER, entityManager)
  }
  
  public async startTransaction<T>(callback: (transactionEntityManager: EntityManager) => Promise<T>): Promise<T> {
    return this.createAsyncSession(() => {
      const existingTransactionManager = this.getTransactionManager()
      if (existingTransactionManager) return callback(existingTransactionManager)
      return databaseService.getConnection().transaction<T>((newTransEntityManager: EntityManager) => {
        this._setTransactionManager(newTransEntityManager)
        return callback(newTransEntityManager)
      })
    })
  }

  public setAuthUser(authUser: JWTPayloadUser): void {
    this._strategy.set<JWTPayloadUser>(SessionData.AUTH_USER, authUser)
  }

  public getAuthUser(): JWTPayloadUser {
    const authUser = this._strategy.get<JWTPayloadUser>(SessionData.AUTH_USER)
    if (!authUser) throw error.server.internalServerError('Missing auth user from session')
    return authUser
  }

  /**
   * Connect to existing transaction, this is only used in migrations files
   * @param {EntityManager} entityManager
   * @param {() => Promise<T>} callback
   * @returns {Promise<T>}
   */
  public async entityManagerSideCall<T>(entityManager: EntityManager, callback: () => Promise<T>): Promise<T> {
    return this.createAsyncSession(async () => {
      this._setTransactionManager(entityManager)
      return callback()
    })
  }
}

export const sessionStrategy = new FastAlsStrategy()
// export const sessionStrategy = new ClsHookedStrategy()
export const sessionUtil = cacheUtil.singleton(() => new SessionUtil({ sessionStrategy }))

Fastify middleware example

import Fastify from 'fastify'
import { fastifyHelperFactory } from '@beecode/msh-node-session/dist/helpers/fastify-helper'
import { sessionStrategy } from 'src/util/session-util'


const fastify = Fastify()

const fastifyHelper = fastifyHelperFactory({sessionStrategy})

await fastify.register(fastifyHelper.beecodeSessionContextPluginFactory())

Express middleware example

FastAls

import express from 'express'
import { expressFastAlsHelperFactory } from '@beecode/msh-node-session/dist/helpers/express-fast-als-helper'
import { sessionStrategy } from 'src/util/session-util'

const expressApp = express()

const expressFastAlsHelper = expressFastAlsHelperFactory({fastAlsStrategy:sessionStrategy})
this._expressApp.use((req, res, next) => expressFastAlsHelper.expressMiddleware(req, res, next))
// other middlewares
// expressApp.use(... 

ClsHooked

import express from 'express'
import { expressClsHookedHelperFactory } from '@beecode/msh-node-session/dist/helpers/express-cls-hooked-helper'
import { sessionStrategy } from 'src/util/session-util'

const expressApp = express()

const expressClsHookedHelperHelper = expressClsHookedHelperFactory({fastAlsStrategy:sessionStrategy})
this._expressApp.use((req, res, next) => expressClsHookedHelperHelper.expressMiddleware(req, res, next))
this._expressApp.use((req, res, next) => expressClsHookedHelperHelper.expressMiddlewareBindEmitter(req, res, next))
// other middlewares
// expressApp.use(...