From f1465f8c539cae9c6bad56043e721a94e5c413b5 Mon Sep 17 00:00:00 2001 From: Bhavnoor Singh Saroya Date: Thu, 10 Jul 2025 16:59:51 -0700 Subject: [PATCH] what the heck is a cors amiright --- index.html | 57 ++++++++++++++++++++++++++++++++++++++ trinket-proxy/Dockerfile | 39 ++++++++++++++++++++++++++ trinket-proxy/fly.toml | 22 +++++++++++++++ trinket-proxy/index.js | 50 +++++++++++++++++++++++++++++++++ trinket-proxy/package.json | 19 +++++++++++++ 5 files changed, 187 insertions(+) create mode 100644 index.html create mode 100644 trinket-proxy/Dockerfile create mode 100644 trinket-proxy/fly.toml create mode 100644 trinket-proxy/index.js create mode 100644 trinket-proxy/package.json diff --git a/index.html b/index.html new file mode 100644 index 0000000..1100e56 --- /dev/null +++ b/index.html @@ -0,0 +1,57 @@ + + + + + + + Byte Camp + + + +
+

Byte Camp

+
+
+ + + + + +
+ + + diff --git a/trinket-proxy/Dockerfile b/trinket-proxy/Dockerfile new file mode 100644 index 0000000..5ed0820 --- /dev/null +++ b/trinket-proxy/Dockerfile @@ -0,0 +1,39 @@ +# syntax = docker/dockerfile:1 + +# Adjust NODE_VERSION as desired +ARG NODE_VERSION=22.13.1 +FROM node:${NODE_VERSION}-slim AS base + +LABEL fly_launch_runtime="Node.js" + +# Node.js app lives here +WORKDIR /app + +# Set production environment +ENV NODE_ENV="production" + + +# Throw-away build stage to reduce size of final image +FROM base AS build + +# Install packages needed to build node modules +RUN apt-get update -qq && \ + apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3 + +# Install node modules +COPY package-lock.json package.json ./ +RUN npm ci + +# Copy application code +COPY . . + + +# Final stage for app image +FROM base + +# Copy built application +COPY --from=build /app /app + +# Start the server by default, this can be overwritten at runtime +EXPOSE 3000 +CMD [ "node", "index.js" ] diff --git a/trinket-proxy/fly.toml b/trinket-proxy/fly.toml new file mode 100644 index 0000000..00f42c7 --- /dev/null +++ b/trinket-proxy/fly.toml @@ -0,0 +1,22 @@ +# fly.toml app configuration file generated for trinket-proxy on 2025-07-10T16:44:50-07:00 +# +# See https://fly.io/docs/reference/configuration/ for information about how to use this file. +# + +app = 'trinket-proxy' +primary_region = 'sea' + +[build] + +[http_service] + internal_port = 3000 + force_https = true + auto_stop_machines = 'suspend' + auto_start_machines = true + min_machines_running = 0 + processes = ['app'] + +[[vm]] + memory = '1gb' + cpu_kind = 'shared' + cpus = 1 diff --git a/trinket-proxy/index.js b/trinket-proxy/index.js new file mode 100644 index 0000000..329909e --- /dev/null +++ b/trinket-proxy/index.js @@ -0,0 +1,50 @@ +const express = require('express'); +const { createProxyMiddleware } = require('http-proxy-middleware'); +const https = require('https'); + +const app = express(); +localTesting = false; // Set to true for testing, false for production +const proxyServerUrl = localTesting ? 'https://trinket-proxy.fly.dev' : 'http://localhost:3000'; + +const modifyHtml = (html) => { + return html + // Style injection for dark mode + .replace( + /
/g, + '
' + ) + // Rewrite absolute URLs + .replace(/https:\/\/trinket\.io/g, proxyServerUrl) + .replace(/\/\/trinket\.io/g, proxyServerUrl); +}; + +app.use('/', async (req, res, next) => { + const targetUrl = `https://trinket.io${req.originalUrl}`; + + // Only modify HTML pages (embed endpoints) + if (req.originalUrl.startsWith('/embed')) { + https.get(targetUrl, (proxyRes) => { + let body = ''; + proxyRes.on('data', (chunk) => body += chunk); + proxyRes.on('end', () => { + res.set('Content-Type', 'text/html'); + res.send(modifyHtml(body)); + }); + }).on('error', (err) => { + console.error('Proxy error:', err); + res.sendStatus(500); + }); + } else { + // Static content (JS, CSS, images, etc.) + createProxyMiddleware({ + target: 'https://trinket.io', + changeOrigin: true, + selfHandleResponse: false, + })(req, res, next); + } +}); + +const PORT = 3000; +app.listen(PORT, () => { + console.log(`Trinket proxy running at http://localhost:${PORT}`); +}); diff --git a/trinket-proxy/package.json b/trinket-proxy/package.json new file mode 100644 index 0000000..44ea458 --- /dev/null +++ b/trinket-proxy/package.json @@ -0,0 +1,19 @@ +{ + "name": "trinket-proxy", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "", + "dependencies": { + "express": "^5.1.0", + "http-proxy-middleware": "^2.0.6" + }, + "devDependencies": { + "@flydotio/dockerfile": "^0.7.10" + } +}