Skip to content

Commit 4f9d017

Browse files
committed
chore: additional font safe guards, remove fs paths
1 parent 09fd529 commit 4f9d017

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

src/module.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from 'node:fs'
22
import { readFile } from 'node:fs/promises'
3+
import { existsSync } from 'node:fs'
34
import {
45
type AddComponentOptions,
56
addComponent,
@@ -293,14 +294,25 @@ export default defineNuxtModule<ModuleOptions>({
293294
}
294295
}
295296
else if (f.path) {
297+
// validate the extension, can only be woff, ttf or otf
298+
const extension = basename(f.path).split('.').pop()!
299+
if (!['woff', 'ttf', 'otf', 'base64'].includes(extension)) {
300+
logger.warn(`The ${f.name}:${f.weight} font was skipped because the file extension ${extension} is not supported. Only woff, ttf and otf are supported.`)
301+
return false
302+
}
296303
// resolve relative paths from public dir
297304
if (!isAbsolute(f.path)) {
298305
// move to assets folder as base64 and set key
299306
f.path = join(nuxt.options.rootDir, nuxt.options.dir.public, f.path)
300307
}
308+
if (!existsSync(f.path)) {
309+
logger.warn(`The ${f.name}:${f.weight} font was skipped because the file does not exist at path ${f.path}.`)
310+
return false
311+
}
301312
const fontData = await readFile(f.path, 'base64')
302313
f.key = `nuxt-og-image:fonts:${f.name}-${f.weight}.ttf.base64`
303314
await fontStorage.setItem(`${basename(f.path)}.base64`, fontData)
315+
delete f.path
304316
}
305317
return f
306318
}))).filter(Boolean) as InputFontConfig[]

src/runtime/nitro/routes/image.ts

-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ export default defineEventHandler(async (e): Promise<any> => {
2020
compatibilityHints.push('Converting PNGs to JPEGs requires Sharp which only runs on Node based systems.')
2121
if (options.renderer === 'chromium')
2222
compatibilityHints.push('Using Chromium to generate images is only supported in Node based environments. It\'s recommended to only use this if you\'re prerendering')
23-
if (options.component !== 'PageScreenshot' && await applyInlineCss(ctx, await fetchIsland(ctx)))
24-
compatibilityHints.push('Inlining CSS is not supported on Cloudflare.')
2523
setHeader(e, 'Content-Type', 'application/json')
2624
return {
2725
siteConfig: {

test/integration/endpoints/debug.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,15 @@ describe('debug', () => {
4040
"cacheKey": "Inter:400",
4141
"key": "nuxt-og-image:fonts:Inter-400.ttf.base64",
4242
"name": "Inter",
43-
"path": "",
4443
"style": "normal",
45-
"weight": "400",
44+
"weight": 400,
4645
},
4746
{
4847
"cacheKey": "Inter:700",
4948
"key": "nuxt-og-image:fonts:Inter-700.ttf.base64",
5049
"name": "Inter",
51-
"path": "",
5250
"style": "normal",
53-
"weight": "700",
51+
"weight": 700,
5452
},
5553
],
5654
"hasNuxtIcon": false,

test/integration/endpoints/fonts.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('fonts', () => {
1414
Blob {
1515
Symbol(kHandle): Blob {},
1616
Symbol(kLength): 310808,
17-
Symbol(kType): "",
17+
Symbol(kType): "font/ttf",
1818
}
1919
`)
2020
}, 60000)

0 commit comments

Comments
 (0)