From 25ea870f71010426022905f4478a21461ee1d3c7 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Sun, 30 Mar 2025 23:06:34 +0200 Subject: [PATCH] refactor: :recycle: Don't use Bun global --- api/api/auth/login/index.ts | 3 ++- api/api/auth/reset/index.ts | 4 ++-- api/media/[hash]/[name]/index.ts | 3 ++- biome.json | 2 +- build-worker.ts | 3 ++- build.ts | 3 ++- classes/database/user.ts | 3 ++- classes/inbox/processor.ts | 3 ++- classes/media/media-hasher.ts | 4 +++- classes/plugin/loader.ts | 5 +++-- middlewares/logger.ts | 3 ++- routes.ts | 3 ++- utils/api.ts | 3 ++- utils/sentry.ts | 5 +++-- utils/server.ts | 4 ++-- 15 files changed, 32 insertions(+), 19 deletions(-) diff --git a/api/api/auth/login/index.ts b/api/api/auth/login/index.ts index b7ed340d..452c5490 100644 --- a/api/api/auth/login/index.ts +++ b/api/api/auth/login/index.ts @@ -1,6 +1,7 @@ import { apiRoute, handleZodError } from "@/api"; import { Application, User } from "@versia/kit/db"; import { Users } from "@versia/kit/tables"; +import { password as bunPassword } from "bun"; import { eq, or } from "drizzle-orm"; import type { Context } from "hono"; import { describeRoute } from "hono-openapi"; @@ -143,7 +144,7 @@ export default apiRoute((app) => if ( !( user && - (await Bun.password.verify( + (await bunPassword.verify( password, user.data.password || "", )) diff --git a/api/api/auth/reset/index.ts b/api/api/auth/reset/index.ts index 58f33312..eac0ee93 100644 --- a/api/api/auth/reset/index.ts +++ b/api/api/auth/reset/index.ts @@ -1,13 +1,13 @@ import { apiRoute, handleZodError } from "@/api"; import { User } from "@versia/kit/db"; import { Users } from "@versia/kit/tables"; +import { password as bunPassword } from "bun"; import { eq } from "drizzle-orm"; import type { Context } from "hono"; import { describeRoute } from "hono-openapi"; import { validator } from "hono-openapi/zod"; import { z } from "zod"; import { config } from "~/config.ts"; - const returnError = ( context: Context, token: string, @@ -68,7 +68,7 @@ export default apiRoute((app) => } await user.update({ - password: await Bun.password.hash(password), + password: await bunPassword.hash(password), passwordResetToken: null, }); diff --git a/api/media/[hash]/[name]/index.ts b/api/media/[hash]/[name]/index.ts index 7b96fcfe..e39fa1ee 100644 --- a/api/media/[hash]/[name]/index.ts +++ b/api/media/[hash]/[name]/index.ts @@ -1,4 +1,5 @@ import { apiRoute, handleZodError } from "@/api"; +import { file as bunFile } from "bun"; import { describeRoute } from "hono-openapi"; import { resolver, validator } from "hono-openapi/zod"; import { z } from "zod"; @@ -57,7 +58,7 @@ export default apiRoute((app) => .map(Number); // [0, 100] // Serve file from filesystem - const file = Bun.file(`./uploads/${hash}/${name}`); + const file = bunFile(`./uploads/${hash}/${name}`); const buffer = await file.arrayBuffer(); diff --git a/biome.json b/biome.json index 03c19ade..7e112d2e 100644 --- a/biome.json +++ b/biome.json @@ -100,7 +100,7 @@ "indentWidth": 4 }, "javascript": { - "globals": ["Bun", "HTMLRewriter", "BufferEncoding"] + "globals": ["HTMLRewriter", "BufferEncoding"] }, "files": { "ignore": ["node_modules", "dist", "cache", "build"] diff --git a/build-worker.ts b/build-worker.ts index 29f9f044..e5b0438d 100644 --- a/build-worker.ts +++ b/build-worker.ts @@ -1,3 +1,4 @@ +import { build } from "bun"; import { $ } from "bun"; import ora from "ora"; @@ -5,7 +6,7 @@ const buildSpinner = ora("Building").start(); await $`rm -rf dist && mkdir dist`; -await Bun.build({ +await build({ entrypoints: ["entrypoints/worker/index.ts"], outdir: "dist", target: "bun", diff --git a/build.ts b/build.ts index c22022aa..b5e18771 100644 --- a/build.ts +++ b/build.ts @@ -1,5 +1,6 @@ import { readdir } from "node:fs/promises"; import { $ } from "bun"; +import { build } from "bun"; import ora from "ora"; import { routes } from "~/routes"; @@ -10,7 +11,7 @@ await $`rm -rf dist && mkdir dist`; // Get all directories under the plugins/ directory const pluginDirs = await readdir("plugins", { withFileTypes: true }); -await Bun.build({ +await build({ entrypoints: [ "index.ts", "cli/index.ts", diff --git a/classes/database/user.ts b/classes/database/user.ts index 28858db3..87ceeea1 100644 --- a/classes/database/user.ts +++ b/classes/database/user.ts @@ -34,6 +34,7 @@ import { Users, } from "@versia/kit/tables"; import { randomUUIDv7 } from "bun"; +import { password as bunPassword } from "bun"; import chalk from "chalk"; import { type InferInsertModel, @@ -890,7 +891,7 @@ export class User extends BaseInterface { password: data.skipPasswordHash || !data.password ? data.password - : await Bun.password.hash(data.password), + : await bunPassword.hash(data.password), email: data.email, note: data.bio ?? "", avatarId: data.avatar?.id, diff --git a/classes/inbox/processor.ts b/classes/inbox/processor.ts index 3e96bd7b..0c5f93d7 100644 --- a/classes/inbox/processor.ts +++ b/classes/inbox/processor.ts @@ -18,6 +18,7 @@ import type { import { Instance, Like, Note, Relationship, User } from "@versia/kit/db"; import { Likes, Notes } from "@versia/kit/tables"; import type { SocketAddress } from "bun"; +import { Glob } from "bun"; import chalk from "chalk"; import { eq } from "drizzle-orm"; import { matches } from "ip-matching"; @@ -31,7 +32,7 @@ import { ApiError } from "../errors/api-error.ts"; * @returns {boolean} - True if defederated, false otherwise. */ function isDefederated(hostname: string): boolean { - const pattern = new Bun.Glob(hostname); + const pattern = new Glob(hostname); return ( config.federation.blocked.find( diff --git a/classes/media/media-hasher.ts b/classes/media/media-hasher.ts index 7e30c032..bbe70f91 100644 --- a/classes/media/media-hasher.ts +++ b/classes/media/media-hasher.ts @@ -3,6 +3,8 @@ * @module MediaManager/Utils */ +import { SHA256 } from "bun"; + /** * Generates a SHA-256 hash for a given file. * @param file - The file to hash. @@ -10,6 +12,6 @@ */ export const getMediaHash = async (file: File): Promise => { const arrayBuffer = await file.arrayBuffer(); - const hash = new Bun.SHA256().update(arrayBuffer).digest("hex"); + const hash = new SHA256().update(arrayBuffer).digest("hex"); return hash; }; diff --git a/classes/plugin/loader.ts b/classes/plugin/loader.ts index ed09e199..bbfd6ccd 100644 --- a/classes/plugin/loader.ts +++ b/classes/plugin/loader.ts @@ -1,5 +1,6 @@ import { readdir } from "node:fs/promises"; import { type Logger, getLogger } from "@logtape/logtape"; +import { file, sleep } from "bun"; import chalk from "chalk"; import { parseJSON5, parseJSONC } from "confbox"; import type { Hono } from "hono"; @@ -59,7 +60,7 @@ export class PluginLoader { manifestPath: string, manifestFile: string, ): Promise { - const manifestText = await Bun.file(manifestPath).text(); + const manifestText = await file(manifestPath).text(); try { if (manifestFile.endsWith(".json")) { @@ -238,7 +239,7 @@ export class PluginLoader { logger.fatal`Here is the error message, please fix the configuration file accordingly:`; logger.fatal`${(e as ValidationError).message}`; - await Bun.sleep(Number.POSITIVE_INFINITY); + await sleep(Number.POSITIVE_INFINITY); } const time2 = performance.now(); diff --git a/middlewares/logger.ts b/middlewares/logger.ts index 4ef3e04a..21e604cf 100644 --- a/middlewares/logger.ts +++ b/middlewares/logger.ts @@ -1,4 +1,5 @@ import { getLogger } from "@logtape/logtape"; +import { SHA256 } from "bun"; import chalk from "chalk"; import { createMiddleware } from "hono/factory"; import { config } from "~/config.ts"; @@ -11,7 +12,7 @@ export const logger = createMiddleware(async (context, next) => { const urlAndMethod = `${chalk.green(context.req.method)} ${chalk.blue(context.req.url)}`; const hash = `${chalk.bold("Hash")}: ${chalk.yellow( - new Bun.SHA256().update(body).digest("hex"), + new SHA256().update(body).digest("hex"), )}`; const headers = `${chalk.bold("Headers")}:\n${Array.from( diff --git a/routes.ts b/routes.ts index 02d48566..f3a225a9 100644 --- a/routes.ts +++ b/routes.ts @@ -1,6 +1,7 @@ import { join } from "node:path"; +import { FileSystemRouter } from "bun"; // Returns the route filesystem path when given a URL -export const routeMatcher = new Bun.FileSystemRouter({ +export const routeMatcher = new FileSystemRouter({ style: "nextjs", dir: `${process.cwd()}/api`, fileExtensions: [".ts", ".js"], diff --git a/utils/api.ts b/utils/api.ts index fb91338a..56634b8a 100644 --- a/utils/api.ts +++ b/utils/api.ts @@ -3,6 +3,7 @@ import type { RolePermission } from "@versia/client/schemas"; import { Application, Emoji, Note, Token, User, db } from "@versia/kit/db"; import { Challenges } from "@versia/kit/tables"; import { extractParams, verifySolution } from "altcha-lib"; +import { SHA256 } from "bun"; import chalk from "chalk"; import { type SQL, eq } from "drizzle-orm"; import type { Context, Hono, MiddlewareHandler } from "hono"; @@ -525,7 +526,7 @@ export const debugRequest = async (req: Request): Promise => { const urlAndMethod = `${chalk.green(req.method)} ${chalk.blue(req.url)}`; const hash = `${chalk.bold("Hash")}: ${chalk.yellow( - new Bun.SHA256().update(body).digest("hex"), + new SHA256().update(body).digest("hex"), )}`; const headers = `${chalk.bold("Headers")}:\n${Array.from( diff --git a/utils/sentry.ts b/utils/sentry.ts index 2502fe29..d17429fb 100644 --- a/utils/sentry.ts +++ b/utils/sentry.ts @@ -1,4 +1,5 @@ import * as Sentry from "@sentry/bun"; +import { env } from "bun"; import { config } from "~/config.ts"; import pkg from "~/package.json"; @@ -13,8 +14,8 @@ const sentryInstance = environment: config.logging.sentry.environment, tracePropagationTargets: config.logging.sentry.trace_propagation_targets, - release: Bun.env.GIT_COMMIT - ? `${pkg.version}-${Bun.env.GIT_COMMIT}` + release: env.GIT_COMMIT + ? `${pkg.version}-${env.GIT_COMMIT}` : pkg.version, integrations: [Sentry.extraErrorDataIntegration()], }); diff --git a/utils/server.ts b/utils/server.ts index 21c09748..0c1b0fac 100644 --- a/utils/server.ts +++ b/utils/server.ts @@ -1,4 +1,4 @@ -import type { Server } from "bun"; +import { type Server, serve } from "bun"; import type { Hono } from "hono"; import type { z } from "zod"; import type { ConfigSchema } from "~/classes/config/schema.ts"; @@ -9,7 +9,7 @@ export const createServer = ( config: z.infer, app: Hono, ): Server => - Bun.serve({ + serve({ port: config.http.bind_port, reusePort: true, tls: config.http.tls