Skip to content

Commit dade3a1

Browse files
committed
use gzip for fixtures
1 parent 718dd0e commit dade3a1

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

src/scraper/test-support/http-recorder.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/**
22
* Simple HTTP recorder for integration tests.
33
*
4-
* Records real HTTP responses to fixture files on first run,
4+
* Records real HTTP responses to gzipped fixture files on first run,
55
* replays from fixtures on subsequent runs.
66
*/
77

88
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
99
import { join } from 'node:path'
10+
import { gunzipSync, gzipSync } from 'node:zlib'
1011
import type { FetchFn } from '../types'
1112

1213
/**
@@ -56,15 +57,22 @@ export class HttpRecorder {
5657
const method = init?.method ?? 'GET'
5758
const fixturePath = this.getFixturePath(method, url)
5859

60+
// Check for gzipped fixture first, then legacy uncompressed
5961
if (existsSync(fixturePath)) {
60-
return this.replay(fixturePath)
62+
return this.replay(fixturePath, true)
63+
}
64+
const legacyPath = fixturePath.replace(/\.gz$/, '')
65+
if (existsSync(legacyPath)) {
66+
return this.replay(legacyPath, false)
6167
}
6268

6369
return this.record(url, method, input, init, fixturePath)
6470
}
6571

66-
private replay(fixturePath: string): Response {
67-
const fixture: RecordedFixture = JSON.parse(readFileSync(fixturePath, 'utf-8'))
72+
private replay(fixturePath: string, gzipped: boolean): Response {
73+
const raw = readFileSync(fixturePath)
74+
const json = gzipped ? gunzipSync(raw).toString('utf-8') : raw.toString('utf-8')
75+
const fixture: RecordedFixture = JSON.parse(json)
6876
const response = new Response(fixture.body, {
6977
status: fixture.status,
7078
headers: new Headers(fixture.headers)
@@ -100,7 +108,8 @@ export class HttpRecorder {
100108
recordedAt: new Date().toISOString()
101109
}
102110

103-
writeFileSync(fixturePath, JSON.stringify(fixture, null, 2))
111+
const compressed = gzipSync(JSON.stringify(fixture, null, 2))
112+
writeFileSync(fixturePath, compressed)
104113

105114
const result = new Response(body, {
106115
status: response.status,
@@ -115,7 +124,7 @@ export class HttpRecorder {
115124
.replace(/^https?:\/\//, '')
116125
.replace(/[^a-zA-Z0-9.-]/g, '_')
117126
.slice(0, 100)
118-
const filename = `${method.toLowerCase()}_${safeUrl}.json`
127+
const filename = `${method.toLowerCase()}_${safeUrl}.json.gz`
119128
return join(this.fixturesDir, filename)
120129
}
121130
}

0 commit comments

Comments
 (0)