Browse Source

minor tweaks

node-7
Sindre Sorhus 9 years ago
parent
commit
f33514146c
  1. 3
      .editorconfig
  2. 9
      index.js
  3. 2
      package.json
  4. 21
      readme.md
  5. 4
      test/arguments.js
  6. 4
      test/retry.js
  7. 2
      test/stream.js

3
.editorconfig

@ -10,6 +10,3 @@ insert_final_newline = true
[{package.json,*.yml}]
indent_style = space
indent_size = 2
[*.md]
trim_trailing_whitespace = false

9
index.js

@ -1,5 +1,4 @@
'use strict';
const EventEmitter = require('events').EventEmitter;
const http = require('http');
const https = require('https');
@ -47,6 +46,7 @@ function requestAsEventEmitter(opts) {
ee.emit('redirect', res, redirectOpts);
get(redirectOpts);
return;
}
@ -177,7 +177,6 @@ function asStream(opts) {
});
ee.on('redirect', proxy.emit.bind(proxy, 'redirect'));
ee.on('error', proxy.emit.bind(proxy, 'error'));
return proxy;
@ -242,9 +241,7 @@ function normalizeArguments(url, opts) {
}
}
opts.method = opts.method || 'GET';
opts.method = opts.method.toUpperCase();
opts.method = (opts.method || 'GET').toUpperCase();
if (opts.hostname === 'unix') {
const matches = /(.+):(.+)/.exec(opts.path);
@ -258,12 +255,14 @@ function normalizeArguments(url, opts) {
if (typeof opts.retries !== 'function') {
const retries = opts.retries;
opts.retries = function backoff(iter, err) {
if (iter > retries || !isRetryAllowed(err)) {
return 0;
}
const noise = Math.random() * 100;
return (1 << iter) * 1000 + noise;
};
}

2
package.json

@ -1,7 +1,7 @@
{
"name": "got",
"version": "6.2.0",
"description": "Simplified HTTP/HTTPS requests",
"description": "Simplified HTTP requests",
"license": "MIT",
"repository": "sindresorhus/got",
"maintainers": [

21
readme.md

@ -6,15 +6,15 @@
<br>
</h1>
> Simplified HTTP/HTTPS requests
> Simplified HTTP requests
[![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) [![Coverage Status](https://coveralls.io/repos/sindresorhus/got/badge.svg?service=github&branch=master)](https://coveralls.io/github/sindresorhus/got?branch=master) [![Downloads](https://img.shields.io/npm/dm/got.svg)](https://npmjs.com/got)
[![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/got/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/got?branch=master) [![Downloads](https://img.shields.io/npm/dm/got.svg)](https://npmjs.com/got)
A nicer interface to the built-in [`http`](http://nodejs.org/api/http.html) module.
It supports following redirects, promises, streams, retries, automagically handling gzip/deflate and some convenience options.
Created because [`request`](https://github.com/mikeal/request) is bloated *(several megabytes!)*.
Created because [`request`](https://github.com/request/request) is bloated *(several megabytes!)*.
## Install
@ -87,14 +87,14 @@ If `body` is a plain object, it will be stringified with [`querystring.stringify
###### encoding
Type: `string`, `null`
Type: `string`, `null`<br>
Default: `'utf8'`
Encoding to be used on `setEncoding` of the response data. If `null`, the body is returned as a Buffer.
###### json
Type: `boolean`
Type: `boolean`<br>
Default: `false`
*This is mutually exclusive with stream mode.*
@ -103,7 +103,7 @@ Parse response body with `JSON.parse` and set `accept` header to `application/js
###### query
Type: `string`, `object`
Type: `string`, `object`<br>
Query string object that will be added to the request URL. This will override the query string in `url`.
@ -115,7 +115,7 @@ Milliseconds to wait for a server to send response headers before aborting reque
###### retries
Type: `number`, `function`
Type: `number`, `function`<br>
Default: `5`
Number of request retries when network errors happens. Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 0).
@ -257,6 +257,7 @@ got('http://unix:/var/run/docker.sock:/containers/json');
got('unix:/var/run/docker.sock:/containers/json');
```
## Tip
It's a good idea to set the `'user-agent'` header so the provider can more easily see how their resource is used. By default, it's the URL to this repo.
@ -281,11 +282,11 @@ got('todomvc.com', {
## Created by
[![Sindre Sorhus](https://avatars.githubusercontent.com/u/170270?v=3&s=100)](http://sindresorhus.com) | [![Vsevolod Strukchinsky](https://avatars.githubusercontent.com/u/365089?v=3&s=100)](https://github.com/floatdrop)
[![Sindre Sorhus](https://avatars.githubusercontent.com/u/170270?v=3&s=100)](https://sindresorhus.com) | [![Vsevolod Strukchinsky](https://avatars.githubusercontent.com/u/365089?v=3&s=100)](https://github.com/floatdrop)
---|---
[Sindre Sorhus](http://sindresorhus.com) | [Vsevolod Strukchinsky](https://github.com/floatdrop)
[Sindre Sorhus](https://sindresorhus.com) | [Vsevolod Strukchinsky](https://github.com/floatdrop)
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)

4
test/arguments.js

@ -53,10 +53,6 @@ test('should throw with auth in url', async t => {
}
});
test('accepts url.parse object as first argument', async t => {
t.is((await got({hostname: s.host, port: s.port, path: '/test'})).body, '/test');
});
test.after('cleanup', async () => {
await s.close();
});

4
test/retry.js

@ -50,7 +50,7 @@ test('function gets iter count', async t => {
t.is(fifth, 6);
});
test('falsy value prevent retries', async t => {
test('falsy value prevents retries', async t => {
try {
await got(`${s.url}/long`, {timeout: 100, retries: () => 0});
} catch (err) {
@ -58,7 +58,7 @@ test('falsy value prevent retries', async t => {
}
});
test('falsy value prevent retries', async t => {
test('falsy value prevents retries #2', async t => {
try {
await got(`${s.url}/long`, {timeout: 100, retries: (iter, err) => {
t.ok(err);

2
test/stream.js

@ -100,7 +100,7 @@ test.cb('have error event', t => {
});
});
test.cb('have error event', t => {
test.cb('have error event #2', t => {
got.stream('.com', {retries: 0})
.on('response', () => {
t.fail('response event should not be emitted');

Loading…
Cancel
Save