Skip to content

Commit

Permalink
Execute the circularReplacer only on circular structure detection
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-joelmut committed Jan 25, 2024
1 parent 2ae44cf commit 1014296
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions libraries/botbuilder-core/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,36 @@ export const assertStoreItems: Assertion<StoreItems> = (val, path) => {
* @param item Item to calculate the change hash for.
*/
export function calculateChangeHash(item: StoreItem): string {
let result = '';
if (!item) {
return '';
return result;
}

const { eTag, ...rest } = item;

const seen = new WeakMap();
const result = JSON.stringify(rest, function circularReplacer(key, value) {
if (value === null || value === undefined || typeof value !== 'object') {
return value;
try {
result = JSON.stringify(rest);
} catch (error) {
if (!error?.message.includes('circular structure')) {
throw error;
}

const path = seen.get(value);
if (path) {
return `[Circular *${path.join('.')}]`;
}
const seen = new WeakMap();
result = JSON.stringify(rest, function circularReplacer(key, value) {
if (value === null || value === undefined || typeof value !== 'object') {
return value;
}

const path = seen.get(value);
if (path) {
return `[Circular *${path.join('.')}]`;
}

const parent = seen.get(this) ?? [];
seen.set(value, [...parent, key]);
return value;
});
const parent = seen.get(this) ?? [];
seen.set(value, [...parent, key]);
return value;
});
}

const hash = createHash('sha256', { encoding: 'utf-8' });
const hashed = hash.update(result).digest('hex');
Expand Down

0 comments on commit 1014296

Please sign in to comment.