parse-multipart.mjs
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠ breaking
12 days ago
| 1 | /** |
| 2 | * Minimal multipart/form-data file parser for the Knowtation gateway. |
| 3 | * Used to extract the first file field from a buffered multipart body without |
| 4 | * forwarding the binary to another Lambda function (which causes serialization issues). |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * Parse the first file field from a raw multipart/form-data buffer. |
| 9 | * @param {Buffer} body - full raw multipart body |
| 10 | * @param {string} boundary - boundary string from Content-Type header (without leading --) |
| 11 | * @returns {{ filename: string, contentType: string, data: Buffer } | null} |
| 12 | */ |
| 13 | export function parseMultipartFile(body, boundary) { |
| 14 | const enc = 'binary'; |
| 15 | const bodyStr = body.toString(enc); |
| 16 | const boundaryLine = '--' + boundary; |
| 17 | const parts = bodyStr.split(boundaryLine); |
| 18 | for (const part of parts) { |
| 19 | if (!part || part.startsWith('--')) continue; |
| 20 | const crlfDoubleCrlf = '\r\n\r\n'; |
| 21 | const headerEnd = part.indexOf(crlfDoubleCrlf); |
| 22 | if (headerEnd === -1) continue; |
| 23 | const headerSection = part.slice(0, headerEnd); |
| 24 | if (!headerSection.toLowerCase().includes('content-disposition')) continue; |
| 25 | // Only process parts that have a filename (file fields, not text fields). |
| 26 | const filenameMatch = headerSection.match(/filename="([^"]+)"/i); |
| 27 | if (!filenameMatch) continue; |
| 28 | const ctMatch = headerSection.match(/content-type:\s*([^\r\n]+)/i); |
| 29 | const contentType = ctMatch ? ctMatch[1].trim() : 'application/octet-stream'; |
| 30 | // File data starts after the double CRLF; strip trailing \r\n added by multipart framing. |
| 31 | const dataStart = headerEnd + crlfDoubleCrlf.length; |
| 32 | let dataStr = part.slice(dataStart); |
| 33 | if (dataStr.endsWith('\r\n')) dataStr = dataStr.slice(0, -2); |
| 34 | return { |
| 35 | filename: filenameMatch[1], |
| 36 | contentType, |
| 37 | data: Buffer.from(dataStr, enc), |
| 38 | }; |
| 39 | } |
| 40 | return null; |
| 41 | } |
File History
4 commits
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠
12 days ago
sha256:d8c648b20a4d53b2673c5c082ee7edfa7b2fc9b11080832da1f38807b6bf940b
fix(7C-L1b): route hosted delegation proposals through cani…
Human
minor
⚠
32 days ago
sha256:2827ba9e7632a4b141c50caf1e8f7d77abbc3515be20e7465f2bccb0ac4edf91
fix: repair endpoint now sets has_active_subscription when …
Human
minor
⚠
52 days ago
sha256:6a102aafafdfe7e70a24f4e59740200f0ee713ce7915f1b53e9d4ba5ee8b4410
Initial Muse snapshot
Human
84 days ago