Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/got
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: bd3315b6c61d20a68944831d8b3a05046d5554ad
Choose a base ref
...
head repository: sindresorhus/got
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 6ce603e99a17d258751ddce23b1c9d424b7be795
Choose a head ref

Commits on May 1, 2018

  1. Require Node.js 8

    sindresorhus committed May 1, 2018
    Copy the full SHA
    2b14537 View commit details
  2. Copy the full SHA
    00fdeea View commit details
  3. Copy the full SHA
    6e7a455 View commit details
  4. Copy the full SHA
    deabbec View commit details
  5. Copy the full SHA
    a03d21e View commit details

Commits on Jun 13, 2018

  1. Copy the full SHA
    54cead2 View commit details

Commits on Jul 3, 2018

  1. fix Buffer.byteLength(req._header) throwing error (#490)

    Anton Egorov authored and lukechilds committed Jul 3, 2018
    Copy the full SHA
    d7641e5 View commit details

Commits on Jul 5, 2018

  1. Copy the full SHA
    058452b View commit details
  2. Catch more errors (#498)

    szmarczak authored and sindresorhus committed Jul 5, 2018
    Copy the full SHA
    f621184 View commit details
  3. Copy the full SHA
    e473a26 View commit details
  4. Copy the full SHA
    74bbee3 View commit details
  5. Bump dependencies

    sindresorhus committed Jul 5, 2018
    Copy the full SHA
    b4f6698 View commit details
  6. Copy the full SHA
    2ee8124 View commit details
  7. Copy the full SHA
    013e668 View commit details
  8. Drop support for body being an Array when form: true

    `new URLSearchParams()` doesn't support this and it's a weird use-case. If you need support for this, just don't set `form: true` and handle stringifying yourself.
    sindresorhus committed Jul 5, 2018
    Copy the full SHA
    dfe5b1c View commit details
  9. Copy the full SHA
    8103bc5 View commit details
  10. Copy the full SHA
    38931e2 View commit details
  11. Code style tweaks

    sindresorhus committed Jul 5, 2018
    Copy the full SHA
    09eee39 View commit details
  12. Copy the full SHA
    b2dab3b View commit details
  13. Copy the full SHA
    346bafc View commit details
  14. Hide the electron import from Webpack

    Becuse Webpack is super annoying: webpack/webpack#196
    sindresorhus committed Jul 5, 2018
    Copy the full SHA
    a4ce0a7 View commit details
  15. Fix merging defaults and options (#499)

    Fixes commit 058452b
    szmarczak authored and sindresorhus committed Jul 5, 2018
    Copy the full SHA
    844c993 View commit details
  16. Document setting multiple cookies

    Fixes #408
    sindresorhus committed Jul 5, 2018
    Copy the full SHA
    0428961 View commit details
  17. Fix the proxy example in the readme

    Fixes #435
    sindresorhus committed Jul 5, 2018
    Copy the full SHA
    77bc901 View commit details

Commits on Jul 6, 2018

  1. Copy the full SHA
    b54b680 View commit details
  2. Meta tweaks

    sindresorhus committed Jul 6, 2018
    Copy the full SHA
    ab5e8e1 View commit details
  3. Copy the full SHA
    d485d7e View commit details

Commits on Jul 7, 2018

  1. Copy the full SHA
    464515f View commit details
  2. Fix timeouts (#501)

    Fixes #478
    Fixes #344
    szmarczak authored and sindresorhus committed Jul 7, 2018
    Copy the full SHA
    be0f17f View commit details
  3. Copy the full SHA
    57ff833 View commit details
  4. Enable AppVeyor

    sindresorhus committed Jul 7, 2018
    Copy the full SHA
    e599a8d View commit details
  5. Copy the full SHA
    ae01825 View commit details

Commits on Jul 8, 2018

  1. Update readme.md

    szmarczak authored Jul 8, 2018
    Copy the full SHA
    76f5847 View commit details
  2. Fix the timeout tests (#506)

    szmarczak authored and sindresorhus committed Jul 8, 2018
    Copy the full SHA
    75dc4c2 View commit details
  3. Simplify wrapping got (#503)

    This renames `got.create()` to `got.extend()` and adds a more powerful `got.create()` method.
    szmarczak authored and sindresorhus committed Jul 8, 2018
    Copy the full SHA
    bc41a49 View commit details

Commits on Jul 9, 2018

  1. Copy the full SHA
    e86aad7 View commit details
  2. Update readme.md

    szmarczak authored Jul 9, 2018
    Copy the full SHA
    112963e View commit details

Commits on Jul 10, 2018

  1. Copy the full SHA
    3eac42a View commit details

Commits on Jul 11, 2018

  1. Copy the full SHA
    28888fa View commit details
  2. Add tests for url-to-options.js (#513)

    alextes authored and sindresorhus committed Jul 11, 2018
    Copy the full SHA
    613fa9b View commit details
  3. Various minor code tweaks (#517)

    jstewmon authored and sindresorhus committed Jul 11, 2018
    Copy the full SHA
    7345a6e View commit details

Commits on Jul 12, 2018

  1. Change reqDelay to 160

    szmarczak authored Jul 12, 2018
    Copy the full SHA
    8fa2bf7 View commit details
  2. Copy the full SHA
    99e3835 View commit details
  3. Support retrying on some HTTP status codes and generally improve the …

    …retry functionality (#508)
    
    Fixes #417 
    Fixes #379
    szmarczak authored and sindresorhus committed Jul 12, 2018
    Copy the full SHA
    98b5664 View commit details

Commits on Jul 13, 2018

  1. Standardize on camelcase (#520)

    jstewmon authored and sindresorhus committed Jul 13, 2018
    Copy the full SHA
    fb5185a View commit details

Commits on Jul 14, 2018

  1. Add beforeRequest hook (#516)

    jstewmon authored and sindresorhus committed Jul 14, 2018
    Copy the full SHA
    107756f View commit details
  2. Copy the full SHA
    7a49ce7 View commit details

Commits on Jul 17, 2018

  1. Set headers on stream proxy (#518)

    Closes #401
    szmarczak authored and sindresorhus committed Jul 17, 2018
    Copy the full SHA
    83bc44c View commit details

Commits on Jul 18, 2018

  1. Properly clear requestTimeoutTimer timeout

    This file needs more tests.
    szmarczak authored Jul 18, 2018
    Copy the full SHA
    46d1217 View commit details
  2. Copy the full SHA
    13bb0fa View commit details
Showing with 5,309 additions and 1,513 deletions.
  1. +1 −2 .gitattributes
  2. +38 −0 .github/ISSUE_TEMPLATE/1-bug-report.md
  3. +18 −0 .github/ISSUE_TEMPLATE/2-feature-request.md
  4. +12 −0 .github/ISSUE_TEMPLATE/3-question.md
  5. +6 −0 .github/PULL_REQUEST_TEMPLATE.md
  6. +3 −4 .travis.yml
  7. +266 −0 advanced-creation.md
  8. +0 −675 index.js
  9. BIN media/logo.sketch
  10. +1 −1 media/logo.svg
  11. +170 −0 migration-guides.md
  12. +26 −47 package.json
  13. +664 −117 readme.md
  14. +107 −0 source/as-promise.js
  15. +92 −0 source/as-stream.js
  16. +79 −0 source/create.js
  17. +36 −22 { → source}/errors.js
  18. +31 −0 source/get-response.js
  19. +60 −0 source/index.js
  20. +8 −0 source/known-hook-events.js
  21. +73 −0 source/merge.js
  22. +248 −0 source/normalize-arguments.js
  23. +96 −0 source/progress.js
  24. +299 −0 source/request-as-event-emitter.js
  25. +12 −0 source/utils/deep-freeze.js
  26. +32 −0 source/utils/get-body-size.js
  27. +4 −0 source/utils/is-form-data.js
  28. +160 −0 source/utils/timed-out.js
  29. +25 −0 source/utils/url-to-options.js
  30. +33 −47 test/agent.js
  31. +169 −46 test/arguments.js
  32. +57 −21 test/cache.js
  33. +33 −34 test/cancel.js
  34. +125 −0 test/cookies.js
  35. +237 −0 test/create.js
  36. +148 −52 test/error.js
  37. +1 −0 test/fixtures/stream-content-length
  38. +43 −43 test/gzip.js
  39. +113 −29 test/headers.js
  40. +14 −14 test/helpers.js
  41. +61 −23 test/helpers/server.js
  42. +332 −0 test/hooks.js
  43. +35 −26 test/http.js
  44. +9 −38 test/https.js
  45. +34 −34 test/json-parse.js
  46. +136 −0 test/merge-instances.js
  47. +20 −21 test/post.js
  48. +34 −34 test/progress.js
  49. +34 −0 test/promise.js
  50. +90 −82 test/redirects.js
  51. +243 −24 test/retry.js
  52. +10 −5 test/socket-destroyed.js
  53. +111 −31 test/stream.js
  54. +468 −34 test/timeout.js
  55. +25 −0 test/timings.js
  56. +11 −7 test/unix-socket.js
  57. +116 −0 test/url-to-options.js
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
* text=auto
* text=auto eol=lf
*.ai binary
*.js text eol=lf
38 changes: 38 additions & 0 deletions .github/ISSUE_TEMPLATE/1-bug-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
name: "🐞 Bug report"
about: Something is not working as it should
---

#### Describe the bug

- Node.js version:
- OS & version:

<!-- A clear and concise description of what the bug is. -->

#### Actual behavior

...

#### Expected behavior

...

#### Code to reproduce

```js
...
```

<!--
We encourage you to submit a pull request with a failing test:
- This will make it more likely for us to prioritize your issue.
- It's a good way to prove that the issue is related to Got and not your code.
Example: https://github.com/avajs/ava/blob/master/docs/01-writing-tests.md#failing-tests
-->

#### Checklist

- [ ] I have read the documentation.
- [ ] I have tried my code with the latest version of Node.js and Got.
18 changes: 18 additions & 0 deletions .github/ISSUE_TEMPLATE/2-feature-request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
name: "⭐ Feature request"
about: Suggest an idea for Got
---

#### What problem are you trying to solve?

...

#### Describe the feature

...

<!-- Include a usage example of the feature. If the feature is currently possible with a workaround, include that too. -->

#### Checklist

- [ ] I have read the documentation and made sure this feature doesn't already exist.
12 changes: 12 additions & 0 deletions .github/ISSUE_TEMPLATE/3-question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
name: "❓ Question"
about: Something is unclear or needs to be discussed
---

#### What would you like to discuss?

...

#### Checklist

- [ ] I have read the documentation.
6 changes: 6 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#### Checklist

- [ ] I have read the documentation.
- [ ] I have included a pull request description of my changes.
- [ ] I have included some tests.
- [ ] If it's a new feature, I have included documentation updates.
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
sudo: false
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
after_success: npm run coveralls
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
266 changes: 266 additions & 0 deletions advanced-creation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
# Advanced creation

> Make calling REST APIs easier by creating niche-specific `got` instances.
#### got.create(settings)

Example: [gh-got](https://github.com/sindresorhus/gh-got/blob/master/index.js)

Configure a new `got` instance with the provided settings. You can access the resolved options with the `.defaults` property on the instance.

**Note:** In contrast to `got.extend()`, this method has no defaults.

##### [options](readme.md#options)

To inherit from parent, set it as `got.defaults.options` or use [`got.mergeOptions(defaults.options, options)`](readme.md#gotmergeoptionsparentoptions-newoptions).<br>
**Note**: Avoid using [object spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals) as it doesn't work recursively.

##### mutableDefaults

Type: `boolean`<br>
Default: `false`

States if the defaults are mutable. It's very useful when you need to [update headers over time](readme.md#hooksafterresponse).

##### handler

Type: `Function`<br>
Default: `undefined`

A function making additional changes to the request.

To inherit from parent, set it as `got.defaults.handler`.<br>
To use the default handler, just omit specifying this.

###### [options](readme.md#options)

**Note:** These options are [normalized](source/normalize-arguments.js).

###### next()

Returns a `Promise` or a `Stream` depending on [`options.stream`](readme.md#stream).

```js
const settings = {
handler: (options, next) => {
if (options.stream) {
// It's a Stream
// We can perform stream-specific actions on it
return next(options)
.on('request', request => setTimeout(() => request.abort(), 50));
}

// It's a Promise
return next(options);
},
options: got.mergeOptions(got.defaults.options, {
json: true
})
};

const jsonGot = got.create(settings);
```

```js
const defaults = {
options: {
retry: {
retries: 2,
methods: [
'GET',
'PUT',
'HEAD',
'DELETE',
'OPTIONS',
'TRACE'
],
statusCodes: [
408,
413,
429,
500,
502,
503,
504
],
errorCodes: [
'ETIMEDOUT',
'ECONNRESET',
'EADDRINUSE',
'ECONNREFUSED',
'EPIPE',
'ENOTFOUND',
'ENETUNREACH',
'EAI_AGAIN'
]
},
headers: {
'user-agent': `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`
},
hooks: {
beforeRequest: [],
beforeRedirect: [],
beforeRetry: [],
afterResponse: []
},
decompress: true,
throwHttpErrors: true,
followRedirect: true,
stream: false,
form: false,
json: false,
cache: false,
useElectronNet: false
},
mutableDefaults: false
};

// Same as:
const defaults = {
handler: got.defaults.handler,
options: got.defaults.options,
mutableDefaults: got.defaults.mutableDefaults
};

const unchangedGot = got.create(defaults);
```

```js
const settings = {
handler: got.defaults.handler,
options: got.mergeOptions(got.defaults.options, {
headers: {
unicorn: 'rainbow'
}
})
};

const unicorn = got.create(settings);

// Same as:
const unicorn = got.extend({headers: {unicorn: 'rainbow'}});
```

### Merging instances

Got supports composing multiple instances together. This is very powerful. You can create a client that limits download speed and then compose it with an instance that signs a request. It's like plugins without any of the plugin mess. You just create instances and then compose them together.

#### got.mergeInstances(instanceA, instanceB, ...)

Merges many instances into a single one:
- options are merged using [`got.mergeOptions()`](readme.md#gotmergeoptionsparentoptions-newoptions) (+ hooks are merged too),
- handlers are stored in an array.

## Examples

Some examples of what kind of instances you could compose together:

#### Denying redirects that lead to other sites than specified

```js
const controlRedirects = got.create({
options: got.defaults.options,
handler: (options, next) => {
const promiseOrStream = next(options);
return promiseOrStream.on('redirect', resp => {
const host = new URL(resp.url).host;
if (options.allowedHosts && !options.allowedHosts.includes(host)) {
promiseOrStream.cancel(`Redirection to ${host} is not allowed`);
}
});
}
});
```

#### Limiting download & upload

It's very useful in case your machine's got a little amount of RAM.

```js
const limitDownloadUpload = got.create({
options: got.defaults.options,
handler: (options, next) => {
let promiseOrStream = next(options);
if (typeof options.downloadLimit === 'number') {
promiseOrStream.on('downloadProgress', progress => {
if (progress.transferred > options.downloadLimit && progress.percent !== 1) {
promiseOrStream.cancel(`Exceeded the download limit of ${options.downloadLimit} bytes`);
}
});
}

if (typeof options.uploadLimit === 'number') {
promiseOrStream.on('uploadProgress', progress => {
if (progress.transferred > options.uploadLimit && progress.percent !== 1) {
promiseOrStream.cancel(`Exceeded the upload limit of ${options.uploadLimit} bytes`);
}
});
}

return promiseOrStream;
}
});
```

#### No user agent

```js
const noUserAgent = got.extend({
headers: {
'user-agent': null
}
});
```

#### Custom endpoint

```js
const httpbin = got.extend({
baseUrl: 'https://httpbin.org/'
});
```

#### Signing requests

```js
const crypto = require('crypto');
const getMessageSignature = (data, secret) => crypto.createHmac('sha256', secret).update(data).digest('hex').toUpperCase();
const signRequest = got.extend({
hooks: {
beforeRequest: [
options => {
options.headers['sign'] = getMessageSignature(options.body || '', process.env.SECRET);
}
]
}
});
```

#### Putting it all together

If these instances are different modules and you don't want to rewrite them, use `got.mergeInstances()`.

**Note**: The `noUserAgent` instance must be placed at the end of chain as the instances are merged in order. Other instances do have the `user-agent` header.

```js
const merged = got.mergeInstances(controlRedirects, limitDownloadUpload, httpbin, signRequest, noUserAgent);

(async () => {
// There's no 'user-agent' header :)
await merged('/');
/* HTTP Request =>
* GET / HTTP/1.1
* accept-encoding: gzip, deflate
* sign: F9E66E179B6747AE54108F82F8ADE8B3C25D76FD30AFDE6C395822C530196169
* Host: httpbin.org
* Connection: close
*/

const MEGABYTE = 1048576;
await merged('http://ipv4.download.thinkbroadband.com/5MB.zip', {downloadLimit: MEGABYTE});
// CancelError: Exceeded the download limit of 1048576 bytes

await merged('https://jigsaw.w3.org/HTTP/300/301.html', {allowedHosts: ['google.com']});
// CancelError: Redirection to jigsaw.w3.org is not allowed
})();
```
Loading