Browse Source

v1.0.2: Update documentation, add combined-stream2 support, remove stray console.log

master
Sven Slootweg 7 years ago
parent
commit
49826545e4
  1. 10
      README.md
  2. 11
      lib/stream-length.coffee
  3. 17
      lib/stream-length.js
  4. 2
      package.json

10
README.md

@ -6,7 +6,7 @@ Supports both Promises and nodebacks.
## License
[WTFPL](http://www.wtfpl.net/txt/copying/) or [CC0](https://creativecommons.org/publicdomain/zero/1.0/), whichever you prefer.
[WTFPL](http://www.wtfpl.net/txt/copying/) or [CC0](https://creativecommons.org/publicdomain/zero/1.0/), whichever you prefer. A donation and/or attribution are appreciated, but not required.
## Donate
@ -22,6 +22,14 @@ Build tool of choice is `gulp`; simply run `gulp` while developing, and it will
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:

11
lib/stream-length.coffee

@ -9,7 +9,6 @@ createRetrieverPromise = (stream, retriever) ->
retriever stream, (result) ->
if result?
if result instanceof Error
console.log "REJECTING...", result
reject result
else
resolve result
@ -55,6 +54,14 @@ retrieveRequestHttpStream = (stream, callback) ->
else
callback null
retrieveCombinedStream = (stream, callback) ->
if stream.getCombinedStreamLength?
stream.getCombinedStreamLength()
.then (length) -> callback(length)
.catch (err) -> callback(err)
else
callback null
module.exports = (stream, options = {}, callback) ->
nodeifyWrapper callback, ->
@ -66,7 +73,7 @@ module.exports = (stream, options = {}, callback) ->
retrieverPromises.push createRetrieverPromise(stream, retriever)
# Then, the standard ones.
for retriever in [retrieveBuffer, retrieveFilesystemStream, retrieveCoreHttpStream, retrieveRequestHttpStream]
for retriever in [retrieveBuffer, retrieveFilesystemStream, retrieveCoreHttpStream, retrieveRequestHttpStream, retrieveCombinedStream]
retrieverPromises.push createRetrieverPromise(stream, retriever)
Promise.any retrieverPromises

17
lib/stream-length.js

@ -1,4 +1,4 @@
var Promise, createRetrieverPromise, fs, nodeifyWrapper, retrieveBuffer, retrieveCoreHttpStream, retrieveFilesystemStream, retrieveRequestHttpStream;
var Promise, createRetrieverPromise, fs, nodeifyWrapper, retrieveBuffer, retrieveCombinedStream, retrieveCoreHttpStream, retrieveFilesystemStream, retrieveRequestHttpStream;
Promise = require("bluebird");
@ -13,7 +13,6 @@ createRetrieverPromise = function(stream, retriever) {
return retriever(stream, function(result) {
if (result != null) {
if (result instanceof Error) {
console.log("REJECTING...", result);
return reject(result);
} else {
return resolve(result);
@ -75,6 +74,18 @@ retrieveRequestHttpStream = function(stream, callback) {
}
};
retrieveCombinedStream = function(stream, callback) {
if (stream.getCombinedStreamLength != null) {
return stream.getCombinedStreamLength().then(function(length) {
return callback(length);
})["catch"](function(err) {
return callback(err);
});
} else {
return callback(null);
}
};
module.exports = function(stream, options, callback) {
if (options == null) {
options = {};
@ -89,7 +100,7 @@ module.exports = function(stream, options, callback) {
retrieverPromises.push(createRetrieverPromise(stream, retriever));
}
}
_ref1 = [retrieveBuffer, retrieveFilesystemStream, retrieveCoreHttpStream, retrieveRequestHttpStream];
_ref1 = [retrieveBuffer, retrieveFilesystemStream, retrieveCoreHttpStream, retrieveRequestHttpStream, retrieveCombinedStream];
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
retriever = _ref1[_j];
retrieverPromises.push(createRetrieverPromise(stream, retriever));

2
package.json

@ -1,6 +1,6 @@
{
"name": "stream-length",
"version": "1.0.1",
"version": "1.0.2",
"description": "For a given Buffer or Stream, this module will attempt to determine the total length of the stream contents. It currently supports Buffers, `fs` streams, `http` responses, and `request` objects, and allows for specifying custom stream types.",
"main": "index.js",
"scripts": {

Loading…
Cancel
Save