From 3bfd2e65f0b9539ab00224422f16d4634881621f Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 19 Sep 2023 03:30:40 -0700 Subject: [PATCH] build: set "target":"es2021" Problem: "es6" is very old and all of our targets support es2021. Targeting es6 disallows new javascript features such as Promise.allSettled(). Solution: - increase target to "es2021". - improve logging - invalidate(): use Promise.allSettled() instead of all() --- .../providers/credentialsProviderManager.ts | 4 +- .../sharedCredentialsProviderFactory.ts | 5 +-- src/auth/sso/cache.ts | 2 +- src/auth/sso/ssoAccessTokenProvider.ts | 14 ++++--- src/shared/utilities/cacheUtils.ts | 37 ++++++++++--------- tsconfig.json | 4 +- 6 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/auth/providers/credentialsProviderManager.ts b/src/auth/providers/credentialsProviderManager.ts index 44c3da81344..70f71e7a90d 100644 --- a/src/auth/providers/credentialsProviderManager.ts +++ b/src/auth/providers/credentialsProviderManager.ts @@ -32,9 +32,7 @@ export class CredentialsProviderManager { telemetry.aws_loadCredentials.emit({ credentialSourceId: telemType, value: 1 }) providers = providers.concat(provider) } else { - getLogger().verbose( - `provider for ${provider.getCredentialsId().credentialTypeId} unavailable in this environment` - ) + getLogger().verbose('auth: "%s" provider unavailable', provider.getCredentialsId().credentialTypeId) } } diff --git a/src/auth/providers/sharedCredentialsProviderFactory.ts b/src/auth/providers/sharedCredentialsProviderFactory.ts index c79da03cdbc..7a8ea49f62e 100644 --- a/src/auth/providers/sharedCredentialsProviderFactory.ts +++ b/src/auth/providers/sharedCredentialsProviderFactory.ts @@ -47,18 +47,17 @@ export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFac private async loadSharedCredentialsProviders(): Promise { this.resetProviders() - this.logger.verbose('Loading all Shared Credentials Sections') const result = await loadSharedCredentialsSections() if (result.errors.length > 0) { const errors = result.errors.map(e => e.message).join('\t\n') - getLogger().verbose(`credentials: errors occurred while parsing:\n%s`, errors) + getLogger().warn(`credentials: errors while parsing:\n%s`, errors) } this.loadedCredentialsModificationMillis = await this.getLastModifiedMillis(getCredentialsFilename()) this.loadedConfigModificationMillis = await this.getLastModifiedMillis(getConfigFilename()) await updateAwsSdkLoadConfigEnvVar() - getLogger().verbose(`credentials: found sections: ${result.sections.map(s => `${s.type}:${s.name}`)}`) + getLogger().verbose(`credentials: found sections: ${result.sections.map(s => `${s.type}:${s.name}`).join(' ')}`) for (const section of result.sections) { if (section.type === 'profile') { await this.addProviderIfValid( diff --git a/src/auth/sso/cache.ts b/src/auth/sso/cache.ts index 2154761b0fb..b245068817d 100644 --- a/src/auth/sso/cache.ts +++ b/src/auth/sso/cache.ts @@ -46,7 +46,7 @@ export function getRegistrationCache(directory = getCacheDir()): KeyedCache ({ ...data, expiresAt: new Date(data.expiresAt) }) const write = (data: ClientRegistration) => ({ ...data, expiresAt: data.expiresAt.toISOString() }) - const logger = (message: string) => getLogger().debug(`SSO registration cache: ${message}`) + const logger = (message: string) => getLogger().debug('auth: SSO registration cache: %s', message) const cache: KeyedCache = createDiskCache( (registrationKey: RegistrationKey) => getRegistrationCacheFile(directory, registrationKey), logger diff --git a/src/auth/sso/ssoAccessTokenProvider.ts b/src/auth/sso/ssoAccessTokenProvider.ts index 03dc93f671b..b616e4116f7 100644 --- a/src/auth/sso/ssoAccessTokenProvider.ts +++ b/src/auth/sso/ssoAccessTokenProvider.ts @@ -75,9 +75,10 @@ export class SsoAccessTokenProvider { ) {} public async invalidate(): Promise { - await Promise.all([ - this.cache.token.clear(this.tokenCacheKey), - this.cache.registration.clear(this.registrationCacheKey), + // Use allSettled() instead of all() to ensure all clear() calls are resolved. + await Promise.allSettled([ + this.cache.token.clear(this.tokenCacheKey, 'SsoAccessTokenProvider.invalidate()'), + this.cache.registration.clear(this.registrationCacheKey, 'SsoAccessTokenProvider.invalidate()'), ]) } @@ -114,7 +115,7 @@ export class SsoAccessTokenProvider { return await this.authorize(registration) } catch (err) { if (err instanceof SSOOIDCServiceException && isClientFault(err)) { - await this.cache.registration.clear(cacheKey) + await this.cache.registration.clear(cacheKey, `client fault: SSOOIDCServiceException: ${err.message}`) } throw err @@ -141,7 +142,10 @@ export class SsoAccessTokenProvider { } as AwsRefreshCredentials) if (err instanceof SSOOIDCServiceException && isClientFault(err)) { - await this.cache.token.clear(this.tokenCacheKey) + await this.cache.token.clear( + this.tokenCacheKey, + `client fault: SSOOIDCServiceException: ${err.message}` + ) } } diff --git a/src/shared/utilities/cacheUtils.ts b/src/shared/utilities/cacheUtils.ts index 76ed6ffd0fc..07185e1949e 100644 --- a/src/shared/utilities/cacheUtils.ts +++ b/src/shared/utilities/cacheUtils.ts @@ -33,11 +33,12 @@ export interface KeyedCache { save(key: K, data: T): Promise /** - * Removes the data stored at {@link key}, if any. + * Deletes data stored at {@link key}, if any. * * @param key Target key to clear. + * @param reason Partial log message explaining why the data is being deleted. */ - clear(key: K): Promise + clear(key: K, reason: string): Promise } /** @@ -71,7 +72,7 @@ export function mapCache(cache: KeyedCache, get: (data: T) => U, const getIf = (data?: T) => (data !== undefined ? get(data) : undefined) return { - clear: key => cache.clear(key), + clear: (key, reason) => cache.clear(key, reason), load: key => cache.load(key).then(getIf), save: (key, data) => cache.save(key, set(data)), } @@ -91,10 +92,10 @@ export function createDiskCache( mapKey: (key: K) => string, logger?: (message: string) => void ): KeyedCache { - function log(prefix: string, key: K): void { + function log(msg: string, key: K): void { if (logger) { const keyMessage = typeof key === 'object' ? JSON.stringify(key) : key - logger(`${prefix} for key '${keyMessage}'`) + logger(`${msg} key: ${keyMessage}`) } } @@ -104,11 +105,11 @@ export function createDiskCache( try { const result = JSON.parse(await SystemUtilities.readFile(target)) - log('load succeeded', key) + log('loaded', key) return result } catch (error) { if (isFileNotFoundError(error)) { - log('load missed', key) + log('read failed (file not found)', key) return } @@ -131,16 +132,16 @@ export function createDiskCache( }) } - log('save succeeded', key) + log('saved', key) }, - clear: async key => { + clear: async (key, reason) => { const target = mapKey(key) try { await SystemUtilities.delete(target) } catch (error) { if (isFileNotFoundError(error)) { - return log('clear succeeded, file does not exist', key) + return log('file not found', key) } throw ToolkitError.chain(error, `Failed to delete "${target}"`, { @@ -149,7 +150,7 @@ export function createDiskCache( }) } - log('clear succeeded', key) + log(`deleted (reason: ${reason})`, key) }, } } @@ -158,9 +159,9 @@ export function createSecretsCache( secrets: vscode.SecretStorage, logger?: (message: string) => void ): KeyedCache { - function log(prefix: string, key: string): void { + function log(msg: string, key: string): void { if (logger) { - logger(`${prefix} for key '${key}'`) + logger(`${msg} key: ${key}`) } } @@ -170,11 +171,11 @@ export function createSecretsCache( const value = await secrets.get(key) if (value === undefined) { - log('load missed', key) + log('read failed (key not found)', key) return } - log('load succeeded', key) + log('loaded', key) return value } catch (error) { throw ToolkitError.chain(error, 'Failed to get value from secrets storage', { @@ -193,9 +194,9 @@ export function createSecretsCache( }) } - log('save succeeded', key) + log('saved', key) }, - clear: async key => { + clear: async (key, reason) => { try { await secrets.delete(key) } catch (error) { @@ -205,7 +206,7 @@ export function createSecretsCache( }) } - log('clear succeeded', key) + log(`deleted (reason: ${reason})`, key) }, } } diff --git a/tsconfig.json b/tsconfig.json index 2818170e3c5..d5b53761c57 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,7 @@ }, "incremental": true, "module": "commonjs", - "target": "es6", + "target": "es2021", "outDir": "dist", "sourceMap": true, "moduleResolution": "node", @@ -16,7 +16,7 @@ "strict": true, "noUnusedLocals": true, "noImplicitOverride": true, - "lib": ["dom", "es6", "esnext.asynciterable"], + "lib": ["dom", "es2021"], "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "jsx": "preserve",