@jsforce/jsforce-node
Version:
Salesforce API Library for JavaScript
203 lines (202 loc) • 8.02 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setDefaults = void 0;
const stream_1 = require("stream");
const undici_1 = require("undici");
const request_helper_1 = require("./request-helper");
const logger_1 = require("./util/logger");
const is_1 = __importDefault(require("@sindresorhus/is"));
/**
*
*/
let defaults = {};
/**
*
*/
function setDefaults(defaults_) {
defaults = defaults_;
}
exports.setDefaults = setDefaults;
/**
*
*/
async function startFetchRequest(request, options, input, output, emitter, counter = 0) {
const logger = (0, logger_1.getLogger)('fetch');
const { httpProxy, followRedirect } = options;
const agent = httpProxy ? new undici_1.ProxyAgent(httpProxy) : undefined;
const { url, body, ...rrequest } = request;
const controller = new AbortController();
let retryCount = 0;
let retry420Count = 0;
const retryOpts = {
statusCodes: options.retry?.statusCodes ?? [420, 429, 500, 502, 503, 504],
maxRetries: options.retry?.maxRetries ?? 5,
minTimeout: options.retry?.minTimeout ?? 500,
timeoutFactor: options.retry?.timeoutFactor ?? 2,
errorCodes: options.retry?.errorCodes ?? [
'ECONNRESET',
'ECONNREFUSED',
'ENOTFOUND',
'ENETDOWN',
'ENETUNREACH',
'EHOSTDOWN',
'UND_ERR_SOCKET',
'ETIMEDOUT',
'EPIPE',
],
methods: options.retry?.methods ?? [
'GET',
'PUT',
'HEAD',
'OPTIONS',
'DELETE',
],
};
const shouldRetryRequest = (maxRetry, resOrErr) => {
if (!retryOpts.methods.includes(request.method))
return false;
if (resOrErr instanceof undici_1.Response) {
// REST API status codes: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/errorcodes.htm
//
// Deleted/expired scratch orgs return 420 and causes a long delay on all requests due to the retry with exponential backoff.
// We still want to retry on 420 (Metadata API requests sometimes return it) so here we'll limit to a maximum of 2 retries.
if (resOrErr.status === 420) {
return retry420Count < 2;
}
else if (retryOpts.statusCodes.includes(resOrErr.status)) {
if (maxRetry === retryCount) {
return false;
}
else {
return true;
}
}
return false;
}
else {
if (maxRetry === retryCount)
return false;
// If the error is not a TypeError, then it's not an operational error and thus we cannot retry.
if (!(resOrErr instanceof TypeError))
return false;
if (is_1.default.nodeStream(body) && stream_1.Readable.isDisturbed(body)) {
logger.debug('Body of type stream was read, unable to retry request.');
return false;
}
const err = resOrErr;
const causativeError = (err.cause instanceof undici_1.errors.SocketError ? err.cause.cause : err.cause);
return !!(causativeError && 'code' in causativeError && causativeError.code && retryOpts?.errorCodes.includes(causativeError.code));
}
};
const fetchWithRetries = async (maxRetry = retryOpts?.maxRetries) => {
const fetchOpts = {
...rrequest,
...(input && /^(post|put|patch)$/i.test(request.method)
? { body: input }
: {}),
duplex: 'half',
redirect: 'manual',
signal: controller.signal,
dispatcher: agent,
};
try {
const res = await (0, undici_1.fetch)(url, fetchOpts);
if (shouldRetryRequest(retryOpts.maxRetries, res)) {
logger.debug(`retrying for the ${retryCount + 1} time`);
logger.debug('reason: statusCode match');
await sleep(retryCount === 0
? retryOpts.minTimeout
: retryOpts.minTimeout * retryOpts.timeoutFactor ** retryCount);
// NOTE: this event is only used by tests and will be removed at any time.
// jsforce may switch to node's fetch which doesn't emit this event on retries.
emitter.emit('retry', retryCount);
retryCount++;
if (res.status === 420) {
retry420Count++;
}
return await fetchWithRetries(maxRetry);
}
// should we throw here if the maxRetry already happened and still got the same statusCode?
return res;
}
catch (err) {
logger.debug('Request failed');
const error = err;
// request was canceled by consumer (AbortController), skip retry and rethrow.
if (error.name === 'AbortError') {
throw error;
}
if (shouldRetryRequest(retryOpts.maxRetries, error)) {
logger.debug(`retrying for the ${retryCount + 1} time`);
logger.debug(`Error: ${err.message}`);
await sleep(retryCount === 0
? retryOpts.minTimeout
: retryOpts.minTimeout * retryOpts.timeoutFactor ** retryCount);
// NOTE: this event is only used by tests and will be removed at any time.
// jsforce may switch to node's fetch which doesn't emit this event on retries.
emitter.emit('retry', retryCount);
retryCount++;
return fetchWithRetries(maxRetry);
}
logger.debug('Skipping retry...');
throw err;
}
};
let res;
// Timeout after 30 minutes without a response
//
// default timeout is 0 and jsforce consumers can't set this when calling `Connection` methods so we set a long default at the fetch wrapper level.
const fetchTimeout = options.timeout ?? 1800000;
try {
res = await (0, request_helper_1.executeWithTimeout)(fetchWithRetries, fetchTimeout, () => controller.abort());
}
catch (err) {
let throwableError;
if (err instanceof DOMException && err.name === 'AbortError') {
throwableError = new DOMException(err.message + ' Request was aborted due to timeout of 30 minutes.', err.name);
}
else {
throwableError = err;
}
emitter.emit('error', throwableError);
return;
}
const headers = {};
for (const headerName of res.headers.keys()) {
headers[headerName.toLowerCase()] = res.headers.get(headerName);
}
const response = {
statusCode: res.status,
headers,
};
if (followRedirect && (0, request_helper_1.isRedirect)(response.statusCode)) {
try {
(0, request_helper_1.performRedirectRequest)(request, response, followRedirect, counter, (req) => startFetchRequest(req, options, undefined, output, emitter, counter + 1));
}
catch (err) {
emitter.emit('error', err);
}
return;
}
emitter.emit('response', response);
if (res.body) {
stream_1.Readable.fromWeb(res.body).pipe(output);
}
else {
output.end();
}
}
/**
*
*/
function request(req, options_ = {}) {
const options = { ...defaults, ...options_ };
const { input, output, stream } = (0, request_helper_1.createHttpRequestHandlerStreams)(req, options);
startFetchRequest(req, options, input, output, stream);
return stream;
}
exports.default = request;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));