got 12.0.2 → 12.1.0

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.
@@ -121,8 +121,6 @@ export default class Request extends Duplex implements RequestEvents<Request> {
121
121
  end?: boolean;
122
122
  }): T;
123
123
  unpipe<T extends NodeJS.WritableStream>(destination: T): this;
124
- private _lockWrite;
125
- private _unlockWrite;
126
124
  private _finalizeBody;
127
125
  private _onResponseBase;
128
126
  private _setRawBody;
@@ -19,6 +19,7 @@ import calculateRetryDelay from './calculate-retry-delay.js';
19
19
  import Options from './options.js';
20
20
  import { isResponseOk } from './response.js';
21
21
  import isClientRequest from './utils/is-client-request.js';
22
+ import isUnixSocketURL from './utils/is-unix-socket-url.js';
22
23
  import { RequestError, ReadError, MaxRedirectsError, HTTPError, TimeoutError, UploadError, CacheError, } from './errors.js';
23
24
  const supportsBrotli = is.string(process.versions.brotli);
24
25
  const methodsWithoutBody = new Set(['GET', 'HEAD']);
@@ -201,24 +202,6 @@ export default class Request extends Duplex {
201
202
  this.redirectUrls = [];
202
203
  this.retryCount = 0;
203
204
  this._stopRetry = noop;
204
- const unlockWrite = () => {
205
- this._unlockWrite();
206
- };
207
- const lockWrite = () => {
208
- this._lockWrite();
209
- };
210
- this.on('pipe', (source) => {
211
- source.prependListener('data', unlockWrite);
212
- source.on('data', lockWrite);
213
- source.prependListener('end', unlockWrite);
214
- source.on('end', lockWrite);
215
- });
216
- this.on('unpipe', (source) => {
217
- source.off('data', unlockWrite);
218
- source.off('data', lockWrite);
219
- source.off('end', unlockWrite);
220
- source.off('end', lockWrite);
221
- });
222
205
  this.on('pipe', source => {
223
206
  if (source.headers) {
224
207
  Object.assign(this.options.headers, source.headers);
@@ -250,12 +233,9 @@ export default class Request extends Duplex {
250
233
  };
251
234
  return;
252
235
  }
253
- const { json, body, form } = this.options;
254
- if (json || body || form) {
255
- this._lockWrite();
256
- }
257
236
  // Important! If you replace `body` in a handler with another stream, make sure it's readable first.
258
237
  // The below is run only once.
238
+ const { body } = this.options;
259
239
  if (is.nodeStream(body)) {
260
240
  body.once('error', error => {
261
241
  if (this._flushed) {
@@ -492,17 +472,6 @@ export default class Request extends Duplex {
492
472
  super.unpipe(destination);
493
473
  return this;
494
474
  }
495
- _lockWrite() {
496
- const onLockedWrite = () => {
497
- throw new TypeError('The payload has been already provided');
498
- };
499
- this.write = onLockedWrite;
500
- this.end = onLockedWrite;
501
- }
502
- _unlockWrite() {
503
- this.write = super.write;
504
- this.end = super.end;
505
- }
506
475
  async _finalizeBody() {
507
476
  const { options } = this;
508
477
  const { headers } = options;
@@ -563,12 +532,6 @@ export default class Request extends Duplex {
563
532
  headers['content-length'] = String(uploadBodySize);
564
533
  }
565
534
  }
566
- else if (cannotHaveBody) {
567
- this._lockWrite();
568
- }
569
- else {
570
- this._unlockWrite();
571
- }
572
535
  if (options.responseType === 'json' && !('accept' in options.headers)) {
573
536
  options.headers.accept = 'application/json';
574
537
  }
@@ -595,6 +558,7 @@ export default class Request extends Duplex {
595
558
  typedResponse.isFromCache = this._nativeResponse.fromCache ?? false;
596
559
  typedResponse.ip = this.ip;
597
560
  typedResponse.retryCount = this.retryCount;
561
+ typedResponse.ok = isResponseOk(typedResponse);
598
562
  this._isFromCache = typedResponse.isFromCache;
599
563
  this._responseSize = Number(response.headers['content-length']) || undefined;
600
564
  this.response = typedResponse;
@@ -668,6 +632,10 @@ export default class Request extends Duplex {
668
632
  // We need this in order to support UTF-8
669
633
  const redirectBuffer = Buffer.from(response.headers.location, 'binary').toString();
670
634
  const redirectUrl = new URL(redirectBuffer, url);
635
+ if (!isUnixSocketURL(url) && isUnixSocketURL(redirectUrl)) {
636
+ this._beforeError(new RequestError('Cannot redirect to UNIX socket', {}, this));
637
+ return;
638
+ }
671
639
  // Redirecting to a different site, clear sensitive data.
672
640
  if (redirectUrl.hostname !== url.hostname || redirectUrl.port !== url.port) {
673
641
  if ('host' in updatedOptions.headers) {
@@ -829,17 +797,12 @@ export default class Request extends Duplex {
829
797
  }
830
798
  })();
831
799
  }
832
- else {
833
- this._unlockWrite();
834
- if (!is.undefined(body)) {
835
- this._writeRequest(body, undefined, () => { });
836
- currentRequest.end();
837
- this._lockWrite();
838
- }
839
- else if (this._cannotHaveBody || this._noPipe) {
840
- currentRequest.end();
841
- this._lockWrite();
842
- }
800
+ else if (!is.undefined(body)) {
801
+ this._writeRequest(body, undefined, () => { });
802
+ currentRequest.end();
803
+ }
804
+ else if (this._cannotHaveBody || this._noPipe) {
805
+ currentRequest.end();
843
806
  }
844
807
  }
845
808
  _prepareCache(cache) {
@@ -701,8 +701,8 @@ export default class Options {
701
701
 
702
702
  __Note #2__: This option is not enumerable and will not be merged with the instance defaults.
703
703
  */
704
- get json(): Record<string, any> | undefined;
705
- set json(value: Record<string, any> | undefined);
704
+ get json(): unknown;
705
+ set json(value: unknown);
706
706
  /**
707
707
  The URL to request, as a string, a [`https.request` options object](https://nodejs.org/api/https.html#https_https_request_options_callback), or a [WHATWG `URL`](https://nodejs.org/api/url.html#url_class_url).
708
708
 
@@ -1102,7 +1102,7 @@ export default class Options {
1102
1102
  request: RequestFunction | undefined;
1103
1103
  username: string;
1104
1104
  password: string;
1105
- json: Record<string, any> | undefined;
1105
+ json: unknown;
1106
1106
  retry: Partial<RetryOptions>;
1107
1107
  agent: Agents;
1108
1108
  h2session: http2wrapper.ClientHttp2Session | undefined;
@@ -1152,7 +1152,7 @@ export default class Options {
1152
1152
  ALPNProtocols: string[] | undefined;
1153
1153
  ca: string | Buffer | (string | Buffer)[] | undefined;
1154
1154
  cert: string | Buffer | (string | Buffer)[] | undefined;
1155
- key: string | Buffer | (Buffer | import("tls").KeyObject)[] | undefined;
1155
+ key: string | Buffer | (string | Buffer | import("tls").KeyObject)[] | undefined;
1156
1156
  passphrase: string | undefined;
1157
1157
  pfx: PfxType;
1158
1158
  rejectUnauthorized: boolean | undefined;
@@ -648,7 +648,6 @@ export default class Options {
648
648
  return this._internals.json;
649
649
  }
650
650
  set json(value) {
651
- assert.any([is.object, is.undefined], value);
652
651
  if (value !== undefined) {
653
652
  assert.undefined(this._internals.body);
654
653
  assert.undefined(this._internals.form);
@@ -80,6 +80,12 @@ export interface PlainResponse extends IncomingMessageWithTimings {
80
80
  The result of the request.
81
81
  */
82
82
  body?: unknown;
83
+ /**
84
+ Whether the response was successful.
85
+
86
+ __Note__: Got throws automatically when `response.ok` is `false` and `throwHttpErrors` is `true`.
87
+ */
88
+ ok: boolean;
83
89
  }
84
90
  export interface Response<T = unknown> extends PlainResponse {
85
91
  /**
@@ -0,0 +1,3 @@
1
+ /// <reference types="node" />
2
+ import { URL } from 'url';
3
+ export default function isUnixSocketURL(url: URL): boolean;
@@ -0,0 +1,4 @@
1
+ // eslint-disable-next-line @typescript-eslint/naming-convention
2
+ export default function isUnixSocketURL(url) {
3
+ return url.protocol === 'unix:' || url.hostname === 'unix';
4
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "got",
3
- "version": "12.0.2",
3
+ "version": "12.1.0",
4
4
  "description": "Human-friendly and powerful HTTP request library for Node.js",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/got",
package/readme.md CHANGED
@@ -46,6 +46,72 @@
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
+ <a href="https://www.useanvil.com/?utm_source=sindresorhus#gh-light-mode-only">
85
+ <div>
86
+ <img src="https://sindresorhus.com/assets/thanks/anvil-logo-light.svg" width="200" alt="Anvil">
87
+ </div>
88
+ <br>
89
+ <b>Paperwork that makes the data work.</b>
90
+ <div>
91
+ <sub>
92
+ Easy APIs for paperwork. PDF generation, e-signature and embeddable no-code webforms.
93
+ <br>
94
+ The easiest way to build paperwork automation into your product.
95
+ </sub>
96
+ </div>
97
+ </a>
98
+ <a href="https://www.useanvil.com/?utm_source=sindresorhus#gh-dark-mode-only">
99
+ <div>
100
+ <img src="https://sindresorhus.com/assets/thanks/anvil-logo-dark.svg" width="200" alt="Anvil">
101
+ </div>
102
+ <br>
103
+ <b>Paperwork that makes the data work.</b>
104
+ <div>
105
+ <sub>
106
+ Easy APIs for paperwork. PDF generation, e-signature and embeddable no-code webforms.
107
+ <br>
108
+ The easiest way to build paperwork automation into your product.
109
+ </sub>
110
+ </div>
111
+ </a>
112
+ <br>
113
+ <br>
114
+ <br>
49
115
  <br>
50
116
  </p>
51
117
  <br>