got 10.5.7 → 11.0.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.
Files changed (60) hide show
  1. package/dist/source/{calculate-retry-delay.d.ts → as-promise/calculate-retry-delay.d.ts} +0 -0
  2. package/dist/source/{calculate-retry-delay.js → as-promise/calculate-retry-delay.js} +6 -7
  3. package/dist/source/as-promise/core.d.ts +13 -0
  4. package/dist/source/as-promise/core.js +124 -0
  5. package/dist/source/as-promise/create-rejection.d.ts +2 -0
  6. package/dist/source/as-promise/create-rejection.js +30 -0
  7. package/dist/source/as-promise/index.d.ts +5 -0
  8. package/dist/source/as-promise/index.js +203 -0
  9. package/dist/source/as-promise/types.d.ts +77 -0
  10. package/dist/source/as-promise/types.js +27 -0
  11. package/dist/source/core/index.d.ts +272 -0
  12. package/dist/source/core/index.js +1091 -0
  13. package/dist/source/{utils → core/utils}/get-body-size.d.ts +0 -0
  14. package/dist/source/{utils → core/utils}/get-body-size.js +0 -0
  15. package/dist/source/core/utils/is-form-data.d.ts +8 -0
  16. package/dist/source/{utils → core/utils}/is-form-data.js +0 -0
  17. package/dist/source/core/utils/options-to-url.d.ts +15 -0
  18. package/dist/source/{utils → core/utils}/options-to-url.js +5 -34
  19. package/dist/source/core/utils/proxy-events.d.ts +3 -0
  20. package/dist/source/core/utils/proxy-events.js +17 -0
  21. package/dist/source/{utils → core/utils}/timed-out.d.ts +0 -0
  22. package/dist/source/{utils → core/utils}/timed-out.js +2 -5
  23. package/dist/source/{utils → core/utils}/unhandle.d.ts +1 -1
  24. package/dist/source/{utils → core/utils}/unhandle.js +0 -0
  25. package/dist/source/{utils → core/utils}/url-to-options.d.ts +0 -0
  26. package/dist/source/{utils → core/utils}/url-to-options.js +0 -0
  27. package/dist/source/core/utils/weakable-map.d.ts +8 -0
  28. package/dist/source/core/utils/weakable-map.js +29 -0
  29. package/dist/source/create.d.ts +3 -79
  30. package/dist/source/create.js +83 -39
  31. package/dist/source/index.d.ts +3 -6
  32. package/dist/source/index.js +13 -19
  33. package/dist/source/types.d.ts +84 -198
  34. package/dist/source/types.js +0 -1
  35. package/package.json +41 -37
  36. package/readme.md +211 -97
  37. package/dist/source/as-promise.d.ts +0 -3
  38. package/dist/source/as-promise.js +0 -152
  39. package/dist/source/as-stream.d.ts +0 -7
  40. package/dist/source/as-stream.js +0 -121
  41. package/dist/source/errors.d.ts +0 -41
  42. package/dist/source/errors.js +0 -103
  43. package/dist/source/get-response.d.ts +0 -6
  44. package/dist/source/get-response.js +0 -25
  45. package/dist/source/known-hook-events.d.ts +0 -88
  46. package/dist/source/known-hook-events.js +0 -11
  47. package/dist/source/normalize-arguments.d.ts +0 -19
  48. package/dist/source/normalize-arguments.js +0 -436
  49. package/dist/source/progress.d.ts +0 -4
  50. package/dist/source/progress.js +0 -40
  51. package/dist/source/request-as-event-emitter.d.ts +0 -12
  52. package/dist/source/request-as-event-emitter.js +0 -284
  53. package/dist/source/utils/dynamic-require.d.ts +0 -3
  54. package/dist/source/utils/dynamic-require.js +0 -4
  55. package/dist/source/utils/is-form-data.d.ts +0 -3
  56. package/dist/source/utils/merge.d.ts +0 -6
  57. package/dist/source/utils/merge.js +0 -35
  58. package/dist/source/utils/options-to-url.d.ts +0 -19
  59. package/dist/source/utils/supports-brotli.d.ts +0 -2
  60. package/dist/source/utils/supports-brotli.js +0 -4
