make Prettier
This commit is contained in:
parent
b6900b937b
commit
9ab91d9721
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"bracketSpacing": true,
|
||||
"printWidth": 80,
|
||||
"singleQuote": true,
|
||||
"tabWidth": 4,
|
||||
"trailingComma": "none",
|
||||
"useTabs": false
|
||||
}
|
198
README.md
198
README.md
|
@ -8,7 +8,7 @@ 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.
|
||||
µRequest is designed to be a drop-in replacement for request. It supports HTTPS and follows redirects by default.
|
||||
|
||||
```bash
|
||||
npm install --save @root/request
|
||||
|
@ -16,10 +16,10 @@ npm install --save @root/request
|
|||
|
||||
```js
|
||||
var request = require('@root/request');
|
||||
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.
|
||||
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.
|
||||
});
|
||||
```
|
||||
|
||||
|
@ -30,25 +30,28 @@ var promisify = require('util').promisify;
|
|||
var request = require('@root/request');
|
||||
request = promisify(request);
|
||||
|
||||
request('http://www.google.com').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) {
|
||||
console.log('error:', error); // Print the error if one occurred
|
||||
});
|
||||
request('http://www.google.com')
|
||||
.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) {
|
||||
console.log('error:', error); // Print the error if one occurred
|
||||
});
|
||||
```
|
||||
|
||||
## Table of contents
|
||||
|
||||
- [Forms](#forms)
|
||||
- [HTTP Authentication](#http-authentication)
|
||||
- [Custom HTTP Headers](#custom-http-headers)
|
||||
- [Unix Domain Sockets](#unix-domain-sockets)
|
||||
- [**All Available Options**](#requestoptions-callback)
|
||||
- [Forms](#forms)
|
||||
- [HTTP Authentication](#http-authentication)
|
||||
- [Custom HTTP Headers](#custom-http-headers)
|
||||
- [Unix Domain Sockets](#unix-domain-sockets)
|
||||
- [**All Available Options**](#requestoptions-callback)
|
||||
|
||||
## Forms
|
||||
|
||||
`urequest` supports `application/x-www-form-urlencoded` and `multipart/form-data` form uploads.
|
||||
|
||||
<!-- For `multipart/related` refer to the `multipart` API. -->
|
||||
|
||||
#### application/x-www-form-urlencoded (URL-Encoded Forms)
|
||||
|
@ -56,16 +59,21 @@ request('http://www.google.com').then(function (response) {
|
|||
URL-encoded forms are simple.
|
||||
|
||||
```js
|
||||
request.post('http://service.com/upload', {form:{key:'value'}})
|
||||
request.post('http://service.com/upload', { form: { key: 'value' } });
|
||||
// or
|
||||
request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ /* ... */ })
|
||||
request.post(
|
||||
{ url: 'http://service.com/upload', form: { key: 'value' } },
|
||||
function(err, httpResponse, body) {
|
||||
/* ... */
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
<!--
|
||||
// or
|
||||
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.
|
||||
|
@ -78,35 +86,39 @@ npm install --save form-data@2
|
|||
|
||||
```js
|
||||
var formData = {
|
||||
// Pass a simple key-value pair
|
||||
my_field: 'my_value',
|
||||
// Pass data via Buffers
|
||||
my_buffer: Buffer.from([1, 2, 3]),
|
||||
// Pass data via Streams
|
||||
my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
|
||||
// Pass multiple values /w an Array
|
||||
attachments: [
|
||||
fs.createReadStream(__dirname + '/attachment1.jpg'),
|
||||
fs.createReadStream(__dirname + '/attachment2.jpg')
|
||||
],
|
||||
// Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
|
||||
// Use case: for some types of streams, you'll need to provide "file"-related information manually.
|
||||
// See the `form-data` README for more information about options: https://github.com/form-data/form-data
|
||||
custom_file: {
|
||||
value: fs.createReadStream('/dev/urandom'),
|
||||
options: {
|
||||
filename: 'topsecret.jpg',
|
||||
contentType: 'image/jpeg'
|
||||
// Pass a simple key-value pair
|
||||
my_field: 'my_value',
|
||||
// Pass data via Buffers
|
||||
my_buffer: Buffer.from([1, 2, 3]),
|
||||
// Pass data via Streams
|
||||
my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
|
||||
// Pass multiple values /w an Array
|
||||
attachments: [
|
||||
fs.createReadStream(__dirname + '/attachment1.jpg'),
|
||||
fs.createReadStream(__dirname + '/attachment2.jpg')
|
||||
],
|
||||
// Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
|
||||
// Use case: for some types of streams, you'll need to provide "file"-related information manually.
|
||||
// See the `form-data` README for more information about options: https://github.com/form-data/form-data
|
||||
custom_file: {
|
||||
value: fs.createReadStream('/dev/urandom'),
|
||||
options: {
|
||||
filename: 'topsecret.jpg',
|
||||
contentType: 'image/jpeg'
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) {
|
||||
if (err) {
|
||||
return console.error('upload failed:', err);
|
||||
}
|
||||
console.log('Upload successful! Server responded with:', body);
|
||||
});
|
||||
request.post(
|
||||
{ url: 'http://service.com/upload', formData: formData },
|
||||
function optionalCallback(err, httpResponse, body) {
|
||||
if (err) {
|
||||
return console.error('upload failed:', err);
|
||||
}
|
||||
console.log('Upload successful! Server responded with:', body);
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
<!--
|
||||
|
||||
For advanced cases, you can access the form-data object itself via `r.form()`. This can be modified until the request is fired on the next cycle of the event-loop. (Note that this calling `form()` will clear the currently set form data for that request.)
|
||||
|
@ -133,27 +145,28 @@ request.get('http://some.server.com/').auth('username', 'password', false);
|
|||
request.get('http://some.server.com/').auth(null, null, true, 'bearerToken');
|
||||
// or
|
||||
-->
|
||||
|
||||
```js
|
||||
request.get('http://some.server.com/', {
|
||||
'auth': {
|
||||
'user': 'username',
|
||||
'pass': 'password',
|
||||
'sendImmediately': false
|
||||
}
|
||||
auth: {
|
||||
user: 'username',
|
||||
pass: 'password',
|
||||
sendImmediately: false
|
||||
}
|
||||
});
|
||||
// or
|
||||
request.get('http://some.server.com/', {
|
||||
'auth': {
|
||||
'bearer': 'bearerToken'
|
||||
}
|
||||
auth: {
|
||||
bearer: 'bearerToken'
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
If passed as an option, `auth` should be a hash containing values:
|
||||
|
||||
- `user` || `username`
|
||||
- `pass` || `password`
|
||||
- `bearer` (optional)
|
||||
- `user` || `username`
|
||||
- `pass` || `password`
|
||||
- `bearer` (optional)
|
||||
|
||||
<!--
|
||||
- `sendImmediately` (optional)
|
||||
|
@ -177,8 +190,8 @@ var username = 'username',
|
|||
password = 'password',
|
||||
url = 'http://' + username + ':' + password + '@some.server.com';
|
||||
|
||||
request({url: url}, function (error, response, body) {
|
||||
// Do more stuff with 'body' here
|
||||
request({ url: url }, function(error, response, body) {
|
||||
// Do more stuff with 'body' here
|
||||
});
|
||||
```
|
||||
|
||||
|
@ -209,18 +222,18 @@ custom `User-Agent` header as well as https.
|
|||
var request = require('request');
|
||||
|
||||
var options = {
|
||||
url: 'https://api.github.com/repos/request/request',
|
||||
headers: {
|
||||
'User-Agent': 'request'
|
||||
}
|
||||
url: 'https://api.github.com/repos/request/request',
|
||||
headers: {
|
||||
'User-Agent': 'request'
|
||||
}
|
||||
};
|
||||
|
||||
function callback(error, response, body) {
|
||||
if (!error && response.statusCode == 200) {
|
||||
var info = JSON.parse(body);
|
||||
console.log(info.stargazers_count + " Stars");
|
||||
console.log(info.forks_count + " Forks");
|
||||
}
|
||||
if (!error && response.statusCode == 200) {
|
||||
var info = JSON.parse(body);
|
||||
console.log(info.stargazers_count + ' Stars');
|
||||
console.log(info.forks_count + ' Forks');
|
||||
}
|
||||
}
|
||||
|
||||
request(options, callback);
|
||||
|
@ -235,8 +248,10 @@ request(options, callback);
|
|||
`urequest` 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'
|
||||
/* Example */ request.get('http://unix:/absolute/path/to/unix.socket:/request/path')
|
||||
/* Pattern */ 'http://unix:SOCKET:PATH';
|
||||
/* Example */ request.get(
|
||||
'http://unix:/absolute/path/to/unix.socket:/request/path'
|
||||
);
|
||||
```
|
||||
|
||||
Note: The `SOCKET` path is assumed to be absolute to the root of the host file system.
|
||||
|
@ -249,9 +264,9 @@ Note: The `SOCKET` path is assumed to be absolute to the root of the host file s
|
|||
|
||||
The first argument can be either a `url` or an `options` object. The only required option is `uri`; all others are optional.
|
||||
|
||||
- `uri` || `url` - fully qualified uri or a parsed url object from `url.parse()`
|
||||
- `method` - http method (default: `"GET"`)
|
||||
- `headers` - http headers (default: `{}`)
|
||||
- `uri` || `url` - fully qualified uri or a parsed url object from `url.parse()`
|
||||
- `method` - http method (default: `"GET"`)
|
||||
- `headers` - http headers (default: `{}`)
|
||||
|
||||
<!-- TODO
|
||||
- `baseUrl` - fully qualified uri string used as the base url. Most useful with `request.defaults`, for example when you want to do many requests to the same domain. If `baseUrl` is `https://example.com/api/`, then requesting `/end/point?test=true` will fetch `https://example.com/api/end/point?test=true`. When `baseUrl` is given, `uri` must also be a string.
|
||||
|
@ -259,8 +274,8 @@ The first argument can be either a `url` or an `options` object. The only requir
|
|||
|
||||
---
|
||||
|
||||
- `body` - entity body for PATCH, POST and PUT requests. Must be a `Buffer`, `String` or `ReadStream`. If `json` is `true`, then `body` must be a JSON-serializable object.
|
||||
- `json` - sets `body` to JSON representation of value and adds `Content-type: application/json` header. Additionally, parses the response body as JSON.
|
||||
- `body` - entity body for PATCH, POST and PUT requests. Must be a `Buffer`, `String` or `ReadStream`. If `json` is `true`, then `body` must be a JSON-serializable object.
|
||||
- `json` - sets `body` to JSON representation of value and adds `Content-type: application/json` header. Additionally, parses the response body as JSON.
|
||||
|
||||
<!-- TODO
|
||||
- `form` - when passed an object or a querystring, this sets `body` to a querystring representation of value, and adds `Content-type: application/x-www-form-urlencoded` header. When passed no options, a `FormData` instance is returned (and is piped to request). See "Forms" section above.
|
||||
|
@ -281,15 +296,15 @@ The first argument can be either a `url` or an `options` object. The only requir
|
|||
|
||||
---
|
||||
|
||||
- `followRedirect` - follow HTTP 3xx responses as redirects (default: `true`). This property can also be implemented as function which gets `response` object as a single argument and should return `true` if redirects should continue or `false` otherwise.
|
||||
- `followAllRedirects` - follow non-GET HTTP 3xx responses as redirects (default: `false`)
|
||||
- `followOriginalHttpMethod` - by default we redirect to HTTP method GET. you can enable this property to redirect to the original HTTP method (default: `false`)
|
||||
- `maxRedirects` - the maximum number of redirects to follow (default: `10`)
|
||||
- `removeRefererHeader` - removes the referer header when a redirect happens (default: `false`). **Note:** if true, referer header set in the initial request is preserved during redirect chain.
|
||||
- `followRedirect` - follow HTTP 3xx responses as redirects (default: `true`). This property can also be implemented as function which gets `response` object as a single argument and should return `true` if redirects should continue or `false` otherwise.
|
||||
- `followAllRedirects` - follow non-GET HTTP 3xx responses as redirects (default: `false`)
|
||||
- `followOriginalHttpMethod` - by default we redirect to HTTP method GET. you can enable this property to redirect to the original HTTP method (default: `false`)
|
||||
- `maxRedirects` - the maximum number of redirects to follow (default: `10`)
|
||||
- `removeRefererHeader` - removes the referer header when a redirect happens (default: `false`). **Note:** if true, referer header set in the initial request is preserved during redirect chain.
|
||||
|
||||
---
|
||||
|
||||
- `encoding` - encoding to be used on `setEncoding` of response data. If `null`, the `body` is returned as a `Buffer`. Anything else **(including the default value of `undefined`)** will be passed as the [encoding](http://nodejs.org/api/buffer.html#buffer_buffer) parameter to `toString()` (meaning this is effectively `utf8` by default). (**Note:** if you expect binary data, you should set `encoding: null`.)
|
||||
- `encoding` - encoding to be used on `setEncoding` of response data. If `null`, the `body` is returned as a `Buffer`. Anything else **(including the default value of `undefined`)** will be passed as the [encoding](http://nodejs.org/api/buffer.html#buffer_buffer) parameter to `toString()` (meaning this is effectively `utf8` by default). (**Note:** if you expect binary data, you should set `encoding: null`.)
|
||||
|
||||
<!-- TODO
|
||||
- `gzip` - if `true`, add an `Accept-Encoding` header to request compressed content encodings from the server (if not already present) and decode supported content encodings in the response. **Note:** Automatic decoding of the response content is performed on the body data returned through `request` (both through the `request` stream and passed to the callback function) but is not performed on the `response` stream (available from the `response` event) which is the unmodified `http.IncomingMessage` object which may contain compressed data. See example below.
|
||||
|
@ -314,30 +329,31 @@ instead, it **returns a wrapper** that has your default settings applied to it.
|
|||
`request.defaults` to add/override defaults that were previously defaulted.
|
||||
|
||||
For example:
|
||||
|
||||
```js
|
||||
//requests using baseRequest() will set the 'x-token' header
|
||||
var baseRequest = request.defaults({
|
||||
headers: {'x-token': 'my-token'}
|
||||
})
|
||||
headers: { 'x-token': 'my-token' }
|
||||
});
|
||||
|
||||
//requests using specialRequest() will include the 'x-token' header set in
|
||||
//baseRequest and will also include the 'special' header
|
||||
var specialRequest = baseRequest.defaults({
|
||||
headers: {special: 'special value'}
|
||||
})
|
||||
headers: { special: 'special value' }
|
||||
});
|
||||
```
|
||||
|
||||
### request.METHOD()
|
||||
|
||||
These HTTP method convenience functions act just like `request()` but with a default method already set for you:
|
||||
|
||||
- *request.get()*: Defaults to `method: "GET"`.
|
||||
- *request.post()*: Defaults to `method: "POST"`.
|
||||
- *request.put()*: Defaults to `method: "PUT"`.
|
||||
- *request.patch()*: Defaults to `method: "PATCH"`.
|
||||
- *request.del() / request.delete()*: Defaults to `method: "DELETE"`.
|
||||
- *request.head()*: Defaults to `method: "HEAD"`.
|
||||
- *request.options()*: Defaults to `method: "OPTIONS"`.
|
||||
- _request.get()_: Defaults to `method: "GET"`.
|
||||
- _request.post()_: Defaults to `method: "POST"`.
|
||||
- _request.put()_: Defaults to `method: "PUT"`.
|
||||
- _request.patch()_: Defaults to `method: "PATCH"`.
|
||||
- _request.del() / request.delete()_: Defaults to `method: "DELETE"`.
|
||||
- _request.head()_: Defaults to `method: "HEAD"`.
|
||||
- _request.options()_: Defaults to `method: "OPTIONS"`.
|
||||
|
||||
---
|
||||
|
||||
|
|
|
@ -5,12 +5,16 @@ 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;
|
||||
}
|
||||
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
|
||||
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
|
||||
});
|
||||
|
|
|
@ -8,21 +8,24 @@ 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(
|
||||
//{ url: 'http://postb.in/syfxxnko'
|
||||
{ url: 'http://localhost:3007/form-data/'
|
||||
, method: 'POST'
|
||||
, headers: { 'X-Foo': 'Bar' }
|
||||
, formData: {
|
||||
foo: 'bar'
|
||||
, baz: require('fs').createReadStream(require('path').join(__dirname, 'get-to-json.js'))
|
||||
//{ url: 'http://postb.in/syfxxnko'
|
||||
{
|
||||
url: 'http://localhost:3007/form-data/',
|
||||
method: 'POST',
|
||||
headers: { 'X-Foo': 'Bar' },
|
||||
formData: {
|
||||
foo: 'bar',
|
||||
baz: require('fs').createReadStream(
|
||||
require('path').join(__dirname, 'get-to-json.js')
|
||||
)
|
||||
}
|
||||
},
|
||||
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('Body Length:', body.length); // body length
|
||||
}
|
||||
}
|
||||
, 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('Body Length:', body.length); // body length
|
||||
}
|
||||
);
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
//var request = require('urequest');
|
||||
var request = require('../');
|
||||
request('https://www.google.com', function (error, response, body) {
|
||||
if (error) {
|
||||
console.log('error:', error); // Print the error if one occurred
|
||||
return;
|
||||
}
|
||||
console.log('response.toJSON()');
|
||||
console.log(response.toJSON());
|
||||
request('https://www.google.com', function(error, response, body) {
|
||||
if (error) {
|
||||
console.log('error:', error); // Print the error if one occurred
|
||||
return;
|
||||
}
|
||||
console.log('response.toJSON()');
|
||||
console.log(response.toJSON());
|
||||
});
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
//var request = require('urequest');
|
||||
var request = require('../');
|
||||
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.
|
||||
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,8 +2,8 @@
|
|||
|
||||
//var request = require('urequest');
|
||||
var request = require('../');
|
||||
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.
|
||||
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,13 +4,17 @@
|
|||
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;
|
||||
}
|
||||
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));
|
||||
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));
|
||||
});
|
||||
|
|
|
@ -8,18 +8,19 @@ 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(
|
||||
{ url: 'http://postb.in/2meyt50C'
|
||||
, method: 'POST'
|
||||
, headers: { 'X-Foo': 'Bar' }
|
||||
, form: { foo: 'bar', baz: 'qux' }
|
||||
}
|
||||
, function (error, response, body) {
|
||||
if (error) {
|
||||
console.log('error:', error); // Print the error if one occurred
|
||||
return;
|
||||
{
|
||||
url: 'http://postb.in/2meyt50C',
|
||||
method: 'POST',
|
||||
headers: { 'X-Foo': 'Bar' },
|
||||
form: { foo: 'bar', baz: 'qux' }
|
||||
},
|
||||
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('Body Length:', body.length); // body length
|
||||
console.log('Response:', response.toJSON()); // body length
|
||||
}
|
||||
console.log('statusCode:', response.statusCode); // The final statusCode
|
||||
console.log('Body Length:', body.length); // body length
|
||||
console.log('Response:', response.toJSON()); // body length
|
||||
}
|
||||
);
|
||||
|
|
826
index.js
826
index.js
|
@ -5,431 +5,485 @@ var https = require('https');
|
|||
var url = require('url');
|
||||
|
||||
function debug() {
|
||||
if (module.exports.debug) {
|
||||
console.log.apply(console, arguments);
|
||||
}
|
||||
if (module.exports.debug) {
|
||||
console.log.apply(console, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
function mergeOrDelete(defaults, updates) {
|
||||
Object.keys(defaults).forEach(function (key) {
|
||||
if (!(key in updates)) {
|
||||
updates[key] = defaults[key];
|
||||
return;
|
||||
}
|
||||
Object.keys(defaults).forEach(function(key) {
|
||||
if (!(key in updates)) {
|
||||
updates[key] = defaults[key];
|
||||
return;
|
||||
}
|
||||
|
||||
// neither accept the prior default nor define an explicit value
|
||||
// CRDT probs...
|
||||
if ('undefined' === typeof updates[key]) {
|
||||
delete updates[key];
|
||||
} else if ('object' === typeof defaults[key] && 'object' === typeof updates[key]) {
|
||||
updates[key] = mergeOrDelete(defaults[key], updates[key]);
|
||||
}
|
||||
});
|
||||
// neither accept the prior default nor define an explicit value
|
||||
// CRDT probs...
|
||||
if ('undefined' === typeof updates[key]) {
|
||||
delete updates[key];
|
||||
} else if (
|
||||
'object' === typeof defaults[key] &&
|
||||
'object' === typeof updates[key]
|
||||
) {
|
||||
updates[key] = mergeOrDelete(defaults[key], updates[key]);
|
||||
}
|
||||
});
|
||||
|
||||
return updates;
|
||||
return updates;
|
||||
}
|
||||
|
||||
// retrieves an existing header, case-sensitive
|
||||
function getHeaderName(reqOpts, header) {
|
||||
var headerNames = {};
|
||||
Object.keys(reqOpts.headers).forEach(function (casedName) {
|
||||
headerNames[casedName.toLowerCase()] = casedName;
|
||||
});
|
||||
// returns the key, which in erroneous cases could be an empty string
|
||||
return headerNames[header.toLowerCase()];
|
||||
var headerNames = {};
|
||||
Object.keys(reqOpts.headers).forEach(function(casedName) {
|
||||
headerNames[casedName.toLowerCase()] = casedName;
|
||||
});
|
||||
// returns the key, which in erroneous cases could be an empty string
|
||||
return headerNames[header.toLowerCase()];
|
||||
}
|
||||
// returns whether or not a header exists, case-insensitive
|
||||
function hasHeader(reqOpts, header) {
|
||||
return 'undefined' !== typeof getHeaderName(reqOpts, header);
|
||||
return 'undefined' !== typeof getHeaderName(reqOpts, header);
|
||||
}
|
||||
|
||||
function toJSONifier(keys) {
|
||||
return function() {
|
||||
var obj = {};
|
||||
var me = this;
|
||||
|
||||
return function () {
|
||||
var obj = {};
|
||||
var me = this;
|
||||
keys.forEach(function(key) {
|
||||
if (me[key] && 'function' === typeof me[key].toJSON) {
|
||||
obj[key] = me[key].toJSON();
|
||||
} else {
|
||||
obj[key] = me[key];
|
||||
}
|
||||
});
|
||||
|
||||
keys.forEach(function (key) {
|
||||
if (me[key] && 'function' === typeof me[key].toJSON) {
|
||||
obj[key] = me[key].toJSON();
|
||||
} else {
|
||||
obj[key] = me[key];
|
||||
}
|
||||
});
|
||||
|
||||
return obj;
|
||||
};
|
||||
return obj;
|
||||
};
|
||||
}
|
||||
|
||||
function setDefaults(defs) {
|
||||
defs = defs || {};
|
||||
defs = defs || {};
|
||||
|
||||
function urequestHelper(opts, cb) {
|
||||
debug("\n[urequest] processed options:");
|
||||
debug(opts);
|
||||
function urequestHelper(opts, cb) {
|
||||
debug('\n[urequest] processed options:');
|
||||
debug(opts);
|
||||
|
||||
function onResponse(resp) {
|
||||
var followRedirect;
|
||||
function onResponse(resp) {
|
||||
var followRedirect;
|
||||
|
||||
Object.keys(defs).forEach(function (key) {
|
||||
if (key in opts && 'undefined' !== typeof opts[key]) {
|
||||
return;
|
||||
}
|
||||
opts[key] = defs[key];
|
||||
});
|
||||
followRedirect = opts.followRedirect;
|
||||
|
||||
resp.toJSON = toJSONifier([ 'statusCode', 'body', 'headers', 'request' ]);
|
||||
|
||||
resp.request = req;
|
||||
resp.request.uri = url.parse(opts.url);
|
||||
//resp.request.method = opts.method;
|
||||
resp.request.headers = finalOpts.headers;
|
||||
resp.request.toJSON = toJSONifier([ 'uri', 'method', 'headers' ]);
|
||||
|
||||
if (followRedirect && resp.headers.location && -1 !== [ 301, 302, 307, 308 ].indexOf(resp.statusCode)) {
|
||||
debug('Following redirect: ' + resp.headers.location);
|
||||
if ('GET' !== opts.method && !opts.followAllRedirects) {
|
||||
followRedirect = false;
|
||||
}
|
||||
if (opts._redirectCount >= opts.maxRedirects) {
|
||||
followRedirect = false;
|
||||
}
|
||||
if ('function' === opts.followRedirect) {
|
||||
if (!opts.followRedirect(resp)) {
|
||||
followRedirect = false;
|
||||
}
|
||||
}
|
||||
if (followRedirect) {
|
||||
if (!opts.followOriginalHttpMethod) {
|
||||
opts.method = 'GET';
|
||||
opts.body = null;
|
||||
delete opts.headers[getHeaderName(opts, 'Content-Length')];
|
||||
delete opts.headers[getHeaderName(opts, 'Transfer-Encoding')];
|
||||
}
|
||||
if (opts.removeRefererHeader && opts.headers) {
|
||||
delete opts.headers.referer;
|
||||
}
|
||||
// TODO needs baseUrl, maybe test for host / socketPath?
|
||||
opts.url = resp.headers.location;
|
||||
opts.uri = url.parse(opts.url);
|
||||
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());
|
||||
cb(null, resp, resp.body);
|
||||
});
|
||||
}
|
||||
|
||||
var req;
|
||||
var finalOpts = {};
|
||||
var _body;
|
||||
var MyFormData;
|
||||
var form;
|
||||
var formHeaders;
|
||||
var requester;
|
||||
|
||||
if (opts.body) {
|
||||
if (true === opts.json) {
|
||||
_body = JSON.stringify(opts.body);
|
||||
} else {
|
||||
_body = opts.body;
|
||||
}
|
||||
} else if (opts.json && true !== opts.json) {
|
||||
_body = JSON.stringify(opts.json);
|
||||
} else if (opts.form) {
|
||||
_body = Object.keys(opts.form).filter(function (key) {
|
||||
if ('undefined' !== typeof opts.form[key]) {
|
||||
return true;
|
||||
}
|
||||
}).map(function (key) {
|
||||
return encodeURIComponent(key) + '=' + encodeURIComponent(String(opts.form[key]));
|
||||
}).join('&');
|
||||
opts.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
}
|
||||
if ('string' === typeof _body) {
|
||||
_body = Buffer.from(_body);
|
||||
}
|
||||
|
||||
Object.keys(opts.uri).forEach(function (key) {
|
||||
finalOpts[key] = opts.uri[key];
|
||||
});
|
||||
|
||||
// A bug should be raised if request does it differently,
|
||||
// but I think we're supposed to pass all acceptable options
|
||||
// on to the raw http request
|
||||
[ 'family', 'host', 'localAddress', 'agent', 'createConnection'
|
||||
, 'timeout', 'setHost'
|
||||
].forEach(function (key) {
|
||||
finalOpts[key] = opts.uri[key];
|
||||
});
|
||||
|
||||
finalOpts.method = opts.method;
|
||||
finalOpts.headers = JSON.parse(JSON.stringify(opts.headers));
|
||||
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
|
||||
finalOpts.headers['Content-Length'] = _body.byteLength || _body.length;
|
||||
}
|
||||
if (opts.auth) {
|
||||
// if opts.uri specifies auth it will be parsed by url.parse and passed directly to the http module
|
||||
if ('string' !== typeof opts.auth) {
|
||||
opts.auth = (opts.auth.user||opts.auth.username||'') + ':' + (opts.auth.pass||opts.auth.password||'');
|
||||
}
|
||||
if ('string' === typeof opts.auth) {
|
||||
finalOpts.auth = opts.auth;
|
||||
}
|
||||
if (false === opts.sendImmediately) {
|
||||
console.warn("[Warn] setting `sendImmediately: false` is not yet supported. Please open an issue if this is an important feature that you need.");
|
||||
}
|
||||
if (opts.bearer) {
|
||||
// having a shortcut for base64 encoding makes sense, but this? Eh, whatevs...
|
||||
finalOpts.header.Authorization = 'Bearer: ' + opts.bearer;
|
||||
}
|
||||
}
|
||||
if (opts.formData) {
|
||||
try {
|
||||
MyFormData = opts.FormData || require('form-data');
|
||||
// potential options https://github.com/felixge/node-combined-stream/blob/master/lib/combined_stream.js#L7-L15
|
||||
} catch(e) {
|
||||
console.error("urequest does not include extra dependencies by default");
|
||||
console.error("if you need to use 'form-data' you may install it, like so:");
|
||||
console.error(" npm install --save form-data");
|
||||
cb(e);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
form = new MyFormData();
|
||||
Object.keys(opts.formData).forEach(function (key) {
|
||||
function add(key, data, opts) {
|
||||
if (data.value) { opts = data.options; data = data.value; }
|
||||
form.append(key, data, opts);
|
||||
}
|
||||
if (Array.isArray(opts.formData[key])) {
|
||||
opts.formData[key].forEach(function (data) {
|
||||
add(key, data);
|
||||
Object.keys(defs).forEach(function(key) {
|
||||
if (key in opts && 'undefined' !== typeof opts[key]) {
|
||||
return;
|
||||
}
|
||||
opts[key] = defs[key];
|
||||
});
|
||||
} else {
|
||||
add(key, opts.formData[key]);
|
||||
}
|
||||
followRedirect = opts.followRedirect;
|
||||
|
||||
resp.toJSON = toJSONifier([
|
||||
'statusCode',
|
||||
'body',
|
||||
'headers',
|
||||
'request'
|
||||
]);
|
||||
|
||||
resp.request = req;
|
||||
resp.request.uri = url.parse(opts.url);
|
||||
//resp.request.method = opts.method;
|
||||
resp.request.headers = finalOpts.headers;
|
||||
resp.request.toJSON = toJSONifier(['uri', 'method', 'headers']);
|
||||
|
||||
if (
|
||||
followRedirect &&
|
||||
resp.headers.location &&
|
||||
-1 !== [301, 302, 307, 308].indexOf(resp.statusCode)
|
||||
) {
|
||||
debug('Following redirect: ' + resp.headers.location);
|
||||
if ('GET' !== opts.method && !opts.followAllRedirects) {
|
||||
followRedirect = false;
|
||||
}
|
||||
if (opts._redirectCount >= opts.maxRedirects) {
|
||||
followRedirect = false;
|
||||
}
|
||||
if ('function' === opts.followRedirect) {
|
||||
if (!opts.followRedirect(resp)) {
|
||||
followRedirect = false;
|
||||
}
|
||||
}
|
||||
if (followRedirect) {
|
||||
if (!opts.followOriginalHttpMethod) {
|
||||
opts.method = 'GET';
|
||||
opts.body = null;
|
||||
delete opts.headers[
|
||||
getHeaderName(opts, 'Content-Length')
|
||||
];
|
||||
delete opts.headers[
|
||||
getHeaderName(opts, 'Transfer-Encoding')
|
||||
];
|
||||
}
|
||||
if (opts.removeRefererHeader && opts.headers) {
|
||||
delete opts.headers.referer;
|
||||
}
|
||||
// TODO needs baseUrl, maybe test for host / socketPath?
|
||||
opts.url = resp.headers.location;
|
||||
opts.uri = url.parse(opts.url);
|
||||
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());
|
||||
cb(null, resp, resp.body);
|
||||
});
|
||||
}
|
||||
|
||||
var req;
|
||||
var finalOpts = {};
|
||||
var _body;
|
||||
var MyFormData;
|
||||
var form;
|
||||
var formHeaders;
|
||||
var requester;
|
||||
|
||||
if (opts.body) {
|
||||
if (true === opts.json) {
|
||||
_body = JSON.stringify(opts.body);
|
||||
} else {
|
||||
_body = opts.body;
|
||||
}
|
||||
} else if (opts.json && true !== opts.json) {
|
||||
_body = JSON.stringify(opts.json);
|
||||
} else if (opts.form) {
|
||||
_body = Object.keys(opts.form)
|
||||
.filter(function(key) {
|
||||
if ('undefined' !== typeof opts.form[key]) {
|
||||
return true;
|
||||
}
|
||||
})
|
||||
.map(function(key) {
|
||||
return (
|
||||
encodeURIComponent(key) +
|
||||
'=' +
|
||||
encodeURIComponent(String(opts.form[key]))
|
||||
);
|
||||
})
|
||||
.join('&');
|
||||
opts.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
}
|
||||
if ('string' === typeof _body) {
|
||||
_body = Buffer.from(_body);
|
||||
}
|
||||
|
||||
Object.keys(opts.uri).forEach(function(key) {
|
||||
finalOpts[key] = opts.uri[key];
|
||||
});
|
||||
} catch(e) {
|
||||
cb(e);
|
||||
return;
|
||||
}
|
||||
formHeaders = form.getHeaders();
|
||||
Object.keys(formHeaders).forEach(function (header) {
|
||||
finalOpts.headers[header] = formHeaders[header];
|
||||
});
|
||||
|
||||
// A bug should be raised if request does it differently,
|
||||
// but I think we're supposed to pass all acceptable options
|
||||
// on to the raw http request
|
||||
[
|
||||
'family',
|
||||
'host',
|
||||
'localAddress',
|
||||
'agent',
|
||||
'createConnection',
|
||||
'timeout',
|
||||
'setHost'
|
||||
].forEach(function(key) {
|
||||
finalOpts[key] = opts.uri[key];
|
||||
});
|
||||
|
||||
finalOpts.method = opts.method;
|
||||
finalOpts.headers = JSON.parse(JSON.stringify(opts.headers));
|
||||
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
|
||||
finalOpts.headers['Content-Length'] =
|
||||
_body.byteLength || _body.length;
|
||||
}
|
||||
if (opts.auth) {
|
||||
// if opts.uri specifies auth it will be parsed by url.parse and passed directly to the http module
|
||||
if ('string' !== typeof opts.auth) {
|
||||
opts.auth =
|
||||
(opts.auth.user || opts.auth.username || '') +
|
||||
':' +
|
||||
(opts.auth.pass || opts.auth.password || '');
|
||||
}
|
||||
if ('string' === typeof opts.auth) {
|
||||
finalOpts.auth = opts.auth;
|
||||
}
|
||||
if (false === opts.sendImmediately) {
|
||||
console.warn(
|
||||
'[Warn] setting `sendImmediately: false` is not yet supported. Please open an issue if this is an important feature that you need.'
|
||||
);
|
||||
}
|
||||
if (opts.bearer) {
|
||||
// having a shortcut for base64 encoding makes sense, but this? Eh, whatevs...
|
||||
finalOpts.header.Authorization = 'Bearer: ' + opts.bearer;
|
||||
}
|
||||
}
|
||||
if (opts.formData) {
|
||||
try {
|
||||
MyFormData = opts.FormData || require('form-data');
|
||||
// potential options https://github.com/felixge/node-combined-stream/blob/master/lib/combined_stream.js#L7-L15
|
||||
} catch (e) {
|
||||
console.error(
|
||||
'urequest does not include extra dependencies by default'
|
||||
);
|
||||
console.error(
|
||||
"if you need to use 'form-data' you may install it, like so:"
|
||||
);
|
||||
console.error(' npm install --save form-data');
|
||||
cb(e);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
form = new MyFormData();
|
||||
Object.keys(opts.formData).forEach(function(key) {
|
||||
function add(key, data, opts) {
|
||||
if (data.value) {
|
||||
opts = data.options;
|
||||
data = data.value;
|
||||
}
|
||||
form.append(key, data, opts);
|
||||
}
|
||||
if (Array.isArray(opts.formData[key])) {
|
||||
opts.formData[key].forEach(function(data) {
|
||||
add(key, data);
|
||||
});
|
||||
} else {
|
||||
add(key, opts.formData[key]);
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
cb(e);
|
||||
return;
|
||||
}
|
||||
formHeaders = form.getHeaders();
|
||||
Object.keys(formHeaders).forEach(function(header) {
|
||||
finalOpts.headers[header] = formHeaders[header];
|
||||
});
|
||||
}
|
||||
|
||||
// TODO support unix sockets
|
||||
if ('https:' === finalOpts.protocol) {
|
||||
// https://nodejs.org/api/https.html#https_https_request_options_callback
|
||||
debug('\n[urequest] https.request(opts):');
|
||||
debug(finalOpts);
|
||||
requester = https;
|
||||
} else if ('http:' === finalOpts.protocol) {
|
||||
// https://nodejs.org/api/http.html#http_http_request_options_callback
|
||||
debug('\n[urequest] http.request(opts):');
|
||||
debug(finalOpts);
|
||||
requester = http;
|
||||
} else {
|
||||
cb(new Error("unknown protocol: '" + opts.uri.protocol + "'"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (form) {
|
||||
debug("\n[urequest] '" + finalOpts.method + "' (request) form");
|
||||
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) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
return;
|
||||
}
|
||||
onResponse(resp);
|
||||
resp.resume();
|
||||
});
|
||||
//req = requester.request(finalOpts, onResponse);
|
||||
//req.on('error', cb);
|
||||
//form.pipe(req);
|
||||
return;
|
||||
}
|
||||
|
||||
req = requester.request(finalOpts, onResponse);
|
||||
req.on('error', cb);
|
||||
|
||||
if (_body) {
|
||||
debug("\n[urequest] '" + finalOpts.method + "' (request) body");
|
||||
debug(_body);
|
||||
// used for chunked encoding
|
||||
//req.write(_body);
|
||||
// used for known content-length
|
||||
req.end(_body);
|
||||
} else {
|
||||
req.end();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO support unix sockets
|
||||
if ('https:' === finalOpts.protocol) {
|
||||
// https://nodejs.org/api/https.html#https_https_request_options_callback
|
||||
debug("\n[urequest] https.request(opts):");
|
||||
debug(finalOpts);
|
||||
requester = https;
|
||||
} else if ('http:' === finalOpts.protocol) {
|
||||
// https://nodejs.org/api/http.html#http_http_request_options_callback
|
||||
debug("\n[urequest] http.request(opts):");
|
||||
debug(finalOpts);
|
||||
requester = http;
|
||||
} else {
|
||||
cb(new Error("unknown protocol: '" + opts.uri.protocol + "'"));
|
||||
return;
|
||||
function parseUrl(str) {
|
||||
var obj = url.parse(str);
|
||||
var paths;
|
||||
if ('unix' !== (obj.hostname || obj.host || '').toLowerCase()) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
obj.href = null;
|
||||
obj.hostname = obj.host = null;
|
||||
|
||||
paths = (obj.pathname || obj.path || '').split(':');
|
||||
|
||||
obj.socketPath = paths.shift();
|
||||
obj.pathname = obj.path = paths.join(':');
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
if (form) {
|
||||
debug("\n[urequest] '" + finalOpts.method + "' (request) form");
|
||||
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) {
|
||||
if (err) { cb(err); return; }
|
||||
onResponse(resp);
|
||||
resp.resume();
|
||||
});
|
||||
//req = requester.request(finalOpts, onResponse);
|
||||
//req.on('error', cb);
|
||||
//form.pipe(req);
|
||||
return;
|
||||
function urequest(opts, cb) {
|
||||
debug('\n[urequest] received options:');
|
||||
debug(opts);
|
||||
var reqOpts = {};
|
||||
// request.js behavior:
|
||||
// encoding: null + json ? unknown
|
||||
// json => attempt to parse, fail silently
|
||||
// encoding => buffer.toString(encoding)
|
||||
// null === encoding => Buffer.concat(buffers)
|
||||
if ('string' === typeof opts) {
|
||||
opts = { url: opts };
|
||||
}
|
||||
|
||||
module.exports._keys.forEach(function(key) {
|
||||
if (key in opts && 'undefined' !== typeof opts[key]) {
|
||||
reqOpts[key] = opts[key];
|
||||
} else if (key in defs) {
|
||||
reqOpts[key] = defs[key];
|
||||
}
|
||||
});
|
||||
|
||||
// TODO url.resolve(defs.baseUrl, opts.url);
|
||||
if ('string' === typeof opts.url || 'string' === typeof opts.uri) {
|
||||
if ('string' === typeof opts.url) {
|
||||
reqOpts.url = opts.url;
|
||||
reqOpts.uri = parseUrl(opts.url);
|
||||
} else if ('string' === typeof opts.uri) {
|
||||
reqOpts.url = opts.uri;
|
||||
reqOpts.uri = parseUrl(opts.uri);
|
||||
}
|
||||
} else {
|
||||
if ('object' === typeof opts.uri) {
|
||||
reqOpts.url = url.format(opts.uri);
|
||||
reqOpts.uri = opts.uri;
|
||||
//reqOpts.uri = url.parse(reqOpts.uri);
|
||||
} else if ('object' === typeof opts.url) {
|
||||
reqOpts.url = url.format(opts.url);
|
||||
reqOpts.uri = opts.url;
|
||||
//reqOpts.uri = url.parse(reqOpts.url);
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
opts.body ||
|
||||
'string' === typeof opts.json ||
|
||||
opts.form ||
|
||||
opts.formData
|
||||
) {
|
||||
// TODO this is probably a deviation from request's API
|
||||
// need to check and probably eliminate it
|
||||
reqOpts.method = (reqOpts.method || 'POST').toUpperCase();
|
||||
} else {
|
||||
reqOpts.method = (reqOpts.method || 'GET').toUpperCase();
|
||||
}
|
||||
if (!reqOpts.headers) {
|
||||
reqOpts.headers = {};
|
||||
}
|
||||
|
||||
// crazy case for easier testing
|
||||
if (!hasHeader(reqOpts, 'CoNTeNT-TyPe')) {
|
||||
if (
|
||||
(true === reqOpts.json && reqOpts.body) ||
|
||||
(true !== reqOpts.json && reqOpts.json)
|
||||
) {
|
||||
reqOpts.headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
}
|
||||
|
||||
return urequestHelper(reqOpts, cb);
|
||||
}
|
||||
|
||||
req = requester.request(finalOpts, onResponse);
|
||||
req.on('error', cb);
|
||||
|
||||
if (_body) {
|
||||
debug("\n[urequest] '" + finalOpts.method + "' (request) body");
|
||||
debug(_body);
|
||||
// used for chunked encoding
|
||||
//req.write(_body);
|
||||
// used for known content-length
|
||||
req.end(_body);
|
||||
} else {
|
||||
req.end();
|
||||
}
|
||||
}
|
||||
|
||||
function parseUrl(str) {
|
||||
var obj = url.parse(str);
|
||||
var paths;
|
||||
if ('unix' !== (obj.hostname||obj.host||'').toLowerCase()) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
obj.href = null;
|
||||
obj.hostname = obj.host = null;
|
||||
|
||||
paths = (obj.pathname||obj.path||'').split(':');
|
||||
|
||||
obj.socketPath = paths.shift();
|
||||
obj.pathname = obj.path = paths.join(':');
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
function urequest(opts, cb) {
|
||||
debug("\n[urequest] received options:");
|
||||
debug(opts);
|
||||
var reqOpts = {};
|
||||
// request.js behavior:
|
||||
// encoding: null + json ? unknown
|
||||
// json => attempt to parse, fail silently
|
||||
// encoding => buffer.toString(encoding)
|
||||
// null === encoding => Buffer.concat(buffers)
|
||||
if ('string' === typeof opts) {
|
||||
opts = { url: opts };
|
||||
}
|
||||
|
||||
module.exports._keys.forEach(function (key) {
|
||||
if (key in opts && 'undefined' !== typeof opts[key]) {
|
||||
reqOpts[key] = opts[key];
|
||||
} else if (key in defs) {
|
||||
reqOpts[key] = defs[key];
|
||||
}
|
||||
});
|
||||
|
||||
// TODO url.resolve(defs.baseUrl, opts.url);
|
||||
if ('string' === typeof opts.url || 'string' === typeof opts.uri) {
|
||||
if ('string' === typeof opts.url) {
|
||||
reqOpts.url = opts.url;
|
||||
reqOpts.uri = parseUrl(opts.url);
|
||||
} else if ('string' === typeof opts.uri) {
|
||||
reqOpts.url = opts.uri;
|
||||
reqOpts.uri = parseUrl(opts.uri);
|
||||
}
|
||||
} else {
|
||||
if ('object' === typeof opts.uri) {
|
||||
reqOpts.url = url.format(opts.uri);
|
||||
reqOpts.uri = opts.uri;
|
||||
//reqOpts.uri = url.parse(reqOpts.uri);
|
||||
} else if ('object' === typeof opts.url) {
|
||||
reqOpts.url = url.format(opts.url);
|
||||
reqOpts.uri = opts.url;
|
||||
//reqOpts.uri = url.parse(reqOpts.url);
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.body || 'string' === typeof opts.json || opts.form || opts.formData) {
|
||||
// TODO this is probably a deviation from request's API
|
||||
// need to check and probably eliminate it
|
||||
reqOpts.method = (reqOpts.method || 'POST').toUpperCase();
|
||||
} else {
|
||||
reqOpts.method = (reqOpts.method || 'GET').toUpperCase();
|
||||
}
|
||||
if (!reqOpts.headers) {
|
||||
reqOpts.headers = {};
|
||||
}
|
||||
|
||||
// crazy case for easier testing
|
||||
if (!hasHeader(reqOpts, 'CoNTeNT-TyPe')) {
|
||||
if ((true === reqOpts.json && reqOpts.body)
|
||||
|| (true !== reqOpts.json && reqOpts.json)) {
|
||||
reqOpts.headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
}
|
||||
|
||||
return urequestHelper(reqOpts, cb);
|
||||
}
|
||||
|
||||
urequest.defaults = function (_defs) {
|
||||
_defs = mergeOrDelete(defs, _defs);
|
||||
return setDefaults(_defs);
|
||||
};
|
||||
[ 'get', 'put', 'post', 'patch', 'delete', 'head', 'options' ].forEach(function (method) {
|
||||
urequest[method] = function (obj, cb) {
|
||||
if ('string' === typeof obj) {
|
||||
obj = { url: obj };
|
||||
}
|
||||
obj.method = method.toUpperCase();
|
||||
urequest(obj, cb);
|
||||
urequest.defaults = function(_defs) {
|
||||
_defs = mergeOrDelete(defs, _defs);
|
||||
return setDefaults(_defs);
|
||||
};
|
||||
});
|
||||
urequest.del = urequest.delete;
|
||||
['get', 'put', 'post', 'patch', 'delete', 'head', 'options'].forEach(
|
||||
function(method) {
|
||||
urequest[method] = function(obj, cb) {
|
||||
if ('string' === typeof obj) {
|
||||
obj = { url: obj };
|
||||
}
|
||||
obj.method = method.toUpperCase();
|
||||
urequest(obj, cb);
|
||||
};
|
||||
}
|
||||
);
|
||||
urequest.del = urequest.delete;
|
||||
|
||||
return urequest;
|
||||
return urequest;
|
||||
}
|
||||
|
||||
var _defaults = {
|
||||
sendImmediately: true
|
||||
, method: 'GET'
|
||||
, headers: {}
|
||||
, useQuerystring: false
|
||||
, followRedirect: true
|
||||
, followAllRedirects: false
|
||||
, followOriginalHttpMethod: false
|
||||
, maxRedirects: 10
|
||||
, removeRefererHeader: false
|
||||
//, encoding: undefined
|
||||
, gzip: false
|
||||
//, body: undefined
|
||||
//, json: undefined
|
||||
sendImmediately: true,
|
||||
method: 'GET',
|
||||
headers: {},
|
||||
useQuerystring: false,
|
||||
followRedirect: true,
|
||||
followAllRedirects: false,
|
||||
followOriginalHttpMethod: false,
|
||||
maxRedirects: 10,
|
||||
removeRefererHeader: false,
|
||||
//, encoding: undefined
|
||||
gzip: false
|
||||
//, body: undefined
|
||||
//, json: undefined
|
||||
};
|
||||
module.exports = setDefaults(_defaults);
|
||||
|
||||
module.exports._keys = Object.keys(_defaults).concat([
|
||||
'encoding'
|
||||
, 'body'
|
||||
, 'json'
|
||||
, 'form'
|
||||
, 'auth'
|
||||
, 'formData'
|
||||
, 'FormData'
|
||||
'encoding',
|
||||
'body',
|
||||
'json',
|
||||
'form',
|
||||
'auth',
|
||||
'formData',
|
||||
'FormData'
|
||||
]);
|
||||
module.exports.debug = (-1 !== (process.env.NODE_DEBUG||'').split(/\s+/g).indexOf('urequest'));
|
||||
module.exports.debug =
|
||||
-1 !== (process.env.NODE_DEBUG || '').split(/\s+/g).indexOf('urequest');
|
||||
|
||||
debug("DEBUG ON for urequest");
|
||||
debug('DEBUG ON for urequest');
|
||||
|
|
54
package.json
54
package.json
|
@ -1,29 +1,29 @@
|
|||
{
|
||||
"name": "@root/request",
|
||||
"version": "1.3.11",
|
||||
"description": "A lightweight, zero-dependency drop-in replacement for request",
|
||||
"main": "index.js",
|
||||
"files": [
|
||||
"lib"
|
||||
],
|
||||
"directories": {
|
||||
"example": "examples"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://git.rootprojects.org/root/request.js.git"
|
||||
},
|
||||
"keywords": [
|
||||
"request",
|
||||
"lightweight",
|
||||
"alternative",
|
||||
"http",
|
||||
"https",
|
||||
"call"
|
||||
],
|
||||
"author": "AJ ONeal <solderjs@gmail.com> (https://solderjs.com/)",
|
||||
"license": "(MIT OR Apache-2.0)"
|
||||
"name": "@root/request",
|
||||
"version": "1.3.11",
|
||||
"description": "A lightweight, zero-dependency drop-in replacement for request",
|
||||
"main": "index.js",
|
||||
"files": [
|
||||
"lib"
|
||||
],
|
||||
"directories": {
|
||||
"example": "examples"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://git.rootprojects.org/root/request.js.git"
|
||||
},
|
||||
"keywords": [
|
||||
"request",
|
||||
"lightweight",
|
||||
"alternative",
|
||||
"http",
|
||||
"https",
|
||||
"call"
|
||||
],
|
||||
"author": "AJ ONeal <solderjs@gmail.com> (https://solderjs.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) {
|
||||
console.info(chunk.toString('utf8'));
|
||||
});
|
||||
})
|
||||
server.listen(3007, function () {
|
||||
console.info("Listening on", this.address());
|
||||
var server = net.createServer(function(socket) {
|
||||
socket.on('data', function(chunk) {
|
||||
console.info(chunk.toString('utf8'));
|
||||
});
|
||||
});
|
||||
server.listen(3007, function() {
|
||||
console.info('Listening on', this.address());
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue