GithubHelp home page GithubHelp logo

pmb-cl / node-stream-length Goto Github PK

View Code? Open in Web Editor NEW

This project forked from joepie91/node-stream-length

0.0 0.0 0.0 8 KB

For a given Buffer or Stream, this module will attempt to determine the total length of the stream contents.

JavaScript 44.92% CoffeeScript 55.08%

node-stream-length's Introduction

stream-length

Attempts to determine the total content length of a Stream or Buffer.

Supports both Promises and nodebacks.

License

WTFPL or CC0, whichever you prefer. A donation and/or attribution are appreciated, but not required.

Donate

My income consists entirely of donations for my projects. If this module is useful to you, consider making a donation!

You can donate using Bitcoin, PayPal, Gratipay, Flattr, cash-in-mail, SEPA transfers, and pretty much anything else.

Contributing

Pull requests welcome. Please make sure your modifications are in line with the overall code style, and ensure that you're editing the .coffee files, not the .js files.

Build tool of choice is gulp; simply run gulp while developing, and it will watch for changes.

Be aware that by making a pull request, you agree to release your modifications under the licenses stated above.

Supported stream types

  • Buffers
  • fs.createReadStream streams
  • http.request and http.get responses
  • request requests
  • combined-stream2 streams

Usage

Using Promises:

var streamLength = require("stream-length");

Promise.try(function(){
	return streamLength(fs.createReadStream("README.md"));
})
.then(function(result){
	console.log("The length of README.md is " + result);
})
.catch(function(err){
	console.log("Could not determine length. Error: " + err.toString());
});

Using nodebacks:

var streamLength = require("stream-length");

streamLength(fs.createReadStream("README.md"), {}, function(err, result){
	if(err)
	{
		console.log("Could not determine length. Error: " + err.toString());
	}
	else
	{
		console.log("The length of README.md is " + result);
	}
});

Custom lengthRetrievers:

Promise.try(function(){
	return streamLength(fs.createReadStream("README.md"), [
		function(stream, callback){
			doSomethingWith(stream, function(err, len){
				callback(err ? err : len);
			})
		}
	]);
})
.then(function(result){
	console.log("The length of README.md is " + result);
})
.catch(function(err){
	console.log("Could not determine length. Error: " + err.toString());
});

API

streamLength(stream, [options, [callback]])

Determines the length of stream, which can be a supported type of Stream or a Buffer. Optionally you can specify options:

  • lengthRetrievers: An array of (potentially asynchronous) functions for establishing stream lengths. You can specify one or more of these if you wish to extend stream-lengths list of supported Stream types. Each retriever function is called with a signature of (stream, callback) where stream is the stream in question, and callback can be called with the result. If an Error occurs, simply pass the Error to the callback instead of the value.

If you define a callback, it will be treated as a nodeback and called when the function completes. If you don't, the function will return a Promise that resolves when the function completes.

node-stream-length's People

Contributors

pmb-cl 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.