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

hostlocal

v1.4.0

Published

Serve files from a directory over HTTP/1.1 and HTTP/2, with live reload notifications over a websocket. It will automatically create self-signed certificates.

Downloads

490

Readme

hostlocal

Serve files from a directory over HTTP/1.1 and HTTP/2, with live reload notifications over a websocket. It will automatically create self-signed certificates.

This server is quite opinionated about its defaults to make setup as easy as possible... if you have a problem shaped like mine.

NOTE:

This server is NOT intended for production use, or any other place where the clients are not fully-trusted. Simplifying assumptions have been made to optimize code size, performance, and usability that make its use in a production context inappropriate. Security issues that are filed that do not take this note into account will not be prioritized.

Run

# Serve the docs directory, and open index.html in your default browser.
npx hostlocal docs

CLI

Usage: hostlocal [options] [directory]

Arguments:
  directory                   Directory to serve. (default: cwd)

Options:
  -6, --ipv6                  Listen on IPv6 only, if host supports both IPv4
                              and IPv6.
  -c, --config <file>         If the given file exists, import it as a module
                              and use its default export as the options.  Name
                              is relative to cwd. Command line parameters
                              overwrite options from the config file. (default:
                              ".hostlocal.js")
  --certDir <directory>       Directory, relative to cwd, to cache cert info.
                              (default: ".cert")
  -e, --exec <shell command>  Execute this command when the glob changes.
                              (default: "npm run build")
  -g, --glob <pattern>        Set of files to watch.  If one of these changes,
                              execute the command in the --exec option.  Can be
                              specified multiple times.
  -h, --help                  display help for command
  -H, --host <address>        Hostname or IP address to listen on. "::" for
                              everything. (default: "localhost")
  -i, --initial               If glob is specified, run the exec command on
                              startup, before listening.
  --logFile <file>            If specified, JSON logs are written to this file.
  --no-script                 If specified, JavScript will not be added to the
                              end of HTML files to do auto-refresh.
  --notAfterDays <number>     How many days is the certificate valid? (default:
                              7)
  -o, --open <path>           Open this path in the default browser.  Relative
                              to server root and prefix, if specified.  If
                              empty (""), do not open anything. (default: ".")
  -O, --no-open               Do not open a page in the default browser.
  -p, --port <number>         Port to serve content from.  Use 0 to get an
                              unused port. (default: 8111)
  -P, --prefix <string>       Make all of the URLs served have paths that start
                              with this prefix, followed by a slash.
  -q, --quiet                 Do less logging.  Can be specified more than
                              once.
  --rawMarkdown               Do not process markdown into HTML.
  -t, --timeout <number>      Time, in ms, to allow exec to run.
  -v, --verbose               Do more logging.  Can be specified more than
                              once.
  -V, --version               output the version number

Config files

Instead of using the command line, you can store your configuration in a file, by default called ".hostlocal.js". This is an ES6 module with a default export containing your config. Its default value is:

/**
 * @type {import('./lib/index.js').HostOptions}
 */
export default {
  certDir: '.cert',
  config: '.hostlocal.js',
  dir: process.cwd(),
  exec: 'npm run build',
  CGI: {},
  glob: [],
  headers: {},
  host: 'localhost',
  index: ['index.html', 'index.htm', 'README.md'],
  initial: false,
  ipv6: false,
  logLevel: 0,
  logFile: null,
  notAfterDays: 7,
  open: '.',
  port: 8111,
  prefix: '',
  rawMarkdown: false,
  script: true,
  signal: null,
  timeout: null,
};

Command-line options take precedence over these. Most of these match their command line options for semantics. Others:

  • dir: Serve this directory (tied to optional directory in CLI).
  • CGI: Object containing a map from original mime type to command. The command is any shell command that emits at least a content-type header with string CR/LF in the header. NOTE: This feature has a high probability of generating a security issue when you misconfigure it and allow external access. This is an example that might work sometimes, but would be catastrophic with untrusted inputs:
{
  "CGI": {
    "application/x-httpd-php": "php-cgi",
  }
}
  • index: Array of strings for files to search for if a directory is requested.
  • logLevel: The sum of the number of -v options (+1 each) an -q options (-1 each). -3: fatal, -2: error, -1: warn, 0: info, 1: debug, 2: trace.
  • open: Specify "" or false to not open a file in the default browser.
  • script: The opposite of the --no-script CLI option.
  • signal: An abort signal that can be used to shut down the server as cleanly as possible.

API

Full API documentation is available.

Example:

import {hostLocal} from 'hostlocal';

const server = await hostLocal({port: 8111});
server.on('listen', baseURL => console.log(baseURL.toString()));
await server.start();

Tests codecov