From b6df5d196fa5238c1382457c87d50cad3a1ec988 Mon Sep 17 00:00:00 2001 From: Severian Date: Sun, 19 Jan 2025 21:47:35 +0800 Subject: [PATCH] chore: remove boilerplate shit --- README.md | 3 + package-lock.json | 4 +- src/app/api/proxy/route.ts | 110 +++++++++++++++++++++---------------- src/app/page.tsx | 8 --- sucker.js | 6 -- 5 files changed, 67 insertions(+), 64 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..d2fe058 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Sucker + +Check package.json for commands, I can't be bothered. diff --git a/package-lock.json b/package-lock.json index 2376481..d6da3d1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "request-interceptor", + "name": "sucker", "version": "0.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "request-interceptor", + "name": "sucker", "version": "0.1.0", "dependencies": { "@radix-ui/react-accordion": "^1.2.2", diff --git a/src/app/api/proxy/route.ts b/src/app/api/proxy/route.ts index 8b5c841..9777f7a 100644 --- a/src/app/api/proxy/route.ts +++ b/src/app/api/proxy/route.ts @@ -1,5 +1,5 @@ -import { NextResponse } from 'next/server'; -import type { NextRequest } from 'next/server'; +import { NextResponse } from "next/server"; +import type { NextRequest } from "next/server"; interface StoredCard extends CardData { timestamp: number; @@ -15,7 +15,9 @@ function generateId(): string { function cleanupExpiredCards() { const now = Date.now(); - extractedCards = extractedCards.filter(card => (now - card.timestamp) < EXPIRY_TIME); + extractedCards = extractedCards.filter( + (card) => now - card.timestamp < EXPIRY_TIME + ); } interface Message { @@ -34,16 +36,18 @@ interface CardData { function extractPersonaName(content: string, personaIndex: number = 0): string { const personaMatches = Array.from(content.matchAll(/'s Persona:/g)); if (personaMatches.length <= personaIndex) return ""; - + const personaIdx = personaMatches[personaIndex].index!; - const lineStartIdx = content.lastIndexOf('\n', personaIdx); + const lineStartIdx = content.lastIndexOf("\n", personaIdx); const lineEndIdx = personaIdx; - - return content.slice(lineStartIdx === -1 ? 0 : lineStartIdx + 1, lineEndIdx).trim(); + + return content + .slice(lineStartIdx === -1 ? 0 : lineStartIdx + 1, lineEndIdx) + .trim(); } function safeReplace(text: string, old: string, newStr: string): string { - return old ? text.replace(new RegExp(old, 'g'), newStr) : text; + return old ? text.replace(new RegExp(old, "g"), newStr) : text; } function extractCardData(messages: Message[]): CardData { @@ -60,7 +64,7 @@ function extractCardData(messages: Message[]): CardData { scenario: "", mes_example: "", personality: "", - first_mes: content1 + first_mes: content1, }; if (personaMatches.length >= 2) { @@ -80,27 +84,32 @@ function extractCardData(messages: Message[]): CardData { if (scenarioMarker) { const scenarioStart = scenarioMarker.index! + scenarioMarker[0].length; const scenarioRemaining = remaining.slice(scenarioStart); - const exampleInScenarioMarker = scenarioRemaining.match(/Example conversations between/); - const scenarioEnd = exampleInScenarioMarker ? exampleInScenarioMarker.index! : scenarioRemaining.length; + const exampleInScenarioMarker = scenarioRemaining.match( + /Example conversations between/ + ); + const scenarioEnd = exampleInScenarioMarker + ? exampleInScenarioMarker.index! + : scenarioRemaining.length; cardData.scenario = scenarioRemaining.slice(0, scenarioEnd).trim(); } if (exampleMarker) { const exampleStart = exampleMarker.index!; const rawExampleStr = remaining.slice(exampleStart).trim(); - const colonIdx = rawExampleStr.indexOf(':'); - cardData.mes_example = colonIdx !== -1 ? - rawExampleStr.slice(colonIdx + 1).trim() : - rawExampleStr.trim(); + const colonIdx = rawExampleStr.indexOf(":"); + cardData.mes_example = + colonIdx !== -1 + ? rawExampleStr.slice(colonIdx + 1).trim() + : rawExampleStr.trim(); } } for (const field in cardData) { - if (field !== 'name') { + if (field !== "name") { const val = cardData[field as keyof CardData]; - if (typeof val === 'string') { - let newVal = safeReplace(val, userName, '{{user}}'); - newVal = safeReplace(newVal, charName, '{{char}}'); + if (typeof val === "string") { + let newVal = safeReplace(val, userName, "{{user}}"); + newVal = safeReplace(newVal, charName, "{{char}}"); cardData[field as keyof CardData] = newVal; } } @@ -110,27 +119,27 @@ function extractCardData(messages: Message[]): CardData { } export async function POST(request: NextRequest) { - if (request.method === 'OPTIONS') { + if (request.method === "OPTIONS") { return new NextResponse(null, { status: 204, headers: { - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'POST, OPTIONS, GET', - 'Access-Control-Allow-Headers': 'Content-Type, Authorization', + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "POST, OPTIONS, GET", + "Access-Control-Allow-Headers": "Content-Type, Authorization", }, }); } try { const body = await request.json(); - + if (!body.messages || body.messages.length < 2) { return NextResponse.json( { error: "Missing messages or insufficient message count" }, - { + { status: 400, headers: { - 'Access-Control-Allow-Origin': '*', + "Access-Control-Allow-Origin": "*", }, } ); @@ -140,25 +149,27 @@ export async function POST(request: NextRequest) { extractedCards.push({ ...cardData, timestamp: Date.now(), - id: generateId() + id: generateId(), }); cleanupExpiredCards(); - return NextResponse.json({ status: "Card stored successfully" }, { - headers: { - 'Access-Control-Allow-Origin': '*', - }, - }); - + return NextResponse.json( + { status: "Card stored successfully" }, + { + headers: { + "Access-Control-Allow-Origin": "*", + }, + } + ); } catch (error) { - console.error('Error processing request:', error); + console.error("Error processing request:", error); return NextResponse.json( { error: "Internal server error" }, - { + { status: 500, headers: { - 'Access-Control-Allow-Origin': '*', + "Access-Control-Allow-Origin": "*", }, } ); @@ -167,24 +178,27 @@ export async function POST(request: NextRequest) { export async function GET() { cleanupExpiredCards(); - - return NextResponse.json({ - status: "online", - cards: extractedCards.map(({ timestamp, ...card }) => card) // Keep ID but remove timestamp - }, { - headers: { - 'Access-Control-Allow-Origin': '*', + + return NextResponse.json( + { + status: "online", + cards: extractedCards.map(({ timestamp, ...card }) => card), }, - }); + { + headers: { + "Access-Control-Allow-Origin": "*", + }, + } + ); } export async function OPTIONS() { return new NextResponse(null, { status: 204, headers: { - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'POST, OPTIONS, GET', - 'Access-Control-Allow-Headers': 'Content-Type, Authorization', + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "POST, OPTIONS, GET", + "Access-Control-Allow-Headers": "Content-Type, Authorization", }, }); -} \ No newline at end of file +} diff --git a/src/app/page.tsx b/src/app/page.tsx index f0cc78b..e75dcae 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -91,7 +91,6 @@ export default function Home() { if (!card.avatarUrl) return; try { - // Use proxy directly instead of attempting CORS const img = new Image(); img.src = `/api/proxy/image?url=${encodeURIComponent(card.avatarUrl)}`; @@ -100,14 +99,12 @@ export default function Home() { img.onerror = reject; }); - // Create a canvas to convert WebP to PNG const canvas = document.createElement("canvas"); canvas.width = img.width; canvas.height = img.height; const ctx = canvas.getContext("2d"); if (!ctx) throw new Error("Could not get canvas context"); - // Draw the image and convert to PNG ctx.drawImage(img, 0, 0); const pngBlob = await new Promise((resolve) => { canvas.toBlob((blob) => { @@ -116,10 +113,8 @@ export default function Home() { }, "image/png"); }); - // Convert blob to array buffer for PNG embedding const arrayBuffer = await pngBlob.arrayBuffer(); - // Prepare card data for embedding const cardData = JSON.stringify({ name: card.name, first_mes: card.first_mes, @@ -129,7 +124,6 @@ export default function Home() { scenario: card.scenario, }); - // Generate PNG with embedded card data const newImageData = Png.Generate(arrayBuffer, cardData); const newFileName = `${ card.name.replace(/\s+/g, "_") || "character" @@ -138,14 +132,12 @@ export default function Home() { type: "image/png", }); - // Download the file const link = URL.createObjectURL(newFile); const a = document.createElement("a"); a.download = newFileName; a.href = link; a.click(); - // Cleanup URL.revokeObjectURL(link); } catch (error) { console.error("Error generating PNG:", error); diff --git a/sucker.js b/sucker.js index 0d02bee..900be61 100644 --- a/sucker.js +++ b/sucker.js @@ -1,19 +1,13 @@ #!/usr/bin/env node const { execSync } = require("child_process"); -// 1. Grab the port from the '-p' argument const portIndex = process.argv.indexOf("-p"); let port = "3000"; if (portIndex !== -1 && portIndex + 1 < process.argv.length) { port = process.argv[portIndex + 1]; } -// 2. Start Next.js using that port try { - // Either pass it directly to next, e.g.: - // execSync(`npx next start -p ${port}`, { stdio: "inherit" }); - // - // Or set the PORT env var and run your npm script: execSync(`PORT=${port} npm run start`, { stdio: "inherit" }); } catch (err) { console.error("Failed to start Next.js:", err);