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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lytjs/middleware

v6.9.6

Published

LytJS Middleware Core System - Onion model middleware architecture

Readme

@lytjs/middleware

LytJS 中间件核心系统,支持洋葱圈模型。

npm version license

简介

@lytjs/middleware 是 LytJS 框架的中间件核心系统,提供了洋葱圈模型的中间件处理机制。

核心特性

  • 洋葱圈模型:标准的中间件执行模式
  • 类型安全:完整的 TypeScript 类型支持
  • 零依赖:不引入任何外部依赖
  • 灵活的上下文:支持自定义中间件共享数据
  • 中间件组合:支持链式调用和组合

安装

npm install @lytjs/middleware

或使用 pnpm:

pnpm add @lytjs/middleware

快速开始

创建中间件链

import { createMiddlewareChain } from '@lytjs/middleware';

const chain = createMiddlewareChain();

// 添加中间件
chain.use(async (req, ctx, next) => {
  console.log('Before 1');
  const response = await next();
  console.log('After 1');
  return response;
});

chain.use(async (req, ctx, next) => {
  console.log('Before 2');
  const response = await next();
  console.log('After 2');
  return response;
});

// 执行
const response = await chain.execute(
  new Request('https://example.com'),
  { params: {}, query: new URLSearchParams() },
  (req, ctx) => {
    return new Response('Hello, World!');
  },
);

中间件函数

import { createMiddlewareChain } from '@lytjs/middleware';

const chain = createMiddlewareChain();

// 日志中间件
chain.use((req, ctx, next) => {
  console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
  return next();
});

// 认证中间件
chain.use((req, ctx, next) => {
  const authHeader = req.headers.get('Authorization');
  if (!authHeader) {
    return new Response('Unauthorized', { status: 401 });
  }
  ctx.user = { id: '123', name: 'Test' };
  return next();
});

// 错误处理中间件
chain.use(async (req, ctx, next) => {
  try {
    return await next();
  } catch (error) {
    console.error('Error:', error);
    return new Response('Internal Server Error', { status: 500 });
  }
});

主要 API

createMiddlewareChain()

创建中间件链实例。

import { createMiddlewareChain } from '@lytjs/middleware';

const chain = createMiddlewareChain();

MiddlewareChain 方法

use(middleware | middlewares)

添加中间件到链。

chain.use((req, ctx, next) => {
  // 中间件逻辑
  return next();
});

// 或者添加多个
chain.use([middleware1, middleware2, middleware3]);

execute(request, context, finalHandler)

执行中间件链。

const response = await chain.execute(request, context, (req, ctx) => {
  return new Response('Final response');
});

size

获取中间件数量。

console.log(chain.size);

clear()

清空中间件链。

chain.clear();

类型定义

中间件函数

type MiddlewareFunction = (
  request: Request,
  context: MiddlewareContext,
  next: () => Promise<Response | null | undefined>,
) => Response | null | undefined | Promise<Response | null | undefined>;

中间件上下文

interface MiddlewareContext {
  params: Record<string, string>;
  query: URLSearchParams;
  [key: string]: any;
}

许可证

MIT License - 查看许可证

贡献指南

欢迎提交 Issue 和 Pull Request!