got 14.5.0 → 14.6.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.
@@ -39,11 +39,37 @@ export type PromiseCookieJar = {
39
39
  getCookieString: (url: string) => Promise<string>;
40
40
  setCookie: (rawCookie: string, url: string) => Promise<unknown>;
41
41
  };
42
+ /**
43
+ Utility type to override specific properties in a type.
44
+
45
+ Uses `Omit` to remove properties before adding them back to ensure proper type replacement rather than intersection, which handles edge cases with optional/required properties correctly.
46
+ */
47
+ type OverrideProperties<T, U> = Omit<T, keyof U> & U;
48
+ /**
49
+ Represents the runtime state of Options as seen by hooks after normalization.
50
+
51
+ Some Options properties accept multiple input types but are normalized to a single type internally by the Options class setters. This type reflects the actual runtime types that hooks receive, ensuring type safety when accessing options within hook functions.
52
+ */
53
+ export type NormalizedOptions = OverrideProperties<Options, {
54
+ url: URL | undefined;
55
+ dnsCache: CacheableLookup | undefined;
56
+ cache: StorageAdapter | undefined;
57
+ prefixUrl: string;
58
+ }>;
42
59
  export type InitHook = (init: OptionsInit, self: Options) => void;
43
- export type BeforeRequestHook = (options: Options) => Promisable<void | Response | ResponseLike>;
44
- export type BeforeRedirectHook = (updatedOptions: Options, plainResponse: PlainResponse) => Promisable<void>;
45
- export type BeforeErrorHook = (error: RequestError) => Promisable<RequestError>;
60
+ export type BeforeRequestHookContext = {
61
+ /**
62
+ The current retry count.
63
+
64
+ It will be `0` for the initial request and increment for each retry.
65
+ */
66
+ retryCount: number;
67
+ };
68
+ export type BeforeRequestHook = (options: NormalizedOptions, context: BeforeRequestHookContext) => Promisable<void | Response | ResponseLike>;
69
+ export type BeforeRedirectHook = (updatedOptions: NormalizedOptions, plainResponse: PlainResponse) => Promisable<void>;
70
+ export type BeforeErrorHook = (error: RequestError) => Promisable<Error>;
46
71
  export type BeforeRetryHook = (error: RequestError, retryCount: number) => Promisable<void>;
72
+ export type BeforeCacheHook = (response: PlainResponse) => false | void;
47
73
  export type AfterResponseHook<ResponseType = unknown> = (response: Response<ResponseType>, retryWithMergedOptions: (options: OptionsInit) => never) => Promisable<Response | CancelableRequest<Response>>;
48
74
  /**
49
75
  All available hooks of Got.
@@ -142,6 +168,8 @@ export type Hooks = {
142
168
  /**
143
169
  Called right before making the request with `options.createNativeRequestOptions()`.
144
170
 
171
+ The second argument is a context object containing request state information.
172
+
145
173
  This hook is especially useful in conjunction with `got.extend()` when you want to sign your request.
146
174
 
147
175
  @default []
@@ -162,7 +190,7 @@ export type Hooks = {
162
190
  json: {payload: 'old'},
163
191
  hooks: {
164
192
  beforeRequest: [
165
- options => {
193
+ (options, context) => {
166
194
  options.body = JSON.stringify({payload: 'new'});
167
195
  options.headers['content-length'] = options.body.length.toString();
168
196
  }
@@ -172,6 +200,28 @@ export type Hooks = {
172
200
  );
173
201
  ```
174
202
 
203
+ **Example using `context.retryCount`:**
204
+
205
+ ```
206
+ import got from 'got';
207
+
208
+ await got('https://httpbin.org/status/500', {
209
+ retry: {
210
+ limit: 2
211
+ },
212
+ hooks: {
213
+ beforeRequest: [
214
+ (options, context) => {
215
+ // Only log on the initial request, not on retries
216
+ if (context.retryCount === 0) {
217
+ console.log('Making initial request');
218
+ }
219
+ }
220
+ ]
221
+ }
222
+ });
223
+ ```
224
+
175
225
  **Tip:**
176
226
  > - You can indirectly override the `request` function by early returning a [`ClientRequest`-like](https://nodejs.org/api/http.html#http_class_http_clientrequest) instance or a [`IncomingMessage`-like](https://nodejs.org/api/http.html#http_class_http_incomingmessage) instance. This is very useful when creating a custom cache mechanism.
177
227
  > - [Read more about this tip](https://github.com/sindresorhus/got/blob/main/documentation/cache.md#advanced-caching-mechanisms).
@@ -206,13 +256,20 @@ export type Hooks = {
206
256
  /**
207
257
  Called with a `RequestError` instance. The error is passed to the hook right before it's thrown.
208
258
 
209
- This is especially useful when you want to have more detailed errors.
259
+ This hook can return any `Error` instance, allowing you to:
260
+ - Return custom error classes for better error handling in your application
261
+ - Extend `RequestError` with additional properties
262
+ - Return plain `Error` instances when you don't need Got-specific error information
263
+
264
+ This is especially useful when you want to have more detailed errors or maintain backward compatibility with existing error handling code.
210
265
 
211
266
  @default []
212
267
 
268
+ @example
213
269
  ```
214
270
  import got from 'got';
215
271
 
272
+ // Modify and return the error
216
273
  await got('https://api.github.com/repos/sindresorhus/got/commits', {
217
274
  responseType: 'json',
218
275
  hooks: {
@@ -229,6 +286,29 @@ export type Hooks = {
229
286
  ]
230
287
  }
231
288
  });
289
+
290
+ // Return a custom error class
291
+ class CustomAPIError extends Error {
292
+ constructor(message, statusCode) {
293
+ super(message);
294
+ this.name = 'CustomAPIError';
295
+ this.statusCode = statusCode;
296
+ }
297
+ }
298
+
299
+ await got('https://api.example.com/endpoint', {
300
+ hooks: {
301
+ beforeError: [
302
+ error => {
303
+ // Return a custom error for backward compatibility with your application
304
+ return new CustomAPIError(
305
+ error.message,
306
+ error.response?.statusCode
307
+ );
308
+ }
309
+ ]
310
+ }
311
+ });
232
312
  ```
233
313
  */
