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

fast-stream

v2.2.2

Published

Fast Stream HTTP Server

Downloads

66

Readme

Fast Stream

NPM

Build Status dependencies

Fast Stream HTTP Server

$ npm install fast-stream

Simple server configuration conf, serve all requests with 200 OK.

const http = require('fast-stream');
const conf = {
    '*': { // host name "*" <for all>, "cb" is the callback function
        404: cb => cb('<html><body><h3>Hello World!</h3></body></html>', null, 200)
    }
};
require('net').createServer( // or require('tls') for HTTPS / SSL
    socket => socket.pipe(new http(conf)).pipe(socket)
).listen(80); // or 443 for HTTPS / SSL

Create conf using fast-config module for static files.

const get = require('fast-config');
const http = require('fast-stream');
const conf = {
    '*': get('/path/src') // static files directory
};
require('net').createServer(socket => socket.pipe(new http(conf)).pipe(socket)).listen(80);

Sample conf for files or readable streams, mimehttp optional.

const fs = require('fs');
const mime = require('mimehttp');
const conf = {
    '*': {
        GET: { // method GET
            '/favicon.ico': cb => cb({
                src: '/dir/favicon.ico' // source: file path
            }, { // additional header
                'Content-Type': mime.type.ico
            }),
            '/vid.mp4': cb => cb({
                src: fs.createReadStream('/dir/vid.mp4') // source: readable Stream
            }, { // additional headers
                'Content-Type': mime.type['mp4'],
                'Content-Disposition': 'inline', // display in browser
                'Content-Duration': 171, // required for web video player
                'X-Content-Duration': 171  // video duration in seconds
            })
        }
    }
};

Function host arguments cb, req and this bind example.

const conf = {
    'localhost:80': { // hostname "localhost" port "80"
        GET: { // URL: http://localhost/
            '/': cb => cb('<html><body>' + // attach small files, or remove JSON.stringify(req), see below
                '<form action="/attach.html" method="post" enctype="multipart/form-data">' +
                '<input type="text" name="t1"><input type="text" name="t2"><input type="text" name="t2">' +
                '<input type="file" name="f1"><input type="file" name="f2"><input type="file" name="f2">' +
                '<input type="submit" value="Submit">' +
                '</form>' +
                '</body></html>')
        },
        POST: { // URL: http://localhost/attach.html (method POST only)
            '/attach.html': function host(cb, req) {
                cb('<html><body>' + // client IP address
                    '<h3>' + this._readableState.pipes.remoteAddress + '</h3>' +
                    '<code>' + JSON.stringify(req) + '</code>' +
                    '</body></html>'); // default 'Content-Type' is 'text/html' utf8
            }
        }
    },
    '127.0.0.1:80': { // another host
        GET: { // URL: http://127.0.0.1/
            '/': cb => cb('Request from 127.0.0.1:80', { 'Content-Type': 'text/plain' })
        }
    }
};

http (config, options) class

  • config Object - host functions list, see the examples above
  • options Object - see below

host (cb, req) host function

  • cb Function - callback function, see below
  • req Object - request, see below
  • this Bind Object - this Stream

cb (data, headers, code) callback function

  • data String|Buffer|Object - response, for Object see below
  • headers Object - optional, default null
  • code Number - optional, http status, default 200

data Object response

  • src String|Object - String file path or Object readable stream
  • length Number - optional, data size, required for range bytes when src is readable stream

req Object request

  • path String
  • query Object - header querystring
  • host String
  • hostname String
  • port Number
  • attach Object - when req.request.method is POST, see below
  • request Object - { method: String, uri: String, protocol: String }
  • header Object - { list: Array, hostname: String, port: Number, length: Number, connection: String, type: String, boundary: String, etag: String, modified: String, range: String }

req.attach Object attach

  • when req.header.type is urlencoded - Object querystring from POST body
  • when req.header.type is multipart - Object { query: Object querystring, files: Array Object { name: String, data: Buffer } }

options Object http class argument

  • limit Number - anti memory overhead, request data maximum size, default 5e8 ~500MB, for big data/files, consider to increase this value
  • ranges Boolean - accept ranges request, default true
  • error String - custom error name event, default httpError
  • name String - Server name/version, default fast-stream/2.2, null - to disable
  • cache Boolean - client cache, send/verify "Last-Modified" and/or "ETag" header, default true
  • closeOnError Boolean - close connection on status code >= 400, default false, don't close
  • chunked Number - if body response size is greater than this value, send "Transfer-Encoding: chunked", default 2e7 ~20MB, 0 - to disable

Fast Stream is licensed under the MIT license. See the included LICENSE file for more details.