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

serverfailsoft

v2.1.1

Published

An extension of ServerResponse that catches errors of the stream being piped to it and closes the connection informatively.

Downloads

20

Readme

ServerFailSoft

A Verbose Error-Handling Extension of http.ServerResponse

If you open a stream and pipe it to ServerFailSoft, those bytes are streamed to the client, just as they would with the base class ServerResponse.

The first bytes piped to ServerFailSoft will trigger the .writeHead function, which will attempt to read statusCode and headers properties from the source. It also attempts to writeHead from source on the end event, in case there wasn't any data.

If the source throws an error (before bytes are sent!), ServerFailSoft will catch it and prepare an informative error response. (Perhaps TOO informative for your tastes if you wish to keep your software versions and stack traces to yourself) - I hope it's clear how to modify the errorresponse object (you can just comment out lines you don't want) to fork this repo and have it suit your tastes.

Usage

Since Node v9.6.0, http.createServer supports an options argument to override the IncomingMessage and ServerResponse constructed for each request.

http.createServer({
    ServerResponse: ServerFailSoft
}, (req, res) => {
    let {pathname} = url.parse(req.url)
    let filepath = path.join(process.cwd(), pathname)
    fs.createReadStream(filepath).pipe(res)
}).listen(3000)

You can run node examples/staticserver.js to try this out. If you request a file that doesn't exist, you'll see the ENOENT error code and everything else.

Besides using built in streams, consider writing your own streams and piping them to ServerFailSoft. Here's an example server where PassThrough (like Transform but without the _transform) is used to create a pipeline that fails half the time, and compliments your good luck the other half.

// an example class that fails half the time
class CoinFlip extends stream.PassThrough {
    constructor(){super()}

    _flush(done){
        if(Math.random() > 0.5){
            done(null, "Good luck.")
        } else {
            done(new Error("bad luck."))
        }
    }
}

http.createServer({
    ServerResponse: ServerFailSoft
}, (req, res) => {
    req.pipe(new CoinFlip).pipe(res)
}).listen(3000)

You can run node examples/luckyserver.js to try it out.

Extending

Pull requests accepted, I've started an errorMap.json to assign proper statusCodes (404 for ENOENT, no entity exists, 423 for EACCESS, insufficient permissions, 416 for the error code thrown by handing nonsensical start and end numbers to fs.createReadStream ).

{
    "ENOENT": 404,
    "ENOTDIR": 404,
    "EACCESS": 423,
    "ERR_OUT_OF_RANGE": 416,
}