emptygif
Idioma: Español

Todos los lenguajes y frameworks

AWS Lambda@Edge (CloudFront) JavaScript

Un fragmento en JavaScript para AWS Lambda@Edge (CloudFront) que responde a las peticiones a /pixel.gif con el GIF vacío, decodificado una sola vez al arrancar.

AWS Lambda@Edge (CloudFront) JavaScript

Archivo
index.js
// Viewer-request trigger: answer /pixel.gif at the edge, pass everything else through.
const GIF_BASE64 = 'R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==';

exports.handler = async (event) => {
  const request = event.Records[0].cf.request;
  if (request.uri !== '/pixel.gif') return request;
  return {
    status: '200',
    statusDescription: 'OK',
    headers: {
      'content-type': [{ key: 'Content-Type', value: 'image/gif' }],
      'cache-control': [{ key: 'Cache-Control', value: 'no-store' }],
    },
    body: GIF_BASE64,
    bodyEncoding: 'base64',
  };
};

Ejecutamos este fragmento y comparamos los bytes que devolvió.

Comprobar

curl -sI http://localhost:8080/pixel.gif

Sitio oficial: AWS Lambda@Edge (CloudFront)

Más en JavaScript