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: v11.8.1
Choose a base ref
...
head repository: sindresorhus/got
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v12.3.0
Choose a head ref
Loading
Showing with 12,789 additions and 7,981 deletions.
  1. +30 −0 .github/workflows/main.yml
  2. +1 −0 .gitignore
  3. +0 −40 .travis.yml
  4. +74 −67 benchmark/index.ts
  5. +3 −4 benchmark/server.ts
  6. +116 −0 documentation/1-promise.md
  7. +104 −0 documentation/10-instances.md
  8. +1,002 −0 documentation/2-options.md
  9. +439 −0 documentation/3-streams.md
  10. +254 −0 documentation/4-pagination.md
  11. +217 −0 documentation/5-https.md
  12. +138 −0 documentation/6-timeout.md
  13. +166 −0 documentation/7-retry.md
  14. +105 −0 documentation/8-errors.md
  15. +299 −0 documentation/9-hooks.md
  16. +0 −126 documentation/advanced-creation.md
  17. +299 −0 documentation/async-stack-traces.md
  18. +188 −0 documentation/cache.md
  19. +146 −0 documentation/examples/advanced-creation.js
  20. +29 −13 documentation/examples/gh-got.js
  21. +51 −0 documentation/examples/h2c.js
  22. +57 −0 documentation/examples/pagination.js
  23. +5 −7 documentation/examples/runkit-example.js
  24. +84 −0 documentation/examples/uppercase-headers.js
  25. +119 −44 documentation/lets-make-a-plugin.md
  26. +0 −153 documentation/migration-guides.md
  27. +101 −0 documentation/migration-guides/axios.md
  28. +103 −0 documentation/migration-guides/nodejs.md
  29. +148 −0 documentation/migration-guides/request.md
  30. +401 −0 documentation/tips.md
  31. +111 −0 documentation/typescript.md
  32. +83 −53 package.json
  33. +326 −2,245 readme.md
  34. +0 −31 source/as-promise/create-rejection.ts
  35. +84 −82 source/as-promise/index.ts
  36. +0 −98 source/as-promise/normalize-arguments.ts
  37. +0 −33 source/as-promise/parse-body.ts
  38. +27 −273 source/as-promise/types.ts
  39. +16 −7 source/core/calculate-retry-delay.ts
  40. +183 −0 source/core/errors.ts
  41. +702 −2,265 source/core/index.ts
  42. +2,526 −0 source/core/options.ts
  43. +44 −0 source/core/parse-link-header.ts
  44. +162 −0 source/core/response.ts
  45. +66 −52 source/core/{utils → }/timed-out.ts
  46. +0 −20 source/core/utils/dns-ip-version.ts
  47. +6 −18 source/core/utils/get-body-size.ts
  48. +0 −21 source/core/utils/get-buffer.ts
  49. +8 −0 source/core/utils/is-client-request.ts
  50. +5 −3 source/core/utils/is-form-data.ts
  51. +0 −8 source/core/utils/is-response-ok.ts
  52. +6 −0 source/core/utils/is-unix-socket-url.ts
  53. +5 −4 source/core/utils/options-to-url.ts
  54. +9 −7 source/core/utils/proxy-events.ts
  55. +4 −4 source/core/utils/unhandle.ts
  56. +6 −6 source/core/utils/url-to-options.ts
  57. +121 −187 source/create.ts
  58. +20 −126 source/index.ts
  59. +71 −181 source/types.ts
  60. +0 −11 source/utils/deep-freeze.ts
  61. +0 −14 source/utils/deprecation-warning.ts
  62. +274 −0 test/abort.ts
  63. +66 −67 test/agent.ts
  64. +256 −148 test/arguments.ts
  65. +106 −26 test/cache.ts
  66. +72 −58 test/cancel.ts
  67. +57 −46 test/cookies.ts
  68. +119 −67 test/create.ts
  69. +19 −0 test/encoding.ts
  70. +199 −53 test/error.ts
  71. +10 −8 test/gzip.ts
  72. +96 −39 test/headers.ts
  73. +7 −4 test/helpers.ts
  74. +7 −7 test/helpers/create-http-test-server.ts
  75. +26 −16 test/helpers/create-https-test-server.ts
  76. +12 −0 test/helpers/invalid-url.ts
  77. +13 −7 test/helpers/slow-data-stream.ts
  78. +4 −3 test/helpers/types.ts
  79. +36 −31 test/helpers/with-server.ts
  80. +376 −264 test/hooks.ts
  81. +97 −52 test/http.ts
  82. +208 −160 test/https.ts
  83. +24 −25 test/merge-instances.ts
  84. +104 −53 test/normalize-arguments.ts
  85. +201 −136 test/pagination.ts
  86. +114 −0 test/parse-link-header.ts
  87. +122 −69 test/post.ts
  88. +71 −21 test/progress.ts
  89. +27 −10 test/promise.ts
  90. +115 −65 test/redirects.ts
  91. +23 −21 test/response-parse.ts
  92. +167 −81 test/retry.ts
  93. +135 −90 test/stream.ts
  94. +220 −122 test/timeout.ts
  95. +53 −0 test/timings.ts
  96. +2 −0 test/types/create-test-server/index.d.ts
  97. +5 −1 test/types/slow-stream/index.d.ts
  98. +45 −4 test/unix-socket.ts
  99. +18 −18 test/url-to-options.ts
  100. +1 −1 test/weakable-map.ts
  101. +12 −5 tsconfig.json
