-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): implement an incremental JSON hasher to hash large endpoint…
… examples. (#5517) * fix(cli): implement an incremental JSON hasher to hash large endpoint examples. * Use MAX_DEPTH instead of hard-stopping on circular references. * eslint override * `hashJSON` unit tests. * update tests * Update tests.
- Loading branch information
Showing
17 changed files
with
121 additions
and
45 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
packages/cli/ete-tests/src/tests/dependencies/__snapshots__/dependencies.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
18 changes: 9 additions & 9 deletions
18
packages/cli/ete-tests/src/tests/ir/__snapshots__/ir.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
packages/cli/generation/ir-generator-tests/src/ir/__test__/hashJSON.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { hashJSON } from "@fern-api/ir-generator"; | ||
|
||
describe("hashJSON Function", () => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const generateLargeObject = (depth: number, breadth: number): any => { | ||
if (depth === 0) { | ||
return "LARGE_STRING_VALUE"; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const obj: any = {}; | ||
for (let i = 0; i < breadth; i++) { | ||
obj[`key_${i}`] = generateLargeObject(depth - 1, breadth); | ||
} | ||
return obj; | ||
}; | ||
|
||
it("should hash a reasonably large object without errors", () => { | ||
const largeObj = generateLargeObject(3, 5); | ||
|
||
expect(() => { | ||
const hash = hashJSON(largeObj); | ||
expect(typeof hash).toBe("string"); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it("should hash a very large object without errors", () => { | ||
const largeObj = generateLargeObject(8, 10); | ||
|
||
expect(() => { | ||
const hash = hashJSON(largeObj); | ||
expect(typeof hash).toBe("string"); | ||
}).not.toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 49 additions & 8 deletions
57
packages/cli/generation/ir-generator/src/utils/hashJSON.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,52 @@ | ||
const MAX_DEPTH = 64; | ||
|
||
export function hashJSON(obj: unknown): string { | ||
const jsonString = JSON.stringify(obj); | ||
let hash = 0x811c9dc5; // Random prime number. | ||
for (let i = 0; i < jsonString.length; i++) { | ||
const char = jsonString.charCodeAt(i); | ||
hash ^= char; | ||
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24); | ||
let hash = 0x811c9dc5; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function traverse(value: any, currentDepth: number) { | ||
if (typeof value === "object" && value != null) { | ||
if (currentDepth > MAX_DEPTH) { | ||
updateHash("[MaxDepthExceeded]"); | ||
return; | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
updateHash("["); | ||
for (let i = 0; i < value.length; i++) { | ||
if (i > 0) { | ||
updateHash(","); | ||
} | ||
traverse(value[i], currentDepth + 1); | ||
} | ||
updateHash("]"); | ||
} else { | ||
updateHash("{"); | ||
const keys = Object.keys(value).sort(); | ||
keys.forEach((key, index) => { | ||
if (index > 0) { | ||
updateHash(","); | ||
} | ||
updateHash(key); | ||
updateHash(":"); | ||
traverse(value[key], currentDepth + 1); | ||
}); | ||
updateHash("}"); | ||
} | ||
} else { | ||
updateHash(String(value)); | ||
} | ||
} | ||
const positiveHash = hash >>> 0; | ||
return positiveHash.toString(16); | ||
|
||
function updateHash(str: string) { | ||
for (let i = 0; i < str.length; i++) { | ||
const char = str.charCodeAt(i); | ||
hash ^= char; | ||
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24); | ||
hash >>>= 0; | ||
} | ||
} | ||
|
||
traverse(obj, 0); | ||
return hash.toString(16); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters