[Pkg-javascript-commits] [node-stream-http] 02/208: Lots more stuff

Bastien Roucariès rouca at moszumanska.debian.org
Sun Aug 13 13:39:22 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 0568617110bc29b5ccf15955b63acb7b4965a5d4
Author: John Hiesey <john at hiesey.com>
Date:   Wed Jul 1 14:04:06 2015 -0700

    Lots more stuff
---
 .gitignore    |  3 +++
 .zuul.yml     | 18 ++++++++++++++
 README.md     | 45 ++++++++++++++++++++++++++++++++++
 capability.js |  8 +++----
 index.html    | 33 ++++++++++++++++++-------
 index.js      | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 package.json  |  5 ++--
 request.js    | 30 ++++++++++++-----------
 response.js   | 33 +++++++++++++------------
 9 files changed, 205 insertions(+), 47 deletions(-)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8f52590
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+**/.DS_Store
+bundle.js
+node_modules
\ No newline at end of file
diff --git a/.zuul.yml b/.zuul.yml
new file mode 100644
index 0000000..232d8ed
--- /dev/null
+++ b/.zuul.yml
@@ -0,0 +1,18 @@
+ui: tape
+browsers:
+  - name: chrome
+    version: 39..latest
+  - name: firefox
+    version: 34..latest
+  - name: safari
+    version: 5..latest
+  - name: ie
+    version: 8..latest
+  - name: opera
+    version: 11..latest
+  - name: iphone
+    version: 4.3..latest
+  - name: ipad
+    version: 4.3..latest
+  - name: android
+    version: 4.0..latest
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ab08cf0
--- /dev/null
+++ b/README.md
@@ -0,0 +1,45 @@
+# httpstream
+
+This module is an implementation of node's native `http` module for the browser.
+It tries to match node's API and behavior as closely as possible, but some features
+aren't available, since browsers don't give nearly as much control over requests for security
+reasons.
+
+This is heavily inspired by, and intended to replace, [http-browserify](https://github.com/substack/http-browserify)
+
+## What does it do?
+
+In accordance with its name, `httpstream` tries to provide data to its caller before
+the request has completed whenever possible.
+
+The following browsers support true streaming, where only a small amount of the request
+has to be held in memory at once:
+* Chrome >=??? (using the `fetch` api)
+* Firefox >=??? (using `moz-chunked-arraybuffer`)
+
+The following browsers support pseudo-streaming, where the data is available before the
+request finishes, but the entire response must be held in memory:
+* Chrome >=(old)
+* Safari >=(old)
+* IE >= 10
+
+Additionally, older browsers other than IE support pseudo-streaming using multipart
+responses, but only for text (FILL IN HERE)
+
+## Example
+
+``` js
+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/capability.js b/capability.js
index 09bffca..951cb22 100644
--- a/capability.js
+++ b/capability.js
@@ -3,22 +3,22 @@ var util = require('util')
 exports.fetch = util.isFunction(window.fetch)
 
 function checkTypeSupport (type) {
-	var xhr = new XMLHttpRequest()
+	var xhr = new window.XMLHttpRequest()
 	xhr.open('GET', '/')
 	try {
 		xhr.responseType = type
 		return xhr.responseType === type
 	} catch (e) {}
-	return false;
+	return false
 }
 
 exports.arraybuffer = checkTypeSupport('arraybuffer')
 exports.msstream = checkTypeSupport('ms-stream')
 exports.mozchunkedarraybuffer = checkTypeSupport('moz-chunked-arraybuffer')
 
-/*if (exports.fetch) {
+if (exports.fetch) {
 	exports.mode = 'fetch'
-} else*/ if (exports.mozchunkedarraybuffer) {
+} else if (exports.mozchunkedarraybuffer) {
 	exports.mode = 'moz-chunked-arraybuffer'
 } else if (exports.msstream) {
 	exports.mode = 'ms-stream'
diff --git a/index.html b/index.html
index 341a029..b066194 100644
--- a/index.html
+++ b/index.html
@@ -4,19 +4,36 @@
 <script src="bundle.js"></script>
 </head>
 <body>
+<div id="result"></div>
 
 <script>
-var req = http.request('/bundle.js')
-req.on('response', function (res) {
+// var req = http.request('/bundle.js')
+// req.on('response', function (res) {
+// 	res.on('data', function (buf) {
+// 		console.log('chunk:', buf.toString())
+// 	})
+// 	res.on('end', function () {
+// 		console.log('done!')
+// 	})
+// })
+// req.end()
+
+// var http = require('httpstream')
+
+http.get('//bundle.js', function (res) {
+	var div = document.getElementById('result');
+	div.innerHTML += 'GET /beep<br>';
+	
 	res.on('data', function (buf) {
-		console.warn('len:', buf.length)
-		console.log('chunk:', buf.toString())
-	})
+		div.innerHTML += buf;
+	});
+	
 	res.on('end', function () {
-		console.log('done!')
-	})
+		div.innerHTML += '<br>__END__';
+	});
 })
-req.end()
+
+
 </script>
 
 
diff --git a/index.js b/index.js
index 2b808ca..0f420e7 100644
--- a/index.js
+++ b/index.js
@@ -1,8 +1,9 @@
 var ClientRequest = require('./request')
 var url = require('url')
-var util = require('util')
 
-exports.request = function request (opts, cb) {
+var http = exports
+
+http.request = function (opts, cb) {
 	if (typeof opts === 'string')
 		opts = url.parse(opts)
 
@@ -21,10 +22,78 @@ exports.request = function request (opts, cb) {
 	return req
 }
 
-exports.get = function get (opts, cb) {
+http.get = function get (opts, cb) {
 	opts.method = 'GET'
-	var req = request(opts, cb)
+	var req = http.request(opts, cb)
 	req.end()
 	return req
 }
 
+http.Agent = function () {}
+http.Agent.defaultMaxSockets = 4
+
+http.STATUS_CODES = {
+	100 : 'Continue',
+	101 : 'Switching Protocols',
+	102 : 'Processing',                 // RFC 2518, obsoleted by RFC 4918
+	200 : 'OK',
+	201 : 'Created',
+	202 : 'Accepted',
+	203 : 'Non-Authoritative Information',
+	204 : 'No Content',
+	205 : 'Reset Content',
+	206 : 'Partial Content',
+	207 : 'Multi-Status',               // RFC 4918
+	300 : 'Multiple Choices',
+	301 : 'Moved Permanently',
+	302 : 'Moved Temporarily',
+	303 : 'See Other',
+	304 : 'Not Modified',
+	305 : 'Use Proxy',
+	307 : 'Temporary Redirect',
+	400 : 'Bad Request',
+	401 : 'Unauthorized',
+	402 : 'Payment Required',
+	403 : 'Forbidden',
+	404 : 'Not Found',
+	405 : 'Method Not Allowed',
+	406 : 'Not Acceptable',
+	407 : 'Proxy Authentication Required',
+	408 : 'Request Time-out',
+	409 : 'Conflict',
+	410 : 'Gone',
+	411 : 'Length Required',
+	412 : 'Precondition Failed',
+	413 : 'Request Entity Too Large',
+	414 : 'Request-URI Too Large',
+	415 : 'Unsupported Media Type',
+	416 : 'Requested Range Not Satisfiable',
+	417 : 'Expectation Failed',
+	418 : 'I\'m a teapot',              // RFC 2324
+	422 : 'Unprocessable Entity',       // RFC 4918
+	423 : 'Locked',                     // RFC 4918
+	424 : 'Failed Dependency',          // RFC 4918
+	425 : 'Unordered Collection',       // RFC 4918
+	426 : 'Upgrade Required',           // RFC 2817
+	428 : 'Precondition Required',      // RFC 6585
+	429 : 'Too Many Requests',          // RFC 6585
+	431 : 'Request Header Fields Too Large',// RFC 6585
+	500 : 'Internal Server Error',
+	501 : 'Not Implemented',
+	502 : 'Bad Gateway',
+	503 : 'Service Unavailable',
+	504 : 'Gateway Time-out',
+	505 : 'HTTP Version Not Supported',
+	506 : 'Variant Also Negotiates',    // RFC 2295
+	507 : 'Insufficient Storage',       // RFC 4918
+	509 : 'Bandwidth Limit Exceeded',
+	510 : 'Not Extended',               // RFC 2774
+	511 : 'Network Authentication Required' // RFC 6585
+}
+
+http.METHODS = [
+	'GET',
+	'POST',
+	'PUT',
+	'DELETE' // TODO: include the methods from RFC 2616 and 2518?
+]
diff --git a/package.json b/package.json
index 93b6188..d26a8a1 100644
--- a/package.json
+++ b/package.json
@@ -5,11 +5,12 @@
   "main": "index.js",
   "scripts": {
     "build": "browserify index.js -s http > bundle.js",
-    "test": "echo \"Error: no test specified\" && exit 1"
+    "test": "zuul -- test/*.js"
   },
   "author": "John Hiesey",
   "license": "MIT",
   "dependencies": {
-    "browserify": "^10.2.4"
+    "browserify": "^10.2.4",
+    "zuul": "^3.1.0"
   }
 }
diff --git a/request.js b/request.js
index 30c2788..ef2b727 100644
--- a/request.js
+++ b/request.js
@@ -55,12 +55,12 @@ ClientRequest.prototype._onFinish = function () {
 	var self = this
 
 	var opts = self._opts
-	var url = opts.protocol + '//' + opts.hostname
-		+ (opts.port ? ':' + opts.port : '') + opts.path
+	var url = opts.protocol + '//' + opts.hostname +
+		(opts.port ? ':' + opts.port : '') + opts.path
 
 	var user, pass
 	if (opts.auth) {
-		var authMatch = opts.auth.match(/^([^:]*):(.*)$)/)
+		var authMatch = opts.auth.match(/^([^:]*):(.*)$/)
 		user = authMatch[0]
 		pass = authMatch[1]
 	}
@@ -68,15 +68,17 @@ ClientRequest.prototype._onFinish = function () {
 	// process and send data
 	var fullHeaders = self._fullHeaders
 	var body
-	if (typeof Blob === 'function') {
-		body = new Blob(self._body.map(function (buffer) {
-			return buffer.toArrayBuffer()
-		}), {
-			type: fullHeaders['content-type'] || ''
-		});		
-	} else {
-		// get utf8 string
-		body = Buffer.concat(self._body).toString()
+	if (opts.method in ['PUT', 'POST']) {
+		if (typeof window.Blob === 'function') {
+			body = new window.Blob(self._body.map(function (buffer) {
+				return buffer.toArrayBuffer()
+			}), {
+				type: fullHeaders['content-type'] || ''
+			})
+		} else {
+			// get utf8 string
+			body = Buffer.concat(self._body).toString()
+		}
 	}
 
 	if (self._mode === 'fetch') {
@@ -84,7 +86,7 @@ ClientRequest.prototype._onFinish = function () {
 			return [name, fullHeaders[name]]
 		})
 
-		fetch(url, {
+		window.fetch(url, {
 			method: self._opts.method,
 			headers: headers,
 			body: body,
@@ -95,7 +97,7 @@ ClientRequest.prototype._onFinish = function () {
 			self._connect()
 		})
 	} else {
-		var xhr = self._xhr = new XMLHttpRequest() // TODO: old IE
+		var xhr = self._xhr = new window.XMLHttpRequest() // TODO: old IE
 		xhr.open(self._opts.method, url, true, user, pass)
 
 		// Can't set responseType on really old browsers
diff --git a/response.js b/response.js
index d6e7639..436b21d 100644
--- a/response.js
+++ b/response.js
@@ -9,7 +9,7 @@ var rStates = exports.readyStates = {
 	DONE: 4
 }
 
-var IncomingMessage = exports.IncomingMessage = function (xhr, fetchResponse, mode) {
+var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
 	var self = this
 	stream.Readable.call(self)
 
@@ -20,34 +20,38 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, fetchResponse, mo
 	self.rawTrailers = []
 
 	if (mode === 'fetch') {
-		self._fetchResponse = fetchResponse
+		self._fetchResponse = response
 
 		self.statusCode = response.status
 		self.statusMessage = response.statusText
 		// backwards compatible version of for (<item> of <iterable>):
 		// for (var <item>,_i,_it = <iterable>[Symbol.iterator](); <item> = (_i = _it.next()).value,!_i.done;)
-		for (var header,_i,_it = response.headers[Symbol.iterator](); header = (_i = _it.next()).value,!_i.done;) {
+		for (var header, _i, _it = response.headers[Symbol.iterator](); header = (_i = _it.next()).value, !_i.done;) {
 			self.headers[header[0].toLowerCase()] = header[1]
-			self.rawHeeaders.push(header)
+			self.rawHeaders.push(header)
 		}
 
 		// TODO: this doesn't respect backpressure. Once WritableStream is available, this can be fixed
 		var reader = response.body.getReader()
-		reader.read().then(function (result) {
-			if (result.done) {
-				self.push(null)
-				self.emit('close')
-				return
-			}
-			self.push(new Buffer(result.value))
-		})
+		function read () {
+			reader.read().then(function (result) {
+				if (result.done) {
+					self.push(null)
+					self.emit('close')
+					return
+				}
+				self.push(new Buffer(result.value))
+				read()
+			})
+		}
+		read()
 
 	} else {
 		self._xhr = xhr
 		self._pos = 0
 
 		self.statusCode = xhr.status
-		self.statusMessage = xhr.statusText.match('/^[0-9]{3} (.*)$')[1]
+		self.statusMessage = xhr.statusText
 		var headers = xhr.getAllResponseHeaders().split(/\r?\n/)
 		headers.forEach(function (header) {
 			var matches = header.match(/^([^:]+):\s*(.*)/)
@@ -104,10 +108,9 @@ IncomingMessage.prototype._onXHRReadyStateChange = function () {
 			self.push(new Buffer(xhr.response))
 			break
 		case 'ms-stream':
-			// 
 			if (xhr.readyState !== rStates.LOADING)
 				break
-			var reader = new MSStreamReader()
+			var reader = new window.MSStreamReader()
 			reader.onprogress = function () {
 				if (reader.result.byteLength > self._pos) {
 					self.push(new Buffer(new Uint8Array(reader.result.slice(self._pos))))

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