GithubHelp home page GithubHelp logo

sbquinlan / stream-static Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 49 KB

A composable, stream-based version of the 'serve-static', 'compression', 'send' modules.

License: MIT License

TypeScript 27.05% JavaScript 72.95%
expressjs javascript node nodejs typescript serve-static streams

stream-static's Introduction

stream-static

NPM Version NPM Downloads Linux Build

This is a rewrite of the npm module serve-static and compression for Node v16+. This module offers a composable solution with components offer specific, minimal functionality and limited dependences by emphasizing the Stream APIs provided by Node.js from the filesystem, compression and HTTP.

Why use this instead?

serve-static and compression are difficult to work with if you want to customize their functionality beyond what is provided. This module is written so that it's easy to quickly customize the implementation to suit your needs.

That said, in the author's opinion, nobody should use either serve-static or this module in production because neither are as performant nor as robust as many CDN offerings that serve static content.

Why use serve-static and compression instead?

serve-static and compression support Node before v16.

serve-static provides some features as configuration options that the author of this module felt were "scope creep" or trivial for consumers to implement themselves. For example, serve-static offers a fallthrough option that will replace the 404 response on non existant files with a "fallthrough" to the next handler in the routing middleware. This feature is fairly easy implemented.

For a full comparison of features differences, diff the tests in this module with the tests in serve-static and compression.

Install

$ npm install stream-static

or

$ yarn add stream-static

Dependencies:

> yarn list --prod
yarn list v1.22.15
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
└─ [email protected]
✨  Done in 0.05s.

API

import { 
  streamStatic,
  normalize_path,
  send,
  basicheaders,
  byterange,
  conditionals,
  compression,
} from 'stream-static'

The provided streamStatic function is a composition itself of the other functions

async function streamStatic(
  root: string, 
  req: IncomingMessage, 
  res: ServerResponse,
  maxage: number = 0,
): Promise<Readable> {
  if (!root) throw new Error('root path required');
  if (req.method === 'POST') {
    throw error(404)
  }
  if (req.method !== 'HEAD' && req.method !== 'GET') {
    res.setHeader('Allow', 'GET, HEAD')
    throw error(405)
  }
  let { path, stat, stream } = await send(root, req.url!);
  try {
    basicheaders(res, path, stat, maxage)
    conditionals(req, res)
  } catch(err) {
    stream.destroy();
    throw err;
  }
  return stream.pipe(byterange(req, res));
}

Or implement your own custom handler serving static files:

async function static_compression(root: string, converter: Transform = null): Promise<void> {
  return async (req, res, next) => {
    if (req.method !== 'GET' && req.method !== 'HEAD') {
      res.statusCode = 405;
      res.setHeader('Allow', 'GET, HEAD')
      res.setHeader('Content-Length', 0)
      res.end();
      return;
    }
    let stream;
    try {
      stream = await streamStatic(resolve(root), req, res);
      if (req.method === 'HEAD') {
        stream.destroy();
        return;
      }
      await pipeline(
        stream,
        byterange(req, res),
        converter ? converter(req, res) : new PassThrough(),
        compression(req, res),
        res,
      );
      res.end();
    } catch (err) {
      console.error(err);
      res.statusCode = isHttpError(err) ? err.statusCode : 500
      if (!res.headersSent) {
        res.setHeader('Content-Length', 0);
      }
      res.end();
      stream?.destroy();
    }
  };
}

Documentation

This replicates the functionality of serve-static if you are looking for a replacement (be mindful of some feature differences described above). This function will return a stream of the requested file in root + req.url. If no file exists it will throw a 404. It also supports byte-ranging requests, cache-control, and conditional header requests.

This validate the path through some simple rules and then resolve it to an absolute path.

This replicates the send module that serve-static uses to validate the file path and open a file. It will validate the path from the URI (using normalize_path), the file exists and is not a directory before returning a Readable stream.

This function will add basic headers to the response based on the file described in stats. This populates headers: Content-Type, Content-Length, Cache-Control, Etag and Last-Modified

This function implements the byte range request logic to support Partial Responses based on the Range header, returning a Duplex stream that should be used as a Transform.

This function implements the handling of conditional headers: if-match, if-none-match, if-unmodified-since, and if-modified-since. This includes support for etags.

This is the replacement for compression. It will respect the requested encoding and return a Transform stream (or PassThrough) to either do the compression or not based on what the client requested.

License

MIT

stream-static's People

Contributors

sbquinlan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.