From 377a00aaf1c374b8efa2551109d5084f2c3d2b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Sun, 21 Jul 2024 19:51:04 -0500 Subject: [PATCH] feat: fast font loading --- src/core/blitzkit/jsxToSvg/index.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/core/blitzkit/jsxToSvg/index.ts b/src/core/blitzkit/jsxToSvg/index.ts index 5bf72835f..a084517f6 100644 --- a/src/core/blitzkit/jsxToSvg/index.ts +++ b/src/core/blitzkit/jsxToSvg/index.ts @@ -1,26 +1,23 @@ -import { readFileSync } from 'fs'; +import { readFile } from 'fs/promises'; import satori from 'satori'; import { RenderConfiguration } from '../renderConfiguration'; import robotoBlack from './Roboto-Black.ttf'; import robotoBold from './Roboto-Bold.ttf'; import roboto from './Roboto.ttf'; -const fonts = [ +const fontsPromise = [ { file: roboto, weight: 400 as const }, { file: robotoBold, weight: 700 as const }, { file: robotoBlack, weight: 900 as const }, -]; +].map(async ({ file, weight }) => { + const data = await readFile(`dist/bot/workers/${file}`); + return { name: 'Roboto', weight, data }; +}); export default async function jsxToSvg( jsx: JSX.Element, renderConfiguration = new RenderConfiguration(), ) { - return await satori(jsx, { - width: renderConfiguration.width, - fonts: fonts.map(({ file, weight }) => ({ - weight, - name: 'Roboto', - data: readFileSync(`${__dirname}/${file}`), - })), - }); + const fonts = await Promise.all(fontsPromise); + return await satori(jsx, { width: renderConfiguration.width, fonts }); }