Skip to content

Commit 01e8493

Browse files
authored
feat: compress large JSON requests (#980)
When you deploy or test a project, a potentially very large JSON payload is sent to the API server. In large projects, the time to upload the payload can be quite significant (depending on connection speed). This PR introduces gzip compression to payloads sent to relevant endpoints, which should make the payload easier to upload. Additionally, those large payload were stringified fully in memory, causing unnecessarily high memory usage. This PR changes the payload to be stringified and streamed with json-stream-stringify instead, which should decrease memory usage. This change only applies to requests that are now also getting compressed.
1 parent bb421ba commit 01e8493

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"git-repo-info": "2.1.1",
8585
"glob": "10.3.1",
8686
"indent-string": "4.0.0",
87+
"json-stream-stringify": "3.1.6",
8788
"json5": "2.2.3",
8889
"jwt-decode": "3.1.2",
8990
"log-symbols": "4.1.0",

packages/cli/src/rest/projects.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { AxiosInstance } from 'axios'
22
import type { GitInformation } from '../services/util'
3+
import { compressJSONPayload } from './util'
34

45
export interface Project {
56
name: string
@@ -60,6 +61,7 @@ class Projects {
6061
return this.api.post<ProjectDeployResponse>(
6162
`/next-v2/projects/deploy?dryRun=${dryRun}&scheduleOnDeploy=${scheduleOnDeploy}`,
6263
resources,
64+
{ transformRequest: compressJSONPayload },
6365
)
6466
}
6567
}

packages/cli/src/rest/test-sessions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { AxiosInstance } from 'axios'
22
import { GitInformation } from '../services/util'
33
import { RetryStrategy } from '../constructs'
4+
import { compressJSONPayload } from './util'
45

56
type RunTestSessionRequest = {
67
name: string,
@@ -38,7 +39,9 @@ class TestSessions {
3839
}
3940

4041
run (payload: RunTestSessionRequest) {
41-
return this.api.post('/next/test-sessions/run', payload)
42+
return this.api.post('/next/test-sessions/run', payload, {
43+
transformRequest: compressJSONPayload,
44+
})
4245
}
4346

4447
trigger (payload: TriggerTestSessionRequest) {

packages/cli/src/rest/util.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import zlib from 'node:zlib'
2+
3+
import { AxiosRequestHeaders } from 'axios'
4+
import { JsonStreamStringify } from 'json-stream-stringify'
5+
6+
export function compressJSONPayload (data: any, headers: AxiosRequestHeaders) {
7+
headers['Content-Type'] = 'application/json'
8+
headers['Content-Encoding'] = 'gzip'
9+
10+
const zipper = zlib.createGzip()
11+
const streamer = new JsonStreamStringify(data)
12+
13+
return streamer.pipe(zipper)
14+
}

0 commit comments

Comments
 (0)