Skip to content

Commit

Permalink
chore: merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel committed Dec 1, 2022
2 parents e81abc6 + 05abc7a commit 9f28fbe
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 70 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@salesforce/core",
"version": "3.32.2",
"version": "3.32.5",
"description": "Core libraries to interact with SFDX projects, orgs, and APIs.",
"main": "lib/exported",
"types": "lib/exported.d.ts",
Expand Down Expand Up @@ -36,10 +36,10 @@
"dependencies": {
"@salesforce/bunyan": "^2.0.0",
"@salesforce/kit": "^1.8.0",
"@salesforce/schemas": "^1.1.0",
"@salesforce/schemas": "^1.4.0",
"@salesforce/ts-types": "^1.5.21",
"@types/graceful-fs": "^4.1.5",
"@types/semver": "^7.3.9",
"@types/semver": "^7.3.13",
"ajv": "^8.11.0",
"archiver": "^5.3.0",
"change-case": "^4.1.2",
Expand All @@ -65,7 +65,7 @@
"@types/lodash": "^4.14.189",
"@types/shelljs": "0.8.11",
"@typescript-eslint/eslint-plugin": "^5.41.0",
"@typescript-eslint/parser": "^5.41.0",
"@typescript-eslint/parser": "^5.44.0",
"chai": "^4.3.4",
"chai-string": "^1.5.0",
"commitizen": "^3.1.2",
Expand All @@ -76,7 +76,7 @@
"eslint-config-salesforce-typescript": "^1.1.1",
"eslint-plugin-header": "^3.1.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsdoc": "^39.3.6",
"eslint-plugin-jsdoc": "^39.6.4",
"husky": "^7.0.4",
"lodash": "^4.17.21",
"mocha": "^9.1.3",
Expand All @@ -95,4 +95,4 @@
"publishConfig": {
"access": "public"
}
}
}
91 changes: 50 additions & 41 deletions src/org/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { URL } from 'url';
import { AsyncResult, DeployOptions, DeployResultLocator } from 'jsforce/api/metadata';
import { Duration, env, maxBy } from '@salesforce/kit';
import { asString, ensure, isString, JsonCollection, JsonMap, Nullable, Optional } from '@salesforce/ts-types';
import { asString, ensure, isString, JsonCollection, JsonMap, Optional } from '@salesforce/ts-types';
import {
Connection as JSForceConnection,
ConnectionConfig,
Expand Down Expand Up @@ -82,6 +82,12 @@ export class Connection<S extends Schema = Schema> extends JSForceConnection<S>
// All connections are tied to a username
private username!: string;

// Save whether we've successfully resolved this connection's instance URL.
private hasResolved = false;

// Save the max API version of this connection's org.
private maxApiVersion!: Optional<string>;

/**
* Constructor
* **Do not directly construct instances of this class -- use {@link Connection.create} instead.**
Expand Down Expand Up @@ -135,10 +141,7 @@ export class Connection<S extends Schema = Schema> extends JSForceConnection<S>
try {
// No version passed in or in the config, so load one.
if (!baseOptions.version) {
const cachedVersion = await conn.loadInstanceApiVersion();
if (cachedVersion) {
conn.setApiVersion(cachedVersion);
}
await conn.useLatestApiVersion();
} else {
conn.logger.debug(
`The org-api-version ${baseOptions.version} was found from ${
Expand All @@ -153,7 +156,7 @@ export class Connection<S extends Schema = Schema> extends JSForceConnection<S>
}
conn.logger.debug(`Error trying to load the API version: ${e.name} - ${e.message}`);
}
conn.logger.debug(`Using apiVersion ${conn.getApiVersion()}`);
conn.logger.debug(`Connection created with apiVersion ${conn.getApiVersion()}`);
return conn;
}

Expand Down Expand Up @@ -235,18 +238,33 @@ export class Connection<S extends Schema = Schema> extends JSForceConnection<S>
* Retrieves the highest api version that is supported by the target server instance.
*/
public async retrieveMaxApiVersion(): Promise<string> {
// Check saved value first, then cache.
if ((this.maxApiVersion ??= this.getCachedApiVersion())) {
return this.maxApiVersion;
}

await this.isResolvable();
type Versioned = { version: string };

this.logger.debug(`Fetching API versions supported for org: ${this.getUsername()}`);
const versions: Versioned[] = await this.request<Versioned[]>(`${this.instanceUrl}/services/data`);
// if the server doesn't return a list of versions, it's possibly a instanceUrl issue where the local file is out of date.
if (!Array.isArray(versions)) {
this.logger.debug(`server response for retrieveMaxApiVersion: ${versions as string}`);
throw messages.createError('noApiVersionsError');
}
this.logger.debug(`response for org versions: ${versions.map((item) => item.version).join(',')}`);
const max = ensure(maxBy(versions, (version: Versioned) => version.version));
this.maxApiVersion = ensure(maxBy(versions, (version: Versioned) => version.version)).version;

// cache the max API version just fetched
await this.options.authInfo.save({
instanceApiVersion: this.maxApiVersion,
// This will get messed up if the user changes their local time on their machine.
// Not a big deal since it will just get updated sooner/later.
instanceApiVersionLastRetrieved: new Date().toLocaleString(),
});

return max.version;
return this.maxApiVersion;
}
/**
* Use the latest API version available on `this.instanceUrl`.
Expand All @@ -268,6 +286,10 @@ export class Connection<S extends Schema = Schema> extends JSForceConnection<S>
* Verify that instance has a reachable DNS entry, otherwise will throw error
*/
public async isResolvable(): Promise<boolean> {
if (this.hasResolved) {
return this.hasResolved;
}

if (!this.options.connectionOptions?.instanceUrl) {
throw messages.createError('noInstanceUrlError');
}
Expand All @@ -276,6 +298,7 @@ export class Connection<S extends Schema = Schema> extends JSForceConnection<S>
});
try {
await resolver.resolve();
this.hasResolved = true;
return true;
} catch (e) {
throw messages.createError('domainNotFoundError', [], [], e as Error);
Expand Down Expand Up @@ -429,10 +452,17 @@ export class Connection<S extends Schema = Schema> extends JSForceConnection<S>
await this.request(requestInfo);
}

