[Pkg-javascript-commits] [node-stream-http] 16/208: Fix arraybuffer mode

Bastien Roucariès rouca at moszumanska.debian.org
Sun Aug 13 13:39:23 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 db953820d054e40981e3b51640e3ddb522c363af
Author: John Hiesey <john at hiesey.com>
Date:   Thu Jul 2 18:18:43 2015 -0700

    Fix arraybuffer mode
---
 lib/capability.js                 |   9 +++++----
 lib/response.js                   |   9 ++++++++-
 test/server/static/browserify.png | Bin 0 -> 31422 bytes
 test/tests/binary.js              |  22 ++++++++++++++++++++++
 4 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/lib/capability.js b/lib/capability.js
index 5728241..f76a397 100644
--- a/lib/capability.js
+++ b/lib/capability.js
@@ -13,11 +13,12 @@ function checkTypeSupport (type) {
 	return false
 }
 
-var arrayBufferGood = util.isFunction(window.ArrayBuffer) && util.isFunction(window.ArrayBuffer.prototype.slice)
+var haveArrayBuffer = util.isFunction(window.ArrayBuffer)
+var haveSlice = haveArrayBuffer && util.isFunction(window.ArrayBuffer.prototype.slice)
 
-exports.arraybuffer = arrayBufferGood && checkTypeSupport(xhr, 'arraybuffer')
-exports.msstream = arrayBufferGood && checkTypeSupport(xhr, 'ms-stream')
-exports.mozchunkedarraybuffer = arrayBufferGood && checkTypeSupport('moz-chunked-arraybuffer')
+exports.arraybuffer = haveArrayBuffer && checkTypeSupport('arraybuffer')
+exports.msstream = haveSlice && checkTypeSupport('ms-stream')
+exports.mozchunkedarraybuffer = haveArrayBuffer && checkTypeSupport('moz-chunked-arraybuffer')
 exports.overrideMimeType = util.isFunction(xhr.overrideMimeType)
 
 xhr = null // Help gc
diff --git a/lib/response.js b/lib/response.js
index 02011dd..9c07e24 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -1,3 +1,4 @@
+var capability = require('./capability')
 var foreach = require('foreach')
 var stream = require('stream')
 var util = require('util')
@@ -64,7 +65,7 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
 		})
 
 		self._charset = 'x-user-defined'
-		if (typeof xhr.overrideMimeType !== 'function') {
+		if (!capability.overrideMimeType) {
 			var mimeType = self.rawHeaders['mime-type']
 			if (mimeType) {
 				var charsetMatch = mimeType.match(/;\s*charset=([^;])(;|$)/)
@@ -105,6 +106,12 @@ IncomingMessage.prototype._onXHRReadyStateChange = function () {
 				self._pos = response.length
 			}
 			break
+		case 'arraybuffer':
+			if (xhr.readyState !== rStates.DONE)
+				break
+			response = xhr.response
+			self.push(new Buffer(new Uint8Array(response)))
+			break
 		case 'moz-chunked-arraybuffer': // take whole
 			response = xhr.response
 			if (xhr.readyState !== rStates.LOADING || !response)
diff --git a/test/server/static/browserify.png b/test/server/static/browserify.png
new file mode 100644
index 0000000..98d6bf5
Binary files /dev/null and b/test/server/static/browserify.png differ
diff --git a/test/tests/binary.js b/test/tests/binary.js
new file mode 100644
index 0000000..4e595a1
--- /dev/null
+++ b/test/tests/binary.js
@@ -0,0 +1,22 @@
+var Buffer = require('buffer').Buffer
+var fs = require('fs');
+var test = require('tape')
+
+var reference = fs.readFileSync(__dirname + '/../server/static/browserify.png');
+
+var http = require('../..')
+
+test('binary download', function (t) {
+	http.get('/browserify.png', function (res) {
+		var buffers = []
+
+		res.on('end', function () {
+			t.ok(reference.equals(Buffer.concat(buffers)), 'contents match')
+			t.end()
+		})
+
+		res.on('data', function (data) {
+			buffers.push(data)
+		})
+	})
+})
\ No newline at end of file

-- 
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