mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(parsers): past 10,000 identical elements every id is still unique (#7919)
This commit is contained in:
@@ -72,6 +72,12 @@ function contentKey(el: Element): string {
|
||||
* twin added or removed), so a save carrying an id served before the edit misses or hits the other
|
||||
* twin until the watcher reloads the preview.
|
||||
*/
|
||||
// Past 10,000 twins the 4-char window is crowded: widen the id with the dup counter, still checked against the set.
|
||||
function dupId(key: string, dup: number): string {
|
||||
if (dup > 10000) return `hf-${(fnv1a(key) >>> 0).toString(36)}-${dup}`;
|
||||
return toHfId(fnv1a(dup ? `${key}#${dup}` : key));
|
||||
}
|
||||
|
||||
// WIRE CONTRACT: id minting is content-keyed (FNV1a of tag, sorted attrs, own text). R7's
|
||||
// preview route relies on mintHfId producing identical ids across mint contexts
|
||||
// (served copy vs. the source a save parses) — see preview.test.ts
|
||||
@@ -84,22 +90,8 @@ export function mintHfId(el: Element, assigned: Set<string>): string {
|
||||
const twins = lastDup.get(assigned) ?? new Map<string, number>();
|
||||
lastDup.set(assigned, twins);
|
||||
let dup = twins.get(key) ?? 0;
|
||||
let id =
|
||||
dup > 10000
|
||||
? `hf-${(fnv1a(key) >>> 0).toString(36)}-${dup}`
|
||||
: toHfId(fnv1a(dup ? `${key}#${dup}` : key));
|
||||
while (dup <= 10000 && assigned.has(id)) {
|
||||
dup += 1;
|
||||
// Graceful fallback instead of a hard throw: rehashing only fails to find a
|
||||
// free 4-char slot in a pathological document (~1.6M identical elements).
|
||||
// Rather than crash the whole parse, widen the id with the dup counter —
|
||||
// still deterministic and unique, just longer than the 4-char norm.
|
||||
if (dup > 10000) {
|
||||
id = `hf-${(fnv1a(key) >>> 0).toString(36)}-${dup}`;
|
||||
break;
|
||||
}
|
||||
id = toHfId(fnv1a(`${key}#${dup}`));
|
||||
}
|
||||
let id = dupId(key, dup);
|
||||
while (assigned.has(id)) id = dupId(key, ++dup);
|
||||
twins.set(key, dup);
|
||||
assigned.add(id);
|
||||
return id;
|
||||
|
||||
@@ -245,6 +245,14 @@ describe("ensureHfIds", () => {
|
||||
expect(lookups).toBeLessThan(3 * twins);
|
||||
});
|
||||
|
||||
it("past 10,000 identical elements, every id is still unique", () => {
|
||||
const { document } = parseHTML(`<p class="x">same</p>`);
|
||||
const el = document.querySelector("p") as Element;
|
||||
const assigned = new Set<string>();
|
||||
for (let i = 0; i < 10050; i += 1) mintHfId(el, assigned);
|
||||
expect(assigned.size).toBe(10050);
|
||||
});
|
||||
|
||||
// Post-persist stability: once data-hf-id is written back to source, edits
|
||||
// don't drift the id because the attribute is already present and pinned.
|
||||
it("pinned id survives text edit after first persist", () => {
|
||||
|
||||
Reference in New Issue
Block a user