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

@uswitch/koa-timeout

v1.6.11

Published

⏰ A Koa middleware to handle timeouts correctly

Downloads

8,272

Readme

Contributors License type language test style

How it works

koa-timeout uses Promise.race to race a setTimeout against your Koa middlewares/response.

Timeout example

Middlewares
              A 1s     B 2s       C 3s      D 4s
         ╭─────┴────────┴─────╳╌╌╌╌┴╌╌╌╌╌╌╌╌╌┴╌╌╌╌╌
Req ─────┤
         ╰────────────────────┬──→ 408 - 2500ms
                           Timeout
                             2.5s

In this example, a request has come in and the timeout is racing middlewares, A, B, C & D. However, the timeout is triggered causing a 408 response after 2500ms.

The signifys a short circuit and prevents middlewares C & D from running.

Successful example

Middlewares
              A 1s     B 2s      C 3s      D 4s
         ╭─────┴────────┴─────────┴─────────┴──────╮
         │                                         │
Req ─────┤                                         ╰──→ 200 - 4500ms
         │
         ╰─────────────────────────────────────────╳╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╮
                                                                     Timeout
                                                                        5s

In this example, all 4 middlewares - A, B, C & D - have finished, resulting in a 200 response after ~4500ms. The timeout is then cleared, signified by the .

Usage

The middleware can be configured with a custom timeout time and status code.

import Koa from 'koa'
import timeout from '@uswitch/koa-timeout'

const app = new Koa()

app.use(timeout(1000))                     // 1s timeout
app.use(timeout(1000, { status: 499 }))    // 1s timeout with 499 status

Short circuiting

N.B. By default, koa-timeout does not short circuit the other middlewares. This means that the in-flight request will continue to be processed even though a response has already been sent.

This behaviour can be useful in development to see where the slow parts of the request are, however, in production you probably want to kill the request as soon as possible.

To enable short circuiting

This will prevent the next middleware after a timeout from triggering.

import { shortCircuit } from '@uswitch/koa-timeout'

app.use(timeout(5000))

app.get('*', shortCircuit(handleInitialState))
app.get('*', shortCircuit(handleRendering))
app.get('*', shortCircuit(handleReturn))

// Or

app.get('*', ...shortCircuit(handleInitialState, handleRendering, handleReturn))