234
314
  beforeError: BeforeErrorHook[];
@@ -266,6 +346,72 @@ export type Hooks = {
266
346
  */
267
347
  beforeRetry: BeforeRetryHook[];
268
348
  /**
349
+ Called right before the response is cached. Allows you to control caching behavior by directly modifying the response or preventing caching.
350
+
351
+ This is especially useful when you want to prevent caching of specific responses or modify cache headers.
352
+
353
+ @default []
354
+
355
+ **Return value:**
356
+ > - `false` - Prevent caching (remaining hooks are skipped)
357
+ > - `void`/`undefined` - Use default caching behavior (mutations take effect)
358
+
359
+ **Modifying the response:**
360
+ > - Hooks can directly mutate response properties like `headers`, `statusCode`, and `statusMessage`
361
+ > - Mutations to `response.headers` affect how the caching layer decides whether to cache the response and for how long
362
+ > - Changes are applied to what gets cached
363
+
364
+ **Note:**
365
+ > - This hook is only called when the `cache` option is enabled.
366
+
367
+ **Note:**
368
+ > - This hook must be synchronous. It cannot return a Promise. If you need async logic to determine caching behavior, use a `beforeRequest` hook instead.
369
+
370
+ **Note:**
371
+ > - When returning `false`, remaining hooks are skipped and the response will not be cached.
372
+
373
+ **Note:**
374
+ > - Returning anything other than `false` or `undefined` will throw a TypeError.
375
+
376
+ **Note:**
377
+ > - If a hook throws an error, it will be propagated and the request will fail. This is consistent with how other hooks in Got handle errors.
378
+
379
+ **Note:**
380
+ > - At this stage, the response body has not been read yet - it's still a stream. Properties like `response.body` and `response.rawBody` are not available. You can only inspect/modify response headers and status code.
381
+
382
+ @example
383
+ ```
384
+ import got from 'got';
385
+
386
+ // Simple: Don't cache errors
387
+ const instance = got.extend({
388
+ cache: new Map(),
389
+ hooks: {
390
+ beforeCache: [
391
+ (response) => response.statusCode >= 400 ? false : undefined
392
+ ]
393
+ }
394
+ });
395
+
396
+ // Advanced: Modify headers for fine control
397
+ const instance2 = got.extend({
398
+ cache: new Map(),
399
+ hooks: {
400
+ beforeCache: [
401
+ (response) => {
402
+ // Force caching with explicit duration
403
+ // Mutations work directly - no need to return
404
+ response.headers['cache-control'] = 'public, max-age=3600';
405
+ }
406
+ ]
407
+ }
408
+ });
409
+
410
+ await instance('https://example.com');
411
+ ```
412
+ */
413
+ beforeCache: BeforeCacheHook[];
414
+ /**
269
415
  Each function should return the response. This is especially useful when you want to refresh an access token.
270
416
 
271
417
  @default []
@@ -487,6 +633,27 @@ export type HttpsOptions = {
487
633
  dhparam?: SecureContextOptions['dhparam'];
488
634
  ecdhCurve?: SecureContextOptions['ecdhCurve'];
489
635
  certificateRevocationLists?: SecureContextOptions['crl'];
636
+ /**
637
+ Optionally affect the OpenSSL protocol behavior, which is not usually necessary. This should be used carefully if at all!
638
+
639
+ The value is a numeric bitmask of the `SSL_OP_*` options from OpenSSL.
640
+
641
+ For example, to allow connections to legacy servers that do not support secure renegotiation, you can use `crypto.constants.SSL_OP_LEGACY_SERVER_CONNECT`.
642
+
643
+ @example
644
+ ```
645
+ import crypto from 'node:crypto';
646
+ import got from 'got';
647
+
648
+ // Allow connections to servers with legacy renegotiation
649
+ await got('https://legacy-server.com', {
650
+ https: {
651
+ secureOptions: crypto.constants.SSL_OP_LEGACY_SERVER_CONNECT
652
+ }
653
+ });
654
+ ```
655
+ */
656
+ secureOptions?: number;
490
657
  };
