(app: Express, cache: Keyv)
| 52 | * storage (the data plane). Artifact bytes never pass through the API. |
| 53 | */ |
| 54 | export const setupAnalysisHandler = (app: Express, cache: Keyv) => { |
| 55 | ensureAnalysesRegistered(); |
| 56 | |
| 57 | app.post("/api/analysis", analysisRateLimiter, async (req, res) => { |
| 58 | const { user } = req; |
| 59 | if (!user) { |
| 60 | res.status(401).json({ error: "Authentication required" }); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | const body = req.body as AnalysisRequest | undefined; |
| 65 | if (!body || !Array.isArray(body.requests)) { |
| 66 | res.status(400).json({ |
| 67 | error: "Request body must be { requests: AnalysisInvocation[] }", |
| 68 | }); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | if (body.requests.length > MAX_BATCH_SIZE) { |
| 73 | res |
| 74 | .status(400) |
| 75 | .json({ error: `Too many requests (max ${MAX_BATCH_SIZE})` }); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | const { graphApi, uploadProvider } = req.context; |
| 80 | |
| 81 | const results = await Promise.all( |
| 82 | body.requests.map((invocation) => |
| 83 | resolveInvocation({ |
| 84 | invocation, |
| 85 | actorId: user.accountId, |
| 86 | graphApi, |
| 87 | uploadProvider, |
| 88 | cache, |
| 89 | }), |
| 90 | ), |
| 91 | ); |
| 92 | |
| 93 | for (const [index, result] of results.entries()) { |
| 94 | const invocation = body.requests[index]!; |
| 95 | telemetry.track(req, "analysis_run", { |
| 96 | analysis: invocation.analysis, |
| 97 | webId: invocation.webId, |
| 98 | status: result.status, |
| 99 | artifactCount: result.artifacts?.length ?? 0, |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | res.json({ results } satisfies AnalysisResponse); |
| 104 | }); |
| 105 | }; |
no test coverage detected