mirror of
				https://github.com/therootcompany/request.js.git
				synced 2024-11-16 17:28:58 +00:00 
			
		
		
		
	Compare commits
	
		
			20 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| bb30d5acf6 | |||
| 5b539deb7b | |||
| 0a2e7afa76 | |||
| ba60df7eab | |||
| bc4f6e59c0 | |||
| 3842ee1d61 | |||
| 9518cb970b | |||
| 2e9a643c0f | |||
| dcd41a33d0 | |||
| 5f5e0b6066 | |||
| ed2bab931f | |||
| a95d003ed5 | |||
| 5149bc9dcb | |||
| 95a12a8285 | |||
| 9395ec96e3 | |||
| 3574e35635 | |||
| 508f1ce591 | |||
| c2c4b5b2de | |||
| f6557b7610 | |||
| 4b9a1f07ee | 
							
								
								
									
										22
									
								
								.jshintrc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								.jshintrc
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | ||||
| { | ||||
|   "browser": true, | ||||
|   "node": true, | ||||
|   "esversion": 11, | ||||
|   "curly": true, | ||||
|   "sub": true, | ||||
|   "bitwise": true, | ||||
|   "eqeqeq": true, | ||||
|   "forin": true, | ||||
|   "freeze": true, | ||||
|   "immed": true, | ||||
|   "latedef": "nofunc", | ||||
|   "nonbsp": true, | ||||
|   "nonew": true, | ||||
|   "plusplus": true, | ||||
|   "undef": true, | ||||
|   "unused": "vars", | ||||
|   "strict": true, | ||||
|   "maxdepth": 3, | ||||
|   "maxstatements": 100, | ||||
|   "maxcomplexity": 40 | ||||
| } | ||||
							
								
								
									
										6
									
								
								CHANGELOG.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								CHANGELOG.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,6 @@ | ||||
| # CHANGELOG | ||||
| 
 | ||||
| ## v1.8.0 | ||||
| 
 | ||||
| -   add `resp.ok` - same as WHATWG fetch `resp.ok = (resp.statusCode >= 200 && resp.statusCode < 300)` | ||||
| -   add `resp.stream.body()` to populate `resp.body` rather than (or perhaps in addition to) continuing to stream (useful for error handling) | ||||
							
								
								
									
										65
									
								
								EXTRA.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								EXTRA.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,65 @@ | ||||
| # Extra | ||||
| 
 | ||||
| There are some niche features of @root/request which are beyond the request.js | ||||
| compatibility. | ||||
| 
 | ||||
| ## async/await & Promises | ||||
| 
 | ||||
| The differences in async support are explained in [README.md](/README.md), up near the top. | ||||
| 
 | ||||
| If you're familiar with Promises (and async/await), then it's pretty self-explanatory. | ||||
| 
 | ||||
| ## ok | ||||
| 
 | ||||
| Just like WHATWG `fetch`, we have `resp.ok`: | ||||
| 
 | ||||
| ```js | ||||
| let resp = await request({ | ||||
|     url: 'https://example.com' | ||||
| }).then(mustOk); | ||||
| ``` | ||||
| 
 | ||||
| ```js | ||||
| function mustOk(resp) { | ||||
|     if (!resp.ok) { | ||||
|         // handle error | ||||
|         throw new Error('BAD RESPONSE'); | ||||
|     } | ||||
|     return resp; | ||||
| } | ||||
| ``` | ||||
| 
 | ||||
| ## streams | ||||
| 
 | ||||
| The differences in stream support are explained in [README.md](/README.md), up near the top. | ||||
| 
 | ||||
| ## userAgent | ||||
| 
 | ||||
| There's a default User-Agent string describing the version of @root/request, node.js, and the OS. | ||||
| 
 | ||||
| Add to the default User-Agent | ||||
| 
 | ||||
| ```js | ||||
| request({ | ||||
|     // ... | ||||
|     userAgent: 'my-package/1.0' // add to agent | ||||
| }); | ||||
| ``` | ||||
| 
 | ||||
| Replace the default User-Agent | ||||
| 
 | ||||
| ```js | ||||
| request({ | ||||
|     // ... | ||||
|     headers: { 'User-Agent': 'replace/0.0' } | ||||
| }); | ||||
| ``` | ||||
| 
 | ||||
| Disable the default User-Agent: | ||||
| 
 | ||||
| ```js | ||||
| request({ | ||||
|     // ... | ||||
|     headers: { 'User-Agent': false } | ||||
| }); | ||||
| ``` | ||||
							
								
								
									
										114
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										114
									
								
								README.md
									
									
									
									
									
								
							| @ -1,4 +1,4 @@ | ||||
