[Pkg-javascript-commits] [node-request-progress] 01/03: Imported Upstream version 0.3.1

Sebastiaan Couwenberg sebastic at moszumanska.debian.org
Sat Mar 21 20:58:01 UTC 2015


This is an automated email from the git hooks/post-receive script.

sebastic pushed a commit to branch master
in repository node-request-progress.

commit a106e9ac137a9cb6abb5e681d6b08fd11dc315e9
Author: Bas Couwenberg <sebastic at xs4all.nl>
Date:   Sat Mar 21 19:20:43 2015 +0100

    Imported Upstream version 0.3.1
---
 .editorconfig | 12 +++++++++
 .gitignore    |  2 ++
 .jshintrc     | 63 ++++++++++++++++++++++++++++++++++++++++++++++
 .travis.yml   |  4 +++
 LICENSE       | 19 ++++++++++++++
 README.md     | 47 +++++++++++++++++++++++++++++++++++
 index.js      | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 package.json  | 34 +++++++++++++++++++++++++
 test/test.js  |  1 +
 9 files changed, 262 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..779f99a
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,12 @@
+root = true
+
+[*]
+indent_style = space
+indent_size = 4
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[*.md]
+trim_trailing_whitespace = false
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..27ccd66
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/node_modules
+/npm-debug.*
diff --git a/.jshintrc b/.jshintrc
new file mode 100644
index 0000000..1bf396d
--- /dev/null
+++ b/.jshintrc
@@ -0,0 +1,63 @@
+{
+    "predef": [
+        "console",
+        "require",
+        "define",
+        "describe",
+        "it",
+        "before",
+        "beforeEach",
+        "after",
+        "afterEach"
+    ],
+
+    "node": true,
+    "devel": true,
+
+    "bitwise": true,
+    "curly": true,
+    "eqeqeq": false,
+    "forin": false,
+    "immed": true,
+    "latedef": false,
+    "newcap": true,
+    "noarg": true,
+    "noempty": false,
+    "nonew": true,
+    "plusplus": false,
+    "regexp": true,
+    "undef": true,
+    "unused": true,
+    "quotmark": "single",
+    "strict": true,
+    "trailing": true,
+
+    "asi": false,
+    "boss": false,
+    "debug": false,
+    "eqnull": true,
+    "es5": false,
+    "esnext": false,
+    "evil": false,
+    "expr": false,
+    "funcscope": false,
+    "globalstrict": false,
+    "iterator": false,
+    "lastsemic": false,
+    "laxbreak": false,
+    "laxcomma": false,
+    "loopfunc": true,
+    "multistr": false,
+    "onecase": true,
+    "regexdash": false,
+    "scripturl": false,
+    "smarttabs": false,
+    "shadow": false,
+    "sub": false,
+    "supernew": false,
+    "validthis": false,
+
+    "nomen": false,
+    "onevar": false,
+    "white": true
+}
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..2ca91f2
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+  - "0.10"
+  - "0.8"
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..92b561c
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2012 IndigoUnited
+
+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.
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..bbbf2d1
--- /dev/null
+++ b/README.md
@@ -0,0 +1,47 @@
+# request-progress [![Build Status](https://secure.travis-ci.org/IndigoUnited/node-request-progress.png)](http://travis-ci.org/IndigoUnited/node-request-progress.png)
+
+Tracks the download progress of a request made with [request](https://github.com/mikeal/request).
+
+
+## Installation
+
+`$ npm install request-progress`
+
+
+## Usage
+
+```js
+var fs = require('fs');
+var request = require('request');
+var progress = require('request-progress');
+
+// Note that the options argument is optional
+progress(request('http://google.com/doodle.png'), {
+    throttle: 2000,  // Throttle the progress event to 2000ms, defaults to 1000ms
+    delay: 1000      // Only start to emit after 1000ms delay, defaults to 0ms
+})
+.on('progress', function (state) {
+    console.log('received size in bytes', state.received);
+    // The properties bellow can be null if response does not contain
+    // the content-length header
+    console.log('total size in bytes', state.total);
+    console.log('percent', state.percent);
+})
+.on('error', function (err) {
+    // Do something with err
+})
+.pipe(fs.createWriteStream('doodle.png'))
+.on('error', function (err) {
+    // Do something with err
+})
+.on('close', function (err) {
+    // Saved to doogle.png!
+})
+```
+
+Note that the `state` object emitted in the `progress` event is reused to avoid creating a new object for each event.
+
+
+## License
+
+Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..17908a1
--- /dev/null
+++ b/index.js
@@ -0,0 +1,80 @@
+'use strict';
+
+var throttle = require('throttleit');
+
+function requestProgress(request, options) {
+    var reporter;
+    var onResponse;
+    var delayTimer;
+    var delayCompleted;
+    var totalSize;
+    var previousReceivedSize;
+    var receivedSize = 0;
+    var state = {};
+
+    options = options || {};
+    options.throttle = options.throttle == null ? 1000 : options.throttle;
+    options.delay = options.delay || 0;
+
+    // Throttle the progress report function
+    reporter = throttle(function () {
+        // If the received size is the same, do not report
+        if (previousReceivedSize === receivedSize) {
+            return;
+        }
+
+        // Update received
+        previousReceivedSize = receivedSize;
+        state.received = receivedSize;
+
+        // Update percentage
+        // Note that the totalSize might available
+        state.percent = totalSize ? Math.round(receivedSize / totalSize * 100) : null;
+
+        request.emit('progress', state);
+    }, options.throttle);
+
+    // On response handler
+    onResponse = function (response) {
+        totalSize = Number(response.headers['content-length']);
+        receivedSize = 0;
+
+        // Note that the totalSize might available
+        state.total = totalSize || null;
+
+        // Delay the progress report
+        delayCompleted = false;
+        delayTimer = setTimeout(function () {
+            delayCompleted = true;
+            delayTimer = null;
+        }, options.delay);
+    };
+
+    request
+    .on('request', function () {
+        receivedSize = 0;
+    })
+    .on('response', onResponse)
+    .on('data', function (data) {
+        receivedSize += data.length;
+
+        if (delayCompleted) {
+            reporter();
+        }
+    })
+    .on('end', function () {
+        if (delayTimer) {
+            clearTimeout(delayTimer);
+            delayTimer = null;
+        }
+    });
+
+    // If we already got a response, call the on response handler
+    if (request.response) {
+        onResponse(request.response);
+    }
+
+    return request;
+}
+
+module.exports = requestProgress;
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..684065c
--- /dev/null
+++ b/package.json
@@ -0,0 +1,34 @@
+{
+  "name": "request-progress",
+  "version": "0.3.1",
+  "description": "Tracks the download progress of a request made with mikeal/request",
+  "main": "index.js",
+  "dependencies": {
+    "throttleit": "~0.0.2"
+  },
+  "devDependencies": {
+    "mocha": "~1.12.0",
+    "expect.js": "~0.2.0"
+  },
+  "scripts": {
+    "test": "mocha -R spec"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git://github.com/IndigoUnited/node-request-progress"
+  },
+  "bugs": {
+    "url": "http://github.com/IndigoUnited/node-request-progress/issues"
+  },
+  "keywords": [
+    "progress",
+    "request",
+    "mikeal",
+    "size",
+    "bytes",
+    "percent",
+    "percentage"
+  ],
+  "author": "IndigoUnited <hello at indigounited.com> (http://indigounited.com)",
+  "license": "MIT"
+}
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..70b786d
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1 @@
+// TODO

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-request-progress.git



More information about the Pkg-javascript-commits mailing list