make work with node js instead cause serverless is stupid

This commit is contained in:
juni
2024-10-25 14:36:48 -04:00
parent b4e44be4bf
commit b081b71e7d
11 changed files with 54 additions and 79 deletions

View File

@@ -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(""));

14
pkgs/app/Dockerfile Normal file
View File

@@ -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" ]

View File

@@ -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"
}
}

View File

@@ -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;
};
}
}

View File

@@ -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<Env>();
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,
})

View File

@@ -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

View File

@@ -11,8 +11,8 @@ export const getOEmbed: Handler<Env, '/oembed'> = 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<Env, '/oembed'> = 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,

View File

@@ -67,9 +67,9 @@ export const getPost: Handler<
<Post
post={fetchedPost}
url={c.req.path}
appDomain={c.env.VIXBLUESKY_APP_DOMAIN}
appDomain={process.env.VIXBLUESKY_APP_DOMAIN}
videoMetadata={videoMetaData}
apiUrl={c.env.VIXBLUESKY_API_URL}
apiUrl={process.env.VIXBLUESKY_API_URL}
images={images}
/>,
);

View File

@@ -21,7 +21,7 @@ export const getProfile: Handler<
<Profile
profile={data}
url={c.req.path}
appDomain={c.env.VIXBLUESKY_APP_DOMAIN}
appDomain={process.env.VIXBLUESKY_APP_DOMAIN}
/>,
);
};

View File

@@ -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",
}
}

View File

@@ -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"