| # [µRequest](https://git.rootprojects.org/root/request.js) | a [Root](https://rootprojects.org) project | ||||
| # [@root/request](https://git.rootprojects.org/root/request.js) | a [Root](https://rootprojects.org) project | ||||
| 
 | ||||
| > Minimalist HTTP client | ||||
| 
 | ||||
| @ -10,15 +10,17 @@ Written from scratch, with zero-dependencies. | ||||
| 
 | ||||
| ## Super simple to use | ||||
| 
 | ||||
| µRequest is designed to be a drop-in replacement for request. It supports HTTPS and follows redirects by default. | ||||
| @root/request is designed to be a drop-in replacement for request. It also supports Promises and async/await by default, enhanced stream support, and a few other things as mentioned below. | ||||
| 
 | ||||
| ```bash | ||||
| npm install --save @root/request | ||||
| 
 | ||||
| # or npm install git+ssh://git@git.therootcompany.com/request.js | ||||
| ``` | ||||
| 
 | ||||
| ```js | ||||
| 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('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. | ||||
| @ -31,26 +33,114 @@ request('http://www.google.com', function(error, response, body) { | ||||
| var request = require('@root/request'); | ||||
| 
 | ||||
| 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('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 | ||||
|     }); | ||||
| ``` | ||||
| 
 | ||||
| **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`. | ||||
| 
 | ||||
| ```diff | ||||
| -var request = require('request'); | ||||
| +var request = require('@root/request'); | ||||
| 
 | ||||
| -var stream = request({ url, headers }); | ||||
| +var stream = await request({ url, headers }); | ||||
| 
 | ||||
|  let attachment = await new MailgunAPI.Attachment({ | ||||
|    data: stream | ||||
|  }) | ||||
| ``` | ||||
| 
 | ||||
| Example: | ||||
| 
 | ||||
| ```js | ||||
| var request = require('@root/request'); | ||||
| 
 | ||||
| var resp = await request({ | ||||
|     url: 'http://www.google.com', | ||||
|     stream: true // true | 'filename.ext' | stream.Writable | ||||
| }); | ||||
| 
 | ||||
| // 'resp' itself is a ReadableStream | ||||
| 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; // returns `undefined` | ||||
| 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') | ||||
| }); | ||||
| ``` | ||||
| 
 | ||||
| Also, `await resp.stream.body()` can be used to get back the full body (the same as if you didn't use the `stream` option: | ||||
| 
 | ||||
| ```js | ||||
| let resp = await request({ | ||||
|     url: 'http://www.google.com', | ||||
|     stream: true | ||||
| }); | ||||
| if (!resp.ok) { | ||||
|     await resp.stream.body(); | ||||
|     console.error(resp.body); | ||||
| } | ||||
| ``` | ||||
| 
 | ||||
| ## Table of contents | ||||
| 
 | ||||
| -   [Extra Features](/EXTRA.md) | ||||
| -   [Forms](#forms) | ||||
| -   [HTTP Authentication](#http-authentication) | ||||
| -   [Custom HTTP Headers](#custom-http-headers) | ||||
| -   [Unix Domain Sockets](#unix-domain-sockets) | ||||
| -   [**All Available Options**](#requestoptions-callback) | ||||
| 
 | ||||
| ## Extra Features | ||||
| 
 | ||||
| The following are features that the original `request` did not have, but have been added for convenience in `@root/request`. | ||||
| 
 | ||||
| -   Support for `async`/`await` & `Promise`s (as explained above) | ||||
| -   `request({ userAgent: 'my-api/1.1' })` (for building API clients) | ||||
| -   `resp.ok` (just like `fetch`) | ||||
| -   `resp.stream` (see above) | ||||
| 
 | ||||
| See [EXTRA.md](/EXTRA.md) | ||||
| 
 | ||||
| ## Forms | ||||
| 
 | ||||
| `urequest` supports `application/x-www-form-urlencoded` and `multipart/form-data` form uploads. | ||||
| `@root/request` supports `application/x-www-form-urlencoded` and `multipart/form-data` form uploads. | ||||
| 
 | ||||
| <!-- For `multipart/related` refer to the `multipart` API. --> | ||||
| 
 | ||||
| @ -63,7 +153,7 @@ request.post('http://service.com/upload', { form: { key: 'value' } }); | ||||
| // or | ||||
| request.post( | ||||
|     { url: 'http://service.com/upload', form: { key: 'value' } }, | ||||
|     function(err, httpResponse, body) { | ||||
|     function (err, httpResponse, body) { | ||||
|         /* ... */ | ||||
|     } | ||||
| ); | ||||
| @ -76,12 +166,12 @@ request.post('http://service.com/upload').form({key:'value'}) | ||||
| 
 | ||||
| #### multipart/form-data (Multipart Form Uploads) | ||||
| 
 | ||||
| For `multipart/form-data` we use the [form-data](https://github.com/form-data/form-data) library by [@felixge](https://github.com/felixge). For the most cases, you can pass your upload form data via the `formData` option. | ||||
| For `multipart/form-data` we use the [form-data](https://github.com/form-data/form-data/tree/v2.5.1) library by [@felixge](https://github.com/felixge). For the most cases, you can pass your upload form data via the `formData` option. | ||||
| 
 | ||||
| To use `form-data`, you must install it separately: | ||||
| 
 | ||||
| ```bash | ||||
| npm install --save form-data@2 | ||||
| npm install --save form-data@2.x | ||||
| ``` | ||||
| 
 | ||||
| ```js | ||||
| @ -190,7 +280,7 @@ var username = 'username', | ||||
|     password = 'password', | ||||
|     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 | ||||
| }); | ||||
| ``` | ||||
| @ -245,7 +335,7 @@ request(options, callback); | ||||
| 
 | ||||
| ## UNIX Domain Sockets | ||||
| 
 | ||||
| `urequest` supports making requests to [UNIX Domain Sockets](https://en.wikipedia.org/wiki/Unix_domain_socket). To make one, use the following URL scheme: | ||||
| `@root/request` supports making requests to [UNIX Domain Sockets](https://en.wikipedia.org/wiki/Unix_domain_socket). To make one, use the following URL scheme: | ||||
| 
 | ||||
| ```js | ||||
| /* Pattern */ 'http://unix:SOCKET:PATH'; | ||||
| @ -361,7 +451,7 @@ These HTTP method convenience functions act just like `request()` but with a def | ||||
| 
 | ||||
