emptygif
Idioma: Português

Todas as linguagens e frameworks

srvx (universal fetch server) TypeScript

Um snippet em TypeScript para srvx (universal fetch server) que responde às requisições para /pixel.gif com o GIF vazio, decodificado uma única vez na inicialização.

srvx (universal fetch server) TypeScript

Instalar
npm i srvx
Arquivo
server.ts
Executar
npx srvx server.ts
import { serve } from 'srvx';

const GIF = Uint8Array.from(atob('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='), (c) => c.charCodeAt(0));

// The same code runs on Node.js, Deno and Bun.
serve({
  port: 8080,
  fetch(request) {
    if (new URL(request.url).pathname !== '/pixel.gif') {
      return new Response('Not Found', { status: 404 });
    }
    return new Response(GIF, {
      headers: {
        'Content-Type': 'image/gif',
        'Content-Length': String(GIF.length),
        'Cache-Control': 'no-store',
      },
    });
  },
});

Executamos este snippet e comparamos os bytes que ele retornou.

Verificar

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

Site oficial: srvx (universal fetch server)

Mais em TypeScript