mirror of
				https://github.com/therootcompany/request.js.git
				synced 2024-11-16 17:28:58 +00:00 
			
		
		
		
	Compare commits
	
		
			No commits in common. "95a12a82850a16c50ea239c7056c8f1807bc7564" and "3574e356359e6a619cd68edf5cd36eb85fce5ca3" have entirely different histories.
		
	
	
		
			95a12a8285
			...
			3574e35635
		
	
		
							
								
								
									
										60
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										60
									
								
								README.md
									
									
									
									
									
								
							| @ -14,13 +14,11 @@ Written from scratch, with zero-dependencies. | |||||||
| 
 | 
 | ||||||
| ```bash | ```bash | ||||||
| npm install --save @root/request | npm install --save @root/request | ||||||
| 
 |  | ||||||
| # or npm install git+ssh://git@git.therootcompany.com/request.js |  | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| ```js | ```js | ||||||
| var request = require('@root/request'); | var request = require('@root/request'); | ||||||
| request('http://www.google.com', function (error, response, body) { | request('http://www.google.com', function(error, response, body) { | ||||||
|     console.log('error:', error); // Print the error if one occurred |     console.log('error:', error); // Print the error if one occurred | ||||||
|     console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received |     console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received | ||||||
|     console.log('body:', body); // Print the HTML for the Google homepage. |     console.log('body:', body); // Print the HTML for the Google homepage. | ||||||
| @ -33,63 +31,15 @@ request('http://www.google.com', function (error, response, body) { | |||||||
| var request = require('@root/request'); | var request = require('@root/request'); | ||||||
| 
 | 
 | ||||||
| request('http://www.google.com') | request('http://www.google.com') | ||||||
|     .then(function (response) { |     .then(function(response) { | ||||||
|         console.log('statusCode:', response.statusCode); // Print the response status code if a response was received |         console.log('statusCode:', response.statusCode); // Print the response status code if a response was received | ||||||
|         console.log('body:', response.body); // Print the HTML for the Google homepage. |         console.log('body:', response.body); // Print the HTML for the Google homepage. | ||||||
|     }) |     }) | ||||||
|     .catch(function (error) { |     .catch(function(error) { | ||||||
|         console.log('error:', error); // Print the error if one occurred |         console.log('error:', error); // Print the error if one occurred | ||||||
|     }); |     }); | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
| **Streaming** |  | ||||||
| 
 |  | ||||||
| In order to keep this library lightweight, performant, and keep the code easy to |  | ||||||
| read, the streaming behavior is **_slightly different_** from that of |  | ||||||
| `request.js`. |  | ||||||
| 
 |  | ||||||
| ```js |  | ||||||
| var request = require('@root/request'); |  | ||||||
| 
 |  | ||||||
| var resp = await request({ |  | ||||||
|     url: 'http://www.google.com', |  | ||||||
|     stream: true |  | ||||||
| }); |  | ||||||
| 
 |  | ||||||
| resp.on('data', function () { |  | ||||||
|     // got some data |  | ||||||
| }); |  | ||||||
| 
 |  | ||||||
| resp.on('end', function () { |  | ||||||
|     // the data has ended |  | ||||||
| }); |  | ||||||
| 
 |  | ||||||
| // resp.stream is a Promise that is resolved when the read stream is destroyed |  | ||||||
| await resp.stream; |  | ||||||
| console.log('Done'); |  | ||||||
| ``` |  | ||||||
| 
 |  | ||||||
| The difference is that we don't add an extra layer of stream abstraction. |  | ||||||
| You must use the response from await, a Promise, or the callback. |  | ||||||
| 
 |  | ||||||
| You can also give a file path: |  | ||||||
| 
 |  | ||||||
| ```js |  | ||||||
| request({ |  | ||||||
|     url: 'http://www.google.com', |  | ||||||
|     stream: '/tmp/google-index.html' |  | ||||||
| }); |  | ||||||
| ``` |  | ||||||
| 
 |  | ||||||
| Which is equivalent to passing a write stream for the file: |  | ||||||
| 
 |  | ||||||
| ```js |  | ||||||
| request({ |  | ||||||
|     url: 'http://www.google.com', |  | ||||||
|     stream: fs.createWriteStream('/tmp/google-index.html') |  | ||||||
| }); |  | ||||||
| ``` |  | ||||||
| 
 |  | ||||||
| ## Table of contents | ## Table of contents | ||||||
| 
 | 
 | ||||||
| -   [Forms](#forms) | -   [Forms](#forms) | ||||||
| @ -113,7 +63,7 @@ request.post('http://service.com/upload', { form: { key: 'value' } }); | |||||||
| // or | // or | ||||||
| request.post( | request.post( | ||||||
|     { url: 'http://service.com/upload', form: { key: 'value' } }, |     { url: 'http://service.com/upload', form: { key: 'value' } }, | ||||||
|     function (err, httpResponse, body) { |     function(err, httpResponse, body) { | ||||||
|         /* ... */ |         /* ... */ | ||||||
|     } |     } | ||||||
| ); | ); | ||||||
| @ -240,7 +190,7 @@ var username = 'username', | |||||||
|     password = 'password', |     password = 'password', | ||||||
|     url = 'http://' + username + ':' + password + '@some.server.com'; |     url = 'http://' + username + ':' + password + '@some.server.com'; | ||||||
| 
 | 
 | ||||||
| request({ url: url }, function (error, response, body) { | request({ url: url }, function(error, response, body) { | ||||||
|     // Do more stuff with 'body' here |     // Do more stuff with 'body' here | ||||||
| }); | }); | ||||||
| ``` | ``` | ||||||
|  | |||||||
| @ -5,15 +5,16 @@ var request = require('../'); | |||||||
| 
 | 
 | ||||||
| // will redirect to https://www.github.com and then https://github.com
 | // will redirect to https://www.github.com and then https://github.com
 | ||||||
| //request('http://www.github.com', function (error, response, body) {
 | //request('http://www.github.com', function (error, response, body) {
 | ||||||
| request( | request({ uri: { protocol: 'http:', hostname: 'www.github.com' } }, function( | ||||||
|     { uri: { protocol: 'http:', hostname: 'www.github.com' } }, |     error, | ||||||
|     function (error, response, body) { |     response, | ||||||
|         if (error) { |     body | ||||||
|             console.log('error:', error); // Print the error if one occurred
 | ) { | ||||||
|             return; |     if (error) { | ||||||
|         } |         console.log('error:', error); // Print the error if one occurred
 | ||||||
|         console.log('statusCode:', response.statusCode); // The final statusCode
 |         return; | ||||||
|         console.log('Final href:', response.request.uri.href); // The final URI
 |  | ||||||
|         console.log('Body Length:', body.length); // body length
 |  | ||||||
|     } |     } | ||||||
| ); |     console.log('statusCode:', response.statusCode); // The final statusCode
 | ||||||
|  |     console.log('Final href:', response.request.uri.href); // The final URI
 | ||||||
|  |     console.log('Body Length:', body.length); // body length
 | ||||||
|  | }); | ||||||
|  | |||||||
| @ -20,7 +20,7 @@ request( | |||||||
|             ) |             ) | ||||||
|         } |         } | ||||||
|     }, |     }, | ||||||
|     function (error, response, body) { |     function(error, response, body) { | ||||||
|         if (error) { |         if (error) { | ||||||
|             console.log('error:', error); // Print the error if one occurred
 |             console.log('error:', error); // Print the error if one occurred
 | ||||||
|             return; |             return; | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| 
 | 
 | ||||||
| //var request = require('urequest');
 | //var request = require('urequest');
 | ||||||
| var request = require('../'); | var request = require('../'); | ||||||
| request('https://www.google.com', function (error, response, body) { | request('https://www.google.com', function(error, response, body) { | ||||||
|     if (error) { |     if (error) { | ||||||
|         console.log('error:', error); // Print the error if one occurred
 |         console.log('error:', error); // Print the error if one occurred
 | ||||||
|         return; |         return; | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| 
 | 
 | ||||||
| //var request = require('urequest');
 | //var request = require('urequest');
 | ||||||
| var request = require('../'); | var request = require('../'); | ||||||
| request('http://www.google.com', function (error, response, body) { | request('http://www.google.com', function(error, response, body) { | ||||||
|     console.log('error:', error); // Print the error if one occurred
 |     console.log('error:', error); // Print the error if one occurred
 | ||||||
|     console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
 |     console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
 | ||||||
|     console.log('body:', body); // Print the HTML for the Google homepage.
 |     console.log('body:', body); // Print the HTML for the Google homepage.
 | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| 
 | 
 | ||||||
| //var request = require('urequest');
 | //var request = require('urequest');
 | ||||||
| var request = require('../'); | var request = require('../'); | ||||||
| request('https://www.google.com', function (error, response, body) { | request('https://www.google.com', function(error, response, body) { | ||||||
|     console.log('error:', error); // Print the error if one occurred
 |     console.log('error:', error); // Print the error if one occurred
 | ||||||
|     console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
 |     console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
 | ||||||
|     console.log('body:', body); // Print the HTML for the Google homepage.
 |     console.log('body:', body); // Print the HTML for the Google homepage.
 | ||||||
|  | |||||||
| @ -4,16 +4,17 @@ | |||||||
| var request = require('../'); | var request = require('../'); | ||||||
| 
 | 
 | ||||||
| // would normally redirect to https://www.github.com and then https://github.com
 | // would normally redirect to https://www.github.com and then https://github.com
 | ||||||
| request( | request({ uri: 'https://www.github.com', followRedirect: false }, function( | ||||||
|     { uri: 'https://www.github.com', followRedirect: false }, |     error, | ||||||
|     function (error, response, body) { |     response, | ||||||
|         if (error) { |     body | ||||||
|             console.log('error:', error); // Print the error if one occurred
 | ) { | ||||||
|             return; |     if (error) { | ||||||
|         } |         console.log('error:', error); // Print the error if one occurred
 | ||||||
|         console.log('href:', response.request.uri.href); // The final URI
 |         return; | ||||||
|         console.log('statusCode:', response.statusCode); // Should be 301 or 302
 |  | ||||||
|         console.log('Location:', response.headers.location); // The redirect
 |  | ||||||
|         console.log('Body:', body || JSON.stringify(body)); |  | ||||||
|     } |     } | ||||||
| ); |     console.log('href:', response.request.uri.href); // The final URI
 | ||||||
|  |     console.log('statusCode:', response.statusCode); // Should be 301 or 302
 | ||||||
|  |     console.log('Location:', response.headers.location); // The redirect
 | ||||||
|  |     console.log('Body:', body || JSON.stringify(body)); | ||||||
|  | }); | ||||||
|  | |||||||
| @ -7,9 +7,9 @@ request({ | |||||||
|     //headers: { 'user-agent': 'test/1.0' } // overwrite
 |     //headers: { 'user-agent': 'test/1.0' } // overwrite
 | ||||||
|     //userAgent: 'test/1.1' // add to the default
 |     //userAgent: 'test/1.1' // add to the default
 | ||||||
| }) | }) | ||||||
|     .then(function (resp) { |     .then(function(resp) { | ||||||
|         console.log(resp.body); |         console.log(resp.body); | ||||||
|     }) |     }) | ||||||
|     .catch(function (err) { |     .catch(function(err) { | ||||||
|         console.error(err); |         console.error(err); | ||||||
|     }); |     }); | ||||||
|  | |||||||
| @ -1,27 +0,0 @@ | |||||||
| 'use strict'; |  | ||||||
| 
 |  | ||||||
| var request = require('../'); |  | ||||||
| 
 |  | ||||||
| async function main() { |  | ||||||
|     var tpath = '/tmp/google-index.html'; |  | ||||||
|     var resp = await request({ |  | ||||||
|         url: 'https://google.com', |  | ||||||
|         encoding: null, |  | ||||||
|         stream: tpath |  | ||||||
|     }); |  | ||||||
|     console.log('[Response Headers]'); |  | ||||||
|     console.log(resp.toJSON().headers); |  | ||||||
| 
 |  | ||||||
|     //console.error(resp.headers, resp.body.byteLength);
 |  | ||||||
|     await resp.stream; |  | ||||||
|     console.log('[Response Body] written to', tpath); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| main() |  | ||||||
|     .then(function () { |  | ||||||
|         console.log('Pass'); |  | ||||||
|     }) |  | ||||||
|     .catch(function (e) { |  | ||||||
|         console.error('Fail'); |  | ||||||
|         console.error(e.stack); |  | ||||||
|     }); |  | ||||||
| @ -1,34 +0,0 @@ | |||||||
| 'use strict'; |  | ||||||
| 
 |  | ||||||
| var request = require('../'); |  | ||||||
| 
 |  | ||||||
| async function main() { |  | ||||||
|     var tpath = '/tmp/google-index.html'; |  | ||||||
|     var resp = await request({ |  | ||||||
|         url: 'https://google.com', |  | ||||||
|         encoding: null, |  | ||||||
|         stream: true |  | ||||||
|     }); |  | ||||||
|     console.log('[Response Headers]'); |  | ||||||
|     console.log(resp.toJSON().headers); |  | ||||||
| 
 |  | ||||||
|     resp.on('data', function (chunk) { |  | ||||||
| 	    console.log('[Data]', chunk.byteLength); |  | ||||||
|     }); |  | ||||||
|     resp.on('end', function (chunk) { |  | ||||||
| 	    console.log('[End]'); |  | ||||||
|     }); |  | ||||||
| 
 |  | ||||||
|     //console.error(resp.headers, resp.body.byteLength);
 |  | ||||||
|     await resp.stream; |  | ||||||
|     console.log('[Close]'); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| main() |  | ||||||
|     .then(function () { |  | ||||||
|         console.log('Pass'); |  | ||||||
|     }) |  | ||||||
|     .catch(function (e) { |  | ||||||
|         console.error('Fail'); |  | ||||||
|         console.error(e.stack); |  | ||||||
|     }); |  | ||||||
| @ -14,7 +14,7 @@ request( | |||||||
|         headers: { 'X-Foo': 'Bar' }, |         headers: { 'X-Foo': 'Bar' }, | ||||||
|         form: { foo: 'bar', baz: 'qux' } |         form: { foo: 'bar', baz: 'qux' } | ||||||
|     }, |     }, | ||||||
|     function (error, response, body) { |     function(error, response, body) { | ||||||
|         if (error) { |         if (error) { | ||||||
|             console.log('error:', error); // Print the error if one occurred
 |             console.log('error:', error); // Print the error if one occurred
 | ||||||
|             return; |             return; | ||||||
|  | |||||||
							
								
								
									
										117
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										117
									
								
								index.js
									
									
									
									
									
								
							| @ -5,7 +5,6 @@ var https = require('https'); | |||||||
| var url = require('url'); | var url = require('url'); | ||||||
| var os = require('os'); | var os = require('os'); | ||||||
| var pkg = require('./package.json'); | var pkg = require('./package.json'); | ||||||
| var fs = require('fs'); // only for streams
 |  | ||||||
| 
 | 
 | ||||||
| function debug() { | function debug() { | ||||||
|     if (module.exports.debug) { |     if (module.exports.debug) { | ||||||
| @ -14,7 +13,7 @@ function debug() { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function mergeOrDelete(defaults, updates) { | function mergeOrDelete(defaults, updates) { | ||||||
|     Object.keys(defaults).forEach(function (key) { |     Object.keys(defaults).forEach(function(key) { | ||||||
|         if (!(key in updates)) { |         if (!(key in updates)) { | ||||||
|             updates[key] = defaults[key]; |             updates[key] = defaults[key]; | ||||||
|             return; |             return; | ||||||
| @ -38,7 +37,7 @@ function mergeOrDelete(defaults, updates) { | |||||||
| // retrieves an existing header, case-sensitive
 | // retrieves an existing header, case-sensitive
 | ||||||
| function getHeaderName(reqOpts, header) { | function getHeaderName(reqOpts, header) { | ||||||
|     var headerNames = {}; |     var headerNames = {}; | ||||||
|     Object.keys(reqOpts.headers).forEach(function (casedName) { |     Object.keys(reqOpts.headers).forEach(function(casedName) { | ||||||
|         headerNames[casedName.toLowerCase()] = casedName; |         headerNames[casedName.toLowerCase()] = casedName; | ||||||
|     }); |     }); | ||||||
|     // returns the key, which in erroneous cases could be an empty string
 |     // returns the key, which in erroneous cases could be an empty string
 | ||||||
| @ -50,11 +49,11 @@ function hasHeader(reqOpts, header) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function toJSONifier(keys) { | function toJSONifier(keys) { | ||||||
|     return function () { |     return function() { | ||||||
|         var obj = {}; |         var obj = {}; | ||||||
|         var me = this; |         var me = this; | ||||||
| 
 | 
 | ||||||
|         keys.forEach(function (key) { |         keys.forEach(function(key) { | ||||||
|             if (me[key] && 'function' === typeof me[key].toJSON) { |             if (me[key] && 'function' === typeof me[key].toJSON) { | ||||||
|                 obj[key] = me[key].toJSON(); |                 obj[key] = me[key].toJSON(); | ||||||
|             } else { |             } else { | ||||||
| @ -79,7 +78,7 @@ function setDefaults(defs) { | |||||||
|         function onResponse(resp) { |         function onResponse(resp) { | ||||||
|             var followRedirect; |             var followRedirect; | ||||||
| 
 | 
 | ||||||
|             Object.keys(defs).forEach(function (key) { |             Object.keys(defs).forEach(function(key) { | ||||||
|                 if (key in opts && 'undefined' !== typeof opts[key]) { |                 if (key in opts && 'undefined' !== typeof opts[key]) { | ||||||
|                     return; |                     return; | ||||||
|                 } |                 } | ||||||
| @ -141,75 +140,13 @@ function setDefaults(defs) { | |||||||
|                     return urequestHelper(opts, cb); |                     return urequestHelper(opts, cb); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
| 
 |  | ||||||
|             if (opts.stream) { |  | ||||||
|                 // make the response await-able
 |  | ||||||
|                 var resolve; |  | ||||||
|                 var reject; |  | ||||||
|                 resp.stream = new Promise(function (_resolve, _reject) { |  | ||||||
|                     resolve = _resolve; |  | ||||||
|                     reject = _reject; |  | ||||||
|                 }); |  | ||||||
| 
 |  | ||||||
|                 // allow specifying a file
 |  | ||||||
|                 if ('string' === typeof opts.stream) { |  | ||||||
|                     try { |  | ||||||
|                         if (opts.debug) { |  | ||||||
|                             console.debug( |  | ||||||
|                                 '[@root/request] file write stream created' |  | ||||||
|                             ); |  | ||||||
|                         } |  | ||||||
|                         opts.stream = fs.createWriteStream(opts.stream); |  | ||||||
|                     } catch (e) { |  | ||||||
|                         cb(e); |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
|                 // or an existing write stream
 |  | ||||||
|                 if ('function' === typeof opts.stream.pipe) { |  | ||||||
|                     if (opts.debug) { |  | ||||||
|                         console.debug('[@root/request] stream piped'); |  | ||||||
|                     } |  | ||||||
|                     resp.pipe(opts.stream); |  | ||||||
|                 } |  | ||||||
|                 resp.on('error', function (e) { |  | ||||||
|                     if (opts.debug) { |  | ||||||
|                         console.debug("[@root/request] stream 'error'"); |  | ||||||
|                         console.error(e.stack); |  | ||||||
|                     } |  | ||||||
|                     resp.destroy(); |  | ||||||
|                     if ('function' === opts.stream.destroy) { |  | ||||||
|                         opts.stream.destroy(e); |  | ||||||
|                     } |  | ||||||
|                     reject(e); |  | ||||||
|                 }); |  | ||||||
|                 resp.on('end', function () { |  | ||||||
|                     if (opts.debug) { |  | ||||||
|                         console.debug("[@root/request] stream 'end'"); |  | ||||||
|                     } |  | ||||||
|                     if ('function' === opts.stream.destroy) { |  | ||||||
|                         opts.stream.end(); |  | ||||||
|                         // this will close the stream (i.e. sync to disk)
 |  | ||||||
|                         opts.stream.destroy(); |  | ||||||
|                     } |  | ||||||
|                 }); |  | ||||||
|                 resp.on('close', function () { |  | ||||||
|                     if (opts.debug) { |  | ||||||
|                         console.debug("[@root/request] stream 'close'"); |  | ||||||
|                     } |  | ||||||
|                     resolve(); |  | ||||||
|                 }); |  | ||||||
|                 // and in all cases, return the stream
 |  | ||||||
|                 cb(null, resp); |  | ||||||
|                 return; |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             if (null === opts.encoding) { |             if (null === opts.encoding) { | ||||||
|                 resp._body = []; |                 resp._body = []; | ||||||
|             } else { |             } else { | ||||||
|                 resp.body = ''; |                 resp.body = ''; | ||||||
|             } |             } | ||||||
|             resp._bodyLength = 0; |             resp._bodyLength = 0; | ||||||
|             resp.on('data', function (chunk) { |             resp.on('data', function(chunk) { | ||||||
|                 if ('string' === typeof resp.body) { |                 if ('string' === typeof resp.body) { | ||||||
|                     resp.body += chunk.toString(opts.encoding); |                     resp.body += chunk.toString(opts.encoding); | ||||||
|                 } else { |                 } else { | ||||||
| @ -217,7 +154,7 @@ function setDefaults(defs) { | |||||||
|                     resp._bodyLength += chunk.length; |                     resp._bodyLength += chunk.length; | ||||||
|                 } |                 } | ||||||
|             }); |             }); | ||||||
|             resp.on('end', function () { |             resp.on('end', function() { | ||||||
|                 if ('string' !== typeof resp.body) { |                 if ('string' !== typeof resp.body) { | ||||||
|                     if (1 === resp._body.length) { |                     if (1 === resp._body.length) { | ||||||
|                         resp.body = resp._body[0]; |                         resp.body = resp._body[0]; | ||||||
| @ -237,9 +174,7 @@ function setDefaults(defs) { | |||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|                 debug('\n[urequest] resp.toJSON():'); |                 debug('\n[urequest] resp.toJSON():'); | ||||||
|                 if (module.exports.debug) { |                 debug(resp.toJSON()); | ||||||
|                     debug(resp.toJSON()); |  | ||||||
|                 } |  | ||||||
|                 if (opts.debug) { |                 if (opts.debug) { | ||||||
|                     console.debug('[@root/request] Response Body:'); |                     console.debug('[@root/request] Response Body:'); | ||||||
|                     console.debug(resp.body); |                     console.debug(resp.body); | ||||||
| @ -264,12 +199,12 @@ function setDefaults(defs) { | |||||||
|             _body = JSON.stringify(opts.json); |             _body = JSON.stringify(opts.json); | ||||||
|         } else if (opts.form) { |         } else if (opts.form) { | ||||||
|             _body = Object.keys(opts.form) |             _body = Object.keys(opts.form) | ||||||
|                 .filter(function (key) { |                 .filter(function(key) { | ||||||
|                     if ('undefined' !== typeof opts.form[key]) { |                     if ('undefined' !== typeof opts.form[key]) { | ||||||
|                         return true; |                         return true; | ||||||
|                     } |                     } | ||||||
|                 }) |                 }) | ||||||
|                 .map(function (key) { |                 .map(function(key) { | ||||||
|                     return ( |                     return ( | ||||||
|                         encodeURIComponent(key) + |                         encodeURIComponent(key) + | ||||||
|                         '=' + |                         '=' + | ||||||
| @ -283,7 +218,7 @@ function setDefaults(defs) { | |||||||
|             _body = Buffer.from(_body); |             _body = Buffer.from(_body); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         Object.keys(opts.uri).forEach(function (key) { |         Object.keys(opts.uri).forEach(function(key) { | ||||||
|             finalOpts[key] = opts.uri[key]; |             finalOpts[key] = opts.uri[key]; | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
| @ -298,7 +233,7 @@ function setDefaults(defs) { | |||||||
|             'createConnection', |             'createConnection', | ||||||
|             'timeout', |             'timeout', | ||||||
|             'setHost' |             'setHost' | ||||||
|         ].forEach(function (key) { |         ].forEach(function(key) { | ||||||
|             finalOpts[key] = opts.uri[key]; |             finalOpts[key] = opts.uri[key]; | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
| @ -364,7 +299,7 @@ function setDefaults(defs) { | |||||||
|             } |             } | ||||||
|             try { |             try { | ||||||
|                 form = new MyFormData(); |                 form = new MyFormData(); | ||||||
|                 Object.keys(opts.formData).forEach(function (key) { |                 Object.keys(opts.formData).forEach(function(key) { | ||||||
|                     function add(key, data, opts) { |                     function add(key, data, opts) { | ||||||
|                         if (data.value) { |                         if (data.value) { | ||||||
|                             opts = data.options; |                             opts = data.options; | ||||||
| @ -373,7 +308,7 @@ function setDefaults(defs) { | |||||||
|                         form.append(key, data, opts); |                         form.append(key, data, opts); | ||||||
|                     } |                     } | ||||||
|                     if (Array.isArray(opts.formData[key])) { |                     if (Array.isArray(opts.formData[key])) { | ||||||
|                         opts.formData[key].forEach(function (data) { |                         opts.formData[key].forEach(function(data) { | ||||||
|                             add(key, data); |                             add(key, data); | ||||||
|                         }); |                         }); | ||||||
|                     } else { |                     } else { | ||||||
| @ -385,7 +320,7 @@ function setDefaults(defs) { | |||||||
|                 return; |                 return; | ||||||
|             } |             } | ||||||
|             formHeaders = form.getHeaders(); |             formHeaders = form.getHeaders(); | ||||||
|             Object.keys(formHeaders).forEach(function (header) { |             Object.keys(formHeaders).forEach(function(header) { | ||||||
|                 finalOpts.headers[header] = formHeaders[header]; |                 finalOpts.headers[header] = formHeaders[header]; | ||||||
|             }); |             }); | ||||||
|         } |         } | ||||||
| @ -411,7 +346,7 @@ function setDefaults(defs) { | |||||||
|             debug(formHeaders); |             debug(formHeaders); | ||||||
|             // generally uploads don't use Chunked Encoding (some systems have issues with it)
 |             // generally uploads don't use Chunked Encoding (some systems have issues with it)
 | ||||||
|             // and I don't want to do the work to calculate the content-lengths. This seems to work.
 |             // and I don't want to do the work to calculate the content-lengths. This seems to work.
 | ||||||
|             req = form.submit(finalOpts, function (err, resp) { |             req = form.submit(finalOpts, function(err, resp) { | ||||||
|                 if (err) { |                 if (err) { | ||||||
|                     cb(err); |                     cb(err); | ||||||
|                     return; |                     return; | ||||||
| @ -445,7 +380,7 @@ function setDefaults(defs) { | |||||||
|             if ('function' === typeof _body.pipe) { |             if ('function' === typeof _body.pipe) { | ||||||
|                 // used for chunked encoding
 |                 // used for chunked encoding
 | ||||||
|                 _body.pipe(req); |                 _body.pipe(req); | ||||||
|                 _body.on('error', function (err) { |                 _body.on('error', function(err) { | ||||||
|                     // https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
 |                     // https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
 | ||||||
|                     // if the Readable stream emits an error during processing,
 |                     // if the Readable stream emits an error during processing,
 | ||||||
|                     // the Writable destination is not closed automatically
 |                     // the Writable destination is not closed automatically
 | ||||||
| @ -492,7 +427,7 @@ function setDefaults(defs) { | |||||||
|             opts = { url: opts }; |             opts = { url: opts }; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         module.exports._keys.forEach(function (key) { |         module.exports._keys.forEach(function(key) { | ||||||
|             if (key in opts && 'undefined' !== typeof opts[key]) { |             if (key in opts && 'undefined' !== typeof opts[key]) { | ||||||
|                 reqOpts[key] = opts[key]; |                 reqOpts[key] = opts[key]; | ||||||
|             } else if (key in defs) { |             } else if (key in defs) { | ||||||
| @ -554,14 +489,14 @@ function setDefaults(defs) { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     function smartPromisify(fn) { |     function smartPromisify(fn) { | ||||||
|         return function (opts) { |         return function(opts) { | ||||||
|             var cb; |             var cb; | ||||||
|             if ('function' === typeof arguments[1]) { |             if ('function' === typeof arguments[1]) { | ||||||
|                 cb = Array.prototype.slice.call(arguments)[1]; |                 cb = Array.prototype.slice.call(arguments)[1]; | ||||||
|                 return fn(opts, cb); |                 return fn(opts, cb); | ||||||
|             } |             } | ||||||
|             return new Promise(function (resolve, reject) { |             return new Promise(function(resolve, reject) { | ||||||
|                 fn(opts, function (err, resp) { |                 fn(opts, function(err, resp) { | ||||||
|                     if (err) { |                     if (err) { | ||||||
|                         err._response = resp; |                         err._response = resp; | ||||||
|                         reject(err); |                         reject(err); | ||||||
| @ -575,13 +510,13 @@ function setDefaults(defs) { | |||||||
| 
 | 
 | ||||||
|     var smartUrequest = smartPromisify(urequest); |     var smartUrequest = smartPromisify(urequest); | ||||||
| 
 | 
 | ||||||
|     smartUrequest.defaults = function (_defs) { |     smartUrequest.defaults = function(_defs) { | ||||||
|         _defs = mergeOrDelete(defs, _defs); |         _defs = mergeOrDelete(defs, _defs); | ||||||
|         return setDefaults(_defs); |         return setDefaults(_defs); | ||||||
|     }; |     }; | ||||||
|     ['get', 'put', 'post', 'patch', 'delete', 'head', 'options'].forEach( |     ['get', 'put', 'post', 'patch', 'delete', 'head', 'options'].forEach( | ||||||
|         function (method) { |         function(method) { | ||||||
|             smartUrequest[method] = smartPromisify(function (obj, cb) { |             smartUrequest[method] = smartPromisify(function(obj, cb) { | ||||||
|                 if ('string' === typeof obj) { |                 if ('string' === typeof obj) { | ||||||
|                     obj = { url: obj }; |                     obj = { url: obj }; | ||||||
|                 } |                 } | ||||||
| @ -633,8 +568,7 @@ var _defaults = { | |||||||
|     followOriginalHttpMethod: false, |     followOriginalHttpMethod: false, | ||||||
|     maxRedirects: 10, |     maxRedirects: 10, | ||||||
|     removeRefererHeader: false, |     removeRefererHeader: false, | ||||||
|     // encoding: undefined,
 |     //, encoding: undefined
 | ||||||
|     // stream: false, // TODO allow a stream?
 |  | ||||||
|     gzip: false |     gzip: false | ||||||
|     //, body: undefined
 |     //, body: undefined
 | ||||||
|     //, json: undefined
 |     //, json: undefined
 | ||||||
| @ -643,7 +577,6 @@ module.exports = setDefaults(_defaults); | |||||||
| 
 | 
 | ||||||
| module.exports._keys = Object.keys(_defaults).concat([ | module.exports._keys = Object.keys(_defaults).concat([ | ||||||
|     'encoding', |     'encoding', | ||||||
|     'stream', |  | ||||||
|     'body', |     'body', | ||||||
|     'json', |     'json', | ||||||
|     'form', |     'form', | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @ -1,5 +1,5 @@ | |||||||
| { | { | ||||||
|     "name": "@root/request", |     "name": "@root/request", | ||||||
|     "version": "1.7.0", |     "version": "1.5.0", | ||||||
|     "lockfileVersion": 1 |     "lockfileVersion": 1 | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,6 +1,6 @@ | |||||||
| { | { | ||||||
|     "name": "@root/request", |     "name": "@root/request", | ||||||
|     "version": "1.7.0", |     "version": "1.6.1", | ||||||
|     "description": "A lightweight, zero-dependency drop-in replacement for request", |     "description": "A lightweight, zero-dependency drop-in replacement for request", | ||||||
|     "main": "index.js", |     "main": "index.js", | ||||||
|     "files": [ |     "files": [ | ||||||
|  | |||||||
| @ -1,11 +1,11 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
| 
 | 
 | ||||||
| var net = require('net'); | var net = require('net'); | ||||||
| var server = net.createServer(function (socket) { | var server = net.createServer(function(socket) { | ||||||
|     socket.on('data', function (chunk) { |     socket.on('data', function(chunk) { | ||||||
|         console.info(chunk.toString('utf8')); |         console.info(chunk.toString('utf8')); | ||||||
|     }); |     }); | ||||||
| }); | }); | ||||||
| server.listen(3007, function () { | server.listen(3007, function() { | ||||||
|     console.info('Listening on', this.address()); |     console.info('Listening on', this.address()); | ||||||
| }); | }); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user