@jsforce/jsforce-node 3.0.0-next.2 → 3.0.0-next.3
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.
- package/lib/request.js +73 -4
- package/lib/types/common.d.ts +6 -1
- package/package.json +1 -1
package/lib/request.js
CHANGED
|
@@ -4,10 +4,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.setDefaults = void 0;
|
|
7
|
+
const stream_1 = require("stream");
|
|
7
8
|
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
8
9
|
const abort_controller_1 = __importDefault(require("abort-controller"));
|
|
9
10
|
const https_proxy_agent_1 = __importDefault(require("https-proxy-agent"));
|
|
10
11
|
const request_helper_1 = require("./request-helper");
|
|
12
|
+
const logger_1 = require("./util/logger");
|
|
13
|
+
const is_1 = __importDefault(require("@sindresorhus/is"));
|
|
11
14
|
/**
|
|
12
15
|
*
|
|
13
16
|
*/
|
|
@@ -23,13 +26,35 @@ exports.setDefaults = setDefaults;
|
|
|
23
26
|
*
|
|
24
27
|
*/
|
|
25
28
|
async function startFetchRequest(request, options, input, output, emitter, counter = 0) {
|
|
29
|
+
const logger = (0, logger_1.getLogger)('fetch');
|
|
26
30
|
const { httpProxy, followRedirect } = options;
|
|
27
31
|
const agent = httpProxy ? (0, https_proxy_agent_1.default)(httpProxy) : undefined;
|
|
28
32
|
const { url, body, ...rrequest } = request;
|
|
29
33
|
const controller = new abort_controller_1.default();
|
|
30
|
-
let
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
let retryCount = 0;
|
|
35
|
+
const retryOpts = {
|
|
36
|
+
maxRetries: options.retry?.maxRetries ?? 5,
|
|
37
|
+
errorCodes: options.retry?.errorCodes ?? [
|
|
38
|
+
'ECONNRESET',
|
|
39
|
+
'ECONNREFUSED',
|
|
40
|
+
'ENOTFOUND',
|
|
41
|
+
'ENETDOWN',
|
|
42
|
+
'ENETUNREACH',
|
|
43
|
+
'EHOSTDOWN',
|
|
44
|
+
'UND_ERR_SOCKET',
|
|
45
|
+
'ETIMEDOUT',
|
|
46
|
+
'EPIPE',
|
|
47
|
+
],
|
|
48
|
+
methods: options.retry?.methods ?? [
|
|
49
|
+
'GET',
|
|
50
|
+
'PUT',
|
|
51
|
+
'HEAD',
|
|
52
|
+
'OPTIONS',
|
|
53
|
+
'DELETE',
|
|
54
|
+
],
|
|
55
|
+
};
|
|
56
|
+
const fetchWithRetries = async (maxRetry = retryOpts?.maxRetries) => {
|
|
57
|
+
const fetchOpts = {
|
|
33
58
|
...rrequest,
|
|
34
59
|
...(input && /^(post|put|patch)$/i.test(request.method)
|
|
35
60
|
? { body: input }
|
|
@@ -37,7 +62,51 @@ async function startFetchRequest(request, options, input, output, emitter, count
|
|
|
37
62
|
redirect: 'manual',
|
|
38
63
|
signal: controller.signal,
|
|
39
64
|
agent,
|
|
40
|
-
}
|
|
65
|
+
};
|
|
66
|
+
try {
|
|
67
|
+
return await (0, node_fetch_1.default)(url, fetchOpts);
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
logger.debug(`Request failed`);
|
|
71
|
+
const error = err;
|
|
72
|
+
// request was canceled by consumer (AbortController), skip retry and rethrow.
|
|
73
|
+
if (error.name === 'AbortError') {
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
const shouldRetry = () => {
|
|
77
|
+
// only retry on operational errors
|
|
78
|
+
if (error.name != 'FetchError')
|
|
79
|
+
return false;
|
|
80
|
+
if (retryCount === maxRetry)
|
|
81
|
+
return false;
|
|
82
|
+
if (!retryOpts?.methods?.includes(request.method))
|
|
83
|
+
return false;
|
|
84
|
+
if (is_1.default.nodeStream(body) && stream_1.Readable.isDisturbed(body)) {
|
|
85
|
+
logger.debug('Body of type stream was read, unable to retry request.');
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
if ('code' in error &&
|
|
89
|
+
error.code &&
|
|
90
|
+
retryOpts?.errorCodes?.includes(error.code))
|
|
91
|
+
return true;
|
|
92
|
+
return false;
|
|
93
|
+
};
|
|
94
|
+
if (shouldRetry()) {
|
|
95
|
+
logger.debug(`retrying for the ${retryCount + 1} time`);
|
|
96
|
+
logger.debug(`Error: ${error}`);
|
|
97
|
+
// NOTE: this event is only used by tests and will be removed at any time.
|
|
98
|
+
// jsforce may switch to node's fetch which doesn't emit this event on retries.
|
|
99
|
+
emitter.emit('retry', retryCount);
|
|
100
|
+
retryCount++;
|
|
101
|
+
return await fetchWithRetries(maxRetry);
|
|
102
|
+
}
|
|
103
|
+
logger.debug('Skipping retry...');
|
|
104
|
+
throw err;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
let res;
|
|
108
|
+
try {
|
|
109
|
+
res = await (0, request_helper_1.executeWithTimeout)(fetchWithRetries, options.timeout, () => controller.abort());
|
|
41
110
|
}
|
|
42
111
|
catch (err) {
|
|
43
112
|
emitter.emit('error', err);
|
package/lib/types/common.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ import * as FormData from 'form-data';
|
|
|
9
9
|
*/
|
|
10
10
|
export type Callback<T, T2 = undefined> = (err: Error | null | undefined, ret?: T, ret2?: T2) => any;
|
|
11
11
|
export type HttpBody = Buffer | URLSearchParams | NodeJS.ReadableStream | string | FormData | null;
|
|
12
|
-
export type HttpMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
|
|
12
|
+
export type HttpMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
|
|
13
13
|
export type HttpRequest = {
|
|
14
14
|
url: string;
|
|
15
15
|
method: HttpMethods;
|
|
@@ -19,6 +19,11 @@ export type HttpRequest = {
|
|
|
19
19
|
body?: HttpBody;
|
|
20
20
|
};
|
|
21
21
|
export type HttpRequestOptions = {
|
|
22
|
+
retry?: {
|
|
23
|
+
maxRetries?: number;
|
|
24
|
+
errorCodes?: string[];
|
|
25
|
+
methods?: HttpMethods[];
|
|
26
|
+
};
|
|
22
27
|
httpProxy?: string;
|
|
23
28
|
timeout?: number;
|
|
24
29
|
followRedirect?: boolean | ((redirectUrl: string) => HttpRequest | null);
|