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

express-safe-send

v0.3.0

Published

Do not send on already sent Express headers.

Downloads

2

Readme

express-safe-send Build Status codecov JavaScript Style Guide

Do not send on already sent Express headers.

NPM

Install

npm install express-safe-send

The Problem

This solves a problem that arises rarely, but persistently, in production deployments. In express route handlers that have defined timeout handling, Node.js timers and timing behaviour can be slighlty undeterministic, because of the nature of Node and libraries that handle timeouts themselves not properly.

Think of a handler that has a 10s timeout defined in which you make a database call (e.g. to postgres) that itself has a timeout of less than 10 seconds set, because you assign it that safe budget. Node.js event loop delays e.g. through parsing of large incoming data, or time that is not accounted for in your database client, like connection delay, may add to your timeout budget, eventually exceeding it.

When the budget is exceeded your API consumer will already have received headers and assume a timeout. However, your handler might not have knowledge about that fact. A synchronous call (they all are) to express send functions, like res.status(200).json({ users: [] }) will then throw an exception, terminating your server hardly.

This plugin exposes a middleware for just ignoring sending, or extending express functions and adding optional callbacks to notify you about it.

Usage

const safeSend = require('express-safe-send')

const app = express()

app.use(safeSend())
app.use(timeout('10s'))

app.get('/user', function (req, res, next) {
  // make a very long running request, that might time out.
  Users.find({}).toArray(function(err, items) {
    if (err) return next(err)

    // if it (at all) returns the, ignore trying to send the data
    // as Node.js would throw in this process.
    res.status(200).safeJsonSend(items)
  })
})

API

safeSend([options, callback])

options

Type: Object

proxyMode

Type: boolean Default: false

Override behaviour on the send and json functions via a JS proxy.

callback

Type: Function

optional callback for global notification

const safeSend = require('express-safe-send')

const app = express()

app.use(safeSend({ proxyMode: false }, (err, req, res, next) => {
  // an error noting the reason why sending was not possible
  console.log(err)
}))

// or

app.use(safeSend((err, req, res, next) => {
  // an error noting the reason why sending was not possible
  console.log(err)
}))

License

MIT © Robert Jefe Lindstaedt