@jsforce/jsforce-node 3.0.0-next.2 → 3.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.
package/lib/core.d.ts CHANGED
@@ -6,7 +6,6 @@ import VERSION from './VERSION';
6
6
  import RecordReference from './record-reference';
7
7
  import RecordStream from './record-stream';
8
8
  export * from './oauth2';
9
- export * from './jwtOAuth2';
10
9
  export * from './connection';
11
10
  export * from './query';
12
11
  export * from './quick-action';
package/lib/core.js CHANGED
@@ -46,7 +46,6 @@ exports.RecordReference = record_reference_1.default;
46
46
  const record_stream_1 = __importDefault(require("./record-stream"));
47
47
  exports.RecordStream = record_stream_1.default;
48
48
  __exportStar(require("./oauth2"), exports);
49
- __exportStar(require("./jwtOAuth2"), exports);
50
49
  __exportStar(require("./connection"), exports);
51
50
  __exportStar(require("./query"), exports);
52
51
  __exportStar(require("./quick-action"), exports);
package/lib/jsforce.d.ts CHANGED
@@ -6,7 +6,6 @@ import OAuth2 from './oauth2';
6
6
  import SfDate from './date';
7
7
  import { Registry } from './registry';
8
8
  import { BrowserClient } from './browser/client';
9
- import { JwtOAuth2 } from './jwtOAuth2';
10
9
  /**
11
10
  *
12
11
  */
@@ -14,7 +13,6 @@ declare class JSforce extends EventEmitter {
14
13
  VERSION: typeof VERSION;
15
14
  Connection: typeof Connection;
16
15
  OAuth2: typeof OAuth2;
17
- JwtOAuth2: typeof JwtOAuth2;
18
16
  SfDate: typeof SfDate;
19
17
  Date: typeof SfDate;
20
18
  BrowserClient: typeof BrowserClient;
package/lib/jsforce.js CHANGED
@@ -34,7 +34,6 @@ const oauth2_1 = __importDefault(require("./oauth2"));
34
34
  const date_1 = __importDefault(require("./date"));
35
35
  const registry_1 = __importDefault(require("./registry"));
36
36
  const client_1 = __importStar(require("./browser/client"));
37
- const jwtOAuth2_1 = require("./jwtOAuth2");
38
37
  /**
39
38
  *
40
39
  */
@@ -42,7 +41,6 @@ class JSforce extends events_1.EventEmitter {
42
41
  VERSION = VERSION_1.default;
43
42
  Connection = connection_1.default;
44
43
  OAuth2 = oauth2_1.default;
45
- JwtOAuth2 = jwtOAuth2_1.JwtOAuth2;
46
44
  SfDate = date_1.default;
47
45
  Date = date_1.default;
48
46
  BrowserClient = client_1.BrowserClient;
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 res;
31
- try {
32
- res = await (0, request_helper_1.executeWithTimeout)(() => (0, node_fetch_1.default)(url, {
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
- }), options.timeout, () => controller.abort());
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);
@@ -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);
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "database.com"
11
11
  ],
12
12
  "homepage": "http://github.com/jsforce/jsforce",
13
- "version": "3.0.0-next.2",
13
+ "version": "3.1.0",
14
14
  "repository": {
15
15
  "type": "git",
16
16
  "url": "git://github.com/jsforce/jsforce.git"
@@ -1,8 +0,0 @@
1
- import OAuth2, { OAuth2Config } from './oauth2';
2
- /**
3
- * @deprecated
4
- */
5
- export declare class JwtOAuth2 extends OAuth2 {
6
- constructor(config: OAuth2Config);
7
- jwtAuthorize(innerToken: string): Promise<any>;
8
- }
package/lib/jwtOAuth2.js DELETED
@@ -1,23 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.JwtOAuth2 = void 0;
7
- const oauth2_1 = __importDefault(require("./oauth2"));
8
- /**
9
- * @deprecated
10
- */
11
- class JwtOAuth2 extends oauth2_1.default {
12
- constructor(config) {
13
- console.warn('JwtOAuth2 is deprecated and will be removed in next stable release, please use OAuth2 instead.');
14
- super(config);
15
- }
16
- jwtAuthorize(innerToken) {
17
- return super._postParams({
18
- grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
19
- assertion: innerToken,
20
- });
21
- }
22
- }
23
- exports.JwtOAuth2 = JwtOAuth2;