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

@mest-fe/porters-middleware

v0.4.0

Published

供 [Porters](https://github.com/mest-io/porters) 项目使用的 Lambda 中间件,适用于 [Nitric](https://nitric.io/docs/installation) 环境。

Downloads

12

Readme

Porters Middleware

Porters 项目使用的 Lambda 中间件,适用于 Nitric 环境。

介绍

Schedule Middleware

标准化外部调用参数,将定时任务平台或手动调用提供的参数序列化后提供给 Lambda。在调用时,单个 Lambda 可能同时被并行调用多次, 每个函数内部请按当前序列号来决定需要处理的任务批次。

函数参数来自于 request body,序列化后可参考:

  • version: 当前调用版本,hash 字符串
  • current: 当前 Lambda 工作的位置,相对于并发数的序列号,从 1 开始,应当小于 concurrency
  • concurrency: 当前调用版本的并发数
  • ignoreRetry: 是否忽略重试

Auth Middleware

使用后 Lambda 需要验证 header 中 token 字符串是否合法,如果合法则继续调用,否则返回 401。

使用

在 Lambda 函数中嵌套使用中间件:

mainApi.post('/api', async ctx => {
  const handler = () => {
    // do something
  }
  await scheduleMiddleware(ctx, handler)
})

多个中间件请按参数调用嵌套:

const authHandler = next => scheduleMiddleware(next, handler)
await authMiddleware(ctx, authHandler)

错误处理

业务异常

业务中出现需要中断进程的错误,可及时捕获并返回错误信息。

const request = () => {
  throw new Error('something wrong')
} 
await scheduleMiddleware(ctx, () => {
  try {
    request()
  } catch (err) {
    return HTTP.badGateway(ctx, `${err.message}`)
  }
})

运行时的警告

多数场景中,Lambda 并不需要因为单个错误中断流程,可以将错误信息记录到日志,并继续执行。

const request = async () => {
  const result = await Promise.all([...])
  const errors = result.filter(item => item.status === 'rejected')
  
  // 大于一定数量的错误,直接抛出业务异常
  if (errors.length > 5) {
    throw new Error(errors.map(item => item.reason)
      .join('\n'))
  }
  // 允许范围内,统计警告并继续运行
  if (errors.length > 0) {
    errors.forEach(item => globalWarning.add(item.reason))
  }
}