mirror of
https://git.coolaj86.com/coolaj86/request.js
synced 2025-04-21 23:00:36 +00:00
Compare commits
No commits in common. "master" and "v1.6.0" have entirely different histories.
22
.jshintrc
22
.jshintrc
@ -1,22 +0,0 @@
|
|||||||
{
|
|
||||||
"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
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
# 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
65
EXTRA.md
@ -1,65 +0,0 @@
|
|||||||
# 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 }
|
|
||||||
});
|
|
||||||
```
|
|
104
README.md
104
README.md
@ -1,4 +1,4 @@
|
|||||||
# [@root/request](https://git.rootprojects.org/root/request.js) | a [Root](https://rootprojects.org) project
|
# [µRequest](https://git.rootprojects.org/root/request.js) | a [Root](https://rootprojects.org) project
|
||||||
|
|
||||||
> Minimalist HTTP client
|
> Minimalist HTTP client
|
||||||
|
|
||||||
@ -10,12 +10,10 @@ Written from scratch, with zero-dependencies.
|
|||||||
|
|
||||||
## Super simple to use
|
## Super simple to use
|
||||||
|
|
||||||
@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.
|
µRequest is designed to be a drop-in replacement for request. It supports HTTPS and follows redirects by default.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install --save @root/request
|
npm install --save @root/request
|
||||||
|
|
||||||
# or npm install git+ssh://git@git.therootcompany.com/request.js
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -42,105 +40,17 @@ request('http://www.google.com')
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
**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
|
## Table of contents
|
||||||
|
|
||||||
- [Extra Features](/EXTRA.md)
|
|
||||||
- [Forms](#forms)
|
- [Forms](#forms)
|
||||||
- [HTTP Authentication](#http-authentication)
|
- [HTTP Authentication](#http-authentication)
|
||||||
- [Custom HTTP Headers](#custom-http-headers)
|
- [Custom HTTP Headers](#custom-http-headers)
|
||||||
- [Unix Domain Sockets](#unix-domain-sockets)
|
- [Unix Domain Sockets](#unix-domain-sockets)
|
||||||
- [**All Available Options**](#requestoptions-callback)
|
- [**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
|
## Forms
|
||||||
|
|
||||||
`@root/request` supports `application/x-www-form-urlencoded` and `multipart/form-data` form uploads.
|
`urequest` supports `application/x-www-form-urlencoded` and `multipart/form-data` form uploads.
|
||||||
|
|
||||||
<!-- For `multipart/related` refer to the `multipart` API. -->
|
<!-- For `multipart/related` refer to the `multipart` API. -->
|
||||||
|
|
||||||
@ -166,12 +76,12 @@ request.post('http://service.com/upload').form({key:'value'})
|
|||||||
|
|
||||||
#### multipart/form-data (Multipart Form Uploads)
|
#### multipart/form-data (Multipart Form Uploads)
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
To use `form-data`, you must install it separately:
|
To use `form-data`, you must install it separately:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install --save form-data@2.x
|
npm install --save form-data@2
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -335,7 +245,7 @@ request(options, callback);
|
|||||||
|
|
||||||
## UNIX Domain Sockets
|
## UNIX Domain Sockets
|
||||||
|
|
||||||
`@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:
|
`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
|
```js
|
||||||
/* Pattern */ 'http://unix:SOCKET:PATH';
|
/* Pattern */ 'http://unix:SOCKET:PATH';
|
||||||
@ -451,7 +361,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`:
|
There are at least <!--three--> two ways to debug the operation of `request`:
|
||||||
|
|
||||||
1. Launch the node process like `NODE_DEBUG=@root/request node script.js`
|
1. Launch the node process like `NODE_DEBUG=urequest node script.js`
|
||||||
(`lib,request,otherlib` works too).
|
(`lib,request,otherlib` works too).
|
||||||
|
|
||||||
2. Set `require('@root/request').debug = true` at any time (this does the same thing
|
2. Set `require('@root/request').debug = true` at any time (this does the same thing
|
||||||
|
@ -5,9 +5,11 @@ var request = require('../');
|
|||||||
|
|
||||||
// will redirect to https://www.github.com and then https://github.com
|
// will redirect to https://www.github.com and then https://github.com
|
||||||
//request('http://www.github.com', function (error, response, body) {
|
//request('http://www.github.com', function (error, response, body) {
|
||||||
request(
|
request({ uri: { protocol: 'http:', hostname: 'www.github.com' } }, function(
|
||||||
{ uri: { protocol: 'http:', hostname: 'www.github.com' } },
|
error,
|
||||||
function (error, response, body) {
|
response,
|
||||||
|
body
|
||||||
|
) {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log('error:', error); // Print the error if one occurred
|
console.log('error:', error); // Print the error if one occurred
|
||||||
return;
|
return;
|
||||||
@ -15,5 +17,4 @@ request(
|
|||||||
console.log('statusCode:', response.statusCode); // The final statusCode
|
console.log('statusCode:', response.statusCode); // The final statusCode
|
||||||
console.log('Final href:', response.request.uri.href); // The final URI
|
console.log('Final href:', response.request.uri.href); // The final URI
|
||||||
console.log('Body Length:', body.length); // body length
|
console.log('Body Length:', body.length); // body length
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
@ -4,9 +4,11 @@
|
|||||||
var request = require('../');
|
var request = require('../');
|
||||||
|
|
||||||
// would normally redirect to https://www.github.com and then https://github.com
|
// would normally redirect to https://www.github.com and then https://github.com
|
||||||
request(
|
request({ uri: 'https://www.github.com', followRedirect: false }, function(
|
||||||
{ uri: 'https://www.github.com', followRedirect: false },
|
error,
|
||||||
function (error, response, body) {
|
response,
|
||||||
|
body
|
||||||
|
) {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log('error:', error); // Print the error if one occurred
|
console.log('error:', error); // Print the error if one occurred
|
||||||
return;
|
return;
|
||||||
@ -15,5 +17,4 @@ request(
|
|||||||
console.log('statusCode:', response.statusCode); // Should be 301 or 302
|
console.log('statusCode:', response.statusCode); // Should be 301 or 302
|
||||||
console.log('Location:', response.headers.location); // The redirect
|
console.log('Location:', response.headers.location); // The redirect
|
||||||
console.log('Body:', body || JSON.stringify(body));
|
console.log('Body:', body || JSON.stringify(body));
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
@ -3,9 +3,9 @@
|
|||||||
var request = require('../');
|
var request = require('../');
|
||||||
request({
|
request({
|
||||||
url: 'https://postb.in/1588134650162-6019286897499?hello=world'
|
url: 'https://postb.in/1588134650162-6019286897499?hello=world'
|
||||||
//headers: { 'user-agent': false } // remove
|
//headers: { 'user-agent': false }
|
||||||
//headers: { 'user-agent': 'test/1.0' } // overwrite
|
//headers: { 'user-agent': 'test/1.0' }
|
||||||
//userAgent: 'test/1.1' // add to the default
|
//userAgent: 'test/1.1' // not presently implemented
|
||||||
})
|
})
|
||||||
.then(function(resp) {
|
.then(function(resp) {
|
||||||
console.log(resp.body);
|
console.log(resp.body);
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
var request = require('../');
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
var tpath = '/tmp/google-index.html';
|
|
||||||
var resp = await request({
|
|
||||||
url: 'https://google.com',
|
|
||||||
encoding: null,
|
|
||||||
stream: tpath
|
|
||||||
});
|
|
||||||
console.log('[Response Headers]');
|
|
||||||
console.log(resp.toJSON().headers);
|
|
||||||
|
|
||||||
//console.error(resp.headers, resp.body.byteLength);
|
|
||||||
await resp.stream;
|
|
||||||
console.log('[Response Body] written to', tpath);
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
|
||||||
.then(function () {
|
|
||||||
console.log('Pass');
|
|
||||||
})
|
|
||||||
.catch(function (e) {
|
|
||||||
console.error('Fail');
|
|
||||||
console.error(e.stack);
|
|
||||||
});
|
|
@ -1,34 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
var request = require('../');
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
var tpath = '/tmp/google-index.html';
|
|
||||||
var resp = await request({
|
|
||||||
url: 'https://google.com',
|
|
||||||
encoding: null,
|
|
||||||
stream: true
|
|
||||||
});
|
|
||||||
console.log('[Response Headers]');
|
|
||||||
console.log(resp.toJSON().headers);
|
|
||||||
|
|
||||||
resp.on('data', function (chunk) {
|
|
||||||
console.log('[Data]', chunk.byteLength);
|
|
||||||
});
|
|
||||||
resp.on('end', function (chunk) {
|
|
||||||
console.log('[End]');
|
|
||||||
});
|
|
||||||
|
|
||||||
//console.error(resp.headers, resp.body.byteLength);
|
|
||||||
await resp.stream;
|
|
||||||
console.log('[Close]');
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
|
||||||
.then(function () {
|
|
||||||
console.log('Pass');
|
|
||||||
})
|
|
||||||
.catch(function (e) {
|
|
||||||
console.error('Fail');
|
|
||||||
console.error(e.stack);
|
|
||||||
});
|
|
186
index.js
186
index.js
@ -5,7 +5,6 @@ var https = require('https');
|
|||||||
var url = require('url');
|
var url = require('url');
|
||||||
var os = require('os');
|
var os = require('os');
|
||||||
var pkg = require('./package.json');
|
var pkg = require('./package.json');
|
||||||
var fs = require('fs'); // only for streams
|
|
||||||
|
|
||||||
function debug() {
|
function debug() {
|
||||||
if (module.exports.debug) {
|
if (module.exports.debug) {
|
||||||
@ -66,102 +65,6 @@ 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) {
|
function setDefaults(defs) {
|
||||||
defs = defs || {};
|
defs = defs || {};
|
||||||
|
|
||||||
@ -172,19 +75,6 @@ function setDefaults(defs) {
|
|||||||
var req;
|
var req;
|
||||||
var finalOpts = {};
|
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) {
|
function onResponse(resp) {
|
||||||
var followRedirect;
|
var followRedirect;
|
||||||
|
|
||||||
@ -196,16 +86,9 @@ function setDefaults(defs) {
|
|||||||
});
|
});
|
||||||
followRedirect = opts.followRedirect;
|
followRedirect = opts.followRedirect;
|
||||||
|
|
||||||
// copied from WHATWG fetch
|
|
||||||
resp.ok = false;
|
|
||||||
if (resp.statusCode >= 200 && resp.statusCode < 300) {
|
|
||||||
resp.ok = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
resp.toJSON = toJSONifier([
|
resp.toJSON = toJSONifier([
|
||||||
'statusCode',
|
'statusCode',
|
||||||
'body',
|
'body',
|
||||||
'ok',
|
|
||||||
'headers',
|
'headers',
|
||||||
'request'
|
'request'
|
||||||
]);
|
]);
|
||||||
@ -257,20 +140,47 @@ function setDefaults(defs) {
|
|||||||
return urequestHelper(opts, cb);
|
return urequestHelper(opts, cb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (null === opts.encoding) {
|
||||||
if (opts.stream) {
|
resp._body = [];
|
||||||
resp.stream = setupPipe(resp, opts);
|
} else {
|
||||||
// can be string, buffer, or json... why not an async function too?
|
resp.body = '';
|
||||||
resp.stream.body = async function () {
|
}
|
||||||
handleResponse(resp, opts, cb);
|
resp._bodyLength = 0;
|
||||||
await resp.stream;
|
resp.on('data', function(chunk) {
|
||||||
return resp.body;
|
if ('string' === typeof resp.body) {
|
||||||
};
|
resp.body += chunk.toString(opts.encoding);
|
||||||
cb(null, resp);
|
} else {
|
||||||
return;
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleResponse(resp, opts, cb);
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var _body;
|
var _body;
|
||||||
@ -280,7 +190,7 @@ function setDefaults(defs) {
|
|||||||
var requester;
|
var requester;
|
||||||
|
|
||||||
if (opts.body) {
|
if (opts.body) {
|
||||||
if (true === opts.json && 'string' !== typeof opts.body) {
|
if (true === opts.json) {
|
||||||
_body = JSON.stringify(opts.body);
|
_body = JSON.stringify(opts.body);
|
||||||
} else {
|
} else {
|
||||||
_body = opts.body;
|
_body = opts.body;
|
||||||
@ -324,9 +234,7 @@ function setDefaults(defs) {
|
|||||||
'timeout',
|
'timeout',
|
||||||
'setHost'
|
'setHost'
|
||||||
].forEach(function(key) {
|
].forEach(function(key) {
|
||||||
if (key in opts) {
|
finalOpts[key] = opts.uri[key];
|
||||||
finalOpts[key] = opts[key];
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
finalOpts.method = opts.method;
|
finalOpts.method = opts.method;
|
||||||
@ -464,7 +372,7 @@ function setDefaults(defs) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
req = requester.request(finalOpts, onResponse);
|
req = requester.request(finalOpts, onResponse);
|
||||||
req.once('error', cb);
|
req.on('error', cb);
|
||||||
|
|
||||||
if (_body) {
|
if (_body) {
|
||||||
debug("\n[urequest] '" + finalOpts.method + "' (request) body");
|
debug("\n[urequest] '" + finalOpts.method + "' (request) body");
|
||||||
@ -472,7 +380,7 @@ function setDefaults(defs) {
|
|||||||
if ('function' === typeof _body.pipe) {
|
if ('function' === typeof _body.pipe) {
|
||||||
// used for chunked encoding
|
// used for chunked encoding
|
||||||
_body.pipe(req);
|
_body.pipe(req);
|
||||||
_body.once('error', function (err) {
|
_body.on('error', function(err) {
|
||||||
// https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
|
// https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
|
||||||
// if the Readable stream emits an error during processing,
|
// if the Readable stream emits an error during processing,
|
||||||
// the Writable destination is not closed automatically
|
// the Writable destination is not closed automatically
|
||||||
@ -660,8 +568,7 @@ var _defaults = {
|
|||||||
followOriginalHttpMethod: false,
|
followOriginalHttpMethod: false,
|
||||||
maxRedirects: 10,
|
maxRedirects: 10,
|
||||||
removeRefererHeader: false,
|
removeRefererHeader: false,
|
||||||
// encoding: undefined,
|
//, encoding: undefined
|
||||||
// stream: false, // TODO allow a stream?
|
|
||||||
gzip: false
|
gzip: false
|
||||||
//, body: undefined
|
//, body: undefined
|
||||||
//, json: undefined
|
//, json: undefined
|
||||||
@ -670,14 +577,13 @@ module.exports = setDefaults(_defaults);
|
|||||||
|
|
||||||
module.exports._keys = Object.keys(_defaults).concat([
|
module.exports._keys = Object.keys(_defaults).concat([
|
||||||
'encoding',
|
'encoding',
|
||||||
'stream',
|
|
||||||
'body',
|
'body',
|
||||||
'json',
|
'json',
|
||||||
'form',
|
'form',
|
||||||
'auth',
|
'auth',
|
||||||
'formData',
|
'formData',
|
||||||
'FormData',
|
'FormData'
|
||||||
'userAgent' // non-standard for request.js
|
//'userAgent'
|
||||||
]);
|
]);
|
||||||
module.exports.debug =
|
module.exports.debug =
|
||||||
-1 !== (process.env.NODE_DEBUG || '').split(/\s+/g).indexOf('urequest');
|
-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",
|
"name": "@root/request",
|
||||||
"version": "1.8.2",
|
"version": "1.5.0",
|
||||||
"lockfileVersion": 1
|
"lockfileVersion": 1
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@root/request",
|
"name": "@root/request",
|
||||||
"version": "1.8.2",
|
"version": "1.6.0",
|
||||||
"description": "A lightweight, zero-dependency drop-in replacement for request",
|
"description": "A lightweight, zero-dependency drop-in replacement for request",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"files": [
|
"files": [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user