Skip to content

Commit

Permalink
feat: compress large JSON requests (#980)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sorccu authored Nov 15, 2024
1 parent bb421ba commit 01e8493
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"git-repo-info": "2.1.1",
"glob": "10.3.1",
"indent-string": "4.0.0",
"json-stream-stringify": "3.1.6",
"json5": "2.2.3",
"jwt-decode": "3.1.2",
"log-symbols": "4.1.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/rest/projects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AxiosInstance } from 'axios'
import type { GitInformation } from '../services/util'
import { compressJSONPayload } from './util'

export interface Project {
name: string
Expand Down Expand Up @@ -60,6 +61,7 @@ class Projects {
return this.api.post<ProjectDeployResponse>(
`/next-v2/projects/deploy?dryRun=${dryRun}&scheduleOnDeploy=${scheduleOnDeploy}`,
resources,
{ transformRequest: compressJSONPayload },
)
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/rest/test-sessions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AxiosInstance } from 'axios'
import { GitInformation } from '../services/util'
import { RetryStrategy } from '../constructs'
import { compressJSONPayload } from './util'

type RunTestSessionRequest = {
name: string,
Expand Down Expand Up @@ -38,7 +39,9 @@ class TestSessions {
}

run (payload: RunTestSessionRequest) {
return this.api.post('/next/test-sessions/run', payload)
return this.api.post('/next/test-sessions/run', payload, {
transformRequest: compressJSONPayload,
})
}

trigger (payload: TriggerTestSessionRequest) {
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/src/rest/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import zlib from 'node:zlib'

import { AxiosRequestHeaders } from 'axios'
import { JsonStreamStringify } from 'json-stream-stringify'

export function compressJSONPayload (data: any, headers: AxiosRequestHeaders) {
headers['Content-Type'] = 'application/json'
headers['Content-Encoding'] = 'gzip'

const zipper = zlib.createGzip()
const streamer = new JsonStreamStringify(data)

return streamer.pipe(zipper)
}

0 comments on commit 01e8493

Please sign in to comment.