@salesforce/core 3.32.3 → 3.32.5

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.
@@ -40,6 +40,8 @@ export declare class Connection<S extends Schema = Schema> extends JSForceConnec
40
40
  private logger;
41
41
  private options;
42
42
  private username;
43
+ private hasResolved;
44
+ private maxApiVersion;
43
45
  /**
44
46
  * Constructor
45
47
  * **Do not directly construct instances of this class -- use {@link Connection.create} instead.**
@@ -164,7 +166,7 @@ export declare class Connection<S extends Schema = Schema> extends JSForceConnec
164
166
  * Useful for the raw methods (request, requestRaw) that use the accessToken directly and don't handle refreshes
165
167
  */
166
168
  refreshAuth(): Promise<void>;
167
- private loadInstanceApiVersion;
169
+ private getCachedApiVersion;
168
170
  }
169
171
  export declare const SingleRecordQueryErrors: {
170
172
  NoRecords: string;
@@ -59,6 +59,8 @@ class Connection extends jsforce_1.Connection {
59
59
  */
60
60
  constructor(options) {
61
61
  super(options.connectionOptions ?? {});
62
+ // Save whether we've successfully resolved this connection's instance URL.
63
+ this.hasResolved = false;
62
64
  this.options = options;
63
65
  this.username = options.authInfo.getUsername();
64
66
  }
@@ -93,10 +95,7 @@ class Connection extends jsforce_1.Connection {
93
95
  try {
94
96
  // No version passed in or in the config, so load one.
95
97
  if (!baseOptions.version) {
96
- const cachedVersion = await conn.loadInstanceApiVersion();
97
- if (cachedVersion) {
98
- conn.setApiVersion(cachedVersion);
99
- }
98
+ await conn.useLatestApiVersion();
100
99
  }
101
100
  else {
102
101
  conn.logger.debug(`The org-api-version ${baseOptions.version} was found from ${options.connectionOptions?.version ? 'passed in options' : 'config'}`);
@@ -109,7 +108,7 @@ class Connection extends jsforce_1.Connection {
109
108
  }
110
109
  conn.logger.debug(`Error trying to load the API version: ${e.name} - ${e.message}`);
111
110
  }
112
- conn.logger.debug(`Using apiVersion ${conn.getApiVersion()}`);
111
+ conn.logger.debug(`Connection created with apiVersion ${conn.getApiVersion()}`);
113
112
  return conn;
114
113
  }
115
114
  /**
@@ -184,7 +183,12 @@ class Connection extends jsforce_1.Connection {
184
183
  * Retrieves the highest api version that is supported by the target server instance.
185
184
  */
186
185
  async retrieveMaxApiVersion() {
186
+ // Check saved value first, then cache.
187
+ if ((this.maxApiVersion ?? (this.maxApiVersion = this.getCachedApiVersion()))) {
188
+ return this.maxApiVersion;
189
+ }
187
190
  await this.isResolvable();
191
+ this.logger.debug(`Fetching API versions supported for org: ${this.getUsername()}`);
188
192
  const versions = await this.request(`${this.instanceUrl}/services/data`);
189
193
  // if the server doesn't return a list of versions, it's possibly a instanceUrl issue where the local file is out of date.
190
194
  if (!Array.isArray(versions)) {
@@ -192,8 +196,15 @@ class Connection extends jsforce_1.Connection {
192
196
  throw messages.createError('noApiVersionsError');
193
197
  }
194
198
  this.logger.debug(`response for org versions: ${versions.map((item) => item.version).join(',')}`);
195
- const max = (0, ts_types_1.ensure)((0, kit_1.maxBy)(versions, (version) => version.version));
196
- return max.version;
199
+ this.maxApiVersion = (0, ts_types_1.ensure)((0, kit_1.maxBy)(versions, (version) => version.version)).version;
200
+ // cache the max API version just fetched
201
+ await this.options.authInfo.save({
202
+ instanceApiVersion: this.maxApiVersion,
203
+ // This will get messed up if the user changes their local time on their machine.
204
+ // Not a big deal since it will just get updated sooner/later.
205
+ instanceApiVersionLastRetrieved: new Date().toLocaleString(),
206
+ });
207
+ return this.maxApiVersion;
197
208
  }
198
209
  /**
199
210
  * Use the latest API version available on `this.instanceUrl`.
@@ -215,6 +226,9 @@ class Connection extends jsforce_1.Connection {
215
226
  * Verify that instance has a reachable DNS entry, otherwise will throw error
216
227
  */
217
228
  async isResolvable() {
229
+ if (this.hasResolved) {
230
+ return this.hasResolved;
231
+ }
218
232
  if (!this.options.connectionOptions?.instanceUrl) {
219
233
  throw messages.createError('noInstanceUrlError');
220
234
  }
@@ -223,6 +237,7 @@ class Connection extends jsforce_1.Connection {
223
237
  });
224
238
  try {
225
239
  await resolver.resolve();
240
+ this.hasResolved = true;
226
241
  return true;
227
242
  }
228
243
  catch (e) {
@@ -345,10 +360,16 @@ class Connection extends jsforce_1.Connection {
345
360
  };
346
361
  await this.request(requestInfo);
347
362
  }
348
- async loadInstanceApiVersion() {
363
+ getCachedApiVersion() {
364
+ // Exit early if the API version cache is disabled.
365
+ if (kit_1.env.getBoolean('SFDX_IGNORE_API_VERSION_CACHE', false)) {
366
+ this.logger.debug('Using latest API version since SFDX_IGNORE_API_VERSION_CACHE = true');
367
+ return;
368
+ }
369
+ // Get API version cache data
349
370
  const authFileFields = this.options.authInfo.getFields();
350
371
  const lastCheckedDateString = authFileFields.instanceApiVersionLastRetrieved;
351
- let version = authFileFields.instanceApiVersion;
372
+ const version = authFileFields.instanceApiVersion;
352
373
  let lastChecked;
353
374
  try {
354
375
  if (lastCheckedDateString && (0, ts_types_1.isString)(lastCheckedDateString)) {
@@ -358,35 +379,20 @@ class Connection extends jsforce_1.Connection {
358
379
  catch (e) {
359
380
  /* Do nothing, it will just request the version again */
360
381
  }
361
- // Grab the latest api version from the server and cache it in the auth file
362
- const useLatest = async () => {
363
- // verifies DNS
364
- await this.useLatestApiVersion();
365
- version = this.getApiVersion();
366
- await this.options.authInfo.save({
367
- instanceApiVersion: version,
368
- // This will get messed up if the user changes their local time on their machine.
369
- // Not a big deal since it will just get updated sooner/later.
370
- instanceApiVersionLastRetrieved: new Date().toLocaleString(),
371
- });
372
- };
373
- const ignoreCache = kit_1.env.getBoolean('SFDX_IGNORE_API_VERSION_CACHE', false);
374
- if (lastChecked && !ignoreCache) {
382
+ // Check if the cache has expired
383
+ if (lastChecked) {
375
384
  const now = new Date();
376
385
  const has24HoursPastSinceLastCheck = now.getTime() - lastChecked > kit_1.Duration.hours(24).milliseconds;
377
- this.logger.debug(`Last checked on ${lastCheckedDateString} (now is ${now.toLocaleString()}) - ${has24HoursPastSinceLastCheck ? '' : 'not '}getting latest`);
378
- if (has24HoursPastSinceLastCheck) {
379
- await useLatest();
386
+ this.logger.debug(`API version cache last checked on ${lastCheckedDateString} (now is ${now.toLocaleString()})`);
387
+ if (!has24HoursPastSinceLastCheck && version) {
388
+ // return cached API version
389
+ this.logger.debug(`Using cached API version: ${version}`);
390
+ return version;
391
+ }
392
+ else {
393
+ this.logger.debug('API version cache expired. Re-fetching latest.');
380
394
  }
381
395
  }
382
- else {
383
- this.logger.debug(`Using the latest because lastChecked=${lastChecked} and SFDX_IGNORE_API_VERSION_CACHE=${ignoreCache}`);
384
- // No version found in the file (we never checked before)
385
- // so get the latest.
386
- await useLatest();
387
- }
388
- this.logger.debug(`Loaded latest org-api-version ${version}`);
389
- return version;
390
396
  }
391
397
  }
392
398
  exports.Connection = Connection;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/core",
3
- "version": "3.32.3",
3
+ "version": "3.32.5",
4
4
  "description": "Core libraries to interact with SFDX projects, orgs, and APIs.",
5
5
  "main": "lib/exported",
6
6
  "types": "lib/exported.d.ts",
@@ -39,7 +39,7 @@
39
39
  "@salesforce/schemas": "^1.4.0",
40
40
  "@salesforce/ts-types": "^1.5.21",
41
41
  "@types/graceful-fs": "^4.1.5",
42
- "@types/semver": "^7.3.9",
42
+ "@types/semver": "^7.3.13",
43
43
  "ajv": "^8.11.0",
44
44
  "archiver": "^5.3.0",
45
45
  "change-case": "^4.1.2",
@@ -65,7 +65,7 @@
65
65
  "@types/lodash": "^4.14.189",
66
66
  "@types/shelljs": "0.8.11",
67
67
  "@typescript-eslint/eslint-plugin": "^5.41.0",
68
- "@typescript-eslint/parser": "^5.41.0",
68
+ "@typescript-eslint/parser": "^5.44.0",
69
69
  "chai": "^4.3.4",
70
70
  "chai-string": "^1.5.0",
71
71
  "commitizen": "^3.1.2",