mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
refactor: ♻️ Don't use Bun global
This commit is contained in:
parent
d55668d529
commit
25ea870f71
|
|
@ -1,6 +1,7 @@
|
||||||
import { apiRoute, handleZodError } from "@/api";
|
import { apiRoute, handleZodError } from "@/api";
|
||||||
import { Application, User } from "@versia/kit/db";
|
import { Application, User } from "@versia/kit/db";
|
||||||
import { Users } from "@versia/kit/tables";
|
import { Users } from "@versia/kit/tables";
|
||||||
|
import { password as bunPassword } from "bun";
|
||||||
import { eq, or } from "drizzle-orm";
|
import { eq, or } from "drizzle-orm";
|
||||||
import type { Context } from "hono";
|
import type { Context } from "hono";
|
||||||
import { describeRoute } from "hono-openapi";
|
import { describeRoute } from "hono-openapi";
|
||||||
|
|
@ -143,7 +144,7 @@ export default apiRoute((app) =>
|
||||||
if (
|
if (
|
||||||
!(
|
!(
|
||||||
user &&
|
user &&
|
||||||
(await Bun.password.verify(
|
(await bunPassword.verify(
|
||||||
password,
|
password,
|
||||||
user.data.password || "",
|
user.data.password || "",
|
||||||
))
|
))
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
import { apiRoute, handleZodError } from "@/api";
|
import { apiRoute, handleZodError } from "@/api";
|
||||||
import { User } from "@versia/kit/db";
|
import { User } from "@versia/kit/db";
|
||||||
import { Users } from "@versia/kit/tables";
|
import { Users } from "@versia/kit/tables";
|
||||||
|
import { password as bunPassword } from "bun";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import type { Context } from "hono";
|
import type { Context } from "hono";
|
||||||
import { describeRoute } from "hono-openapi";
|
import { describeRoute } from "hono-openapi";
|
||||||
import { validator } from "hono-openapi/zod";
|
import { validator } from "hono-openapi/zod";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { config } from "~/config.ts";
|
import { config } from "~/config.ts";
|
||||||
|
|
||||||
const returnError = (
|
const returnError = (
|
||||||
context: Context,
|
context: Context,
|
||||||
token: string,
|
token: string,
|
||||||
|
|
@ -68,7 +68,7 @@ export default apiRoute((app) =>
|
||||||
}
|
}
|
||||||
|
|
||||||
await user.update({
|
await user.update({
|
||||||
password: await Bun.password.hash(password),
|
password: await bunPassword.hash(password),
|
||||||
passwordResetToken: null,
|
passwordResetToken: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { apiRoute, handleZodError } from "@/api";
|
import { apiRoute, handleZodError } from "@/api";
|
||||||
|
import { file as bunFile } from "bun";
|
||||||
import { describeRoute } from "hono-openapi";
|
import { describeRoute } from "hono-openapi";
|
||||||
import { resolver, validator } from "hono-openapi/zod";
|
import { resolver, validator } from "hono-openapi/zod";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
@ -57,7 +58,7 @@ export default apiRoute((app) =>
|
||||||
.map(Number); // [0, 100]
|
.map(Number); // [0, 100]
|
||||||
|
|
||||||
// Serve file from filesystem
|
// Serve file from filesystem
|
||||||
const file = Bun.file(`./uploads/${hash}/${name}`);
|
const file = bunFile(`./uploads/${hash}/${name}`);
|
||||||
|
|
||||||
const buffer = await file.arrayBuffer();
|
const buffer = await file.arrayBuffer();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@
|
||||||
"indentWidth": 4
|
"indentWidth": 4
|
||||||
},
|
},
|
||||||
"javascript": {
|
"javascript": {
|
||||||
"globals": ["Bun", "HTMLRewriter", "BufferEncoding"]
|
"globals": ["HTMLRewriter", "BufferEncoding"]
|
||||||
},
|
},
|
||||||
"files": {
|
"files": {
|
||||||
"ignore": ["node_modules", "dist", "cache", "build"]
|
"ignore": ["node_modules", "dist", "cache", "build"]
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { build } from "bun";
|
||||||
import { $ } from "bun";
|
import { $ } from "bun";
|
||||||
import ora from "ora";
|
import ora from "ora";
|
||||||
|
|
||||||
|
|
@ -5,7 +6,7 @@ const buildSpinner = ora("Building").start();
|
||||||
|
|
||||||
await $`rm -rf dist && mkdir dist`;
|
await $`rm -rf dist && mkdir dist`;
|
||||||
|
|
||||||
await Bun.build({
|
await build({
|
||||||
entrypoints: ["entrypoints/worker/index.ts"],
|
entrypoints: ["entrypoints/worker/index.ts"],
|
||||||
outdir: "dist",
|
outdir: "dist",
|
||||||
target: "bun",
|
target: "bun",
|
||||||
|
|
|
||||||
3
build.ts
3
build.ts
|
|
@ -1,5 +1,6 @@
|
||||||
import { readdir } from "node:fs/promises";
|
import { readdir } from "node:fs/promises";
|
||||||
import { $ } from "bun";
|
import { $ } from "bun";
|
||||||
|
import { build } from "bun";
|
||||||
import ora from "ora";
|
import ora from "ora";
|
||||||
import { routes } from "~/routes";
|
import { routes } from "~/routes";
|
||||||
|
|
||||||
|
|
@ -10,7 +11,7 @@ await $`rm -rf dist && mkdir dist`;
|
||||||
// Get all directories under the plugins/ directory
|
// Get all directories under the plugins/ directory
|
||||||
const pluginDirs = await readdir("plugins", { withFileTypes: true });
|
const pluginDirs = await readdir("plugins", { withFileTypes: true });
|
||||||
|
|
||||||
await Bun.build({
|
await build({
|
||||||
entrypoints: [
|
entrypoints: [
|
||||||
"index.ts",
|
"index.ts",
|
||||||
"cli/index.ts",
|
"cli/index.ts",
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ import {
|
||||||
Users,
|
Users,
|
||||||
} from "@versia/kit/tables";
|
} from "@versia/kit/tables";
|
||||||
import { randomUUIDv7 } from "bun";
|
import { randomUUIDv7 } from "bun";
|
||||||
|
import { password as bunPassword } from "bun";
|
||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
import {
|
import {
|
||||||
type InferInsertModel,
|
type InferInsertModel,
|
||||||
|
|
@ -890,7 +891,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
||||||
password:
|
password:
|
||||||
data.skipPasswordHash || !data.password
|
data.skipPasswordHash || !data.password
|
||||||
? data.password
|
? data.password
|
||||||
: await Bun.password.hash(data.password),
|
: await bunPassword.hash(data.password),
|
||||||
email: data.email,
|
email: data.email,
|
||||||
note: data.bio ?? "",
|
note: data.bio ?? "",
|
||||||
avatarId: data.avatar?.id,
|
avatarId: data.avatar?.id,
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ import type {
|
||||||
import { Instance, Like, Note, Relationship, User } from "@versia/kit/db";
|
import { Instance, Like, Note, Relationship, User } from "@versia/kit/db";
|
||||||
import { Likes, Notes } from "@versia/kit/tables";
|
import { Likes, Notes } from "@versia/kit/tables";
|
||||||
import type { SocketAddress } from "bun";
|
import type { SocketAddress } from "bun";
|
||||||
|
import { Glob } from "bun";
|
||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { matches } from "ip-matching";
|
import { matches } from "ip-matching";
|
||||||
|
|
@ -31,7 +32,7 @@ import { ApiError } from "../errors/api-error.ts";
|
||||||
* @returns {boolean} - True if defederated, false otherwise.
|
* @returns {boolean} - True if defederated, false otherwise.
|
||||||
*/
|
*/
|
||||||
function isDefederated(hostname: string): boolean {
|
function isDefederated(hostname: string): boolean {
|
||||||
const pattern = new Bun.Glob(hostname);
|
const pattern = new Glob(hostname);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
config.federation.blocked.find(
|
config.federation.blocked.find(
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@
|
||||||
* @module MediaManager/Utils
|
* @module MediaManager/Utils
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { SHA256 } from "bun";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a SHA-256 hash for a given file.
|
* Generates a SHA-256 hash for a given file.
|
||||||
* @param file - The file to hash.
|
* @param file - The file to hash.
|
||||||
|
|
@ -10,6 +12,6 @@
|
||||||
*/
|
*/
|
||||||
export const getMediaHash = async (file: File): Promise<string> => {
|
export const getMediaHash = async (file: File): Promise<string> => {
|
||||||
const arrayBuffer = await file.arrayBuffer();
|
const arrayBuffer = await file.arrayBuffer();
|
||||||
const hash = new Bun.SHA256().update(arrayBuffer).digest("hex");
|
const hash = new SHA256().update(arrayBuffer).digest("hex");
|
||||||
return hash;
|
return hash;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { readdir } from "node:fs/promises";
|
import { readdir } from "node:fs/promises";
|
||||||
import { type Logger, getLogger } from "@logtape/logtape";
|
import { type Logger, getLogger } from "@logtape/logtape";
|
||||||
|
import { file, sleep } from "bun";
|
||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
import { parseJSON5, parseJSONC } from "confbox";
|
import { parseJSON5, parseJSONC } from "confbox";
|
||||||
import type { Hono } from "hono";
|
import type { Hono } from "hono";
|
||||||
|
|
@ -59,7 +60,7 @@ export class PluginLoader {
|
||||||
manifestPath: string,
|
manifestPath: string,
|
||||||
manifestFile: string,
|
manifestFile: string,
|
||||||
): Promise<unknown> {
|
): Promise<unknown> {
|
||||||
const manifestText = await Bun.file(manifestPath).text();
|
const manifestText = await file(manifestPath).text();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (manifestFile.endsWith(".json")) {
|
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`Here is the error message, please fix the configuration file accordingly:`;
|
||||||
logger.fatal`${(e as ValidationError).message}`;
|
logger.fatal`${(e as ValidationError).message}`;
|
||||||
|
|
||||||
await Bun.sleep(Number.POSITIVE_INFINITY);
|
await sleep(Number.POSITIVE_INFINITY);
|
||||||
}
|
}
|
||||||
|
|
||||||
const time2 = performance.now();
|
const time2 = performance.now();
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { getLogger } from "@logtape/logtape";
|
import { getLogger } from "@logtape/logtape";
|
||||||
|
import { SHA256 } from "bun";
|
||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
import { createMiddleware } from "hono/factory";
|
import { createMiddleware } from "hono/factory";
|
||||||
import { config } from "~/config.ts";
|
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 urlAndMethod = `${chalk.green(context.req.method)} ${chalk.blue(context.req.url)}`;
|
||||||
|
|
||||||
const hash = `${chalk.bold("Hash")}: ${chalk.yellow(
|
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(
|
const headers = `${chalk.bold("Headers")}:\n${Array.from(
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
|
import { FileSystemRouter } from "bun";
|
||||||
// Returns the route filesystem path when given a URL
|
// Returns the route filesystem path when given a URL
|
||||||
export const routeMatcher = new Bun.FileSystemRouter({
|
export const routeMatcher = new FileSystemRouter({
|
||||||
style: "nextjs",
|
style: "nextjs",
|
||||||
dir: `${process.cwd()}/api`,
|
dir: `${process.cwd()}/api`,
|
||||||
fileExtensions: [".ts", ".js"],
|
fileExtensions: [".ts", ".js"],
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import type { RolePermission } from "@versia/client/schemas";
|
||||||
import { Application, Emoji, Note, Token, User, db } from "@versia/kit/db";
|
import { Application, Emoji, Note, Token, User, db } from "@versia/kit/db";
|
||||||
import { Challenges } from "@versia/kit/tables";
|
import { Challenges } from "@versia/kit/tables";
|
||||||
import { extractParams, verifySolution } from "altcha-lib";
|
import { extractParams, verifySolution } from "altcha-lib";
|
||||||
|
import { SHA256 } from "bun";
|
||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
import { type SQL, eq } from "drizzle-orm";
|
import { type SQL, eq } from "drizzle-orm";
|
||||||
import type { Context, Hono, MiddlewareHandler } from "hono";
|
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 urlAndMethod = `${chalk.green(req.method)} ${chalk.blue(req.url)}`;
|
||||||
|
|
||||||
const hash = `${chalk.bold("Hash")}: ${chalk.yellow(
|
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(
|
const headers = `${chalk.bold("Headers")}:\n${Array.from(
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import * as Sentry from "@sentry/bun";
|
import * as Sentry from "@sentry/bun";
|
||||||
|
import { env } from "bun";
|
||||||
import { config } from "~/config.ts";
|
import { config } from "~/config.ts";
|
||||||
import pkg from "~/package.json";
|
import pkg from "~/package.json";
|
||||||
|
|
||||||
|
|
@ -13,8 +14,8 @@ const sentryInstance =
|
||||||
environment: config.logging.sentry.environment,
|
environment: config.logging.sentry.environment,
|
||||||
tracePropagationTargets:
|
tracePropagationTargets:
|
||||||
config.logging.sentry.trace_propagation_targets,
|
config.logging.sentry.trace_propagation_targets,
|
||||||
release: Bun.env.GIT_COMMIT
|
release: env.GIT_COMMIT
|
||||||
? `${pkg.version}-${Bun.env.GIT_COMMIT}`
|
? `${pkg.version}-${env.GIT_COMMIT}`
|
||||||
: pkg.version,
|
: pkg.version,
|
||||||
integrations: [Sentry.extraErrorDataIntegration()],
|
integrations: [Sentry.extraErrorDataIntegration()],
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import type { Server } from "bun";
|
import { type Server, serve } from "bun";
|
||||||
import type { Hono } from "hono";
|
import type { Hono } from "hono";
|
||||||
import type { z } from "zod";
|
import type { z } from "zod";
|
||||||
import type { ConfigSchema } from "~/classes/config/schema.ts";
|
import type { ConfigSchema } from "~/classes/config/schema.ts";
|
||||||
|
|
@ -9,7 +9,7 @@ export const createServer = (
|
||||||
config: z.infer<typeof ConfigSchema>,
|
config: z.infer<typeof ConfigSchema>,
|
||||||
app: Hono<HonoEnv>,
|
app: Hono<HonoEnv>,
|
||||||
): Server =>
|
): Server =>
|
||||||
Bun.serve({
|
serve({
|
||||||
port: config.http.bind_port,
|
port: config.http.bind_port,
|
||||||
reusePort: true,
|
reusePort: true,
|
||||||
tls: config.http.tls
|
tls: config.http.tls
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue