Skip to content

Commit

Permalink
fix: catch decoding errors
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel committed Nov 8, 2023
1 parent 8ee9f14 commit 446c806
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
45 changes: 31 additions & 14 deletions src/collections/decodeableMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Logger } from '@salesforce/core';
import { isString } from '@salesforce/ts-types';

/**
* This is an extension of the Map class that can match keys whether they are encoded or decoded.
Expand All @@ -29,13 +31,22 @@ export class DecodeableMap<K extends string, V> extends Map<string, V> {
// before `this.set()` is called or `TypeErrors` will be thrown.
private internalkeysMap!: Map<string, string>;

private internalLogger!: Logger;

private get keysMap(): Map<string, string> {
if (!this.internalkeysMap) {
this.internalkeysMap = new Map();
}
return this.internalkeysMap;
}

private get logger(): Logger {
if (!this.internalLogger) {
this.internalLogger = Logger.childFromRoot(this.constructor.name);
}
return this.internalLogger;
}

/**
* boolean indicating whether an element with the specified key (matching decoded) exists or not.
*/
Expand Down Expand Up @@ -77,21 +88,27 @@ export class DecodeableMap<K extends string, V> extends Map<string, V> {
if (super.has(key)) {
return key;
} else {
const decodedKey = decodeURIComponent(key);
if (key !== decodedKey) {
// The key is encoded; If this is part of a set operation,
// set the { decodedKey : encodedKey } in the internal map.
if (setInKeysMap) {
this.keysMap.set(decodedKey, key);
}
if (super.has(decodedKey)) {
return decodedKey;
}
} else {
const encodedKey = this.keysMap.get(decodedKey);
if (encodedKey && super.has(encodedKey)) {
return encodedKey;
try {
const decodedKey = decodeURIComponent(key);
if (key !== decodedKey) {
// The key is encoded; If this is part of a set operation,
// set the { decodedKey : encodedKey } in the internal map.
if (setInKeysMap) {
this.keysMap.set(decodedKey, key);
}
if (super.has(decodedKey)) {
return decodedKey;
}
} else {
const encodedKey = this.keysMap.get(decodedKey);
if (encodedKey && super.has(encodedKey)) {
return encodedKey;
}
}
} catch (e: unknown) {
// Log the error and the key
const errMsg = e instanceof Error ? e.message : isString(e) ? e : 'unknown';
this.logger.debug(`Could not decode metadata key: ${key} due to: ${errMsg}`);
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/collections/decodeableMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ describe('DecodeableMap', () => {
expect(hasMapSpy.calledWith(nonExistent_key_encoded)).to.be.true;
expect(hasMapSpy.calledWith(nonExistent_key_decoded)).to.be.true;
});

it('should not match non-decodeable key', () => {
// trying to decode '%E0%A4%A' throws a URIError so DecodeableMap
// should not throw when a non-decodeable key is encountered.
expect(dMap.has('%E0%A4%A')).to.be.false;
});
});

describe('get()', () => {
Expand Down

2 comments on commit 446c806

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 446c806 Previous: 1d01296 Ratio
eda-componentSetCreate-linux 223 ms 256 ms 0.87
eda-sourceToMdapi-linux 4636 ms 5398 ms 0.86
eda-sourceToZip-linux 4520 ms 5020 ms 0.90
eda-mdapiToSource-linux 2941 ms 3872 ms 0.76
lotsOfClasses-componentSetCreate-linux 371 ms 496 ms 0.75
lotsOfClasses-sourceToMdapi-linux 6116 ms 7532 ms 0.81
lotsOfClasses-sourceToZip-linux 5323 ms 7803 ms 0.68
lotsOfClasses-mdapiToSource-linux 3360 ms 4389 ms 0.77
lotsOfClassesOneDir-componentSetCreate-linux 647 ms 826 ms 0.78
lotsOfClassesOneDir-sourceToMdapi-linux 8501 ms 11052 ms 0.77
lotsOfClassesOneDir-sourceToZip-linux 6972 ms 11000 ms 0.63
lotsOfClassesOneDir-mdapiToSource-linux 5937 ms 7041 ms 0.84

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 446c806 Previous: 1d01296 Ratio
eda-componentSetCreate-win32 614 ms 434 ms 1.41
eda-sourceToMdapi-win32 8373 ms 6380 ms 1.31
eda-sourceToZip-win32 8156 ms 5206 ms 1.57
eda-mdapiToSource-win32 8918 ms 5912 ms 1.51
lotsOfClasses-componentSetCreate-win32 1225 ms 866 ms 1.41
lotsOfClasses-sourceToMdapi-win32 14001 ms 10682 ms 1.31
lotsOfClasses-sourceToZip-win32 10892 ms 7322 ms 1.49
lotsOfClasses-mdapiToSource-win32 10727 ms 7423 ms 1.45
lotsOfClassesOneDir-componentSetCreate-win32 2174 ms 1450 ms 1.50
lotsOfClassesOneDir-sourceToMdapi-win32 23820 ms 16727 ms 1.42
lotsOfClassesOneDir-sourceToZip-win32 16148 ms 11172 ms 1.45
lotsOfClassesOneDir-mdapiToSource-win32 19594 ms 13474 ms 1.45

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.