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

connect-stream

v1.1.3

Published

A module for streaming static files. Does etags, caching, partial response, etc.

Downloads

62

Readme

Connect Stream

Serving static file with the given path. Inspired by isaacs/st.

  • compatible with return code 200, 206, 304, 403, 404, 500
  • cache fd, stat and gzip content
  • gzip deflatable
  • useful to serve movie to mobile device

Install

$ npm install connect-stream

Usage

Include in your project:

var stream = require('connect-stream'); // function
app.use(stream(path.resolve('public')));

Use in your routes:

app.get(function (req, res) {
  return res.stream('movie.mp4');
});
  • It returns 200 if normal request.
  • It returns 206 if partial request.
  • It returns 304 if ETag or Modified-Since matched.
  • It returns 404 if movie.mp4 not exists.
  • It returns 403 if movie.mp4 permission denied.
  • It returns 500 if error.

How to use

connect-stream respond to Range Request (HTTP 206) correctly.

14.35 Range - W3, RFC2616

Here are all the options described with their defaults values and a few possible settings you might choose to use:

stream = require('connect-stream');

app.use(stream(path.resolve('public'), { // root path for static files. defaults to `process.cwd()`
  trim: false, // do not trim query strings
  trim: true, // trim all query strings using url.parse

  concatenate: 'resolve', // use path.resolve on concatenate root and src path
  concatenate: 'join', // use path.join on concatenate root and src path

  passthrough: true, // calls next() instead of returning a 404 error
  passthrough: false, // returns 404 when a file is not found

  cache: { // specify cache:false to turn off caching entirely
    fd: {
      max: 1000, // number of fd's to hang on to
      maxAge: 1000 * 60 * 60, // amount of ms before fd's expire
    },

    stat: {
      max: 5000, // number of stat objects to hang on to
      maxAge: 1000 * 60, // number of ms that stats are good for
    },

    content: {
      max: 1024 * 1024 * 64, // how much memory to use on caching contents
      maxAge: 1000 * 60 * 10, // how long to cache contents for
    }
  }
}));

var callback = function (err, range, isFirstStream) {
  console.error(err); // instanceof Error or null
  console.log(range);
  // isArray ([ini, end])         : on range request
  // isArray ([0, stat.size - 1]) : on normal request
  // Null                         : on error
  console.log(isFirstStream);
  // bool, request is first range request or not
};

app.get(/^\/(.*)\.mp4$/, function (req, res) {
  res.stream(req.params[0] + '.mp4', callback);
});

var options = {
  headers: {
    'content-type': 'application/octet-stream',
    'cache-control': 'public'
  }
};

app.get(/^\/download\/(.*)\.mp4$/, function (req, res) {
  res.stream(req.params[0] + '.mp4', options, callback);
});

Upgrade Guide (0.x -> 1.x)

interface

app.use(require('connect-stream'));    // old
app.use(require('connect-stream')());  // new

behavior

var stream = require('connect-stream');
app.use(stream(path.resolve('public')));

app.get(function (req, res) {
  res.stream('/tmp/a.mp4');
  // old, always stream "/tmp/a.mp4"
  // new, stream from "public/tmp/a.mp4"
});
var stream = require('connect-stream');
app.use(stream(path.resolve('public'), {
  concatenate: 'resolve'
});

app.get(function (req, res) {
  res.stream('/tmp/a.mp4');
  // old, always stream "/tmp/a.mp4"
  // new, stream from "/tmp/a.mp4"
});

Tips

count up your database on range request (e.g. playing movie)

range-request requests sequential at one request.

you should use filter in callback for count up play-times.

Example:

app.get('/movie.mp4', function (req, res) {
  res.stream('movie.mp4', function (err, range, isFirstStream) {
    if (isFirstStream) {
      countUpYourDatabaseHere();
    }
  });
});

MIT LICENSE

Copyright © 2013 geta6 licensed under MIT

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.