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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@seepine/hono-global-context

v0.0.2

Published

provide global context

Readme

hono-global-context

npm version npm downloads bundle License

基于 Hono 的中间件,实现类似 Java ThreadLocal 的全局上下文功能。用于在一次请求生命周期内安全地存储和获取数据,适用于日志追踪、用户信息、请求 ID 等场景。

主要场景

在实际开发中,常常需要在 service 层、工具函数等深层调用链中访问请求相关的数据(如用户信息、traceId、请求参数等)。传统做法需要将 context 或相关数据层层传递,导致代码冗余且不易维护。

hono-global-context 通过全局上下文机制,解决了这一痛点:

  • 无需在 service 层显式传递 context
    只需在中间件或 controller 层设置数据,service 层可随时获取,无需参数传递。
  • 简化代码结构
    让业务逻辑更聚焦,减少重复参数传递。
  • 适用于异步和多层调用场景
    保证每个请求的数据隔离,异步场景下也能安全访问。

安装

npm install @seepine/hono-global-context

使用方法

1. 注册中间件

import { Hono } from 'hono'
import { globalContextMiddleware } from 'hono-global-context'

const app = new Hono()
app.use(globalContextMiddleware())

2. 设置数据

app.use(async (c, next) => {
  // 例如根据token设置用户信息
  c.set('user', findUser(c.req.header('token')))
  await next()
})

3. 获取数据

// service/userService.ts
import { getGlobalContext } from 'hono-global-context'

export function getCurrentUser() {
  // 通过 ctx.get() 可以获取到 hono 的 context 实例
  return findById(ctx.get().var.user.userId)
}
// route
import { getCurrentUserId } from './service/userService'

app.get('/userinfo', c => {
  return c.json(getCurrentUser())
})

注意事项

  • 仅在请求生命周期内有效,跨请求不可用
  • 建议不要滥用,更多是为了简化业务层的 context 传递
  • 若公共方法,还是建议显式传递 context