Swadhin Biswas fix(ts): eliminate all 127 TypeScript errors · 4/21/2026
18087d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* CI Artifacts Library
* Upload, download, and manage workflow run artifacts
*/
import { getDatabase, schema } from "@/db";
import { and, eq } from "drizzle-orm";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { createReadStream, createWriteStream } from "fs";
import fs from "fs/promises";
import path from "path";
import type { Readable } from "stream";
import { pipeline } from "stream/promises";
import { createGunzip, createGzip } from "zlib";
import { logger } from "./logger";
// ─── Types ───────────────────────────────────────────────────────────────────
export interface Artifact {
id: string;
runId: string;
jobId: string | null;
name: string;
sizeBytes: number;
mimeType: string | null;
downloadCount: number;
expiresAt: Date | null;
createdAt: Date;
}
export interface UploadArtifactOptions {
runId: string;
jobId?: string;
name: string;
/** Absolute path to the file or directory to upload */
sourcePath: string;
mimeType?: string;
/** Artifact retention in days (default: 90) */
retentionDays?: number;
}
// ─── Configuration ───────────────────────────────────────────────────────────
const ARTIFACTS_BASE_DIR =
process.env.RUNNER_ARTIFACTS_DIR || "./data/actions/artifacts";
const DEFAULT_RETENTION_DAYS = 90;
const MAX_ARTIFACT_SIZE_BYTES = 500 * 1024 * 1024; // 500MB
// ─── Core Functions ──────────────────────────────────────────────────────────
/**
* Upload an artifact from a workflow run.
* Stores the file compressed (gzip) on filesystem and records metadata in DB.
*/
export async function uploadArtifact(
options: UploadArtifactOptions,
): Promise<Artifact> {
const { runId, jobId, name, sourcePath, mimeType, retentionDays } = options;
const id = crypto.randomUUID();
// Validate source exists
const stat = await fs.stat(sourcePath);
if (stat.size > MAX_ARTIFACT_SIZE_BYTES) {
throw new Error(
`Artifact "${name}" exceeds maximum size of ${MAX_ARTIFACT_SIZE_BYTES / (1024 * 1024)}MB`,
);
}
// Calculate expiration
const expiresAt = new Date();
expiresAt.setDate(
expiresAt.getDate() + (retentionDays ?? DEFAULT_RETENTION_DAYS),
);
// Determine storage path: artifacts/<runId>/<artifactId>/<name>.gz
const artifactDir = path.join(ARTIFACTS_BASE_DIR, runId, id);
await fs.mkdir(artifactDir, { recursive: true });
const storedFileName = `${name}.gz`;
const storagePath = path.join(artifactDir, storedFileName);
// Compress and write
if (stat.isDirectory()) {
// For directories, tar+gzip them
const { execAsync } = await import("./exec");
const tarPath = path.join(artifactDir, `${name}.tar.gz`);
await execAsync(
`tar -czf "${tarPath}" -C "${path.dirname(sourcePath)}" "${path.basename(sourcePath)}"`,
{
timeout: 120_000,
},
);
const finalStat = await fs.stat(tarPath);
// Record in DB
const db = getDatabase();
// @ts-expect-error - Drizzle multi-db union type issue
await db.insert(schema.workflowArtifacts).values({
id,
runId,
jobId: jobId ?? null,
name,
sizeBytes: finalStat.size,
storagePath: tarPath,
mimeType: "application/gzip",
downloadCount: 0,
expiresAt,
createdAt: new Date(),
});
logger.info(
{ artifactId: id, name, runId, size: finalStat.size },
"Artifact uploaded (directory)",
);
return {
id,
runId,
jobId: jobId ?? null,
name,
sizeBytes: finalStat.size,
mimeType: "application/gzip",
downloadCount: 0,
expiresAt,
createdAt: new Date(),
};
} else {
// Single file — gzip it
await pipeline(
createReadStream(sourcePath),
createGzip(),
createWriteStream(storagePath),
);
const compressedStat = await fs.stat(storagePath);
// Record in DB
const db = getDatabase();
// @ts-expect-error - Drizzle multi-db union type issue
await db.insert(schema.workflowArtifacts).values({
id,
runId,
jobId: jobId ?? null,
name,
sizeBytes: stat.size, // original size
storagePath,
mimeType: mimeType ?? "application/octet-stream",
downloadCount: 0,
expiresAt,
createdAt: new Date(),
});
logger.info(
{
artifactId: id,
name,
runId,
size: stat.size,
compressed: compressedStat.size,
},
"Artifact uploaded",
);
return {
id,
runId,
jobId: jobId ?? null,
name,
sizeBytes: stat.size,
mimeType: mimeType ?? "application/octet-stream",
downloadCount: 0,
expiresAt,
createdAt: new Date(),
};
}
}
/**
* Download an artifact by run ID and name.
* Returns a readable stream of the decompressed content.
*/
export async function downloadArtifact(
runId: string,
name: string,
): Promise<{ stream: Readable; artifact: Artifact }> {
const db = getDatabase();
const record = await db.query.workflowArtifacts.findFirst({
where: and(
eq(schema.workflowArtifacts.runId, runId),
eq(schema.workflowArtifacts.name, name),
),
});
if (!record) {
throw new Error(`Artifact "${name}" not found for run ${runId}`);
}
// Check if expired
if (record.expiresAt && new Date(record.expiresAt) < new Date()) {
throw new Error(`Artifact "${name}" has expired`);
}
// Verify file exists
try {
await fs.access(record.storagePath);
} catch {
throw new Error(`Artifact file not found on disk: ${record.storagePath}`);
}
// Increment download count
await (db as NodePgDatabase<typeof schema>)
.update(schema.workflowArtifacts)
.set({ downloadCount: (record.downloadCount ?? 0) + 1 })
.where(eq(schema.workflowArtifacts.id, record.id));
// Return decompressed stream
const isGzip =
record.storagePath.endsWith(".gz") ||
record.storagePath.endsWith(".tar.gz");
const readStream = createReadStream(record.storagePath);
const stream =
isGzip && !record.storagePath.endsWith(".tar.gz")
? readStream.pipe(createGunzip())
: readStream;
return {
stream: stream as Readable,
artifact: {
id: record.id,
runId: record.runId,
jobId: record.jobId,
name: record.name,
sizeBytes: record.sizeBytes,
mimeType: record.mimeType,
downloadCount: (record.downloadCount ?? 0) + 1,
expiresAt: record.expiresAt,
createdAt: record.createdAt,
},
};
}
/**
* List all artifacts for a workflow run
*/
export async function listArtifacts(runId: string): Promise<Artifact[]> {
const db = getDatabase();
const records = await db.query.workflowArtifacts.findMany({
where: eq(schema.workflowArtifacts.runId, runId),
orderBy: (artifacts, { desc }) => [desc(artifacts.createdAt)],
});
return records.map((r) => ({
id: r.id,
runId: r.runId,
jobId: r.jobId,
name: r.name,
sizeBytes: r.sizeBytes,
mimeType: r.mimeType,
downloadCount: r.downloadCount ?? 0,
expiresAt: r.expiresAt,
createdAt: r.createdAt,
}));
}
/**
* Delete an artifact (by ID)
*/
export async function deleteArtifact(artifactId: string): Promise<void> {
const db = getDatabase();
const record = await db.query.workflowArtifacts.findFirst({
where: eq(schema.workflowArtifacts.id, artifactId),
});
if (!record) return;
// Remove file from disk
try {
await fs.rm(path.dirname(record.storagePath), {
recursive: true,
force: true,
});
} catch {
logger.warn(
{ artifactId, path: record.storagePath },
"Failed to delete artifact file",
);
}
// Remove DB record
await (db as NodePgDatabase<typeof schema>)
.delete(schema.workflowArtifacts)
.where(eq(schema.workflowArtifacts.id, artifactId));
logger.info({ artifactId, name: record.name }, "Artifact deleted");
}
/**
* Clean up expired artifacts (run periodically)
*/
export async function cleanupExpiredArtifacts(): Promise<number> {
const db = getDatabase();
const { lt } = await import("drizzle-orm");
const expired = await db.query.workflowArtifacts.findMany({
where: lt(schema.workflowArtifacts.expiresAt, new Date()),
});
let cleaned = 0;
for (const record of expired) {
try {
await deleteArtifact(record.id);
cleaned++;
} catch (error) {
logger.error(
{ error, artifactId: record.id },
"Failed to clean expired artifact",
);
}
}
if (cleaned > 0) {
logger.info({ count: cleaned }, "Expired artifacts cleaned up");
}
return cleaned;
}