30 changes: 30 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
node-version:
- 18
- 16
- 14
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
# - uses: codecov/codecov-action@v2
# if: matrix.os == 'ubuntu-latest' && matrix.node-version == 16
# with:
# fail_ci_if_error: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -3,3 +3,4 @@ yarn.lock
coverage
.nyc_output
dist
*.0x
40 changes: 0 additions & 40 deletions .travis.yml

This file was deleted.

141 changes: 74 additions & 67 deletions benchmark/index.ts
Original file line number Diff line number Diff line change
@@ -1,161 +1,165 @@
'use strict';
import {URL} from 'url';
import https = require('https');
import {URL} from 'node:url';
import https from 'node:https';
import axios from 'axios';
import Benchmark = require('benchmark');
import Benchmark from 'benchmark';
import fetch from 'node-fetch';
import request = require('request');
import got from '../source';
import Request, {kIsNormalizedAlready} from '../source/core';

const {normalizeArguments} = Request;
import request from 'request';
import got from '../source/index.js';
import Request from '../source/core/index.js';
import Options, {OptionsInit} from '../source/core/options.js';

// Configuration
const httpsAgent = new https.Agent({
keepAlive: true,
rejectUnauthorized: false
rejectUnauthorized: false,
});

const url = new URL('https://127.0.0.1:8080');
const url = new URL('https://127.0.0.1:8081');
const urlString = url.toString();

const gotOptions = {
const gotOptions: OptionsInit & {isStream?: true} = {
agent: {
https: httpsAgent
https: httpsAgent,
},
https: {
rejectUnauthorized: false
rejectUnauthorized: false,
},
retry: {
limit: 0,
},
retry: 0
};

const normalizedGotOptions = normalizeArguments(url, gotOptions);
normalizedGotOptions[kIsNormalizedAlready] = true;
const normalizedGotOptions = new Options(url, gotOptions);

const requestOptions = {
// eslint-disable-next-line @typescript-eslint/naming-convention
strictSSL: false,
agent: httpsAgent
agent: httpsAgent,
};

const fetchOptions = {
agent: httpsAgent
agent: httpsAgent,
};

const axiosOptions = {
url: urlString,
httpsAgent,
https: {
rejectUnauthorized: false
}
rejectUnauthorized: false,
},
};

const axiosStreamOptions: typeof axiosOptions & {responseType: 'stream'} = {
...axiosOptions,
responseType: 'stream'
responseType: 'stream',
};

const httpsOptions = {
https: {
rejectUnauthorized: false
rejectUnauthorized: false,
},
agent: httpsAgent
agent: httpsAgent,
};

const suite = new Benchmark.Suite();