@@ -1,284 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const fs_1 = require("fs");
4
- const CacheableRequest = require("cacheable-request");
5
- const EventEmitter = require("events");
6
- const http = require("http");
7
- const stream = require("stream");
8
- const url_1 = require("url");
9
- const util_1 = require("util");
10
- const is_1 = require("@sindresorhus/is");
11
- const http_timer_1 = require("@szmarczak/http-timer");
12
- const calculate_retry_delay_1 = require("./calculate-retry-delay");
13
- const errors_1 = require("./errors");
14
- const get_response_1 = require("./get-response");
15
- const normalize_arguments_1 = require("./normalize-arguments");
16
- const progress_1 = require("./progress");
17
- const timed_out_1 = require("./utils/timed-out");
18
- const types_1 = require("./types");
19
- const url_to_options_1 = require("./utils/url-to-options");
20
- const pEvent = require("p-event");
21
- const setImmediateAsync = async () => new Promise(resolve => setImmediate(resolve));
22
- const pipeline = util_1.promisify(stream.pipeline);
23
- const redirectCodes = new Set([300, 301, 302, 303, 304, 307, 308]);
24
- exports.default = (options) => {
25
- const emitter = new EventEmitter();
26
- const requestUrl = options.url.toString();
27
- const redirects = [];
28
- let retryCount = 0;
29
- let currentRequest;
30
- // `request.aborted` is a boolean since v11.0.0: https://github.com/nodejs/node/commit/4b00c4fafaa2ae8c41c1f78823c0feb810ae4723#diff-e3bc37430eb078ccbafe3aa3b570c91a
31
- const isAborted = () => typeof currentRequest.aborted === 'number' || currentRequest.aborted;
32
- const emitError = async (error) => {
33
- try {
34
- for (const hook of options.hooks.beforeError) {
35
- // eslint-disable-next-line no-await-in-loop
36
- error = await hook(error);
37
- }
38
- emitter.emit('error', error);
39
- }
40
- catch (error_) {
41
- emitter.emit('error', error_);
42
- }
43
- };
44
- const get = async () => {
45
- let httpOptions = await normalize_arguments_1.normalizeRequestArguments(options);
46
- const handleResponse = async (response) => {
47
- var _a;
48
- try {
49
- /* istanbul ignore next: fixes https://github.com/electron/electron/blob/cbb460d47628a7a146adf4419ed48550a98b2923/lib/browser/api/net.js#L59-L65 */
50
- if (options.useElectronNet) {
51
- response = new Proxy(response, {
52
- get: (target, name) => {
53
- if (name === 'trailers' || name === 'rawTrailers') {
54
- return [];
55
- }
56
- const value = target[name];
57
- return is_1.default.function_(value) ? value.bind(target) : value;
58
- }
59
- });
60
- }
61
- const typedResponse = response;
62
- const { statusCode } = typedResponse;
63
- typedResponse.statusMessage = is_1.default.nonEmptyString(typedResponse.statusMessage) ? typedResponse.statusMessage : http.STATUS_CODES[statusCode];
64
- typedResponse.url = options.url.toString();
65
- typedResponse.requestUrl = requestUrl;
66
- typedResponse.retryCount = retryCount;
67
- typedResponse.redirectUrls = redirects;
68
- typedResponse.request = { options };
69
- typedResponse.isFromCache = (_a = typedResponse.fromCache, (_a !== null && _a !== void 0 ? _a : false));
70
- delete typedResponse.fromCache;
71
- if (!typedResponse.isFromCache) {
72
- typedResponse.ip = response.socket.remoteAddress;
73
- }
74
- const rawCookies = typedResponse.headers['set-cookie'];
75
- if (Reflect.has(options, 'cookieJar') && rawCookies) {
76
- let promises = rawCookies.map(async (rawCookie) => options.cookieJar.setCookie(rawCookie, typedResponse.url));
77
- if (options.ignoreInvalidCookies) {
78
- promises = promises.map(async (p) => p.catch(() => { }));
79
- }
80
- await Promise.all(promises);
81
- }
82
- if (options.followRedirect && Reflect.has(typedResponse.headers, 'location') && redirectCodes.has(statusCode)) {
83
- typedResponse.resume(); // We're being redirected, we don't care about the response.
84
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-boolean-literal-compare
85
- if (statusCode === 303 || options.methodRewriting === false) {
86
- if (options.method !== 'GET' && options.method !== 'HEAD') {
87
- // Server responded with "see other", indicating that the resource exists at another location,
88
- // and the client should request it from that location via GET or HEAD.
89
- options.method = 'GET';
90
- }
91
- if (Reflect.has(options, 'body')) {
92
- delete options.body;
93
- }
94
- if (Reflect.has(options, 'json')) {
95
- delete options.json;
96
- }
97
- if (Reflect.has(options, 'form')) {
98
- delete options.form;
99
- }
100
- }
101
- if (redirects.length >= options.maxRedirects) {
102
- throw new errors_1.MaxRedirectsError(typedResponse, options.maxRedirects, options);
103
- }
104
- // Handles invalid URLs. See https://github.com/sindresorhus/got/issues/604
105
- const redirectBuffer = Buffer.from(typedResponse.headers.location, 'binary').toString();
106
- const redirectUrl = new url_1.URL(redirectBuffer, options.url);
107
- // Redirecting to a different site, clear cookies.
108
- if (redirectUrl.hostname !== options.url.hostname && Reflect.has(options.headers, 'cookie')) {
109
- delete options.headers.cookie;
110
- }
111
- redirects.push(redirectUrl.toString());
112
- options.url = redirectUrl;
113
- for (const hook of options.hooks.beforeRedirect) {
114
- // eslint-disable-next-line no-await-in-loop
115
- await hook(options, typedResponse);
116
- }
117
- emitter.emit('redirect', response, options);
118
- await get();
119
- return;
120
- }
121
- await get_response_1.default(typedResponse, options, emitter);
122
- }
123
- catch (error) {
124
- emitError(error);
125
- }
126
- };
127
- const handleRequest = async (request) => {
128
- let isPiped = false;
129
- let isFinished = false;
130
- // `request.finished` doesn't indicate whether this has been emitted or not
131
- request.once('finish', () => {
132
- isFinished = true;
133
- });
134
- currentRequest = request;
135
- const onError = (error) => {
136
- if (error instanceof timed_out_1.TimeoutError) {
137
- error = new errors_1.TimeoutError(error, request.timings, options);
138
- }
139
- else {
140
- error = new errors_1.RequestError(error, options);
141
- }
142
- if (!emitter.retry(error)) {
143
- emitError(error);
144
- }
145
- };
146
- request.on('error', error => {
147
- if (isPiped) {
148
- // Check if it's caught by `stream.pipeline(...)`
149
- if (!isFinished) {
150
- return;
151
- }
152
- // We need to let `TimedOutTimeoutError` through, because `stream.pipeline(…)` aborts the request automatically.
153
- if (isAborted() && !(error instanceof timed_out_1.TimeoutError)) {
154
- return;
155
- }
156
- }
157
- onError(error);
158
- });
159
- try {
160
- http_timer_1.default(request);
161
- timed_out_1.default(request, options.timeout, options.url);
162
- emitter.emit('request', request);
163
- const uploadStream = progress_1.createProgressStream('uploadProgress', emitter, httpOptions.headers['content-length']);
164
- isPiped = true;
165
- await pipeline(httpOptions.body, uploadStream, request);
166
- request.emit('upload-complete');
167
- }
168
- catch (error) {
169
- if (isAborted() && error.message === 'Premature close') {
170
- // The request was aborted on purpose
171
- return;
172
- }
173
- onError(error);
174
- }
175
- };
176
- if (options.cache) {
177
- // `cacheable-request` doesn't support Node 10 API, fallback.
178
- httpOptions = {
179
- ...httpOptions,
180
- ...url_to_options_1.default(options.url)
181
- };
182
- // @ts-ignore `cacheable-request` has got invalid types
183
- const cacheRequest = options.cacheableRequest(httpOptions, handleResponse);
184
- cacheRequest.once('error', (error) => {
185
- if (error instanceof CacheableRequest.RequestError) {
186
- emitError(new errors_1.RequestError(error, options));
187
- }
188
- else {
189
- emitError(new errors_1.CacheError(error, options));
190
- }
191
- });
192
- cacheRequest.once('request', handleRequest);
193
- }
194
- else {
195
- // Catches errors thrown by calling `requestFn(…)`
196
- try {
197
- handleRequest(httpOptions[types_1.requestSymbol](options.url, httpOptions, handleResponse));
198
- }
199
- catch (error) {
200
- emitError(new errors_1.RequestError(error, options));
201
- }
202
- }
203
- };
204
- emitter.retry = error => {
205
- let backoff;
206
- retryCount++;
207
- try {
208
- backoff = options.retry.calculateDelay({
209
- attemptCount: retryCount,
210
- retryOptions: options.retry,
211
- error,
212
- computedValue: calculate_retry_delay_1.default({
213
- attemptCount: retryCount,
214
- retryOptions: options.retry,
215
- error,
216
- computedValue: 0
217
- })
218
- });
219
- }
220
- catch (error_) {
221
- emitError(error_);
222
- return false;
223
- }
224
- if (backoff) {
225
- const retry = async (options) => {
226
- try {
227
- for (const hook of options.hooks.beforeRetry) {
228
- // eslint-disable-next-line no-await-in-loop
229
- await hook(options, error, retryCount);
230
- }
231
- await get();
232
- }
233
- catch (error_) {
234
- emitError(error_);
235
- }
236
- };
237
- setTimeout(retry, backoff, { ...options, forceRefresh: true });
238
- return true;
239
- }
240
- return false;
241
- };
242
- emitter.abort = () => {
243
- emitter.prependListener('request', (request) => {
244
- request.abort();
245
- });
246
- if (currentRequest) {
247
- currentRequest.abort();
248
- }
249
- };
250
- (async () => {
251
- try {
252
- if (options.body instanceof fs_1.ReadStream) {
253
- await pEvent(options.body, 'open');
254
- }
255
- // Promises are executed immediately.
256
- // If there were no `setImmediate` here,
257
- // `promise.json()` would have no effect
258
- // as the request would be sent already.
259
- await setImmediateAsync();
260
- for (const hook of options.hooks.beforeRequest) {
261
- // eslint-disable-next-line no-await-in-loop
262
- await hook(options);
263
- }
264
- await get();
265
- }
266
- catch (error) {
267
- emitError(error);
268
- }
269
- })();
270
- return emitter;
271
- };
272
- exports.proxyEvents = (proxy, emitter) => {
273
- const events = [
274
- 'request',
275
- 'redirect',
276
- 'uploadProgress',
277
- 'downloadProgress'
278
- ];
279
- for (const event of events) {
280
- emitter.on(event, (...args) => {
281
- proxy.emit(event, ...args);
282
- });
283
- }
284
- };
@@ -1,3 +0,0 @@
1
- /// <reference types="node" />
2
- declare const _default: (moduleObject: NodeModule, moduleId: string) => unknown;
3
- export default _default;
@@ -1,4 +0,0 @@
1
- "use strict";
2
- /* istanbul ignore file: used for webpack */
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.default = (moduleObject, moduleId) => moduleObject.require(moduleId);
@@ -1,3 +0,0 @@
1
- import FormData = require('form-data');
2
- declare const _default: (body: unknown) => body is FormData;
3
- export default _default;
@@ -1,6 +0,0 @@
1
- import { Merge } from 'type-fest';
2
- export default function merge<Target extends {
3
- [key: string]: any;
4
- }, Source extends {
5
- [key: string]: any;
6
- }>(target: Target, ...sources: Source[]): Merge<Source, Target>;
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const url_1 = require("url");
4
- const is_1 = require("@sindresorhus/is");
5
- function merge(target, ...sources) {
6
- for (const source of sources) {
7
- for (const [key, sourceValue] of Object.entries(source)) {
8
- const targetValue = target[key];
9
- if (is_1.default.urlInstance(targetValue) && is_1.default.string(sourceValue)) {
10
- // @ts-ignore TS doesn't recognise Target accepts string keys
11
- target[key] = new url_1.URL(sourceValue, targetValue);
12
- }
13
- else if (is_1.default.plainObject(sourceValue)) {
14
- if (is_1.default.plainObject(targetValue)) {
15
- // @ts-ignore TS doesn't recognise Target accepts string keys
16
- target[key] = merge({}, targetValue, sourceValue);
17
- }
18
- else {
19
- // @ts-ignore TS doesn't recognise Target accepts string keys
20
- target[key] = merge({}, sourceValue);
21
- }
22
- }
23
- else if (is_1.default.array(sourceValue)) {
24
- // @ts-ignore TS doesn't recognise Target accepts string keys
25
- target[key] = sourceValue.slice();
26
- }
27
- else {
28
- // @ts-ignore TS doesn't recognise Target accepts string keys
29
- target[key] = sourceValue;
30
- }
31
- }
32
- }
33
- return target;
34
- }
35
- exports.default = merge;
@@ -1,19 +0,0 @@
1
- /// <reference types="node" />
2
- import { URL, URLSearchParams } from 'url';
3
- export interface URLOptions {
4
- href?: string;
5
- origin?: string;
6
- protocol?: string;
7
- username?: string;
8
- password?: string;
9
- host?: string;
10
- hostname?: string;
11
- port?: string | number;
12
- pathname?: string;
13
- search?: string;
14
- searchParams?: Record<string, string | number | boolean | null> | URLSearchParams | string;
15
- hash?: string;
16
- path?: string;
17
- }
18
- declare const _default: (options: URLOptions) => URL;
19
- export default _default;
@@ -1,2 +0,0 @@
1
- declare const _default: boolean;
2
- export default _default;
@@ -1,4 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const zlib = require("zlib");
4
- exports.default = typeof zlib.createBrotliDecompress === 'function';