parse-multipart.mjs
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠ breaking
3 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
2 commits
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠
3 days ago
sha256:9103f98c89257ed2b01c237cea895dabb3e85ea337dccb1161c175e4422355b6
docs: accept Calendar Events v0 spec with Phase 0 security …
Human
3 days ago