|
2 | 2 | // Same source URL, different content between deployments must yield different public URLs, |
3 | 3 | // otherwise a long-cached asset serves stale bytes against a fresh SRI hash. |
4 | 4 | import type { AssetBundlerTransformerOptions } from '../../packages/script/src/plugins/transform' |
| 5 | +import { createHash } from 'node:crypto' |
5 | 6 | import { hash } from 'ohash' |
6 | 7 | import { hasProtocol } from 'ufo' |
7 | 8 | import { describe, expect, it, vi } from 'vitest' |
@@ -100,13 +101,15 @@ describe('two-deploy bundle repro (#724)', () => { |
100 | 101 | }) |
101 | 102 |
|
102 | 103 | it('integrity hash matches the final served bytes', async () => { |
103 | | - mockUpstreamBody(Buffer.from('/* adsbygoogle v1 */')) |
| 104 | + const body = Buffer.from('/* adsbygoogle v1 */') |
| 105 | + mockUpstreamBody(body) |
104 | 106 | const code = await runTransform(src, { integrity: true }) |
105 | 107 | const url = extractPublicUrl(code) |
106 | 108 | const integrityMatch = code.match(/integrity: '(sha384-[^']+)'/) |
107 | 109 |
|
| 110 | + const expectedIntegrity = `sha384-${createHash('sha384').update(body).digest('base64')}` |
108 | 111 | expect(url).toMatch(/[a-f0-9]{16}\.js$/) |
109 | | - expect(integrityMatch).toBeTruthy() |
| 112 | + expect(integrityMatch?.[1]).toBe(expectedIntegrity) |
110 | 113 | // Filename and integrity both derive from the same post-rewrite bytes, |
111 | 114 | // so they cannot drift apart across deployments. |
112 | 115 | }) |
|
0 commit comments