Skip to content

Commit 7856ec1

Browse files
authored
feat: gzip html reporter's metadata (#3113)
1 parent 7347179 commit 7856ec1

File tree

6 files changed

+33
-8
lines changed

6 files changed

+33
-8
lines changed

packages/ui/client/composables/client/static.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { BirpcReturn } from 'birpc'
22
import type { VitestClient } from '@vitest/ws-client'
33
import type { WebSocketHandlers } from 'vitest/src/api/types'
44
import { parse } from 'flatted'
5+
import { decompressSync, strFromU8 } from 'fflate'
56
import type { File, ModuleGraphData, ResolvedConfig } from 'vitest/src/types'
67
import { StateManager } from '../../../../vitest/src/node/state'
78

@@ -72,7 +73,15 @@ export function createStaticClient(): VitestClient {
7273

7374
async function registerMetadata() {
7475
const res = await fetch(window.METADATA_PATH!)
75-
metadata = parse(await res.text()) as HTMLReportMetadata
76+
const contentType = res.headers.get('content-type')?.toLowerCase() || ''
77+
if (contentType.includes('application/gzip')) {
78+
const compressed = new Uint8Array(await res.arrayBuffer())
79+
const decompressed = strFromU8(decompressSync(compressed))
80+
metadata = parse(decompressed) as HTMLReportMetadata
81+
}
82+
else {
83+
metadata = parse(await res.text()) as HTMLReportMetadata
84+
}
7685
const event = new Event('open')
7786
ctx.ws.dispatchEvent(event)
7887
}

packages/ui/node/reporter.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { promises as fs } from 'node:fs'
22
import { fileURLToPath } from 'node:url'
3+
import { promisify } from 'node:util'
4+
import { gzip, constants as zlibConstants } from 'node:zlib'
35
import { basename, dirname, relative, resolve } from 'pathe'
46
import c from 'picocolors'
57
import fg from 'fast-glob'
@@ -48,11 +50,15 @@ export default class HTMLReporter implements Reporter {
4850
const htmlFileName = basename(htmlFile)
4951
const htmlDir = resolve(this.ctx.config.root, dirname(htmlFile))
5052

51-
const metaFile = resolve(htmlDir, 'html.meta.json')
53+
const metaFile = resolve(htmlDir, 'html.meta.json.gz')
5254

5355
await fs.mkdir(resolve(htmlDir, 'assets'), { recursive: true })
5456

55-
await fs.writeFile(metaFile, report, 'utf-8')
57+
const promiseGzip = promisify(gzip)
58+
const data = await promiseGzip(report, {
59+
level: zlibConstants.Z_BEST_COMPRESSION,
60+
})
61+
await fs.writeFile(metaFile, data, 'base64')
5662
const ui = resolve(distDir, 'client')
5763
// copy ui
5864
const files = fg.sync('**/*', { cwd: ui })

packages/ui/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"dependencies": {
4242
"@vitest/utils": "workspace:*",
4343
"fast-glob": "^3.2.12",
44+
"fflate": "^0.7.4",
4445
"flatted": "^3.2.7",
4546
"pathe": "^1.1.0",
4647
"picocolors": "^1.0.0",

packages/ui/vite.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const config: UserConfig = {
6565
name: 'debug-html-report',
6666
apply: 'serve',
6767
transformIndexHtml(html) {
68-
return html.replace('<!-- !LOAD_METADATA! -->', `<script>window.METADATA_PATH="${debugLink}/html.meta.json"</script>`)
68+
return html.replace('<!-- !LOAD_METADATA! -->', `<script>window.METADATA_PATH="${debugLink}/html.meta.json.gz"</script>`)
6969
},
7070
},
7171
],

pnpm-lock.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/reporters/tests/html.test.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs'
2+
import zlib from 'zlib'
23
import { resolve } from 'pathe'
34
import { execa } from 'execa'
45
import { describe, expect, it } from 'vitest'
@@ -21,7 +22,8 @@ describe.skipIf(skip)('html reporter', async () => {
2122
},
2223
stdio: 'inherit',
2324
}).catch(e => e)
24-
const metaJson = fs.readFileSync(resolve(root, `${basePath}/html.meta.json`), { encoding: 'utf-8' })
25+
const metaJsonGzipeed = fs.readFileSync(resolve(root, `${basePath}/html.meta.json.gz`))
26+
const metaJson = zlib.gunzipSync(metaJsonGzipeed).toString('utf-8')
2527
const indexHtml = fs.readFileSync(resolve(root, `${basePath}/index.html`), { encoding: 'utf-8' })
2628
const resultJson = parse(metaJson.replace(new RegExp(vitestRoot, 'g'), '<rootDir>'))
2729
resultJson.config = {} // doesn't matter for a test
@@ -38,7 +40,7 @@ describe.skipIf(skip)('html reporter', async () => {
3840
expect(task.result.error).not.toBeDefined()
3941
expect(task.result.logs).not.toBeDefined()
4042
expect(resultJson).toMatchSnapshot(`tests are ${expected}`)
41-
expect(indexHtml).toMatch('window.METADATA_PATH="html.meta.json"')
43+
expect(indexHtml).toMatch('window.METADATA_PATH="html.meta.json.gz"')
4244
}, 120000)
4345

4446
it('resolves to "failing" status for test file "json-fail"', async () => {
@@ -52,7 +54,8 @@ describe.skipIf(skip)('html reporter', async () => {
5254
},
5355
stdio: 'inherit',
5456
}).catch(e => e)
55-
const metaJson = fs.readFileSync(resolve(root, `${basePath}/html.meta.json`), { encoding: 'utf-8' })
57+
const metaJsonGzipped = fs.readFileSync(resolve(root, `${basePath}/html.meta.json.gz`))
58+
const metaJson = zlib.gunzipSync(metaJsonGzipped).toString('utf-8')
5659
const indexHtml = fs.readFileSync(resolve(root, `${basePath}/index.html`), { encoding: 'utf-8' })
5760
const resultJson = parse(metaJson.replace(new RegExp(vitestRoot, 'g'), '<rootDir>'))
5861
resultJson.config = {} // doesn't matter for a test
@@ -77,6 +80,6 @@ describe.skipIf(skip)('html reporter', async () => {
7780
task.logs[0].taskId = 0
7881
task.logs[0].time = 0
7982
expect(resultJson).toMatchSnapshot(`tests are ${expected}`)
80-
expect(indexHtml).toMatch('window.METADATA_PATH="html.meta.json"')
83+
expect(indexHtml).toMatch('window.METADATA_PATH="html.meta.json.gz"')
8184
}, 120000)
8285
})

0 commit comments

Comments
 (0)