|
| 1 | +import { expect, test } from "vitest"; |
| 2 | +import { sanitize, clearWindow, removed } from "../src/index"; |
| 3 | + |
| 4 | +const CHUNK = 500; |
| 5 | +const ROUNDS = 5; |
| 6 | +const DIRTY_HTML = `<div onclick="alert(1)"><script>xss</script><b>safe</b></div>`; |
| 7 | + |
| 8 | +function heapMB(): number { |
| 9 | + global.gc?.(); |
| 10 | + return process.memoryUsage().heapUsed / 1024 / 1024; |
| 11 | +} |
| 12 | + |
| 13 | +function runChunk(n: number): { avgMs: number } { |
| 14 | + const start = performance.now(); |
| 15 | + for (let i = 0; i < n; i++) { |
| 16 | + sanitize(DIRTY_HTML); |
| 17 | + } |
| 18 | + return { avgMs: (performance.now() - start) / n }; |
| 19 | +} |
| 20 | + |
| 21 | +// Yield to the event loop so GC can sweep closed jsdom windows |
| 22 | +function tick(): Promise<void> { |
| 23 | + return new Promise((resolve) => setTimeout(resolve, 0)); |
| 24 | +} |
| 25 | + |
| 26 | +test("clearWindow resets heap growth and prevents time degradation", { timeout: 60_000 }, async () => { |
| 27 | + // Warm up |
| 28 | + runChunk(50); |
| 29 | + |
| 30 | + // --- Run WITHOUT clearing --- |
| 31 | + const noClearHeaps: number[] = []; |
| 32 | + const noClearTimes: number[] = []; |
| 33 | + |
| 34 | + for (let r = 0; r < ROUNDS; r++) { |
| 35 | + const { avgMs } = runChunk(CHUNK); |
| 36 | + noClearTimes.push(avgMs); |
| 37 | + noClearHeaps.push(heapMB()); |
| 38 | + } |
| 39 | + |
| 40 | + const noClearHeapGrowth = noClearHeaps[ROUNDS - 1] - noClearHeaps[0]; |
| 41 | + const noClearTimeRatio = noClearTimes[ROUNDS - 1] / noClearTimes[0]; |
| 42 | + |
| 43 | + // Reset before the next phase |
| 44 | + clearWindow(); |
| 45 | + await tick(); |
| 46 | + global.gc?.(); |
| 47 | + await tick(); |
| 48 | + |
| 49 | + // --- Run WITH clearing between rounds --- |
| 50 | + const withClearHeaps: number[] = []; |
| 51 | + const withClearTimes: number[] = []; |
| 52 | + |
| 53 | + for (let r = 0; r < ROUNDS; r++) { |
| 54 | + const { avgMs } = runChunk(CHUNK); |
| 55 | + withClearTimes.push(avgMs); |
| 56 | + clearWindow(); |
| 57 | + await tick(); |
| 58 | + withClearHeaps.push(heapMB()); |
| 59 | + } |
| 60 | + |
| 61 | + const withClearHeapGrowth = withClearHeaps[ROUNDS - 1] - withClearHeaps[0]; |
| 62 | + const withClearTimeRatio = withClearTimes[ROUNDS - 1] / withClearTimes[0]; |
| 63 | + |
| 64 | + // --- Report --- |
| 65 | + console.table({ |
| 66 | + "Without clearWindow": { |
| 67 | + "heap start (MB)": noClearHeaps[0].toFixed(1), |
| 68 | + "heap end (MB)": noClearHeaps[ROUNDS - 1].toFixed(1), |
| 69 | + "heap growth (MB)": noClearHeapGrowth.toFixed(1), |
| 70 | + "avg ms/call first round": noClearTimes[0].toFixed(3), |
| 71 | + "avg ms/call last round": noClearTimes[ROUNDS - 1].toFixed(3), |
| 72 | + "time ratio (last/first)": noClearTimeRatio.toFixed(2), |
| 73 | + }, |
| 74 | + "With clearWindow": { |
| 75 | + "heap start (MB)": withClearHeaps[0].toFixed(1), |
| 76 | + "heap end (MB)": withClearHeaps[ROUNDS - 1].toFixed(1), |
| 77 | + "heap growth (MB)": withClearHeapGrowth.toFixed(1), |
| 78 | + "avg ms/call first round": withClearTimes[0].toFixed(3), |
| 79 | + "avg ms/call last round": withClearTimes[ROUNDS - 1].toFixed(3), |
| 80 | + "time ratio (last/first)": withClearTimeRatio.toFixed(2), |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + // Without clearing, per-call time should degrade noticeably (>1.5x) |
| 85 | + expect(noClearTimeRatio).toBeGreaterThan(1.5); |
| 86 | + |
| 87 | + // With clearing, per-call time should stay roughly stable (<1.5x) |
| 88 | + expect(withClearTimeRatio).toBeLessThan(1.5); |
| 89 | + |
| 90 | + // With clearing, heap growth should be significantly less (only reliable with --expose-gc) |
| 91 | + if (global.gc) { |
| 92 | + expect(withClearHeapGrowth).toBeLessThan(noClearHeapGrowth); |
| 93 | + } |
| 94 | +}); |
| 95 | + |
| 96 | +test("sanitize works correctly after clearWindow", () => { |
| 97 | + const before = sanitize(DIRTY_HTML); |
| 98 | + clearWindow(); |
| 99 | + const after = sanitize(DIRTY_HTML); |
| 100 | + |
| 101 | + expect(before).toBe("<div><b>safe</b></div>"); |
| 102 | + expect(after).toBe(before); |
| 103 | +}); |
| 104 | + |
| 105 | +test("removed named export reflects new instance after clearWindow", () => { |
| 106 | + sanitize('<img src="x" onerror="alert(1)">'); |
| 107 | + expect(removed.length).toBeGreaterThan(0); |
| 108 | + |
| 109 | + clearWindow(); |
| 110 | + |
| 111 | + // After clearing, removed should be empty (fresh instance, no sanitize calls yet) |
| 112 | + expect(removed.length).toBe(0); |
| 113 | + |
| 114 | + // After sanitizing on the new instance, removed should update |
| 115 | + sanitize('<img src="x" onerror="alert(1)">'); |
| 116 | + expect(removed.length).toBeGreaterThan(0); |
| 117 | +}); |
0 commit comments