got 12.1.0 → 12.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -128,6 +128,10 @@ export default function asPromise(firstRequest) {
128
128
  emitter.on(event, fn);
129
129
  return promise;
130
130
  };
131
+ promise.off = (event, fn) => {
132
+ emitter.off(event, fn);
133
+ return promise;
134
+ };
131
135
  const shortcut = (responseType) => {
132
136
  const newPromise = (async () => {
133
137
  // Wait until downloading has ended
@@ -81,4 +81,10 @@ An error which always triggers a new retry when thrown.
81
81
  export declare class RetryError extends RequestError {
82
82
  constructor(request: Request);
83
83
  }
84
+ /**
85
+ An error to be thrown when the request is aborted by AbortController.
86
+ */
87
+ export declare class AbortError extends RequestError {
88
+ constructor(request: Request);
89
+ }
84
90
  export {};
@@ -166,3 +166,13 @@ export class RetryError extends RequestError {
166
166
  this.code = 'ERR_RETRYING';
167
167
  }
168
168
  }
169
+ /**
170
+ An error to be thrown when the request is aborted by AbortController.
171
+ */
172
+ export class AbortError extends RequestError {
173
+ constructor(request) {
174
+ super('This operation was aborted.', {}, request);
175
+ this.code = 'ERR_ABORTED';
176
+ this.name = 'AbortError';
177
+ }
178
+ }
@@ -1,4 +1,9 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ /// <reference types="node" />
5
+ /// <reference types="node" />
6
+ /// <reference types="node" />
2
7
  import { Duplex } from 'node:stream';
3
8
  import { URL } from 'node:url';
4
9
  import { ServerResponse } from 'node:http';
@@ -26,6 +31,8 @@ export declare type GotEventFunction<T> =
26
31
 
27
32
  @example
28
33
  ```
34
+ import got from 'got';
35
+
29
36
  got.stream('https://github.com')
30
37
  .on('request', request => setTimeout(() => request.destroy(), 50));
31
38
  ```
@@ -55,6 +62,8 @@ If the `content-length` header is missing, `total` will be `undefined`.
55
62
 
56
63
  @example
57
64
  ```
65
+ import got from 'got';
66
+
58
67
  const response = await got('https://sindresorhus.com')
59
68
  .on('downloadProgress', progress => {
60
69
  // Report download progress
@@ -78,6 +87,7 @@ See `got.options.retry` for more information.
78
87
  export interface RequestEvents<T> {
79
88
  on: GotEventFunction<T>;
80
89
  once: GotEventFunction<T>;
90
+ off: GotEventFunction<T>;
81
91
  }
82
92
  export declare type CacheableRequestFunction = (options: string | URL | NativeRequestOptions, cb?: (response: ServerResponse | ResponseLike) => void) => CacheableRequest.Emitter;
83
93
  declare type UrlType = ConstructorParameters<typeof Options>[0];
@@ -8,7 +8,7 @@ import CacheableRequest from 'cacheable-request';
8
8
  import decompressResponse from 'decompress-response';
9
9
  import is from '@sindresorhus/is';
10
10
  import { buffer as getBuffer } from 'get-stream';
11
- import { FormDataEncoder, isFormDataLike } from 'form-data-encoder';
11
+ import { FormDataEncoder, isFormData as isFormDataLike } from 'form-data-encoder';
12
12
  import getBodySize from './utils/get-body-size.js';
13
13
  import isFormData from './utils/is-form-data.js';
14
14
  import proxyEvents from './utils/proxy-events.js';
@@ -20,7 +20,7 @@ import Options from './options.js';
20
20
  import { isResponseOk } from './response.js';
21
21
  import isClientRequest from './utils/is-client-request.js';
22
22
  import isUnixSocketURL from './utils/is-unix-socket-url.js';
23
- import { RequestError, ReadError, MaxRedirectsError, HTTPError, TimeoutError, UploadError, CacheError, } from './errors.js';
23
+ import { RequestError, ReadError, MaxRedirectsError, HTTPError, TimeoutError, UploadError, CacheError, AbortError, } from './errors.js';
24
24
  const supportsBrotli = is.string(process.versions.brotli);
25
25
  const methodsWithoutBody = new Set(['GET', 'HEAD']);
26
26
  const cacheableStore = new WeakableMap();
@@ -41,6 +41,7 @@ export default class Request extends Duplex {
41
41
  // It needs to be zero because we're just proxying the data to another stream
42
42
  highWaterMark: 0,
43
43
  });
44
+ // @ts-expect-error - Ignoring for now.
44
45
  Object.defineProperty(this, 'constructor', {
45
46
  enumerable: true,
46
47
  configurable: true,
@@ -233,6 +234,12 @@ export default class Request extends Duplex {
233
234
  };
234
235
  return;
235
236
  }
237
+ if (this.options.signal?.aborted) {
238
+ this.destroy(new AbortError(this));
239
+ }
240
+ this.options.signal?.addEventListener('abort', () => {
241
+ this.destroy(new AbortError(this));
242
+ });
236
243
  // Important! If you replace `body` in a handler with another stream, make sure it's readable first.
237
244
  // The below is run only once.
238
245
  const { body } = this.options;
@@ -1,4 +1,12 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ /// <reference types="node" />
5
+ /// <reference types="node" />
6
+ /// <reference types="node" />
7
+ /// <reference types="node" />
8
+ /// <reference types="node" />
9
+ /// <reference types="node" />
2
10
  import { Buffer } from 'node:buffer';
3
11
  import { URL, URLSearchParams } from 'node:url';
4
12
  import { checkServerIdentity } from 'node:tls';
@@ -731,6 +739,28 @@ export default class Options {
731
739
  get cookieJar(): PromiseCookieJar | ToughCookieJar | undefined;
732
740
  set cookieJar(value: PromiseCookieJar | ToughCookieJar | undefined);
733
741
  /**
742
+ You can abort the `request` using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
743
+
744
+ *Requires Node.js 16 or later.*
745
+
746
+ @example
747
+ ```
748
+ import got from 'got';
749
+
750
+ const abortController = new AbortController();
751
+
752
+ const request = got('https://httpbin.org/anything', {
753
+ signal: abortController.signal
754
+ });
755
+
756
+ setTimeout(() => {
757
+ abortController.abort();
758
+ }, 100);
759
+ ```
760
+ */
761
+ get signal(): any | undefined;
762
+ set signal(value: any | undefined);
763
+ /**
734
764
  Ignore invalid cookies instead of throwing an error.
735
765
  Only useful when the `cookieJar` option has been set. Not recommended.
736
766
 
@@ -1096,6 +1126,8 @@ export default class Options {
1096
1126
  set setHost(value: boolean);
1097
1127
  get maxHeaderSize(): number | undefined;
1098
1128
  set maxHeaderSize(value: number | undefined);
1129
+ get enableUnixSockets(): boolean;
1130
+ set enableUnixSockets(value: boolean);
1099
1131
  toJSON(): {
1100
1132
  headers: Headers;
1101
1133
  timeout: Delays;
@@ -1112,6 +1144,7 @@ export default class Options {
1112
1144
  form: Record<string, any> | undefined;
1113
1145
  url: string | URL | undefined;
1114
1146
  cookieJar: PromiseCookieJar | ToughCookieJar | undefined;
1147
+ signal: any;
1115
1148
  ignoreInvalidCookies: boolean;
1116
1149
  searchParams: string | SearchParameters | URLSearchParams | undefined;
1117
1150
  dnsLookup: {
@@ -1147,6 +1180,7 @@ export default class Options {
1147
1180
  pagination: PaginationOptions<unknown, unknown>;
1148
1181
  setHost: boolean;
1149
1182
  maxHeaderSize: number | undefined;
1183
+ enableUnixSockets: boolean;
1150
1184
  };
1151
1185
  createNativeRequestOptions(): {
1152
1186
  ALPNProtocols: string[] | undefined;
@@ -9,9 +9,9 @@ import is, { assert } from '@sindresorhus/is';
9
9
  import lowercaseKeys from 'lowercase-keys';
10
10
  import CacheableLookup from 'cacheable-lookup';
11
11
  import http2wrapper from 'http2-wrapper';
12
- import { isFormDataLike } from 'form-data-encoder';
12
+ import { isFormData } from 'form-data-encoder';
13
13
  import parseLinkHeader from './parse-link-header.js';
14
- const [major, minor] = process.versions.node.split('.').map(v => Number(v));
14
+ const [major, minor] = process.versions.node.split('.').map(Number);
15
15
  function validateSearchParameters(searchParameters) {
16
16
  // eslint-disable-next-line guard-for-in
17
17
  for (const key in searchParameters) {
@@ -180,6 +180,8 @@ const defaultInternals = {
180
180
  },
181
181
  setHost: true,
182
182
  maxHeaderSize: undefined,
183
+ signal: undefined,
184
+ enableUnixSockets: true,
183
185
  };
184
186
  const cloneInternals = (internals) => {
185
187
  const { hooks, retry } = internals;
@@ -607,7 +609,7 @@ export default class Options {
607
609
  return this._internals.body;
608
610
  }
609
611
  set body(value) {
610
- assert.any([is.string, is.buffer, is.nodeStream, is.generator, is.asyncGenerator, isFormDataLike, is.undefined], value);
612
+ assert.any([is.string, is.buffer, is.nodeStream, is.generator, is.asyncGenerator, isFormData, is.undefined], value);
611
613
  if (is.nodeStream(value)) {
612
614
  assert.truthy(value.readable);
613
615
  }
@@ -709,6 +711,9 @@ export default class Options {
709
711
  this._internals.searchParams = undefined;
710
712
  }
711
713
  if (url.hostname === 'unix') {
714
+ if (!this._internals.enableUnixSockets) {
715
+ throw new Error('Using UNIX domain sockets but option `enableUnixSockets` is not enabled');
716
+ }
712
717
  const matches = /(?<socketPath>.+?):(?<path>.+)/.exec(`${url.pathname}${url.search}`);
713
718
  if (matches?.groups) {
714
719
  const { socketPath, path } = matches.groups;
@@ -756,6 +761,35 @@ export default class Options {
756
761
  }
757
762
  }
758
763
  /**
764
+ You can abort the `request` using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
765
+
766
+ *Requires Node.js 16 or later.*
767
+
768
+ @example
769
+ ```
770
+ import got from 'got';
771
+
772
+ const abortController = new AbortController();
773
+
774
+ const request = got('https://httpbin.org/anything', {
775
+ signal: abortController.signal
776
+ });
777
+
778
+ setTimeout(() => {
779
+ abortController.abort();
780
+ }, 100);
781
+ ```
782
+ */
783
+ // TODO: Replace `any` with `AbortSignal` when targeting Node 16.
784
+ get signal() {
785
+ return this._internals.signal;
786
+ }
787
+ // TODO: Replace `any` with `AbortSignal` when targeting Node 16.
788
+ set signal(value) {
789
+ assert.object(value);
790
+ this._internals.signal = value;
791
+ }
792
+ /**
759
793
  Ignore invalid cookies instead of throwing an error.
760
794
  Only useful when the `cookieJar` option has been set. Not recommended.
761
795
 
@@ -1498,6 +1532,13 @@ export default class Options {
1498
1532
  assert.any([is.number, is.undefined], value);
1499
1533
  this._internals.maxHeaderSize = value;
1500
1534
  }
1535
+ get enableUnixSockets() {
1536
+ return this._internals.enableUnixSockets;
1537
+ }
1538
+ set enableUnixSockets(value) {
1539
+ assert.boolean(value);
1540
+ this._internals.enableUnixSockets = value;
1541
+ }
1501
1542
  // eslint-disable-next-line @typescript-eslint/naming-convention
1502
1543
  toJSON() {
1503
1544
  return { ...this._internals };
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import type { Buffer } from 'node:buffer';
3
4
  import type { URL } from 'node:url';
4
5
  import type { IncomingMessageWithTimings, Timings } from '@szmarczak/http-timer';
@@ -106,4 +107,4 @@ export declare class ParseError extends RequestError {
106
107
  readonly response: Response;
107
108
  constructor(error: Error, response: Response);
108
109
  }
109
- export declare const parseBody: (response: Response, responseType: ResponseType, parseJson: ParseJsonFunction, encoding?: BufferEncoding | undefined) => unknown;
110
+ export declare const parseBody: (response: Response, responseType: ResponseType, parseJson: ParseJsonFunction, encoding?: BufferEncoding) => unknown;
@@ -7,7 +7,7 @@ export * from './core/response.js';
7
7
  export type { default as Request } from './core/index.js';
8
8
  export * from './core/index.js';
9
9
  export * from './core/errors.js';
10
- export { Delays } from './core/timed-out.js';
10
+ export type { Delays } from './core/timed-out.js';
11
11
  export { default as calculateRetryDelay } from './core/calculate-retry-delay.js';
12
12
  export * from './as-promise/types.js';
13
13
  export * from './types.js';
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import type { Buffer } from 'node:buffer';
3
4
  import type { URL } from 'node:url';
4
5
  import type { CancelableRequest } from './as-promise/types.js';
@@ -109,6 +110,8 @@ export interface GotPaginate {
109
110
 
110
111
  @example
111
112
  ```
113
+ import got from 'got';
114
+
112
115
  const countLimit = 10;
113
116
 
114
117
  const pagination = got.paginate('https://api.github.com/repos/sindresorhus/got/commits', {
@@ -130,6 +133,8 @@ export interface GotPaginate {
130
133
 
131
134
  @example
132
135
  ```
136
+ import got from 'got';
137
+
133
138
  const countLimit = 10;
134
139
 
135
140
  const results = await got.paginate.all('https://api.github.com/repos/sindresorhus/got/commits', {
@@ -203,6 +208,8 @@ export interface Got extends Record<HTTPAlias, GotRequestFunction>, GotRequestFu
203
208
 
204
209
  @example
205
210
  ```
211
+ import got from 'got';
212
+
206
213
  const countLimit = 10;
207
214
 
208
215
  const pagination = got.paginate('https://api.github.com/repos/sindresorhus/got/commits', {
@@ -234,6 +241,8 @@ export interface Got extends Record<HTTPAlias, GotRequestFunction>, GotRequestFu
234
241
 
235
242
  @example
236
243
  ```
244
+ import got from 'got';
245
+
237
246
  const client = got.extend({
238
247
  prefixUrl: 'https://example.com',
239
248
  headers: {
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "got",
3
- "version": "12.1.0",
3
+ "version": "12.3.1",
4
4
  "description": "Human-friendly and powerful HTTP request library for Node.js",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/got",
7
7
  "funding": "https://github.com/sindresorhus/got?sponsor=1",
8
8
  "type": "module",
9
9
  "exports": "./dist/source/index.js",
10
+ "types": "./dist/source/index.d.ts",
10
11
  "engines": {
11
12
  "node": ">=14.16"
12
13
  },
@@ -44,14 +45,14 @@
44
45
  "ky"
45
46
  ],
46
47
  "dependencies": {
47
- "@sindresorhus/is": "^4.6.0",
48
+ "@sindresorhus/is": "^5.2.0",
48
49
  "@szmarczak/http-timer": "^5.0.1",
49
50
  "@types/cacheable-request": "^6.0.2",
50
51
  "@types/responselike": "^1.0.0",
51
52
  "cacheable-lookup": "^6.0.4",
52
53
  "cacheable-request": "^7.0.2",
53
54
  "decompress-response": "^6.0.0",
54
- "form-data-encoder": "1.7.1",
55
+ "form-data-encoder": "^2.0.1",
55
56
  "get-stream": "^6.0.1",
56
57
  "http2-wrapper": "^2.1.10",
57
58
  "lowercase-keys": "^3.0.0",
@@ -59,12 +60,12 @@
59
60
  "responselike": "^2.0.0"
60
61
  },
61
62
  "devDependencies": {
62
- "@hapi/bourne": "^2.0.0",
63
+ "@hapi/bourne": "^3.0.0",
63
64
  "@sindresorhus/tsconfig": "^2.0.0",
64
65
  "@sinonjs/fake-timers": "^9.1.1",
65
66
  "@types/benchmark": "^2.1.1",
66
67
  "@types/express": "^4.17.13",
67
- "@types/node": "^17.0.21",
68
+ "@types/node": "^18.0.1",
68
69
  "@types/pem": "^1.9.6",
69
70
  "@types/pify": "^5.0.1",
70
71
  "@types/readable-stream": "^2.3.13",
@@ -73,7 +74,7 @@
73
74
  "@types/sinonjs__fake-timers": "^8.1.1",
74
75
  "@types/tough-cookie": "^4.0.1",
75
76
  "ava": "^3.15.0",
76
- "axios": "^0.26.1",
77
+ "axios": "^0.27.2",
77
78
  "benchmark": "^2.1.4",
78
79
  "bluebird": "^3.7.2",
79
80
  "body-parser": "^1.19.2",
@@ -90,20 +91,19 @@
90
91
  "nyc": "^15.1.0",
91
92
  "p-event": "^5.0.1",
92
93
  "pem": "^1.14.6",
93
- "pify": "^5.0.0",
94
- "readable-stream": "^3.6.0",
94
+ "pify": "^6.0.0",
95
+ "readable-stream": "^4.0.0",
95
96
  "request": "^2.88.2",
96
- "sinon": "^13.0.1",
97
+ "sinon": "^14.0.0",
97
98
  "slow-stream": "0.0.4",
98
- "tempy": "^2.0.0",
99
+ "tempy": "^3.0.0",
99
100
  "then-busboy": "^5.1.1",
100
101
  "to-readable-stream": "^3.0.0",
101
102
  "tough-cookie": "^4.0.0",
102
- "ts-node": "^10.7.0",
103
- "typescript": "4.6.2",
104
- "xo": "^0.48.0"
103
+ "ts-node": "^10.8.2",
104
+ "typescript": "^4.7.4",
105
+ "xo": "^0.50.0"
105
106
  },
106
- "types": "dist/source",
107
107
  "sideEffects": false,
108
108
  "ava": {
109
109
  "files": [
@@ -140,9 +140,9 @@
140
140
  ],
141
141
  "rules": {
142
142
  "@typescript-eslint/no-empty-function": "off",
143
- "node/no-deprecated-api": "off",
144
- "node/prefer-global/url": "off",
145
- "node/prefer-global/url-search-params": "off",
143
+ "n/no-deprecated-api": "off",
144
+ "n/prefer-global/url": "off",
145
+ "n/prefer-global/url-search-params": "off",
146
146
  "@typescript-eslint/no-implicit-any-catch": "off",
147
147
  "unicorn/prefer-node-protocol": "off",
148
148
  "ava/assertion-arguments": "off",
@@ -151,6 +151,7 @@
151
151
  "@typescript-eslint/no-unsafe-assignment": "off",
152
152
  "@typescript-eslint/no-unsafe-call": "off",
153
153
  "@typescript-eslint/await-thenable": "off",
154
+ "@typescript-eslint/no-redundant-type-constituents": "off",
154
155
  "no-lone-blocks": "off",
155
156
  "unicorn/no-await-expression-member": "off"
156
157
  }
package/readme.md CHANGED
@@ -46,41 +46,6 @@
46
46
  <br>
47
47
  <br>
48
48
  <br>
49
- <a href="https://neverinstall.com/spaces/devtools?utm_source=github&utm_medium=sponsor&utm_campaign=sindre#gh-light-mode-only">
50
- <div>
51
- <img src="https://sindresorhus.com/assets/thanks/neverinstall-logo-light.svg" width="200" alt="neverinstall">
52
- </div>
53
- <br>
54
- <b>All your favourite IDE's now available on the cloud</b>
55
- <div>
56
- <sub>
57
- Neverinstall gives you an uninterrupted development experience and improved accessibility,
58
- <br>
59
- allowing you to code faster, better and on-the-go on your favourite IDEs like
60
- <br>
61
- Android Studio, VS Code, Jupyter and PyCharm using your browser.
62
- </sub>
63
- </div>
64
- </a>
65
- <a href="https://neverinstall.com/spaces/devtools?utm_source=github&utm_medium=sponsor&utm_campaign=sindre#gh-dark-mode-only">
66
- <div>
67
- <img src="https://sindresorhus.com/assets/thanks/neverinstall-logo-dark.svg" width="200" alt="neverinstall">
68
- </div>
69
- <br>
70
- <b>All your favourite IDE's now available on the cloud</b>
71
- <div>
72
- <sub>
73
- Neverinstall gives you an uninterrupted development experience and improved accessibility,
74
- <br>
75
- allowing you to code faster, better and on-the-go on your favourite IDEs like
76
- <br>
77
- Android Studio, VS Code, Jupyter and PyCharm using your browser.
78
- </sub>
79
- </div>
80
- </a>
81
- <br>
82
- <br>
83
- <br>
84
49
  <a href="https://www.useanvil.com/?utm_source=sindresorhus#gh-light-mode-only">
85
50
  <div>
86
51
  <img src="https://sindresorhus.com/assets/thanks/anvil-logo-light.svg" width="200" alt="Anvil">
@@ -140,7 +105,7 @@ For browser usage, we recommend [Ky](https://github.com/sindresorhus/ky) by the
140
105
  npm install got
141
106
  ```
142
107
 
143
- **Warning:** This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and no longer provides a CommonJS export. If your project uses CommonJS, you'll have to [convert to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) or use the [dynamic `import()`](https://v8.dev/features/dynamic-import) function. Please don't open issues for questions regarding CommonJS / ESM. You can also use [Got v11](https://github.com/sindresorhus/got/tree/v11.8.3) instead which is pretty stable.
108
+ **Warning:** This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and no longer provides a CommonJS export. If your project uses CommonJS, you'll have to [convert to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) or use the [dynamic `import()`](https://v8.dev/features/dynamic-import) function. Please don't open issues for questions regarding CommonJS / ESM. You can also use [Got v11](https://github.com/sindresorhus/got/tree/v11.8.3) instead which is pretty stable. We will backport security fixes to v11 for the foreseeable future.
144
109
 
145
110
  ## Take a peek
146
111
 
@@ -168,12 +133,14 @@ For advanced JSON usage, check out the [`parseJson`](documentation/2-options.md#
168
133
 
169
134
  ## Highlights
170
135
 
171
- - [Used by 6K+ packages and 3M+ repos](https://github.com/sindresorhus/got/network/dependents)
136
+ - [Used by 8K+ packages and 4M+ repos](https://github.com/sindresorhus/got/network/dependents)
172
137
  - [Actively maintained](https://github.com/sindresorhus/got/graphs/contributors)
173
138
  - [Trusted by many companies](#widely-used)
174
139
 
175
140
  ## Documentation
176
141
 
142
+ By default, Got will retry on failure. To disable this option, set [`options.retry.limit`](documentation/7-retry.md#retry) to 0.
143
+
177
144
  #### Main API
178
145
 
179
146
  - [x] [Promise API](documentation/1-promise.md)
@@ -202,7 +169,7 @@ For advanced JSON usage, check out the [`parseJson`](documentation/2-options.md#
202
169
 
203
170
  - [x] [RFC compliant caching](documentation/cache.md)
204
171
  - [x] [Proxy support](documentation/tips.md#proxying)
205
- - [x] [Unix Domain Sockets](documentation/tips.md#unix)
172
+ - [x] [Unix Domain Sockets](documentation/2-options.md#enableunixsockets)
206
173
 
207
174
  #### Integration
208
175