Skip to content

Commit

Permalink
Merge pull request #258 from alexindigo/master
Browse files Browse the repository at this point in the history
Replaced async with asynckit
  • Loading branch information
alexindigo committed Sep 17, 2016
2 parents 158443d + 1749b78 commit 4d36c1c
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 113 deletions.
31 changes: 5 additions & 26 deletions .eslintrc
@@ -1,22 +1,11 @@
{
"env": {
"node": true,
"browser": false
},
"rules": {
// 2-space indentation
"indent": [2, 2, {"SwitchCase": 1}],
// Require strings to use single quotes
"quotes": [2, "single"],
// Allow only unix line-endings
"linebreak-style": [2, "unix"],
// Always require semicolons
"semi": [2, "always"],
// Require curly braces for multi-line control statements
"curly": [2, "multi-line"],
// Always handle callback error cases
"handle-callback-err": [2, "^err"],
// Require JSDoc for all function definitions
"valid-jsdoc": [2, {
"requireReturn": false,
"requireReturnDescription": false,
Expand All @@ -31,34 +20,24 @@
}],
"no-redeclare": [2, { "builtinGlobals": true }],
"no-shadow": [2, { "builtinGlobals": true, "hoist": "all" }],
// Disallow using variables before they've been defined
// functions are ok
"no-use-before-define": [2, "nofunc"],
"no-shadow-restricted-names": 2,
"no-extra-semi": 2,
// Disallow unused variables
"no-unused-vars": 2,
"no-undef": 2,
// Use if () { }
// ^ space
"keyword-spacing": 2,
// Use if () { }
// ^ space
"space-before-blocks": [2, "always"],
// eslint can't handle this, so the check is disabled.
"no-irregular-whitespace": 2,
"no-console": 2,
"key-spacing": 0,
"strict": 0,
// Do not force dot-notation
"dot-notation": 0,
"eol-last": 0,
"no-new": 0,
"semi-spacing": 0,
// Allow multi spaces around operators since they are
// used for alignment. This is not consistent in the
// code.
"no-multi-spaces": 0,
"eqeqeq": 0,
"no-mixed-requires": 0,
"no-console": 0
},
"env": {
"node": true
}
}
8 changes: 4 additions & 4 deletions .istanbul.yml
Expand Up @@ -11,7 +11,7 @@ reporting:
- json
dir: ./coverage
watermarks:
statements: [80, 95]
lines: [80, 95]
functions: [80, 95]
branches: [80, 95]
statements: [90, 95]
lines: [90, 95]
functions: [90, 95]
branches: [90, 95]
18 changes: 15 additions & 3 deletions .travis.yml
@@ -1,19 +1,31 @@
sudo: false

language: node_js
node_js:
- "0.10"
- "0.12"
- "iojs"
- "4"
- "5"
- "6"

os:
- osx
- linux

install:
- travis_retry npm install

script:
- uname -a
- node --version
- npm --version
- npm run lint
- npm run ci-lint
- npm run test
after_script:
- npm run check

after_success:
- "cat coverage/lcov.info | ./node_modules/.bin/coveralls"

notifications:
webhooks:
urls:
Expand Down
10 changes: 5 additions & 5 deletions Readme.md
Expand Up @@ -5,12 +5,12 @@ A library to create readable ```"multipart/form-data"``` streams. Can be used to
The API of this library is inspired by the [XMLHttpRequest-2 FormData Interface][xhr2-fd].

[xhr2-fd]: http://dev.w3.org/2006/webapi/XMLHttpRequest-2/Overview.html#the-formdata-interface
[streams2-thing]: http://nodejs.org/api/stream.html#stream_compatibility_with_older_node_versions

