[Pkg-javascript-commits] [node-stream-http] 150/208: Support timeout option in http.request

Bastien Roucariès rouca at moszumanska.debian.org
Sun Aug 13 13:39:38 UTC 2017


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

rouca pushed a commit to branch master
in repository node-stream-http.

commit 57d0ab245fc1602300f2253ecce9472dbd200195
Author: Connor Peet <connor at peet.io>
Date:   Mon Oct 17 18:45:00 2016 -0700

    Support timeout option in http.request
---
 README.md               |  6 ++++--
 lib/request.js          | 12 ++++++++++--
 test/browser/timeout.js | 24 ++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/README.md b/README.md
index 723be8b..e9dcfd9 100644
--- a/README.md
+++ b/README.md
@@ -94,6 +94,8 @@ the server.
 * `message.trailers` and `message.rawTrailers` will remain empty.
 * Redirects are followed silently by the browser, so it isn't possible to access the 301/302
 redirect pages.
+* The `timeout` options in the `request` method is non-operational in Safari <= 5 and
+Opera <= 12.
 
 ## Example
 
@@ -101,11 +103,11 @@ redirect pages.
 http.get('/bundle.js', function (res) {
 	var div = document.getElementById('result');
 	div.innerHTML += 'GET /beep<br>';
-	
+
 	res.on('data', function (buf) {
 		div.innerHTML += buf;
 	});
-	
+
 	res.on('end', function () {
 		div.innerHTML += '<br>__END__';
 	});
diff --git a/lib/request.js b/lib/request.js
index 5a43036..5d9d167 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -38,8 +38,9 @@ var ClientRequest = module.exports = function (opts) {
 
 	var preferBinary
 	var useFetch = true
-	if (opts.mode === 'disable-fetch') {
-		// If the use of XHR should be preferred and includes preserving the 'content-type' header
+	if (opts.mode === 'disable-fetch' || 'timeout' in opts) {
+		// If the use of XHR should be preferred and includes preserving the 'content-type' header.
+		// Force XHR to be used since the Fetch API does not yet support timeouts.
 		useFetch = false
 		preferBinary = true
 	} else if (opts.mode === 'prefer-streaming') {
@@ -149,6 +150,13 @@ ClientRequest.prototype._onFinish = function () {
 		if (self._mode === 'text' && 'overrideMimeType' in xhr)
 			xhr.overrideMimeType('text/plain; charset=x-user-defined')
 
+		if ('timeout' in opts) {
+			xhr.timeout = opts.timeout
+			xhr.ontimeout = function () {
+				self.emit('timeout')
+			}
+		}
+
 		Object.keys(headersObj).forEach(function (name) {
 			xhr.setRequestHeader(headersObj[name].name, headersObj[name].value)
 		})
diff --git a/test/browser/timeout.js b/test/browser/timeout.js
new file mode 100644
index 0000000..503026b
--- /dev/null
+++ b/test/browser/timeout.js
@@ -0,0 +1,24 @@
+var test = require('tape')
+
+var http = require('../..')
+
+var skipTimeout = ((browserName === 'Opera' && browserVersion <= 12) ||
+    (browserName === 'Safari' && browserVersion <= 5))
+
+
+test('emits timeout events', function (t) {
+    if (skipTimeout) {
+        return t.skip('Browser does not support setting timeouts')
+    }
+
+    var req = http.request({
+        path: '/basic.txt',
+        timeout: 1
+    })
+
+    req.on('timeout', function () {
+        t.end() // the test will timeout if this does not happen
+    })
+
+    req.end()
+})

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



More information about the Pkg-javascript-commits mailing list