commit 36002bb24724e9ffd4712db3f3da42200088ad53 Author: n Date: Wed Apr 30 21:52:02 2025 +0100 initial commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5c5d155 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM denoland/deno:2.3.1 + +RUN mkdir /app +WORKDIR /app +COPY . /app + +EXPOSE 8000 +ENTRYPOINT ["deno", "serve", "--unstable-sloppy-imports", "/app"] diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..638a526 --- /dev/null +++ b/index.ts @@ -0,0 +1,42 @@ +import { Buffer } from "node:buffer" +import { inflateRawSync } from "node:zlib" +import { parse as parseXML } from "https://deno.land/x/xml@6.0.4/parse.ts" + +const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'OPTIONS, POST' } + +export default { + async fetch(req) { + if (req.method === 'OPTIONS') return new Response("OK", { headers: corsHeaders }) + if (req.method !== 'POST') return new Response("Only POST requests are allowed.", { status: 400 }) + const uri = new URL(req.url) + const query = new URLSearchParams(uri.search) + let buffer, inflated, output = null + + const body = req.body && await req.text() || false + if (!body) return new Response('POST request must have a body', { status: 400 }) + + try { + buffer = Buffer.from(body, 'base64') + if (!buffer) return new Response('Failed to convert body to Buffer.', { status: 500 }) + } catch (err) { + return new Response(`Failed to convert body to Buffer: ${err}`, { status: 500 }) + } + + try { + inflated = inflateRawSync(buffer) + if (!inflated) return new Response('Failed to inflate Buffer.', { status: 500 }) + } catch (err) { + return new Response(`Failed to inflate Buffer: ${err}`, { status: 500 }) + } + + try { + output = inflated.toString('utf-8') + if (!output) return new Response('Failed to convert output to UTF-8.', { status: 500 }) + } catch (err) { + return new Response(`Failed to convert output to UTF-8: ${err}`, { status: 500 }) + } + + if (query.has('json')) output = JSON.stringify(parseXML(output)) + return new Response(output) + } +}