private async loadInstanceApiVersion(): Promise<Nullable<string>> {
private getCachedApiVersion(): Optional<string> {
// Exit early if the API version cache is disabled.
if (env.getBoolean('SFDX_IGNORE_API_VERSION_CACHE', false)) {
this.logger.debug('Using latest API version since SFDX_IGNORE_API_VERSION_CACHE = true');
return;
}

// Get API version cache data
const authFileFields = this.options.authInfo.getFields();
const lastCheckedDateString = authFileFields.instanceApiVersionLastRetrieved;
let version = authFileFields.instanceApiVersion;
const version = authFileFields.instanceApiVersion;
let lastChecked: Optional<number>;

try {
Expand All @@ -443,41 +473,20 @@ export class Connection<S extends Schema = Schema> extends JSForceConnection<S>
/* Do nothing, it will just request the version again */
}

// Grab the latest api version from the server and cache it in the auth file
const useLatest = async (): Promise<void> => {
// verifies DNS
await this.useLatestApiVersion();
version = this.getApiVersion();
await this.options.authInfo.save({
instanceApiVersion: version,
// This will get messed up if the user changes their local time on their machine.
// Not a big deal since it will just get updated sooner/later.
instanceApiVersionLastRetrieved: new Date().toLocaleString(),
});
};

const ignoreCache = env.getBoolean('SFDX_IGNORE_API_VERSION_CACHE', false);
if (lastChecked && !ignoreCache) {
// Check if the cache has expired
if (lastChecked) {
const now = new Date();
const has24HoursPastSinceLastCheck = now.getTime() - lastChecked > Duration.hours(24).milliseconds;
this.logger.debug(
`Last checked on ${lastCheckedDateString} (now is ${now.toLocaleString()}) - ${
has24HoursPastSinceLastCheck ? '' : 'not '
}getting latest`
);
if (has24HoursPastSinceLastCheck) {
await useLatest();
this.logger.debug(`API version cache last checked on ${lastCheckedDateString} (now is ${now.toLocaleString()})`);

if (!has24HoursPastSinceLastCheck && version) {
// return cached API version
this.logger.debug(`Using cached API version: ${version}`);
return version;
} else {
this.logger.debug('API version cache expired. Re-fetching latest.');
}
} else {
this.logger.debug(
`Using the latest because lastChecked=${lastChecked} and SFDX_IGNORE_API_VERSION_CACHE=${ignoreCache}`
);
// No version found in the file (we never checked before)
// so get the latest.
await useLatest();
}
this.logger.debug(`Loaded latest org-api-version ${version}`);
return version;
}
}

Expand Down
4 changes: 4 additions & 0 deletions test/unit/org/connectionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ describe('Connection', () => {
it('throws error when no valid API version', async () => {
const conn = await Connection.create({ authInfo: fromStub(testAuthInfoWithDomain) });

// @ts-ignore (resetting private property)
conn.maxApiVersion = undefined;

$$.SANDBOX.restore();
$$.SANDBOX.stub(MyDomainResolver.prototype, 'resolve').resolves(TEST_IP);
$$.SANDBOX.stub(conn, 'isResolvable').resolves(true);
Expand Down Expand Up @@ -110,6 +113,7 @@ describe('Connection', () => {
});

it('create() should create a connection with the cached API version updated with latest', async () => {
$$.SANDBOX.stub(Connection.prototype, 'isResolvable').resolves(true);
testAuthInfo.getFields.returns({
instanceApiVersionLastRetrieved: 123,
instanceApiVersion: '40.0',
Expand Down
80 changes: 57 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,10 @@
esquery "^1.4.0"
jsdoc-type-pratt-parser "1.0.4"

"@es-joy/jsdoccomment@~0.31.0":
version "0.31.0"
resolved "https://registry.yarnpkg.com/@es-joy/jsdoccomment/-/jsdoccomment-0.31.0.tgz#dbc342cc38eb6878c12727985e693eaef34302bc"
integrity sha512-tc1/iuQcnaiSIUVad72PBierDFpsxdUHtEF/OrfqvM1CBAsIoMP51j52jTMb3dXriwhieTo289InzZj72jL3EQ==
"@es-joy/jsdoccomment@~0.36.1":
version "0.36.1"
resolved "https://registry.yarnpkg.com/@es-joy/jsdoccomment/-/jsdoccomment-0.36.1.tgz#c37db40da36e4b848da5fd427a74bae3b004a30f"
integrity sha512-922xqFsTpHs6D0BUiG4toiyPOMc8/jafnWKxz1KWgS4XzKPy2qXf1Pe6UFuNSCQqt6tOuhAWXBNuuyUhJmw9Vg==
dependencies:
comment-parser "1.3.1"
esquery "^1.4.0"
Expand Down Expand Up @@ -716,10 +716,10 @@
resolved "https://registry.yarnpkg.com/@salesforce/prettier-config/-/prettier-config-0.0.2.tgz#ded39bf7cb75238edc9db6dd093649111350f8bc"
integrity sha512-KExM355BLbxCW6siGBV7oUOotXvvVp0tAWERgzUkM2FcMb9fWrjwXDrIHc8V0UdDlA3UXtFltDWgN+Yqi+BA/g==

"@salesforce/schemas@^1.1.0":
version "1.1.3"
resolved "https://registry.yarnpkg.com/@salesforce/schemas/-/schemas-1.1.3.tgz#fce83f55c7557d47b9c814d5d02978ad734300b3"
integrity sha512-XWohlOT2oQDqAJH00OXS3f2MGjkwZ6pr4emnnkHSQbg7UdGW0rvGpEnRKqBbDUfZ4K5YKSo9Gj216ZtaP3JLXg==
"@salesforce/schemas@^1.4.0":
version "1.4.0"
resolved "https://registry.yarnpkg.com/@salesforce/schemas/-/schemas-1.4.0.tgz#7dff427c8059895d8108176047aee600703c82d6"
integrity sha512-BJ25uphssN42Zy6kksheFHMTLiR98AAHe/Wxnv0T4dYxtrEbUjSXVAGKZqfewJPFXA4xB5gxC+rQZtfz6xKCFg==

"@salesforce/ts-sinon@^1.4.2":
version "1.4.2"
Expand Down Expand Up @@ -936,7 +936,7 @@
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==

"@types/semver@^7.3.12", "@types/semver@^7.3.9":
"@types/semver@^7.3.12", "@types/semver@^7.3.13":
version "7.3.13"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.13.tgz#da4bfd73f49bd541d28920ab0e2bf0ee80f71c91"
integrity sha512-21cFJr9z3g5dW8B0CVI9g2O9beqaThGQ6ZFBqHfwhzLDKUxaqTIy3vnfah/UPkfOiF2pLq+tGz+W8RyCskuslw==
Expand Down Expand Up @@ -975,14 +975,14 @@
semver "^7.3.7"
tsutils "^3.21.0"

"@typescript-eslint/parser@^5.41.0":
version "5.41.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.41.0.tgz#0414a6405007e463dc527b459af1f19430382d67"
integrity sha512-HQVfix4+RL5YRWZboMD1pUfFN8MpRH4laziWkkAzyO1fvNOY/uinZcvo3QiFJVS/siNHupV8E5+xSwQZrl6PZA==
"@typescript-eslint/parser@^5.44.0":
version "5.44.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.44.0.tgz#99e2c710a2252191e7a79113264f438338b846ad"
integrity sha512-H7LCqbZnKqkkgQHaKLGC6KUjt3pjJDx8ETDqmwncyb6PuoigYajyAwBGz08VU/l86dZWZgI4zm5k2VaKqayYyA==
dependencies:
"@typescript-eslint/scope-manager" "5.41.0"
"@typescript-eslint/types" "5.41.0"
"@typescript-eslint/typescript-estree" "5.41.0"
"@typescript-eslint/scope-manager" "5.44.0"
"@typescript-eslint/types" "5.44.0"
"@typescript-eslint/typescript-estree" "5.44.0"
debug "^4.3.4"

"@typescript-eslint/[email protected]":
Expand All @@ -993,6 +993,14 @@
"@typescript-eslint/types" "5.41.0"
"@typescript-eslint/visitor-keys" "5.41.0"

"@typescript-eslint/[email protected]":
version "5.44.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.44.0.tgz#988c3f34b45b3474eb9ff0674c18309dedfc3e04"
integrity sha512-2pKml57KusI0LAhgLKae9kwWeITZ7IsZs77YxyNyIVOwQ1kToyXRaJLl+uDEXzMN5hnobKUOo2gKntK9H1YL8g==
dependencies:
"@typescript-eslint/types" "5.44.0"
"@typescript-eslint/visitor-keys" "5.44.0"

"@typescript-eslint/[email protected]":
version "5.41.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/type-utils/-/type-utils-5.41.0.tgz#2371601171e9f26a4e6da918a7913f7266890cdf"
Expand All @@ -1008,6 +1016,11 @@
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.41.0.tgz#6800abebc4e6abaf24cdf220fb4ce28f4ab09a85"
integrity sha512-5BejraMXMC+2UjefDvrH0Fo/eLwZRV6859SXRg+FgbhA0R0l6lDqDGAQYhKbXhPN2ofk2kY5sgGyLNL907UXpA==

"@typescript-eslint/[email protected]":
version "5.44.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.44.0.tgz#f3f0b89aaff78f097a2927fe5688c07e786a0241"
integrity sha512-Tp+zDnHmGk4qKR1l+Y1rBvpjpm5tGXX339eAlRBDg+kgZkz9Bw+pqi4dyseOZMsGuSH69fYfPJCBKBrbPCxYFQ==

"@typescript-eslint/[email protected]":
version "5.41.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.41.0.tgz#bf5c6b3138adbdc73ba4871d060ae12c59366c61"
Expand All @@ -1021,6 +1034,19 @@
semver "^7.3.7"
tsutils "^3.21.0"

"@typescript-eslint/[email protected]":
version "5.44.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.44.0.tgz#0461b386203e8d383bb1268b1ed1da9bc905b045"
integrity sha512-M6Jr+RM7M5zeRj2maSfsZK2660HKAJawv4Ud0xT+yauyvgrsHu276VtXlKDFnEmhG+nVEd0fYZNXGoAgxwDWJw==
dependencies:
"@typescript-eslint/types" "5.44.0"
"@typescript-eslint/visitor-keys" "5.44.0"
debug "^4.3.4"
globby "^11.1.0"
is-glob "^4.0.3"
semver "^7.3.7"
tsutils "^3.21.0"

"@typescript-eslint/[email protected]":
version "5.41.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.41.0.tgz#f41ae5883994a249d00b2ce69f4188f3a23fa0f9"
Expand All @@ -1043,6 +1069,14 @@
"@typescript-eslint/types" "5.41.0"
eslint-visitor-keys "^3.3.0"

"@typescript-eslint/[email protected]":
version "5.44.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.44.0.tgz#10740dc28902bb903d12ee3a005cc3a70207d433"
integrity sha512-a48tLG8/4m62gPFbJ27FxwCOqPKxsb8KC3HkmYoq2As/4YyjQl1jDbRr1s63+g4FS/iIehjmN3L5UjmKva1HzQ==
dependencies:
"@typescript-eslint/types" "5.44.0"
eslint-visitor-keys "^3.3.0"

"@ungap/[email protected]":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz#aa58042711d6e3275dd37dc597e5d31e8c290a44"
Expand Down Expand Up @@ -2391,17 +2425,17 @@ eslint-plugin-jsdoc@^35.1.2:
semver "^7.3.5"
spdx-expression-parse "^3.0.1"

eslint-plugin-jsdoc@^39.3.6:
version "39.3.6"
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-39.3.6.tgz#6ba29f32368d72a51335a3dc9ccd22ad0437665d"
integrity sha512-R6dZ4t83qPdMhIOGr7g2QII2pwCjYyKP+z0tPOfO1bbAbQyKC20Y2Rd6z1te86Lq3T7uM8bNo+VD9YFpE8HU/g==
eslint-plugin-jsdoc@^39.6.4:
version "39.6.4"
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-39.6.4.tgz#b940aebd3eea26884a0d341785d2dc3aba6a38a7"
integrity sha512-fskvdLCfwmPjHb6e+xNGDtGgbF8X7cDwMtVLAP2WwSf9Htrx68OAx31BESBM1FAwsN2HTQyYQq7m4aW4Q4Nlag==
dependencies:
"@es-joy/jsdoccomment" "~0.31.0"
"@es-joy/jsdoccomment" "~0.36.1"
comment-parser "1.3.1"
debug "^4.3.4"
escape-string-regexp "^4.0.0"
esquery "^1.4.0"
semver "^7.3.7"
semver "^7.3.8"
spdx-expression-parse "^3.0.1"

eslint-plugin-prefer-arrow@^1.2.1:
Expand Down Expand Up @@ -5415,7 +5449,7 @@ semver@^6.0.0, semver@^6.3.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==

semver@^7.2.1, semver@^7.3.2, semver@^7.3.4, semver@^7.3.5, semver@^7.3.7:
semver@^7.2.1, semver@^7.3.2, semver@^7.3.4, semver@^7.3.5, semver@^7.3.7, semver@^7.3.8:
version "7.3.8"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.8.tgz#07a78feafb3f7b32347d725e33de7e2a2df67798"
integrity sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==
Expand Down

0 comments on commit 9f28fbe

Please sign in to comment.