Skip to content

Commit 8ce311e

Browse files
committed
feat: batched artifacts upload
1 parent a4e2db5 commit 8ce311e

File tree

2 files changed

+66
-25
lines changed

2 files changed

+66
-25
lines changed

src/transform.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import { loadConfig } from './transform/config'
1010
import { FLYTRAP_API_BASE, setFlytrapConfig } from './core/config'
1111
import { log } from './core/logging'
1212
import { Artifact, extractArtifacts } from './transform/artifacts'
13-
import { post } from './core/util'
13+
import { tryCatch } from './core/util'
1414
import { readFileSync } from 'node:fs'
1515
import { excludeDirectoriesIncludeFilePath } from './transform/excludes'
1616
import { containsScriptTags, parseScriptTags } from './transform/parseScriptTags'
17+
import { batchedArtifactsUpload } from './transform/batchedArtifactsUpload'
1718

1819
const transformedFiles: string[] = []
1920

@@ -141,7 +142,23 @@ export const unpluginOptions: UnpluginOptions = {
141142
},
142143
async buildEnd() {
143144
const config = await loadConfig()
144-
if (!config) return
145+
if (!config) {
146+
const log = createHumanLog({
147+
event: 'transform_failed',
148+
explanation: 'config_not_found',
149+
solution: 'define_flytrap_config'
150+
})
151+
throw log.toString()
152+
}
153+
154+
if (!config.projectId || !config.secretApiKey) {
155+
const log = createHumanLog({
156+
event: 'transform_failed',
157+
explanation: 'invalid_config',
158+
solution: 'configuration_fix'
159+
})
160+
throw log.toString()
161+
}
145162
// Find package root
146163
const pkgDirPath = packageDirectorySync()
147164
if (!pkgDirPath) {
@@ -185,37 +202,22 @@ export const unpluginOptions: UnpluginOptions = {
185202
'storage',
186203
`Created ${artifacts.length} artifacts. Size: ${JSON.stringify(artifacts).length}`
187204
)
188-
log.info(
189-
'api-calls',
190-
`Pushing ${artifacts.length} artifacts to the Flytrap API. Payload size: ${
191-
JSON.stringify(artifacts).length
192-
}`
193-
)
194205

195-
const { data, error } = await post(
196-
`${FLYTRAP_API_BASE}/api/v1/artifacts/${config?.projectId}`,
197-
JSON.stringify({
198-
artifacts
199-
}),
200-
{
201-
headers: new Headers({
202-
Authorization: `Bearer ${config?.secretApiKey}`,
203-
'Content-Type': 'application/json'
204-
})
205-
}
206+
const { data: uploadedBatches, error } = await tryCatch(
207+
batchedArtifactsUpload(artifacts, config.secretApiKey, config.projectId)
206208
)
207209
if (error) {
208210
console.error(
209211
`Oops! Something went wrong while pushing artifacts to the Flytrap API. Error:`
210212
)
211213
console.error(error)
212214
}
213-
if (data) {
214-
log.info(
215-
'api-calls',
216-
`Successfully pushed ${artifacts.length} artifacts to the Flytrap API.`
217-
)
218-
}
215+
log.info(
216+
'api-calls',
217+
`Pushed ${artifacts.length} artifacts in ${
218+
uploadedBatches?.length
219+
} batches to the Flytrap API. Payload size: ${JSON.stringify(artifacts).length}`
220+
)
219221
}
220222
}
221223
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { FLYTRAP_API_BASE } from '../core/config'
2+
import { post } from '../core/util'
3+
import { Artifact } from './artifacts'
4+
5+
const ARTIFACTS_BATCH_SIZE = 20
6+
7+
export async function batchedArtifactsUpload(
8+
artifacts: Artifact[],
9+
secretApiKey: string,
10+
projectId: string
11+
) {
12+
const batches: Artifact[][] = []
13+
for (let i = 0; i < artifacts.length; i += ARTIFACTS_BATCH_SIZE) {
14+
const artifactsInBatch = artifacts.slice(i, i + ARTIFACTS_BATCH_SIZE)
15+
batches.push(artifactsInBatch)
16+
}
17+
18+
const uploadedArtifactBatches = await Promise.all(
19+
batches.map(async (batch) => {
20+
const { data, error } = await post(
21+
`${FLYTRAP_API_BASE}/api/v1/artifacts/${projectId}`,
22+
JSON.stringify({
23+
artifacts: batch
24+
}),
25+
{
26+
headers: new Headers({
27+
Authorization: `Bearer ${secretApiKey}`,
28+
'Content-Type': 'application/json'
29+
})
30+
}
31+
)
32+
if (error) {
33+
throw error
34+
}
35+
return data
36+
})
37+
)
38+
return uploadedArtifactBatches
39+
}

0 commit comments

Comments
 (0)