Handle binary responses and compression
mainAPI Gateway requires binary responses to be base64-encoded. @fastify/aws-lambda handles this via two methods:
binaryMimeTypes: An allowlist ofContent-Typevalues (e.g.,image/png).enforceBase64: A custom function for logic-based encoding.
Note: If you provide a custom enforceBase64 function, it replaces the built-in default (which automatically handles compressed responses like gzip or br). If you still want compression-based encoding, you must re-implement that check in your custom function.
// Example: Custom rule for binary detection
exports.handler = awsLambdaFastify(app, {
binaryMimeTypes: ['application/octet-stream', 'image/png'],
enforceBase64: (res) => {
if (res.headers['content-type'] === 'application/x-protobuf') return true
const enc = res.headers['content-encoding']
return !!enc && enc !== 'identity'
}
})