// Benchmarking
suite.add('got - promise', {
defer: true,
fn: async (deferred: {resolve: () => void}) => {
async fn(deferred: {resolve: () => void}) {
await got(url, gotOptions);
deferred.resolve();
}
},
}).add('got - stream', {
defer: true,
fn: async (deferred: {resolve: () => void}) => {
async fn(deferred: {resolve: () => void}) {
got.stream(url, gotOptions).resume().once('end', () => {
deferred.resolve();
});
}
},
}).add('got - core', {
defer: true,
fn: async (deferred: {resolve: () => void}) => {
async fn(deferred: {resolve: () => void}) {
const stream = new Request(url, gotOptions);
void stream.flush();
stream.resume().once('end', () => {
deferred.resolve();
});
}
},
}).add('got - core - normalized options', {
defer: true,
fn: async (deferred: {resolve: () => void}) => {
const stream = new Request(undefined as any, normalizedGotOptions);
async fn(deferred: {resolve: () => void}) {
const stream = new Request(undefined, undefined, normalizedGotOptions);
void stream.flush();
stream.resume().once('end', () => {
deferred.resolve();
});
}
},
}).add('request - callback', {
defer: true,
fn: (deferred: {resolve: () => void}) => {
fn(deferred: {resolve: () => void}) {
request(urlString, requestOptions, (error: Error) => {
if (error) {
throw error;
}

deferred.resolve();
});
}
},
}).add('request - stream', {
defer: true,
fn: (deferred: {resolve: () => void}) => {
fn(deferred: {resolve: () => void}) {
const stream = request(urlString, requestOptions);
stream.resume();
stream.once('end', () => {
deferred.resolve();
});
}
},
}).add('node-fetch - promise', {
defer: true,
fn: async (deferred: {resolve: () => void}) => {
const response = await fetch(url, fetchOptions);
async fn(deferred: {resolve: () => void}) {
const response = await fetch(urlString, fetchOptions);
await response.text();

deferred.resolve();
}
},
}).add('node-fetch - stream', {
defer: true,
fn: async (deferred: {resolve: () => void}) => {
const {body} = await fetch(url, fetchOptions);
async fn(deferred: {resolve: () => void}) {
const {body} = await fetch(urlString, fetchOptions);

body.resume();
body.once('end', () => {
body!.resume();
body!.once('end', () => {
deferred.resolve();
});
}
},
}).add('axios - promise', {
defer: true,
fn: async (deferred: {resolve: () => void}) => {
async fn(deferred: {resolve: () => void}) {
await axios.request(axiosOptions);
deferred.resolve();
}
},
}).add('axios - stream', {
defer: true,
fn: async (deferred: {resolve: () => void}) => {
const {data} = await axios.request(axiosStreamOptions);
async fn(deferred: {resolve: () => void}) {
const result = await axios.request(axiosStreamOptions);
const {data}: any = result;

data.resume();
data.once('end', () => {
deferred.resolve();
});
}
},
}).add('https - stream', {
defer: true,
fn: (deferred: {resolve: () => void}) => {
fn(deferred: {resolve: () => void}) {
https.request(urlString, httpsOptions, response => {
response.resume();
response.once('end', () => {
deferred.resolve();
});
}).end();
}
},
}).on('cycle', (event: Benchmark.Event) => {
console.log(String(event.target));
}).on('complete', function (this: any) {
@@ -169,9 +173,10 @@ const internalBenchmark = (): void => {

const internalSuite = new Benchmark.Suite();
internalSuite.add('got - normalize options', {
fn: () => {
normalizeArguments(url, gotOptions);
}
fn() {
// eslint-disable-next-line no-new
new Options(url, gotOptions);
},
}).on('cycle', (event: Benchmark.Event) => {
console.log(String(event.target));
});
@@ -180,17 +185,19 @@ const internalBenchmark = (): void => {
};

// Results (i7-7700k, CPU governor: performance):
// got - promise x 3,003 ops/sec ±6.26% (70 runs sampled)
// got - stream x 3,538 ops/sec ±5.86% (67 runs sampled)
// got - core x 5,828 ops/sec ±3.11% (79 runs sampled)
// got - core - normalized options x 7,596 ops/sec ±1.60% (85 runs sampled)
// request - callback x 6,530 ops/sec ±6.84% (72 runs sampled)
// request - stream x 7,348 ops/sec ±3.62% (78 runs sampled)
// node-fetch - promise x 6,284 ops/sec ±5.50% (76 runs sampled)
// node-fetch - stream x 7,746 ops/sec ±3.32% (80 runs sampled)
// axios - promise x 6,301 ops/sec ±6.24% (77 runs sampled)
// axios - stream x 8,605 ops/sec ±2.73% (87 runs sampled)
// https - stream x 10,477 ops/sec ±3.64% (80 runs sampled)

// H2O server:
// got - promise x 2,846 ops/sec ±3.71% (74 runs sampled)
// got - stream x 3,840 ops/sec ±1.97% (83 runs sampled)
// got - core x 3,929 ops/sec ±2.31% (83 runs sampled)
// got - core - normalized options x 4,483 ops/sec ±2.25% (80 runs sampled)
// request - callback x 4,784 ops/sec ±4.25% (77 runs sampled)
// request - stream x 5,138 ops/sec ±2.10% (80 runs sampled)
// node-fetch - promise x 6,693 ops/sec ±4.56% (77 runs sampled)
// node-fetch - stream x 7,332 ops/sec ±3.22% (80 runs sampled)
// axios - promise x 5,365 ops/sec ±4.30% (74 runs sampled)
// axios - stream x 7,424 ops/sec ±3.09% (80 runs sampled)
// https - stream x 8,850 ops/sec ±2.77% (71 runs sampled)
// Fastest is https - stream

// got - normalize options x 90,974 ops/sec ±0.57% (93 runs sampled)
// got - normalize options x 73,484 ops/sec ±0.85% (95 runs sampled)
7 changes: 3 additions & 4 deletions benchmark/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {AddressInfo} from 'net';
import https = require('https');
import {AddressInfo} from 'node:net';
import https from 'node:https';
// @ts-expect-error No types
import createCert = require('create-cert');
import createCert from 'create-cert';

(async () => {
const keys = await createCert({days: 365, commonName: 'localhost'});
@@ -10,7 +10,6 @@ import createCert = require('create-cert');
response.end('ok');
}).listen(8080, () => {
const {port} = server.address() as AddressInfo;

console.log(`Listening at https://localhost:${port}`);
});
})();
Loading