Browse Source

Whoops, forgot the build for v1.0.0

master
Sven Slootweg 7 years ago
parent
commit
1835ee7e54
  1. 28
      gulpfile.js
  2. 1
      index.js
  3. 99
      lib/stream-length.js
  4. 12
      package.json

28
gulpfile.js

@ -0,0 +1,28 @@
var gulp = require('gulp');
/* CoffeeScript compile deps */
var path = require('path');
var gutil = require('gulp-util');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var coffee = require('gulp-coffee');
var cache = require('gulp-cached');
var remember = require('gulp-remember');
var plumber = require('gulp-plumber');
var source = ["lib/**/*.coffee", "index.coffee"]
gulp.task('coffee', function() {
return gulp.src(source, {base: "."})
.pipe(plumber())
.pipe(cache("coffee"))
.pipe(coffee({bare: true}).on('error', gutil.log)).on('data', gutil.log)
.pipe(remember("coffee"))
.pipe(gulp.dest("."));
});
gulp.task('watch', function () {
gulp.watch(source, ['coffee']);
});
gulp.task('default', ['coffee', 'watch']);

1
index.js

@ -0,0 +1 @@
module.exports = require("./lib/stream-length");

99
lib/stream-length.js

@ -0,0 +1,99 @@
var Promise, createRetrieverPromise, fs, nodeifyWrapper, retrieveBuffer, retrieveCoreHttpStream, retrieveFilesystemStream, retrieveRequestHttpStream;
Promise = require("bluebird");
fs = Promise.promisifyAll(require("fs"));
nodeifyWrapper = function(callback, func) {
return func().nodeify(callback);
};
createRetrieverPromise = function(stream, retriever) {
return new Promise(function(resolve, reject) {
return retriever(stream, function(result) {
if (result != null) {
if (result instanceof Error) {
console.log("REJECTING...", result);
return reject(result);
} else {
return resolve(result);
}
} else {
return reject(new Error("Could not find a length using this lengthRetriever."));
}
});
});
};
retrieveBuffer = function(stream, callback) {
if (stream instanceof Buffer) {
return callback(stream.length);
} else {
return callback(null);
}
};
retrieveFilesystemStream = function(stream, callback) {
var _ref;
if (stream.hasOwnProperty("fd")) {
if (stream.end !== void 0 && stream.end !== Infinity && stream.start !== void 0) {
return callback(stream.end + 1 - ((_ref = stream.start) != null ? _ref : 0));
} else {
return Promise["try"](function() {
return fs.statAsync(stream.path);
}).then(function(stat) {
var _ref1;
return callback(stat.size - ((_ref1 = stream.start) != null ? _ref1 : 0));
})["catch"](function(err) {
return callback(err);
});
}
} else {
return callback(null);
}
};
retrieveCoreHttpStream = function(stream, callback) {
if (stream.hasOwnProperty("httpVersion") && (stream.headers["content-length"] != null)) {
return callback(parseInt(stream.headers["content-length"]));
} else {
return callback(null);
}
};
retrieveRequestHttpStream = function(stream, callback) {
if (stream.hasOwnProperty("httpModule")) {
return stream.on("response", function(response) {
if (response.headers["content-length"] != null) {
return callback(parseInt(response.headers["content-length"]));
} else {
return callback(null);
}
});
} else {
return callback(null);
}
};
module.exports = function(stream, options, callback) {
if (options == null) {
options = {};
}
return nodeifyWrapper(callback, function() {
var retriever, retrieverPromises, _i, _j, _len, _len1, _ref, _ref1;
retrieverPromises = [];
if (options.lengthRetrievers != null) {
_ref = options.lengthRetrievers;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
retriever = _ref[_i];
retrieverPromises.push(createRetrieverPromise(stream, retriever));
}
}
_ref1 = [retrieveBuffer, retrieveFilesystemStream, retrieveCoreHttpStream, retrieveRequestHttpStream];
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
retriever = _ref1[_j];
retrieverPromises.push(createRetrieverPromise(stream, retriever));
}
return Promise.any(retrieverPromises);
});
};

12
package.json

@ -21,6 +21,16 @@
"bluebird": "^2.6.2"
},
"devDependencies": {
"request": "^2.51.0"
"request": "^2.51.0",
"gulp": "~3.8.0",
"gulp-cached": "~0.0.3",
"gulp-coffee": "~2.0.1",
"gulp-concat": "~2.2.0",
"gulp-livereload": "~2.1.0",
"gulp-nodemon": "~1.0.4",
"gulp-plumber": "~0.6.3",
"gulp-remember": "~0.2.0",
"gulp-rename": "~1.2.0",
"gulp-util": "~2.2.17"
}
}
Loading…
Cancel
Save