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

@hitchy/plugin-static

v0.2.4

Published

Hitchy plugin serving static files

Downloads

27

Readme

@hitchy/plugin-static pipeline status

Hitchy plugin serving static files

License

MIT

Install

In a Hitchy-based project run following command for adding this plugin as a dependency:

npm install @hitchy/plugin-static

Configuration

Create a file config/static.js in your Hitchy-based project to export a list of folders for statically exposing any contained file.

exports.static = [
	{
		prefix: "/files",
		folder: "./static/files",
	},
	{
		prefix: "/media",
		folder: "media",
		redirect: true,
	},
	{
		prefix: "/app",
		folder: "app",
		fallback: "index.html",
		mime: {
			// adding new MIME types
			".key": "application/x-my-custom-mime-type",

			// replacing existing ones
			".json": "text/json",
		},
		download: {
			// adding declarations for MIME types to be exposed for download
			"application/x-my-custom-mime-type": true,
			
			// adjusting existing records
			"application/xml": true,
		},
        filter( path, filename, isRequested ) {
            // prevent access on files in folder private/
            if ( path.startsWith( "private/" ) ) {
                throw new this.services.HttpException( 403, "this file is private!" );
            }
        },
        process( path, filename, stream ) {
            // deliver any file's content converted to uppercase, only
            const transformed = new ( require( "stream" ).Transform )( {
                transform( chunk, encoding, callback ) {
                    callback( null, chunk.toString( "utf8" ).toUpperCase() );
                }
            } );
        
            stream.pipe( transformed );
        
            return transformed;
        },
	},
];

On using a configuration like this, requesting pathname /files/some/file.ext will respond with content of file /static/files/some/file.ext. The plugin supports several common types of files while delivering any unknown type of file with announcing its content type as application/octet-stream.

The last example is including additionally supported configuration parameters:

  • fallback selects a file to be delivered on requests for actually missing files. This is useful in combination with delivering files of web applications that handle routes themselves and thus require all its probable routes referring to the application's bootstrap file.

  • mime is a map of filename extensions into MIME types to be exposed when providing file matching either extension. This map is merged with an internal map and may extend the latter or replace existing records with different MIME types.

  • download is a map of MIME types into booleans controlling whether files of either MIME type should be exposed for download or not. The provided map is merged with some internally defined one and may add new entries as well as replace existing ones.

  • filter is an optional callback invoked per request to decide early whether some picked file may be delivered to the client or not.

    Any provided function is invoked with the request's context as this. In addition, provided arguments are

    • the local path name used in requesting URL,
    • the path name of local file to deliver and
    • a boolean set true when tested file is the one actually requested in opposition to the fallback.

    The callback is meant to return if fetching the file is okay. It may throw to prevent retrieval of requested file. By throwing HttpException it may control status returned to the client. In addition, by throwing it with status 404 on the actually requested file the fallback retrieval can be triggered.

  • process is another optional callback invoked on delivering a requested file's content. It is meant to take some opened file stream and replace it with another stream to fetch delivered content from instead.

    As an option, it might take the provided file stream and handle the request all by itself, returning null-ish replacement for provided file.

    Any function provided in process is invoked with the request's context as this. In addition, provided arguments are

    • the local path name used in requesting URL,
    • the path name of local file to deliver and
    • the stream opened for reading.

    The callback should handle errors on provided stream when replacing it. It may return promise for the replacing stream either. It may throw exception or return some eventually rejected promise as well. When throwing or rejecting with HttpException it might affect the response's status code.

  • redirect is a boolean switch controlling whether clients are redirected to existing index.html document on fetching URL of a folder rather than retrieving that found document's content implicitly.

    This redirection was default behaviour in versions before v0.2.0 but has been changed then, so this option must be set true to explicitly keep the previous behaviour.