[![Linux Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=linux:0.10-6.x)](https://travis-ci.org/form-data/form-data)
[![Windows Build](https://img.shields.io/appveyor/ci/alexindigo/form-data/master.svg?label=windows:0.10-6.x)](https://ci.appveyor.com/project/alexindigo/form-data)
[![Coverage Status](https://img.shields.io/coveralls/form-data/form-data/master.svg?label=code+coverage)](https://coveralls.io/github/form-data/form-data?branch=master)
[![Linux Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=linux:0.12-6.x)](https://travis-ci.org/form-data/form-data)
[![MacOS Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=macos:0.12-6.x)](https://travis-ci.org/form-data/form-data)
[![Windows Build](https://img.shields.io/appveyor/ci/alexindigo/form-data/master.svg?label=windows:0.12-6.x)](https://ci.appveyor.com/project/alexindigo/form-data)

[![Coverage Status](https://img.shields.io/coveralls/form-data/form-data/master.svg?label=code+coverage)](https://coveralls.io/github/form-data/form-data?branch=master)
[![Dependency Status](https://img.shields.io/david/form-data/form-data.svg)](https://david-dm.org/form-data/form-data)
[![bitHound Overall Score](https://www.bithound.io/github/form-data/form-data/badges/score.svg)](https://www.bithound.io/github/form-data/form-data)

Expand Down Expand Up @@ -210,7 +210,7 @@ fetch('http://example.com', { method: 'POST', body: form })
## Notes

- ```getLengthSync()``` method DOESN'T calculate length for streams, use ```knownLength``` options as workaround.
- If it feels like FormData hangs after submit, please check [Compatibility with Older Node Versions][streams2-thing]
- Starting version `2.x` FormData has dropped support for `node@0.10.x`.

## License

Expand Down
1 change: 0 additions & 1 deletion appveyor.yml
@@ -1,6 +1,5 @@
environment:
matrix:
- nodejs_version: '0.10'
- nodejs_version: '0.12'
- nodejs_version: '1.0'
- nodejs_version: '4'
Expand Down
128 changes: 59 additions & 69 deletions lib/form_data.js
Expand Up @@ -6,7 +6,7 @@ var https = require('https');
var parseUrl = require('url').parse;
var fs = require('fs');
var mime = require('mime-types');
var async = require('async');
var asynckit = require('asynckit');
var populate = require('./populate.js');

// Public API
Expand All @@ -24,12 +24,12 @@ util.inherits(FormData, CombinedStream);
*/
function FormData() {
if (!(this instanceof FormData)) {
throw new TypeError('Failed to construct FormData: Please use the _new_ operator, this object constructor cannot be called as a function.');
return new FormData();
}

this._overheadLength = 0;
this._valueLength = 0;
this._lengthRetrievers = [];
this._valuesToMeasure = [];

CombinedStream.call(this);
}
Expand Down Expand Up @@ -101,60 +101,62 @@ FormData.prototype._trackLength = function(header, value, options) {

// no need to bother with the length
if (!options.knownLength) {
this._lengthRetrievers.push(function(next) {

if (value.hasOwnProperty('fd')) {

// take read range into a account
// `end` = Infinity –> read file till the end
//
// TODO: Looks like there is bug in Node fs.createReadStream
// it doesn't respect `end` options without `start` options
// Fix it when node fixes it.
// https://github.com/joyent/node/issues/7819
if (value.end != undefined && value.end != Infinity && value.start != undefined) {

// when end specified
// no need to calculate range
// inclusive, starts with 0
next(null, value.end + 1 - (value.start ? value.start : 0));

// not that fast snoopy
} else {
// still need to fetch file size from fs
fs.stat(value.path, function(err, stat) {

var fileSize;

if (err) {
next(err);
return;
}

// update final size based on the range options
fileSize = stat.size - (value.start ? value.start : 0);
next(null, fileSize);
});
this._valuesToMeasure.push(value);
}
};

FormData.prototype._lengthRetriever = function(value, callback) {

if (value.hasOwnProperty('fd')) {

// take read range into a account
// `end` = Infinity –> read file till the end
//
// TODO: Looks like there is bug in Node fs.createReadStream
// it doesn't respect `end` options without `start` options
// Fix it when node fixes it.
// https://github.com/joyent/node/issues/7819
if (value.end != undefined && value.end != Infinity && value.start != undefined) {

// when end specified
// no need to calculate range
// inclusive, starts with 0
callback(null, value.end + 1 - (value.start ? value.start : 0));

// not that fast snoopy
} else {
// still need to fetch file size from fs
fs.stat(value.path, function(err, stat) {

var fileSize;

if (err) {
callback(err);
return;
}

// or http response
} else if (value.hasOwnProperty('httpVersion')) {
next(null, +value.headers['content-length']);

// or request stream http://github.com/mikeal/request
} else if (value.hasOwnProperty('httpModule')) {
// wait till response come back
value.on('response', function(response) {
value.pause();
next(null, +response.headers['content-length']);
});
value.resume();

// something else
} else {
next('Unknown stream');
}
// update final size based on the range options
fileSize = stat.size - (value.start ? value.start : 0);
callback(null, fileSize);
});
}

// or http response
} else if (value.hasOwnProperty('httpVersion')) {
callback(null, +value.headers['content-length']);

// or request stream http://github.com/mikeal/request
} else if (value.hasOwnProperty('httpModule')) {
// wait till response come back
value.on('response', function(response) {
value.pause();
callback(null, +response.headers['content-length']);
});
value.resume();

// something else
} else {
callback('Unknown stream');
}
};

Expand Down Expand Up @@ -291,18 +293,6 @@ FormData.prototype.getHeaders = function(userHeaders) {
return formHeaders;
};

// TODO: Looks like unused function
FormData.prototype.getCustomHeaders = function(contentType) {
contentType = contentType ? contentType : 'multipart/form-data';

var formHeaders = {
'content-type': contentType + '; boundary=' + this.getBoundary(),
'content-length': this.getLengthSync()
};

return formHeaders;
};

FormData.prototype.getBoundary = function() {
if (!this._boundary) {
this._generateBoundary();
Expand Down Expand Up @@ -335,7 +325,7 @@ FormData.prototype.getLengthSync = function() {
}

// https://github.com/form-data/form-data/issues/40
if (this._lengthRetrievers.length) {
if (this._valuesToMeasure.length) {
// Some async length retrievers are present
// therefore synchronous length calculation is false.
// Please use getLength(callback) to get proper length
Expand All @@ -352,12 +342,12 @@ FormData.prototype.getLength = function(cb) {
knownLength += this._lastBoundary().length;
}

if (!this._lengthRetrievers.length) {
if (!this._valuesToMeasure.length) {
process.nextTick(cb.bind(this, null, knownLength));
return;
}

async.parallel(this._lengthRetrievers, function(err, values) {
asynckit.parallel(this._valuesToMeasure, this._lengthRetriever, function(err, values) {
if (err) {
cb(err);
return;
Expand Down
12 changes: 7 additions & 5 deletions package.json
Expand Up @@ -13,7 +13,8 @@
"pretest": "rimraf coverage test/tmp",
"test": "istanbul cover test/run.js",
"posttest": "istanbul report lcov text",
"lint": "eslint lib/*.js test/*.js test/**/*.js",
"lint": "eslint lib/*.js test/*.js test/integration/*.js",
"ci-lint": "is-node-modern && npm run lint || is-node-not-modern",
"predebug": "rimraf coverage test/tmp",
"debug": "verbose=1 ./test/run.js",
"check": "istanbul check-coverage coverage/coverage*.json",
Expand All @@ -30,21 +31,22 @@
"check"
],
"engines": {
"node": ">= 0.10"
"node": ">= 0.12"
},
"dependencies": {
"async": "^2.0.1",
"asynckit": "^0.4.0",
"combined-stream": "^1.0.5",
"mime-types": "^2.1.11"
},
"devDependencies": {
"coveralls": "^2.11.12",
"coveralls": "^2.11.13",
"cross-spawn": "^4.0.0",
"eslint": "^2.13.1",
"eslint": "^3.5.0",
"fake": "^0.2.2",
"far": "^0.0.7",
"formidable": "^1.0.17",
"in-publish": "^2.0.0",
"is-node-modern": "^1.0.0",
"istanbul": "^0.4.5",
"pkgfiles": "^2.3.0",
"pre-commit": "^1.1.3",
Expand Down

0 comments on commit 4d36c1c

Please sign in to comment.