491
658
  export type PaginateData<BodyType, ElementType> = {
492
659
  response: Response<BodyType>;
@@ -735,7 +902,7 @@ export default class Options {
735
902
 
736
903
  __Note #4__: This option is not enumerable and will not be merged with the instance defaults.
737
904
 
738
- The `content-length` header will be automatically set if `body` is a `string` / `Buffer` / [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) / [`form-data` instance](https://github.com/form-data/form-data), and `content-length` and `transfer-encoding` are not manually set in `options.headers`.
905
+ The `content-length` header will be automatically set if `body` is a `string` / `Buffer` / typed array ([`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array), etc.) / [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) / [`form-data` instance](https://github.com/form-data/form-data), and `content-length` and `transfer-encoding` are not manually set in `options.headers`.
739
906
 
740
907
  Since Got 12, the `content-length` is not automatically set when `body` is a `fs.createReadStream`.
741
908
 
@@ -756,8 +923,8 @@ export default class Options {
756
923
  });
757
924
  ```
758
925
  */
759
- get body(): string | Buffer | Readable | Generator | AsyncGenerator | Iterable<unknown> | AsyncIterable<unknown> | FormDataLike | undefined;
760
- set body(value: string | Buffer | Readable | Generator | AsyncGenerator | Iterable<unknown> | AsyncIterable<unknown> | FormDataLike | undefined);
926
+ get body(): string | Buffer | Readable | Generator | AsyncGenerator | Iterable<unknown> | AsyncIterable<unknown> | FormDataLike | ArrayBufferView | undefined;
927
+ set body(value: string | Buffer | Readable | Generator | AsyncGenerator | Iterable<unknown> | AsyncIterable<unknown> | FormDataLike | ArrayBufferView | undefined);
761
928
  /**
762
929
  The form body is converted to a query string using [`(new URLSearchParams(object)).toString()`](https://nodejs.org/api/url.html#url_constructor_new_urlsearchparams_obj).
763
930
 
@@ -770,7 +937,9 @@ export default class Options {
770
937
  get form(): Record<string, any> | undefined;
771
938
  set form(value: Record<string, any> | undefined);
772
939
  /**
773
- JSON body. If the `Content-Type` header is not set, it will be set to `application/json`.
940
+ JSON request body. If the `content-type` header is not set, it will be set to `application/json`.
941
+
942
+ __Important__: This option only affects the request body you send to the server. To parse the response as JSON, you must either call `.json()` on the promise or set `responseType: 'json'` in the options.
774
943
 
775
944
  __Note #1__: If you provide this option, `got.stream()` will be read-only.
776
945
 
@@ -985,6 +1154,57 @@ export default class Options {
985
1154
  get allowGetBody(): boolean;
986
1155
  set allowGetBody(value: boolean);
987
1156
  /**
1157
+ Automatically copy headers from piped streams.
1158
+
1159
+ When piping a request into a Got stream (e.g., `request.pipe(got.stream(url))`), this controls whether headers from the source stream are automatically merged into the Got request headers.
1160
+
1161
+ Note: Piped headers overwrite any explicitly set headers with the same name. To override this, either set `copyPipedHeaders` to `false` and manually copy safe headers, or use a `beforeRequest` hook to force specific header values after piping.
1162
+
1163
+ Useful for proxy scenarios, but you may want to disable this to filter out headers like `Host`, `Connection`, `Authorization`, etc.
1164
+
1165
+ @default true
1166
+
1167
+ @example
1168
+ ```
1169
+ import got from 'got';
1170
+ import {pipeline} from 'node:stream/promises';
1171
+
1172
+ // Disable automatic header copying and manually copy only safe headers
1173
+ server.get('/proxy', async (request, response) => {
1174
+ const gotStream = got.stream('https://example.com', {
1175
+ copyPipedHeaders: false,
1176
+ headers: {
1177
+ 'user-agent': request.headers['user-agent'],
1178
+ 'accept': request.headers['accept'],
1179
+ // Explicitly NOT copying host, connection, authorization, etc.
1180
+ }
1181
+ });
1182
+
1183
+ await pipeline(request, gotStream, response);
1184
+ });
1185
+ ```
1186
+
1187
+ @example
1188
+ ```
1189
+ import got from 'got';
1190
+
1191
+ // Override piped headers using beforeRequest hook
1192
+ const gotStream = got.stream('https://example.com', {
1193
+ hooks: {
1194
+ beforeRequest: [
1195
+ options => {
1196
+ // Force specific header values after piping
1197
+ options.headers.host = 'example.com';
1198
+ delete options.headers.authorization;
1199
+ }
1200
+ ]
1201
+ }
1202
+ });
1203
+ ```
1204
+ */
1205
+ get copyPipedHeaders(): boolean;
1206
+ set copyPipedHeaders(value: boolean);
1207
+ /**
988
1208
  Request headers.
989
1209
 
990
1210
  Existing headers will be overwritten. Headers set to `undefined` will be omitted.
@@ -1202,6 +1422,18 @@ export default class Options {
1202
1422
  set maxHeaderSize(value: number | undefined);
1203
1423
  get enableUnixSockets(): boolean;
1204
1424
  set enableUnixSockets(value: boolean);
1425
+ /**
1426
+ Throw an error if the server response's `content-length` header value doesn't match the number of bytes received.
1427
+
1428
+ This is useful for detecting truncated responses and follows RFC 9112 requirements for message completeness.
1429
+
1430
+ __Note__: Responses without a `content-length` header are not validated.
1431
+ __Note__: When enabled and validation fails, a `ReadError` with code `ERR_HTTP_CONTENT_LENGTH_MISMATCH` will be thrown.
1432
+
1433
+ @default false
1434
+ */
1435
+ get strictContentLength(): boolean;
1436
+ set strictContentLength(value: boolean);
1205
1437
  toJSON(): {
1206
1438
  headers: Headers;
1207
1439
  timeout: Delays;
@@ -1215,7 +1447,7 @@ export default class Options {
1215
1447
  h2session: ClientHttp2Session | undefined;
1216
1448
  decompress: boolean;
1217
1449
  prefixUrl: string | URL;
1218
- body: string | Buffer | Readable | Generator | AsyncGenerator | Iterable<unknown> | AsyncIterable<unknown> | FormDataLike | undefined;
1450
+ body: string | Buffer | Readable | Generator | AsyncGenerator | Iterable<unknown> | AsyncIterable<unknown> | FormDataLike | ArrayBufferView | undefined;
1219
1451
  form: Record<string, any> | undefined;
1220
1452
  url: string | URL | undefined;
1221
1453
  cookieJar: PromiseCookieJar | ToughCookieJar | undefined;
@@ -1231,6 +1463,7 @@ export default class Options {
1231
1463
  throwHttpErrors: boolean;
1232
1464
  http2: boolean;
1233
1465
  allowGetBody: boolean;
1466
+ copyPipedHeaders: boolean;
1234
1467
  methodRewriting: boolean;
1235
1468
  dnsLookupIpVersion: DnsLookupIpVersion;
1236
1469
  parseJson: ParseJsonFunction;
@@ -1248,6 +1481,7 @@ export default class Options {
1248
1481
  setHost: boolean;
1249
1482
  maxHeaderSize: number | undefined;
1250
1483
  enableUnixSockets: boolean;
1484
+ strictContentLength: boolean;
1251
1485
  };
1252
1486
  createNativeRequestOptions(): {
1253
1487
  ALPNProtocols: string[] | undefined;
@@ -1268,6 +1502,7 @@ export default class Options {
1268
1502
  dhparam: string | Buffer<ArrayBufferLike> | undefined;
1269
1503
  ecdhCurve: string | undefined;
1270
1504
  crl: string | Buffer<ArrayBufferLike> | (string | Buffer<ArrayBufferLike>)[] | undefined;
1505
+ secureOptions: number | undefined;
1271
1506
  lookup: {
1272
1507
  (hostname: string, family: import("cacheable-lookup").IPFamily, callback: (error: NodeJS.ErrnoException | null, address: string, family: import("cacheable-lookup").IPFamily) => void): void;
1273
1508
  (hostname: string, callback: (error: NodeJS.ErrnoException | null, address: string, family: import("cacheable-lookup").IPFamily) => void): void;
@@ -1314,7 +1549,6 @@ export default class Options {
1314
1549
  clientCertEngine?: string | undefined;
1315
1550
  privateKeyEngine?: string | undefined;
1316
1551
  privateKeyIdentifier?: string | undefined;
1317
- secureOptions?: number | undefined;
1318
1552
  secureProtocol?: string | undefined;
1319
1553
  sessionIdContext?: string | undefined;
1320
1554
  ticketKeys?: Buffer | undefined;