From b081b71e7d273c942cfd0da00a56c80f409d825a Mon Sep 17 00:00:00 2001 From: juni Date: Fri, 25 Oct 2024 14:36:48 -0400 Subject: [PATCH] make work with node js instead cause serverless is stupid --- pkgs/api/src/index.ts | 5 +-- pkgs/app/Dockerfile | 14 ++++++++ pkgs/app/package.json | 10 +++--- pkgs/app/src/globals.d.ts | 18 ----------- pkgs/app/src/index.ts | 39 ++++++++--------------- pkgs/app/src/lib/parseEmbedDescription.ts | 4 ++- pkgs/app/src/routes/getOEmbed.ts | 6 ++-- pkgs/app/src/routes/getPost.tsx | 4 +-- pkgs/app/src/routes/getProfile.tsx | 2 +- pkgs/app/tsconfig.json | 15 +++++---- pkgs/app/wrangler.toml | 16 ---------- 11 files changed, 54 insertions(+), 79 deletions(-) create mode 100644 pkgs/app/Dockerfile delete mode 100644 pkgs/app/src/globals.d.ts delete mode 100644 pkgs/app/wrangler.toml diff --git a/pkgs/api/src/index.ts b/pkgs/api/src/index.ts index 278aa6c..7bb5dd3 100644 --- a/pkgs/api/src/index.ts +++ b/pkgs/api/src/index.ts @@ -22,17 +22,18 @@ declare global { interface ProcessEnv { readonly NODE_ENV: Env; readonly PORT: string | undefined; + readonly EMPTY_REDIR: string; } } } -const { NODE_ENV: env = "development", PORT } = process.env; +const { NODE_ENV: env = "development", PORT, EMPTY_REDIR } = process.env; const app = fastify({ logger: envToLogger[env] }); app.addContentTypeParser("*", (_, __, done) => done(null)); -app.get("/", async (_, res) => res.redirect("https://bskyx.app")); +app.get("/", async (_, res) => res.redirect(EMPTY_REDIR)); // serve 0 bytes favicon so browsers don't spam the server app.get("/favicon.ico", (_, res) => res.send("")); diff --git a/pkgs/app/Dockerfile b/pkgs/app/Dockerfile new file mode 100644 index 0000000..0f06d90 --- /dev/null +++ b/pkgs/app/Dockerfile @@ -0,0 +1,14 @@ +#FROM node:22-alpine +FROM node:22.2.0-slim + +WORKDIR /app + +COPY package*.json ./ + +RUN npm i -g pnpm && pnpm install + +COPY . . + +#RUN pnpm run dev + +CMD [ "pnpm", "dev" ] diff --git a/pkgs/app/package.json b/pkgs/app/package.json index 2220bdc..51591d2 100644 --- a/pkgs/app/package.json +++ b/pkgs/app/package.json @@ -1,17 +1,19 @@ { "scripts": { - "dev": "wrangler dev src/index.ts", - "deploy": "wrangler deploy --minify src/index.ts" + "dev": "tsx watch src/index.ts" }, "dependencies": { "@atcute/bluesky": "^1.0.7", "@atcute/client": "^2.0.3", - "hono": "^4.5.1" + "hono": "^4.5.1", + "@hono/node-server": "^1.13.2" }, "devDependencies": { "@cloudflare/workers-types": "^4.20230628.0", "prettier": "^3.3.3", "typescript": "^5.1.6", - "wrangler": "^3.75.0" + "wrangler": "^3.75.0", + "@types/node": "^20.11.17", + "tsx": "^4.7.1" } } diff --git a/pkgs/app/src/globals.d.ts b/pkgs/app/src/globals.d.ts deleted file mode 100644 index dae7c88..0000000 --- a/pkgs/app/src/globals.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { XRPC } from '@atcute/client'; -import type { KVNamespace } from '@cloudflare/workers-types'; - -declare global { - interface Env { - Bindings: { - BSKY_SERVICE_URL: string; - BSKY_AUTH_USERNAME: string; - BSKY_AUTH_PASSWORD: string; - VIXBLUESKY_APP_DOMAIN: string; - VIXBLUESKY_API_URL: string; - bskyx: KVNamespace; - }; - Variables: { - Agent: XRPC; - }; - } -} diff --git a/pkgs/app/src/index.ts b/pkgs/app/src/index.ts index 446125f..9bc4bff 100644 --- a/pkgs/app/src/index.ts +++ b/pkgs/app/src/index.ts @@ -1,3 +1,4 @@ +import { serve } from '@hono/node-server' import { Hono } from 'hono'; import { XRPC, CredentialManager, AtpSessionData } from '@atcute/client'; import '@atcute/bluesky/lexicons'; @@ -8,33 +9,18 @@ import { getProfileData } from './routes/getProfileData'; import { getProfile } from './routes/getProfile'; import { HTTPException } from 'hono/http-exception'; -const app = new Hono(); +const app = new Hono(); +const bskyxC: any = {}; app.use('*', async (c, next) => { - const creds = new CredentialManager({ - service: c.env.BSKY_SERVICE_URL, - onRefresh(session) { - return c.env.bskyx.put('session', JSON.stringify(session)); - }, - onExpired(session) { - return c.env.bskyx.delete('session'); - }, - onSessionUpdate(session) { - return c.env.bskyx.put('session', JSON.stringify(session)); - }, - }); + const creds = new CredentialManager({service: process.env.BSKY_SERVICE_URL}); const agent = new XRPC({ handler: creds }); try { - const rawSession = await c.env.bskyx.get('session'); - if (rawSession) { - const session = JSON.parse(rawSession) as AtpSessionData; - await creds.resume(session); - } else { - await creds.login({ - identifier: c.env.BSKY_AUTH_USERNAME, - password: c.env.BSKY_AUTH_PASSWORD, - }); - } + await creds.login({ + identifier: process.env.BSKY_AUTH_USERNAME, + password: process.env.BSKY_AUTH_PASSWORD, + }); + console.log(creds.session); c.set('Agent', agent); } catch (error) { const err = new Error('Failed to login to Bluesky!', { @@ -48,7 +34,7 @@ app.use('*', async (c, next) => { }); app.get('/', async (c) => { - return c.redirect('https://github.com/Rapougnac/VixBluesky'); + return c.redirect(process.env.EMPTY_REDIR); }); app.get('/profile/:user/post/:post', getPost); @@ -65,4 +51,7 @@ app.get('/https://bsky.app/profile/:user/json', getProfileData); app.get('/oembed', getOEmbed); -export default app; +serve({ + fetch: app.fetch, + port: process.env.PORT, +}) diff --git a/pkgs/app/src/lib/parseEmbedDescription.ts b/pkgs/app/src/lib/parseEmbedDescription.ts index 050d16a..08ca4a5 100644 --- a/pkgs/app/src/lib/parseEmbedDescription.ts +++ b/pkgs/app/src/lib/parseEmbedDescription.ts @@ -8,7 +8,9 @@ export function parseEmbedDescription(post: AppBskyFeedDefs.PostView): string { checkType('app.bsky.embed.recordWithMedia#view', post.embed)); // @ts-expect-error - const embed = post.embed.record?.record ?? post.embed.record; + let embed; + if(isQuote) + embed = post.embed.record?.record ?? post.embed.record; return isQuote ? // @ts-expect-error diff --git a/pkgs/app/src/routes/getOEmbed.ts b/pkgs/app/src/routes/getOEmbed.ts index e18ac91..43b7ef8 100644 --- a/pkgs/app/src/routes/getOEmbed.ts +++ b/pkgs/app/src/routes/getOEmbed.ts @@ -11,8 +11,8 @@ export const getOEmbed: Handler = async (c) => { const avatar = c.req.query('avatar'); const defaults = { - provider_name: 'VixBluesky', - provider_url: 'https://bskyx.app/', + provider_name: 'girlcockbsky', + provider_url: 'https://girlcockbsky.app/', thumbnail_width: 1000, thumbnail_height: 1000, }; @@ -42,7 +42,7 @@ export const getOEmbed: Handler = async (c) => { const { replies, reposts, likes, description } = c.req.query(); return c.json({ ...defaults, - provider_name: `VixBluesky\n\n🗨️ ${replies} ♻️ ${reposts} 💙 ${likes}`, + provider_name: `girlcockbsky\n\n🗨️ ${replies} ♻️ ${reposts} 💙 ${likes}`, description, title: description, author_name: description, diff --git a/pkgs/app/src/routes/getPost.tsx b/pkgs/app/src/routes/getPost.tsx index bc6446a..0616761 100644 --- a/pkgs/app/src/routes/getPost.tsx +++ b/pkgs/app/src/routes/getPost.tsx @@ -67,9 +67,9 @@ export const getPost: Handler< , ); diff --git a/pkgs/app/src/routes/getProfile.tsx b/pkgs/app/src/routes/getProfile.tsx index a9395cf..cbad827 100644 --- a/pkgs/app/src/routes/getProfile.tsx +++ b/pkgs/app/src/routes/getProfile.tsx @@ -21,7 +21,7 @@ export const getProfile: Handler< , ); }; diff --git a/pkgs/app/tsconfig.json b/pkgs/app/tsconfig.json index 0097e4b..7719c23 100644 --- a/pkgs/app/tsconfig.json +++ b/pkgs/app/tsconfig.json @@ -1,13 +1,14 @@ { "compilerOptions": { "target": "ESNext", - "module": "ESNext", - "moduleResolution": "Bundler", - "esModuleInterop": true, + "module": "NodeNext", "strict": true, - "lib": ["esnext"], - "types": ["@cloudflare/workers-types"], + "verbatimModuleSyntax": true, + "skipLibCheck": true, + "types": [ + "node" + ], "jsx": "react-jsx", - "jsxImportSource": "hono/jsx" + "jsxImportSource": "hono/jsx", } -} +} \ No newline at end of file diff --git a/pkgs/app/wrangler.toml b/pkgs/app/wrangler.toml deleted file mode 100644 index d15be37..0000000 --- a/pkgs/app/wrangler.toml +++ /dev/null @@ -1,16 +0,0 @@ -name = "vixbluesky" -main = "src/index.ts" -compatibility_date = "2023-01-01" - -workers_dev = false -route = { pattern = "bskyx.app/*", zone_name = "bskyx.app" } - -[vars] -BSKY_SERVICE_URL="https://bsky.social/" -VIXBLUESKY_APP_DOMAIN="bskyx.app" -VIXBLUESKY_API_URL="https://api.bskyx.app/" - -[[kv_namespaces]] -binding = "bskyx" -id = "ee913536e1fb47dcb9fa6275725f5a6f" -preview_id = "5e180765d24346c9b238be57d9c7001f"