| There are at least <!--three--> two ways to debug the operation of `request`: | ||||
| 
 | ||||
| 1. Launch the node process like `NODE_DEBUG=urequest node script.js` | ||||
| 1. Launch the node process like `NODE_DEBUG=@root/request node script.js` | ||||
|    (`lib,request,otherlib` works too). | ||||
| 
 | ||||
| 2. Set `require('@root/request').debug = true` at any time (this does the same thing | ||||
|  | ||||
| @ -5,16 +5,15 @@ var request = require('../'); | ||||
| 
 | ||||
| // will redirect to https://www.github.com and then https://github.com
 | ||||
| //request('http://www.github.com', function (error, response, body) {
 | ||||
| request({ uri: { protocol: 'http:', hostname: 'www.github.com' } }, function( | ||||
|     error, | ||||
|     response, | ||||
|     body | ||||
| ) { | ||||
|     if (error) { | ||||
|         console.log('error:', error); // Print the error if one occurred
 | ||||
|         return; | ||||
| request( | ||||
|     { uri: { protocol: 'http:', hostname: 'www.github.com' } }, | ||||
|     function (error, response, body) { | ||||
|         if (error) { | ||||
|             console.log('error:', error); // Print the error if one occurred
 | ||||
|             return; | ||||
|         } | ||||
|         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
 | ||||
|     } | ||||
|     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) { | ||||
|             console.log('error:', error); // Print the error if one occurred
 | ||||
|             return; | ||||
|  | ||||
| @ -2,7 +2,7 @@ | ||||
| 
 | ||||
| //var request = require('urequest');
 | ||||
| var request = require('../'); | ||||
| request('https://www.google.com', function(error, response, body) { | ||||
| request('https://www.google.com', function (error, response, body) { | ||||
|     if (error) { | ||||
|         console.log('error:', error); // Print the error if one occurred
 | ||||
|         return; | ||||
|  | ||||
| @ -2,7 +2,7 @@ | ||||
| 
 | ||||
| //var request = require('urequest');
 | ||||
| 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('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.
 | ||||
|  | ||||
| @ -2,7 +2,7 @@ | ||||
| 
 | ||||
| //var request = require('urequest');
 | ||||
| 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('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.
 | ||||
|  | ||||
| @ -4,17 +4,16 @@ | ||||
| var request = require('../'); | ||||
| 
 | ||||
| // would normally redirect to https://www.github.com and then https://github.com
 | ||||
| request({ uri: 'https://www.github.com', followRedirect: false }, function( | ||||
|     error, | ||||
|     response, | ||||
|     body | ||||
| ) { | ||||
|     if (error) { | ||||
|         console.log('error:', error); // Print the error if one occurred
 | ||||
|         return; | ||||
| request( | ||||
|     { uri: 'https://www.github.com', followRedirect: false }, | ||||
|     function (error, response, body) { | ||||
|         if (error) { | ||||
|             console.log('error:', error); // Print the error if one occurred
 | ||||
|             return; | ||||
|         } | ||||
|         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)); | ||||
|     } | ||||
|     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)); | ||||
| }); | ||||
| ); | ||||
|  | ||||
							
								
								
									
										15
									
								
								examples/postbin.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								examples/postbin.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | ||||
| 'use strict'; | ||||
| 
 | ||||
| var request = require('../'); | ||||
| request({ | ||||
|     url: 'https://postb.in/1588134650162-6019286897499?hello=world' | ||||
|     //headers: { 'user-agent': false } // remove
 | ||||
|     //headers: { 'user-agent': 'test/1.0' } // overwrite
 | ||||
|     //userAgent: 'test/1.1' // add to the default
 | ||||
| }) | ||||
|     .then(function (resp) { | ||||
|         console.log(resp.body); | ||||
|     }) | ||||
|     .catch(function (err) { | ||||
|         console.error(err); | ||||
|     }); | ||||
							
								
								
									
										27
									
								
								examples/stream-to-file.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								examples/stream-to-file.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,27 @@ | ||||
| '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); | ||||
|     }); | ||||
							
								
								
									
										34
									
								
								examples/stream.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								examples/stream.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,34 @@ | ||||
| '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' }, | ||||
|         form: { foo: 'bar', baz: 'qux' } | ||||
|     }, | ||||
|     function(error, response, body) { | ||||
|     function (error, response, body) { | ||||
|         if (error) { | ||||
|             console.log('error:', error); // Print the error if one occurred
 | ||||
|             return; | ||||
|  | ||||
							
								
								
									
										272
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										272
									
								
								index.js
									
									
									
									
									
								
							| @ -3,6 +3,9 @@ | ||||
| var http = require('http'); | ||||
| var https = require('https'); | ||||
| var url = require('url'); | ||||
| var os = require('os'); | ||||
| var pkg = require('./package.json'); | ||||
| var fs = require('fs'); // only for streams
 | ||||
| 
 | ||||
| function debug() { | ||||
|     if (module.exports.debug) { | ||||
| @ -11,7 +14,7 @@ function debug() { | ||||
| } | ||||
| 
 | ||||
| function mergeOrDelete(defaults, updates) { | ||||
|     Object.keys(defaults).forEach(function(key) { | ||||
|     Object.keys(defaults).forEach(function (key) { | ||||
|         if (!(key in updates)) { | ||||
|             updates[key] = defaults[key]; | ||||
|             return; | ||||
| @ -35,7 +38,7 @@ function mergeOrDelete(defaults, updates) { | ||||
| // retrieves an existing header, case-sensitive
 | ||||
| function getHeaderName(reqOpts, header) { | ||||
|     var headerNames = {}; | ||||
|     Object.keys(reqOpts.headers).forEach(function(casedName) { | ||||
|     Object.keys(reqOpts.headers).forEach(function (casedName) { | ||||
|         headerNames[casedName.toLowerCase()] = casedName; | ||||
|     }); | ||||
|     // returns the key, which in erroneous cases could be an empty string
 | ||||
| @ -47,11 +50,11 @@ function hasHeader(reqOpts, header) { | ||||
| } | ||||
| 
 | ||||
| function toJSONifier(keys) { | ||||
|     return function() { | ||||
|     return function () { | ||||
|         var obj = {}; | ||||
|         var me = this; | ||||
| 
 | ||||
|         keys.forEach(function(key) { | ||||
|         keys.forEach(function (key) { | ||||
|             if (me[key] && 'function' === typeof me[key].toJSON) { | ||||
|                 obj[key] = me[key].toJSON(); | ||||
|             } else { | ||||
| @ -63,6 +66,102 @@ function toJSONifier(keys) { | ||||
|     }; | ||||
| } | ||||
| 
 | ||||
| function setupPipe(resp, opts) { | ||||
|     // make the response await-able
 | ||||
|     var resolve; | ||||
|     var reject; | ||||
|     var p = new Promise(function (_resolve, _reject) { | ||||
|         resolve = _resolve; | ||||
|         reject = _reject; | ||||
|     }); | ||||
| 
 | ||||
|     // 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.once('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.once('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.once('close', function () { | ||||
|         if (opts.debug) { | ||||
|             console.debug("[@root/request] stream 'close'"); | ||||
|         } | ||||
|         resolve(); | ||||
|     }); | ||||
|     return p; | ||||
| } | ||||
| 
 | ||||
| function handleResponse(resp, opts, cb) { | ||||
|     // body can be buffer, string, or json
 | ||||
|     if (null === opts.encoding) { | ||||
|         resp._body = []; | ||||
|     } else { | ||||
|         resp.body = ''; | ||||
|     } | ||||
|     resp._bodyLength = 0; | ||||
|     resp.on('readable', function () { | ||||
|         var chunk; | ||||
|         while ((chunk = resp.read())) { | ||||
|             if ('string' === typeof resp.body) { | ||||
|                 resp.body += chunk.toString(opts.encoding); | ||||
|             } else { | ||||
|                 resp._body.push(chunk); | ||||
|                 resp._bodyLength += chunk.length; | ||||
|             } | ||||
|         } | ||||
|     }); | ||||
|     resp.once('end', function () { | ||||
|         if ('string' !== typeof resp.body) { | ||||
|             if (1 === resp._body.length) { | ||||
|                 resp.body = resp._body[0]; | ||||
|             } else { | ||||
|                 resp.body = Buffer.concat(resp._body, resp._bodyLength); | ||||
|             } | ||||
|             resp._body = null; | ||||
|         } | ||||
|         if (opts.json && 'string' === typeof resp.body) { | ||||
|             // TODO I would parse based on Content-Type
 | ||||
|             // but request.js doesn't do that.
 | ||||
|             try { | ||||
|                 resp.body = JSON.parse(resp.body); | ||||
|             } catch (e) { | ||||
|                 // ignore
 | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         debug('\n[urequest] resp.toJSON():'); | ||||
|         if (module.exports.debug) { | ||||
|             debug(resp.toJSON()); | ||||
|         } | ||||
|         if (opts.debug) { | ||||
|             console.debug('[@root/request] Response Body:'); | ||||
|             console.debug(resp.body); | ||||
|         } | ||||
|         cb(null, resp, resp.body); | ||||
|     }); | ||||
| } | ||||
| 
 | ||||
| function setDefaults(defs) { | ||||
|     defs = defs || {}; | ||||
| 
 | ||||
| @ -73,10 +172,23 @@ function setDefaults(defs) { | ||||
|         var req; | ||||
|         var finalOpts = {}; | ||||
| 
 | ||||
|         // allow specifying a file
 | ||||
|         if ('string' === typeof opts.stream) { | ||||
|             if (opts.debug) { | ||||
|                 console.debug('[@root/request] creating file write stream'); | ||||
|             } | ||||
|             try { | ||||
|                 opts.stream = fs.createWriteStream(opts.stream); | ||||
|             } catch (e) { | ||||
|                 cb(e); | ||||
|                 return; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         function onResponse(resp) { | ||||
|             var followRedirect; | ||||
| 
 | ||||
|             Object.keys(defs).forEach(function(key) { | ||||
|             Object.keys(defs).forEach(function (key) { | ||||
|                 if (key in opts && 'undefined' !== typeof opts[key]) { | ||||
|                     return; | ||||
|                 } | ||||
| @ -84,9 +196,16 @@ function setDefaults(defs) { | ||||
|             }); | ||||
|             followRedirect = opts.followRedirect; | ||||
| 
 | ||||
|             // copied from WHATWG fetch
 | ||||
|             resp.ok = false; | ||||
|             if (resp.statusCode >= 200 && resp.statusCode < 300) { | ||||
|                 resp.ok = true; | ||||
|             } | ||||
| 
 | ||||
|             resp.toJSON = toJSONifier([ | ||||
|                 'statusCode', | ||||
|                 'body', | ||||
|                 'ok', | ||||
|                 'headers', | ||||
|                 'request' | ||||
|             ]); | ||||
| @ -138,47 +257,20 @@ function setDefaults(defs) { | ||||
|                     return urequestHelper(opts, cb); | ||||
|                 } | ||||
|             } | ||||
|             if (null === opts.encoding) { | ||||
|                 resp._body = []; | ||||
|             } else { | ||||
|                 resp.body = ''; | ||||
|             } | ||||
|             resp._bodyLength = 0; | ||||
|             resp.on('data', function(chunk) { | ||||
|                 if ('string' === typeof resp.body) { | ||||
|                     resp.body += chunk.toString(opts.encoding); | ||||
|                 } else { | ||||
|                     resp._body.push(chunk); | ||||
|                     resp._bodyLength += chunk.length; | ||||
|                 } | ||||
|             }); | ||||
|             resp.on('end', function() { | ||||
|                 if ('string' !== typeof resp.body) { | ||||
|                     if (1 === resp._body.length) { | ||||
|                         resp.body = resp._body[0]; | ||||
|                     } else { | ||||
|                         resp.body = Buffer.concat(resp._body, resp._bodyLength); | ||||
|                     } | ||||
|                     resp._body = null; | ||||
|                 } | ||||
|                 if (opts.json && 'string' === typeof resp.body) { | ||||
|                     // TODO I would parse based on Content-Type
 | ||||
|                     // but request.js doesn't do that.
 | ||||
|                     try { | ||||
|                         resp.body = JSON.parse(resp.body); | ||||
|                     } catch (e) { | ||||
|                         // ignore
 | ||||
|                     } | ||||
|                 } | ||||
| 
 | ||||
|                 debug('\n[urequest] resp.toJSON():'); | ||||
|                 debug(resp.toJSON()); | ||||
|                 if (opts.debug) { | ||||
|                     console.debug('[@root/request] Response Body:'); | ||||
|                     console.debug(resp.body); | ||||
|                 } | ||||
|                 cb(null, resp, resp.body); | ||||
|             }); | ||||
|             if (opts.stream) { | ||||
|                 resp.stream = setupPipe(resp, opts); | ||||
|                 // can be string, buffer, or json... why not an async function too?
 | ||||
|                 resp.stream.body = async function () { | ||||
|                     handleResponse(resp, opts, cb); | ||||
|                     await resp.stream; | ||||
|                     return resp.body; | ||||
|                 }; | ||||
|                 cb(null, resp); | ||||
|                 return; | ||||
|             } | ||||
| 
 | ||||
|             handleResponse(resp, opts, cb); | ||||
|         } | ||||
| 
 | ||||
|         var _body; | ||||
| @ -188,7 +280,7 @@ function setDefaults(defs) { | ||||
|         var requester; | ||||
| 
 | ||||
|         if (opts.body) { | ||||
|             if (true === opts.json) { | ||||
|             if (true === opts.json && 'string' !== typeof opts.body) { | ||||
|                 _body = JSON.stringify(opts.body); | ||||
|             } else { | ||||
|                 _body = opts.body; | ||||
| @ -197,12 +289,12 @@ function setDefaults(defs) { | ||||
|             _body = JSON.stringify(opts.json); | ||||
|         } else if (opts.form) { | ||||
|             _body = Object.keys(opts.form) | ||||
|                 .filter(function(key) { | ||||
|                 .filter(function (key) { | ||||
|                     if ('undefined' !== typeof opts.form[key]) { | ||||
|                         return true; | ||||
|                     } | ||||
|                 }) | ||||
|                 .map(function(key) { | ||||
|                 .map(function (key) { | ||||
|                     return ( | ||||
|                         encodeURIComponent(key) + | ||||
|                         '=' + | ||||
| @ -216,7 +308,7 @@ function setDefaults(defs) { | ||||
|             _body = Buffer.from(_body); | ||||
|         } | ||||
| 
 | ||||
|         Object.keys(opts.uri).forEach(function(key) { | ||||
|         Object.keys(opts.uri).forEach(function (key) { | ||||
|             finalOpts[key] = opts.uri[key]; | ||||
|         }); | ||||
| 
 | ||||
| @ -231,12 +323,24 @@ function setDefaults(defs) { | ||||
|             'createConnection', | ||||
|             'timeout', | ||||
|             'setHost' | ||||
|         ].forEach(function(key) { | ||||
|             finalOpts[key] = opts.uri[key]; | ||||
|         ].forEach(function (key) { | ||||
|             if (key in opts) { | ||||
|                 finalOpts[key] = opts[key]; | ||||
|             } | ||||
|         }); | ||||
| 
 | ||||
|         finalOpts.method = opts.method; | ||||
|         finalOpts.headers = JSON.parse(JSON.stringify(opts.headers)); | ||||
| 
 | ||||
|         var uaHeader = getHeaderName(finalOpts, 'User-Agent') || 'User-Agent'; | ||||
|         // set a default user-agent
 | ||||
|         if (!finalOpts.headers[uaHeader]) { | ||||
|             if (false === finalOpts.headers[uaHeader]) { | ||||
|                 delete finalOpts.headers[uaHeader]; | ||||
|             } else { | ||||
|                 finalOpts.headers[uaHeader] = getUserAgent(opts.userAgent); | ||||
|             } | ||||
|         } | ||||
|         if (_body) { | ||||
|             // Most APIs expect (or require) Content-Length except in the case of multipart uploads
 | ||||
|             // Transfer-Encoding: Chunked (the default) is generally only well-supported downstream
 | ||||
| @ -287,7 +391,7 @@ function setDefaults(defs) { | ||||
|             } | ||||
|             try { | ||||
|                 form = new MyFormData(); | ||||
|                 Object.keys(opts.formData).forEach(function(key) { | ||||
|                 Object.keys(opts.formData).forEach(function (key) { | ||||
|                     function add(key, data, opts) { | ||||
|                         if (data.value) { | ||||
|                             opts = data.options; | ||||
| @ -296,7 +400,7 @@ function setDefaults(defs) { | ||||
|                         form.append(key, data, opts); | ||||
|                     } | ||||
|                     if (Array.isArray(opts.formData[key])) { | ||||
|                         opts.formData[key].forEach(function(data) { | ||||
|                         opts.formData[key].forEach(function (data) { | ||||
|                             add(key, data); | ||||
|                         }); | ||||
|                     } else { | ||||
| @ -308,7 +412,7 @@ function setDefaults(defs) { | ||||
|                 return; | ||||
|             } | ||||
|             formHeaders = form.getHeaders(); | ||||
|             Object.keys(formHeaders).forEach(function(header) { | ||||
|             Object.keys(formHeaders).forEach(function (header) { | ||||
|                 finalOpts.headers[header] = formHeaders[header]; | ||||
|             }); | ||||
|         } | ||||
| @ -334,7 +438,7 @@ function setDefaults(defs) { | ||||
|             debug(formHeaders); | ||||
|             // 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.
 | ||||
|             req = form.submit(finalOpts, function(err, resp) { | ||||
|             req = form.submit(finalOpts, function (err, resp) { | ||||
|                 if (err) { | ||||
|                     cb(err); | ||||
|                     return; | ||||
| @ -360,7 +464,7 @@ function setDefaults(defs) { | ||||
|             } | ||||
|         } | ||||
|         req = requester.request(finalOpts, onResponse); | ||||
|         req.on('error', cb); | ||||
|         req.once('error', cb); | ||||
| 
 | ||||
|         if (_body) { | ||||
|             debug("\n[urequest] '" + finalOpts.method + "' (request) body"); | ||||
| @ -368,6 +472,13 @@ function setDefaults(defs) { | ||||
|             if ('function' === typeof _body.pipe) { | ||||
|                 // used for chunked encoding
 | ||||
|                 _body.pipe(req); | ||||
|                 _body.once('error', function (err) { | ||||
|                     // https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
 | ||||
|                     // if the Readable stream emits an error during processing,
 | ||||
|                     // the Writable destination is not closed automatically
 | ||||
|                     _body.destroy(); | ||||
|                     req.destroy(err); | ||||
|                 }); | ||||
|             } else { | ||||
|                 // used for known content-length
 | ||||
|                 req.end(_body); | ||||
| @ -408,7 +519,7 @@ function setDefaults(defs) { | ||||
|             opts = { url: opts }; | ||||
|         } | ||||
| 
 | ||||
|         module.exports._keys.forEach(function(key) { | ||||
|         module.exports._keys.forEach(function (key) { | ||||
|             if (key in opts && 'undefined' !== typeof opts[key]) { | ||||
|                 reqOpts[key] = opts[key]; | ||||
|             } else if (key in defs) { | ||||
| @ -470,14 +581,14 @@ function setDefaults(defs) { | ||||
|     } | ||||
| 
 | ||||
|     function smartPromisify(fn) { | ||||
|         return function(opts) { | ||||
|         return function (opts) { | ||||
|             var cb; | ||||
|             if ('function' === typeof arguments[1]) { | ||||
|                 cb = Array.prototype.slice.call(arguments)[1]; | ||||
|                 return fn(opts, cb); | ||||
|             } | ||||
|             return new Promise(function(resolve, reject) { | ||||
|                 fn(opts, function(err, resp) { | ||||
|             return new Promise(function (resolve, reject) { | ||||
|                 fn(opts, function (err, resp) { | ||||
|                     if (err) { | ||||
|                         err._response = resp; | ||||
|                         reject(err); | ||||
| @ -491,13 +602,13 @@ function setDefaults(defs) { | ||||
| 
 | ||||
|     var smartUrequest = smartPromisify(urequest); | ||||
| 
 | ||||
|     smartUrequest.defaults = function(_defs) { | ||||
|     smartUrequest.defaults = function (_defs) { | ||||
|         _defs = mergeOrDelete(defs, _defs); | ||||
|         return setDefaults(_defs); | ||||
|     }; | ||||
|     ['get', 'put', 'post', 'patch', 'delete', 'head', 'options'].forEach( | ||||
|         function(method) { | ||||
|             smartUrequest[method] = smartPromisify(function(obj, cb) { | ||||
|         function (method) { | ||||
|             smartUrequest[method] = smartPromisify(function (obj, cb) { | ||||
|                 if ('string' === typeof obj) { | ||||
|                     obj = { url: obj }; | ||||
|                 } | ||||
| @ -511,6 +622,34 @@ function setDefaults(defs) { | ||||
|     return smartUrequest; | ||||
| } | ||||
| 
 | ||||
| var nodeUa = | ||||
|     '@root+request/' + | ||||
|     pkg.version + | ||||
|     ' ' + | ||||
|     process.release.name + | ||||
|     '/' + | ||||
|     process.version + | ||||
|     ' ' + | ||||
|     os.platform() + | ||||
|     '/' + | ||||
|     os.release() + | ||||
|     ' ' + | ||||
|     os.type() + | ||||
|     '/' + | ||||
|     process.arch; | ||||
| function getUserAgent(additional) { | ||||
|     // See https://tools.ietf.org/html/rfc8555#section-6.1
 | ||||
|     // And https://tools.ietf.org/html/rfc7231#section-5.5.3
 | ||||
|     // And https://community.letsencrypt.org/t/user-agent-flag-explained/3843/2
 | ||||
| 
 | ||||
|     var ua = nodeUa; | ||||
|     if (additional) { | ||||
|         ua = additional + ' ' + ua; | ||||
|     } | ||||
| 
 | ||||
|     return ua; | ||||
| } | ||||
| 
 | ||||
| var _defaults = { | ||||
|     sendImmediately: true, | ||||
|     method: '', | ||||
| @ -521,7 +660,8 @@ var _defaults = { | ||||
|     followOriginalHttpMethod: false, | ||||
|     maxRedirects: 10, | ||||
|     removeRefererHeader: false, | ||||
|     //, encoding: undefined
 | ||||
|     // encoding: undefined,
 | ||||
|     // stream: false, // TODO allow a stream?
 | ||||
|     gzip: false | ||||
|     //, body: undefined
 | ||||
|     //, json: undefined
 | ||||
| @ -530,12 +670,14 @@ module.exports = setDefaults(_defaults); | ||||
| 
 | ||||
| module.exports._keys = Object.keys(_defaults).concat([ | ||||
|     'encoding', | ||||
|     'stream', | ||||
|     'body', | ||||
|     'json', | ||||
|     'form', | ||||
|     'auth', | ||||
|     'formData', | ||||
|     'FormData' | ||||
|     'FormData', | ||||
|     'userAgent' // non-standard for request.js
 | ||||
| ]); | ||||
| module.exports.debug = | ||||
|     -1 !== (process.env.NODE_DEBUG || '').split(/\s+/g).indexOf('urequest'); | ||||
|  | ||||
							
								
								
									
										2
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @ -1,5 +1,5 @@ | ||||
| { | ||||
|     "name": "@root/request", | ||||
|     "version": "1.5.0", | ||||
|     "version": "1.8.2", | ||||
|     "lockfileVersion": 1 | ||||
| } | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| { | ||||
|     "name": "@root/request", | ||||
|     "version": "1.5.0", | ||||
|     "version": "1.8.2", | ||||
|     "description": "A lightweight, zero-dependency drop-in replacement for request", | ||||
|     "main": "index.js", | ||||
|     "files": [ | ||||
| @ -24,6 +24,6 @@ | ||||
|         "https", | ||||
|         "call" | ||||
|     ], | ||||
|     "author": "AJ ONeal <solderjs@gmail.com> (https://solderjs.com/)", | ||||
|     "author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)", | ||||
|     "license": "(MIT OR Apache-2.0)" | ||||
| } | ||||
|  | ||||
| @ -1,11 +1,11 @@ | ||||
| 'use strict'; | ||||
| 
 | ||||
| var net = require('net'); | ||||
| var server = net.createServer(function(socket) { | ||||
|     socket.on('data', function(chunk) { | ||||
| var server = net.createServer(function (socket) { | ||||
|     socket.on('data', function (chunk) { | ||||
|         console.info(chunk.toString('utf8')); | ||||
|     }); | ||||
| }); | ||||
| server.listen(3007, function() { | ||||
| server.listen(3007, function () { | ||||
|     console.info('Listening on', this.address()); | ||||
| }); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user