[Pkg-javascript-commits] [node-stream-http] 91/208: Change `window` to `global` for WebWorker support

Bastien Roucariès rouca at moszumanska.debian.org
Sun Aug 13 13:39:32 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 8cab16f26e1442a3f7b381432a25a736a9eb8ca5
Author: John Hiesey <john at hiesey.com>
Date:   Mon Sep 14 17:16:14 2015 -0700

    Change `window` to `global` for WebWorker support
---
 lib/capability.js            | 16 +++++++++-------
 lib/request.js               |  6 +++---
 lib/response.js              |  4 ++--
 test/node/http-browserify.js |  7 ++++---
 4 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/lib/capability.js b/lib/capability.js
index bc637d8..57dcd62 100644
--- a/lib/capability.js
+++ b/lib/capability.js
@@ -1,4 +1,4 @@
-exports.fetch = isFunction(window.fetch) && isFunction(window.ReadableByteStream)
+exports.fetch = isFunction(global.fetch) && isFunction(global.ReadableByteStream)
 
 exports.blobConstructor = false
 try {
@@ -6,8 +6,10 @@ try {
 	exports.blobConstructor = true
 } catch (e) {}
 
-var xhr = new window.XMLHttpRequest()
-xhr.open('GET', '/')
+var xhr = new global.XMLHttpRequest()
+// If location.host is empty, e.g. if this page/worker was loaded
+// from a Blob, then use example.com to avoid an error
+xhr.open('GET', global.location.host ? '/' : 'https://example.com')
 
 function checkTypeSupport (type) {
 	try {
@@ -17,10 +19,10 @@ function checkTypeSupport (type) {
 	return false
 }
 
-// For some strange reason, Safari 7.0 reports typeof window.ArrayBuffer === 'object'.
+// For some strange reason, Safari 7.0 reports typeof global.ArrayBuffer === 'object'.
 // Safari 7.1 appears to have fixed this bug.
-var haveArrayBuffer = typeof window.ArrayBuffer !== 'undefined'
-var haveSlice = haveArrayBuffer && isFunction(window.ArrayBuffer.prototype.slice)
+var haveArrayBuffer = typeof global.ArrayBuffer !== 'undefined'
+var haveSlice = haveArrayBuffer && isFunction(global.ArrayBuffer.prototype.slice)
 
 exports.arraybuffer = haveArrayBuffer && checkTypeSupport('arraybuffer')
 // These next two tests unavoidably show warnings in Chrome. Since fetch will always
@@ -29,7 +31,7 @@ exports.msstream = !exports.fetch && haveSlice && checkTypeSupport('ms-stream')
 exports.mozchunkedarraybuffer = !exports.fetch && haveArrayBuffer &&
 	checkTypeSupport('moz-chunked-arraybuffer')
 exports.overrideMimeType = isFunction(xhr.overrideMimeType)
-exports.vbArray = isFunction(window.VBArray)
+exports.vbArray = isFunction(global.VBArray)
 
 function isFunction (value) {
   return typeof value === 'function'
diff --git a/lib/request.js b/lib/request.js
index b952a6a..504f974 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -98,7 +98,7 @@ ClientRequest.prototype._onFinish = function () {
 	var body
 	if (opts.method === 'POST' || opts.method === 'PUT') {
 		if (capability.blobConstructor) {
-			body = new window.Blob(self._body.map(function (buffer) {
+			body = new global.Blob(self._body.map(function (buffer) {
 				return buffer.toArrayBuffer()
 			}), {
 				type: (headersObj['content-type'] || {}).value || ''
@@ -114,7 +114,7 @@ ClientRequest.prototype._onFinish = function () {
 			return [headersObj[name].name, headersObj[name].value]
 		})
 
-		window.fetch(self._opts.url, {
+		global.fetch(self._opts.url, {
 			method: self._opts.method,
 			headers: headers,
 			body: body,
@@ -127,7 +127,7 @@ ClientRequest.prototype._onFinish = function () {
 			self.emit('error', reason)
 		})
 	} else {
-		var xhr = self._xhr = new window.XMLHttpRequest()
+		var xhr = self._xhr = new global.XMLHttpRequest()
 		try {
 			xhr.open(self._opts.method, self._opts.url, true)
 		} catch (err) {
diff --git a/lib/response.js b/lib/response.js
index 9994d90..378291a 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -107,7 +107,7 @@ IncomingMessage.prototype._onXHRProgress = function () {
 				break
 			try {
 				// This fails in IE8
-				response = new window.VBArray(xhr.responseBody).toArray()
+				response = new global.VBArray(xhr.responseBody).toArray()
 			} catch (e) {}
 			if (response !== null) {
 				self.push(new Buffer(response))
@@ -151,7 +151,7 @@ IncomingMessage.prototype._onXHRProgress = function () {
 			response = xhr.response
 			if (xhr.readyState !== rStates.LOADING)
 				break
-			var reader = new window.MSStreamReader()
+			var reader = new global.MSStreamReader()
 			reader.onprogress = function () {
 				if (reader.result.byteLength > self._pos) {
 					self.push(new Buffer(new Uint8Array(reader.result.slice(self._pos))))
diff --git a/test/node/http-browserify.js b/test/node/http-browserify.js
index 4cb6a12..3ca7d39 100644
--- a/test/node/http-browserify.js
+++ b/test/node/http-browserify.js
@@ -6,8 +6,8 @@ var url = require('url')
 var location = 'http://localhost:8081/foo/123'
 
 var noop = function() {}
-global.window = {}
-window.XMLHttpRequest = function() {
+global.location = url.parse(location)
+global.XMLHttpRequest = function() {
 	this.open = noop
 	this.send = noop
 	this.withCredentials = false
@@ -107,7 +107,8 @@ test('Test relative path in url', function(t) {
 })
 
 test('Cleanup', function (t) {
-	delete global.window
+	delete global.location
+	delete global.XMLHttpRequest
 	delete require.cache[moduleName]
 	t.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