refactor: ♻️ Don't use Bun global

This commit is contained in:
Jesse Wierzbinski 2025-03-30 23:06:34 +02:00
parent d55668d529
commit 25ea870f71
No known key found for this signature in database
15 changed files with 32 additions and 19 deletions

View file

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

View file

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

View file

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

View file

@ -100,7 +100,7 @@
"indentWidth": 4
},
"javascript": {
"globals": ["Bun", "HTMLRewriter", "BufferEncoding"]
"globals": ["HTMLRewriter", "BufferEncoding"]
},
"files": {
"ignore": ["node_modules", "dist", "cache", "build"]

View file

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

View file

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

View file

@ -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<typeof Users, UserWithRelations> {
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,

View file

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

View file

@ -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<string> => {
const arrayBuffer = await file.arrayBuffer();
const hash = new Bun.SHA256().update(arrayBuffer).digest("hex");
const hash = new SHA256().update(arrayBuffer).digest("hex");
return hash;
};

View file

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

View file

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

View file

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

View file

@ -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<void> => {
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(

View file

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

View file

@ -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<typeof ConfigSchema>,
app: Hono<HonoEnv>,
): Server =>
Bun.serve({
serve({
port: config.http.bind_port,
reusePort: true,
tls: config.http.tls