nuxt-og-image 7.0.6 → 7.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/tw4.mjs +1 -2
- package/dist/chunks/uno.mjs +5 -3
- package/dist/content-valibot.d.mts +9 -0
- package/dist/content-valibot.d.ts +9 -0
- package/dist/content-valibot.mjs +11 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +1 -2
- package/dist/runtime/app/client-utils.d.ts +1 -1
- package/dist/runtime/app/client-utils.js +27 -9
- package/dist/runtime/app/composables/_defineOgImageRaw.js +2 -3
- package/dist/runtime/app/utils.js +13 -5
- package/dist/runtime/server/og-image/bindings/font-assets/dev-prerender.js +4 -3
- package/dist/runtime/server/og-image/bindings/takumi/node-dev.d.ts +0 -2
- package/dist/runtime/server/og-image/bindings/takumi/node-dev.js +84 -59
- package/dist/runtime/server/og-image/bindings/takumi/node.d.ts +0 -2
- package/dist/runtime/server/og-image/bindings/takumi/node.js +1 -3
- package/dist/runtime/server/og-image/bindings/takumi/wasm.d.ts +0 -2
- package/dist/runtime/server/og-image/bindings/takumi/wasm.js +1 -3
- package/dist/runtime/server/og-image/core/plugins/imageSrc.js +5 -3
- package/dist/runtime/server/og-image/dev-font-state.d.ts +11 -0
- package/dist/runtime/server/og-image/dev-font-state.js +18 -0
- package/dist/runtime/server/og-image/devtools.js +2 -1
- package/dist/runtime/server/og-image/fonts.d.ts +9 -6
- package/dist/runtime/server/og-image/fonts.js +69 -38
- package/dist/runtime/server/og-image/satori/renderer.js +3 -3
- package/dist/runtime/server/og-image/satori/vnodes.js +2 -2
- package/dist/runtime/server/og-image/takumi/instances.d.ts +0 -2
- package/dist/runtime/server/og-image/takumi/instances.js +0 -3
- package/dist/runtime/server/og-image/takumi/renderer.js +89 -168
- package/dist/runtime/server/og-image/templates/html.js +2 -2
- package/dist/runtime/server/routes/debug.json.js +6 -6
- package/dist/runtime/server/routes/static-font.js +3 -3
- package/dist/runtime/server/signing-secret.js +19 -7
- package/dist/runtime/server/util/cloudflareAssets.js +1 -1
- package/dist/runtime/server/util/fetchLocalAsset.d.ts +1 -1
- package/dist/runtime/server/util/fetchLocalAsset.js +16 -10
- package/dist/shared/{nuxt-og-image.C4hc520M.mjs → nuxt-og-image.Dr6blNqj.mjs} +232 -100
- package/package.json +65 -52
- package/skills/nuxt-og-image/SKILL.md +2 -1
- package/types/virtual.d.ts +5 -0
- package/dist/runtime/server/og-image/bindings/takumi/resource-urls.d.ts +0 -2
- package/dist/runtime/server/og-image/bindings/takumi/resource-urls.js +0 -91
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { resolve } from "#og-image-virtual/public-assets.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
2
|
+
import { getComponentFontMap, getFontRequirements } from "#og-image/font-requirements";
|
|
3
|
+
import { getFonts } from "#og-image/fonts";
|
|
4
|
+
import { getAvailableFonts } from "#og-image/fonts-available";
|
|
5
5
|
import { logger } from "../../logger.js";
|
|
6
6
|
import { fontArrayCache, fontCache } from "./cache/lru.js";
|
|
7
7
|
import { isWoff2Binary, parseFontBinary } from "./font-binary.js";
|
|
8
8
|
import { fontFormat, selectFontSource } from "./font-source.js";
|
|
9
|
-
import { renameSubsetFonts } from "./font-subsets.js";
|
|
10
9
|
import { codepointsIntersectRanges, parseUnicodeRange } from "./unicode-range.js";
|
|
11
10
|
export { buildSubsetFamilyChain, renameSubsetFonts, resolveSubsetChain } from "./font-subsets.js";
|
|
12
11
|
export { codepointsIntersectRanges, extractCodepoints, parseUnicodeRange } from "./unicode-range.js";
|
|
@@ -86,25 +85,25 @@ export function loadAllFontsDebug(component) {
|
|
|
86
85
|
return {
|
|
87
86
|
component,
|
|
88
87
|
componentReqs: componentReqs ? { families: componentReqs.families, weights: componentReqs.weights, hasDynamicBindings: componentReqs.hasDynamicBindings } : null,
|
|
89
|
-
globalReqs: { families:
|
|
88
|
+
globalReqs: { families: getFontRequirements().families, weights: getFontRequirements().weights },
|
|
90
89
|
componentFontMapKeys: Object.keys(map),
|
|
91
|
-
resolvedFontFamilies: [...new Set(
|
|
90
|
+
resolvedFontFamilies: [...new Set(getFonts().map((f) => f.family))]
|
|
92
91
|
};
|
|
93
92
|
}
|
|
94
|
-
|
|
93
|
+
function selectFonts(options) {
|
|
95
94
|
const map = getComponentFontMap();
|
|
96
95
|
const componentReqs = options.component ? map[options.component] : null;
|
|
97
96
|
const usingGlobalFallback = !!(componentReqs && componentReqs.hasDynamicBindings);
|
|
98
|
-
const reqs = componentReqs && !componentReqs.hasDynamicBindings ? componentReqs :
|
|
99
|
-
const fonts = selectFontsForRequirements(
|
|
97
|
+
const reqs = componentReqs && !componentReqs.hasDynamicBindings ? componentReqs : getFontRequirements();
|
|
98
|
+
const fonts = selectFontsForRequirements(getFonts(), reqs);
|
|
100
99
|
if (options.fontFamilyOverride) {
|
|
101
100
|
const loadedFamilies = new Set(fonts.map((f) => f.family));
|
|
102
101
|
if (!loadedFamilies.has(options.fontFamilyOverride)) {
|
|
103
|
-
const overrideFonts =
|
|
102
|
+
const overrideFonts = getAvailableFonts().filter((f) => f.family === options.fontFamilyOverride);
|
|
104
103
|
if (overrideFonts.length > 0) {
|
|
105
104
|
fonts.push(...overrideFonts);
|
|
106
105
|
} else {
|
|
107
|
-
const resolvedOverride =
|
|
106
|
+
const resolvedOverride = getFonts().filter((f) => f.family === options.fontFamilyOverride);
|
|
108
107
|
fonts.push(...resolvedOverride);
|
|
109
108
|
}
|
|
110
109
|
}
|
|
@@ -115,7 +114,7 @@ export async function loadAllFonts(event, options) {
|
|
|
115
114
|
if (selection)
|
|
116
115
|
resolved.push({ font: f, ...selection });
|
|
117
116
|
}
|
|
118
|
-
const
|
|
117
|
+
const selected = options.codepoints && options.codepoints.size > 0 ? resolved.filter(({ font: f, isStaticFallback }) => {
|
|
119
118
|
if (isStaticFallback || !f.unicodeRange)
|
|
120
119
|
return true;
|
|
121
120
|
const ranges = parseUnicodeRange(f.unicodeRange);
|
|
@@ -123,6 +122,37 @@ export async function loadAllFonts(event, options) {
|
|
|
123
122
|
return true;
|
|
124
123
|
return codepointsIntersectRanges(options.codepoints, ranges);
|
|
125
124
|
}) : resolved;
|
|
125
|
+
return { fonts, reqs, usingGlobalFallback, selected };
|
|
126
|
+
}
|
|
127
|
+
function warnMissingWeights(options, { reqs, usingGlobalFallback }, loaded) {
|
|
128
|
+
if (!import.meta.dev || reqs.weights.length === 0 || isCommunityComponent(options.component))
|
|
129
|
+
return;
|
|
130
|
+
const families = reqs.families.length > 0 ? reqs.families : [...new Set(loaded.map((f) => f.family))];
|
|
131
|
+
const component = options.component ? ` (${options.component})` : "";
|
|
132
|
+
const fallbackNote = usingGlobalFallback ? " Note: using global font requirements because this component has dynamic font bindings." : "";
|
|
133
|
+
for (const family of families) {
|
|
134
|
+
const loadedWeights = [...new Set(loaded.filter((f) => f.family === family).map((f) => f.weight))].toSorted((a, b) => a - b);
|
|
135
|
+
if (loadedWeights.length === 0)
|
|
136
|
+
continue;
|
|
137
|
+
for (const reqWeight of reqs.weights) {
|
|
138
|
+
if (loadedWeights.includes(reqWeight))
|
|
139
|
+
continue;
|
|
140
|
+
const closest = findClosestWeight(reqWeight, loadedWeights);
|
|
141
|
+
const warnKey = `${family}-${reqWeight}-${closest}-${options.component || ""}`;
|
|
142
|
+
if (_warnedFontKeys.has(warnKey))
|
|
143
|
+
continue;
|
|
144
|
+
if (_warnedFontKeys.size >= 100)
|
|
145
|
+
_warnedFontKeys.clear();
|
|
146
|
+
_warnedFontKeys.add(warnKey);
|
|
147
|
+
logger.warn(`Font "${family}" weight ${reqWeight} not configured${component}, using ${closest} instead.${fallbackNote}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function isCommunityComponent(component) {
|
|
152
|
+
return !!component && getComponentFontMap()[component]?.category === "community";
|
|
153
|
+
}
|
|
154
|
+
export async function loadAllFonts(event, options) {
|
|
155
|
+
const { fonts, reqs, usingGlobalFallback, selected: filtered } = selectFonts(options);
|
|
126
156
|
const results = await Promise.all(
|
|
127
157
|
filtered.map(async ({ font: f, src: initialSrc, isStaticFallback }) => {
|
|
128
158
|
let src = initialSrc;
|
|
@@ -155,8 +185,7 @@ export async function loadAllFonts(event, options) {
|
|
|
155
185
|
if (cachedArray)
|
|
156
186
|
return cachedArray;
|
|
157
187
|
fontArrayCache.set(fingerprint, loaded);
|
|
158
|
-
|
|
159
|
-
if (!options.supportedFormats.has("woff2") && loaded.length === 0 && fonts.length > 0 && !isCommunity) {
|
|
188
|
+
if (!options.supportedFormats.has("woff2") && loaded.length === 0 && fonts.length > 0 && !isCommunityComponent(options.component)) {
|
|
160
189
|
const variableFamilies = [...new Set(fonts.map((f) => f.family))];
|
|
161
190
|
const warnKey = `variable-fonts-${variableFamilies.join(",")}`;
|
|
162
191
|
if (!_warnedFontKeys.has(warnKey)) {
|
|
@@ -166,30 +195,32 @@ export async function loadAllFonts(event, options) {
|
|
|
166
195
|
logger.warn(`All fonts are variable fonts (${variableFamilies.join(", ")}). Variable fonts are not supported by Satori renderer. Falling back to bundled Inter font. Consider using the 'takumi' renderer for variable font support.`);
|
|
167
196
|
}
|
|
168
197
|
}
|
|
169
|
-
|
|
170
|
-
const families = reqs.families.length > 0 ? reqs.families : [...new Set(loaded.map((f) => f.family))];
|
|
171
|
-
const component = options.component ? ` (${options.component})` : "";
|
|
172
|
-
const fallbackNote = usingGlobalFallback ? " Note: using global font requirements because this component has dynamic font bindings." : "";
|
|
173
|
-
for (const family of families) {
|
|
174
|
-
const loadedWeights = [...new Set(loaded.filter((f) => f.family === family).map((f) => f.weight))].toSorted((a, b) => a - b);
|
|
175
|
-
if (loadedWeights.length === 0)
|
|
176
|
-
continue;
|
|
177
|
-
for (const reqWeight of reqs.weights) {
|
|
178
|
-
if (loadedWeights.includes(reqWeight))
|
|
179
|
-
continue;
|
|
180
|
-
const closest = findClosestWeight(reqWeight, loadedWeights);
|
|
181
|
-
const warnKey = `${family}-${reqWeight}-${closest}-${options.component || ""}`;
|
|
182
|
-
if (_warnedFontKeys.has(warnKey))
|
|
183
|
-
continue;
|
|
184
|
-
if (_warnedFontKeys.size >= 100)
|
|
185
|
-
_warnedFontKeys.clear();
|
|
186
|
-
_warnedFontKeys.add(warnKey);
|
|
187
|
-
logger.warn(`Font "${family}" weight ${reqWeight} not configured${component}, using ${closest} instead.${fallbackNote}`);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
198
|
+
warnMissingWeights(options, { reqs, usingGlobalFallback }, loaded);
|
|
191
199
|
return loaded;
|
|
192
200
|
}
|
|
201
|
+
export function selectLazyFonts(event, options) {
|
|
202
|
+
const { reqs, usingGlobalFallback, selected } = selectFonts(options);
|
|
203
|
+
warnMissingWeights(options, { reqs, usingGlobalFallback }, selected.map(({ font }) => font));
|
|
204
|
+
return selected.map(({ font, src, isStaticFallback }) => ({
|
|
205
|
+
...font,
|
|
206
|
+
src,
|
|
207
|
+
...isStaticFallback ? { unicodeRange: void 0 } : {},
|
|
208
|
+
cacheKey: `${font.family}-${font.weight}-${font.style}-${src}`,
|
|
209
|
+
data: async () => {
|
|
210
|
+
const data = await loadFont(event, font, src);
|
|
211
|
+
if (!data)
|
|
212
|
+
throw new Error(`Failed to load font ${font.family} (${src})`);
|
|
213
|
+
return toUint8Array(data);
|
|
214
|
+
}
|
|
215
|
+
}));
|
|
216
|
+
}
|
|
217
|
+
function toUint8Array(data) {
|
|
218
|
+
if (data instanceof Uint8Array)
|
|
219
|
+
return data;
|
|
220
|
+
if (data instanceof ArrayBuffer)
|
|
221
|
+
return new Uint8Array(data);
|
|
222
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
223
|
+
}
|
|
193
224
|
const MAX_DEFINED_FONTS = 10;
|
|
194
225
|
const MAX_FONT_PATH_LENGTH = 2048;
|
|
195
226
|
function isDataUri(path) {
|
|
@@ -244,10 +275,10 @@ export async function loadFontsForRenderer(event, options) {
|
|
|
244
275
|
const baseFonts = await loadAllFonts(event.e, options);
|
|
245
276
|
const customFonts = Array.isArray(options.fontDefs) && options.fontDefs.length > 0 ? await loadDefinedFonts(event, options.fontDefs, options.supportedFormats) : [];
|
|
246
277
|
const allFonts = customFonts.length > 0 ? [...baseFonts, ...customFonts] : baseFonts;
|
|
247
|
-
return
|
|
278
|
+
return allFonts;
|
|
248
279
|
}
|
|
249
280
|
export function getDefaultFontFamily(options) {
|
|
250
281
|
const fontFamilyOverride = options.props?.fontFamily;
|
|
251
|
-
const defaultFont =
|
|
282
|
+
const defaultFont = getFonts()[0]?.family;
|
|
252
283
|
return { fontFamilyOverride, defaultFont };
|
|
253
284
|
}
|
|
@@ -3,7 +3,7 @@ import { tw4FontVars } from "#og-image-virtual/tw4-theme.mjs";
|
|
|
3
3
|
import compatibility from "#og-image/compatibility";
|
|
4
4
|
import { withTimeout } from "../../util/withTimeout.js";
|
|
5
5
|
import { useOgImageRuntimeConfig } from "../../utils.js";
|
|
6
|
-
import { buildSubsetFamilyChain, extractCodepoints, getDefaultFontFamily, loadAllFontsDebug, loadFontsForRenderer, resolveSubsetChain } from "../fonts.js";
|
|
6
|
+
import { buildSubsetFamilyChain, extractCodepoints, getDefaultFontFamily, loadAllFontsDebug, loadFontsForRenderer, renameSubsetFonts, resolveSubsetChain } from "../fonts.js";
|
|
7
7
|
import { resolveSvgFilters } from "./filters.js";
|
|
8
8
|
import { getResvg, getSatori, getSharp } from "./instances.js";
|
|
9
9
|
import { createVNodes } from "./vnodes.js";
|
|
@@ -34,13 +34,13 @@ export async function createSvg(event) {
|
|
|
34
34
|
]);
|
|
35
35
|
const codepoints = extractCodepoints(vnodes);
|
|
36
36
|
const hasCustomFonts = Array.isArray(options.fonts) && options.fonts.length > 0;
|
|
37
|
-
const fonts = await timings.measure("font-load", () => loadFontsForRenderer(event, {
|
|
37
|
+
const fonts = await timings.measure("font-load", async () => renameSubsetFonts(await loadFontsForRenderer(event, {
|
|
38
38
|
supportedFormats: /* @__PURE__ */ new Set(["ttf", "otf", "woff"]),
|
|
39
39
|
component: options.component,
|
|
40
40
|
fontFamilyOverride: fontFamilyOverride || defaultFont,
|
|
41
41
|
codepoints,
|
|
42
42
|
fontDefs: options.fonts
|
|
43
|
-
}));
|
|
43
|
+
})));
|
|
44
44
|
const hookTimeout = event.runtimeConfig.security?.renderTimeout ?? 15e3;
|
|
45
45
|
await withTimeout(
|
|
46
46
|
event._nitro.hooks.callHook("nuxt-og-image:satori:vnodes", vnodes, event),
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { getFonts } from "#og-image/fonts";
|
|
2
2
|
import { walkTree } from "../core/plugins.js";
|
|
3
3
|
import { createVNodes as coreCreateVNodes, resolveSvgDimension } from "../core/vnodes.js";
|
|
4
4
|
import classes from "./plugins/classes.js";
|
|
5
5
|
import nuxtIcon from "./plugins/nuxt-icon.js";
|
|
6
6
|
export { htmlToVNode, resolveSvgDimension, SVG_CAMEL_ATTR_VALUES, warnUnsupportedSvgElements } from "../core/vnodes.js";
|
|
7
7
|
function getDefaultFontFamily() {
|
|
8
|
-
const fonts =
|
|
8
|
+
const fonts = getFonts();
|
|
9
9
|
const firstFont = fonts[0];
|
|
10
10
|
if (firstFont)
|
|
11
11
|
return `'${firstFont.family}', sans-serif`;
|
|
@@ -1,65 +1,19 @@
|
|
|
1
|
-
import { defu } from "defu";
|
|
2
1
|
import { withBase } from "ufo";
|
|
3
|
-
import { logger } from "../../../logger.js";
|
|
4
2
|
import { fetchLocalAsset } from "../../util/fetchLocalAsset.js";
|
|
5
3
|
import { getFetchTimeout } from "../../util/fetchTimeout.js";
|
|
6
4
|
import { fetchWithRedirectValidation, isBlockedUrl } from "../../util/ssrf.js";
|
|
7
5
|
import { withTimeout } from "../../util/withTimeout.js";
|
|
8
6
|
import { TAKUMI_FORMATS } from "../font-source.js";
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
7
|
+
import { getDefaultFontFamily, loadFontsForRenderer, parseUnicodeRange, selectLazyFonts } from "../fonts.js";
|
|
8
|
+
import { getTakumi } from "./instances.js";
|
|
11
9
|
import { createTakumiNodes } from "./nodes.js";
|
|
12
|
-
|
|
13
|
-
async function getTakumiState(event) {
|
|
10
|
+
async function getTakumiRenderer(event) {
|
|
14
11
|
const nitro = event._nitro;
|
|
15
|
-
if (nitro.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
lock: Promise.resolve()
|
|
21
|
-
};
|
|
22
|
-
return nitro._takumiState;
|
|
23
|
-
}
|
|
24
|
-
function withTakumiLock(state, timeoutMs, fn, onLockTimeout) {
|
|
25
|
-
const guarded = async () => {
|
|
26
|
-
let timer;
|
|
27
|
-
try {
|
|
28
|
-
return await Promise.race([
|
|
29
|
-
fn(),
|
|
30
|
-
new Promise((_, reject) => {
|
|
31
|
-
timer = setTimeout(
|
|
32
|
-
() => reject(new Error(`takumi render timed out after ${timeoutMs}ms (lock-held)`)),
|
|
33
|
-
timeoutMs
|
|
34
|
-
);
|
|
35
|
-
})
|
|
36
|
-
]);
|
|
37
|
-
} catch (err) {
|
|
38
|
-
try {
|
|
39
|
-
const Renderer = await getTakumi();
|
|
40
|
-
state.renderer = new Renderer();
|
|
41
|
-
} catch (resetErr) {
|
|
42
|
-
logger.warn(`failed to reset takumi renderer after lock timeout: ${resetErr?.message || resetErr}`);
|
|
43
|
-
}
|
|
44
|
-
throw err;
|
|
45
|
-
} finally {
|
|
46
|
-
clearTimeout(timer);
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
const work = state.lock.then(guarded, guarded);
|
|
50
|
-
state.lock = work.catch((err) => {
|
|
51
|
-
void err;
|
|
52
|
-
});
|
|
53
|
-
let acquireTimer;
|
|
54
|
-
return Promise.race([
|
|
55
|
-
work,
|
|
56
|
-
new Promise((_, reject) => {
|
|
57
|
-
acquireTimer = setTimeout(() => {
|
|
58
|
-
onLockTimeout?.();
|
|
59
|
-
reject(new Error(`takumi lock acquire timed out after ${timeoutMs}ms`));
|
|
60
|
-
}, timeoutMs);
|
|
61
|
-
})
|
|
62
|
-
]).finally(() => clearTimeout(acquireTimer));
|
|
12
|
+
if (!nitro._takumiRenderer) {
|
|
13
|
+
const Renderer = await getTakumi();
|
|
14
|
+
nitro._takumiRenderer = new Renderer();
|
|
15
|
+
}
|
|
16
|
+
return nitro._takumiRenderer;
|
|
63
17
|
}
|
|
64
18
|
function dedupeFontsByBinary(fonts) {
|
|
65
19
|
const byBinary = /* @__PURE__ */ new Map();
|
|
@@ -77,6 +31,7 @@ function dedupeFontsByBinary(fonts) {
|
|
|
77
31
|
family: font.family,
|
|
78
32
|
style: font.style,
|
|
79
33
|
data: font.data,
|
|
34
|
+
unicodeRange: font.unicodeRange,
|
|
80
35
|
weights: /* @__PURE__ */ new Set([font.weight]),
|
|
81
36
|
hasRange: !!font.weightRange
|
|
82
37
|
});
|
|
@@ -90,25 +45,36 @@ function dedupeFontsByBinary(fonts) {
|
|
|
90
45
|
family: entry.family,
|
|
91
46
|
style: entry.style,
|
|
92
47
|
data: entry.data,
|
|
48
|
+
unicodeRange: entry.unicodeRange,
|
|
93
49
|
weight: isVariable ? void 0 : [...entry.weights][0]
|
|
94
50
|
});
|
|
95
51
|
}
|
|
96
52
|
return result;
|
|
97
53
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (
|
|
102
|
-
|
|
103
|
-
|
|
54
|
+
const RE_HTTP_URL = /^https?:\/\//i;
|
|
55
|
+
const unicodeRanges = /* @__PURE__ */ new Map();
|
|
56
|
+
function parseUnicodeRangeOnce(range) {
|
|
57
|
+
if (!unicodeRanges.has(range)) {
|
|
58
|
+
unicodeRanges.set(range, parseUnicodeRange(range));
|
|
59
|
+
}
|
|
60
|
+
return unicodeRanges.get(range);
|
|
104
61
|
}
|
|
105
|
-
function
|
|
62
|
+
function createFontLoader(entry, failedKeys) {
|
|
63
|
+
const unicodeRange = entry.unicodeRange ? parseUnicodeRangeOnce(entry.unicodeRange) : null;
|
|
106
64
|
return {
|
|
107
65
|
key: entry.binaryKey,
|
|
108
66
|
name: entry.family,
|
|
109
|
-
data:
|
|
67
|
+
data: async () => {
|
|
68
|
+
try {
|
|
69
|
+
return await entry.data();
|
|
70
|
+
} catch (err) {
|
|
71
|
+
failedKeys.add(entry.binaryKey);
|
|
72
|
+
throw err;
|
|
73
|
+
}
|
|
74
|
+
},
|
|
110
75
|
...entry.weight !== void 0 ? { weight: entry.weight } : {},
|
|
111
|
-
...entry.style ? { style: entry.style } : {}
|
|
76
|
+
...entry.style ? { style: entry.style } : {},
|
|
77
|
+
...unicodeRange ? { unicodeRange } : {}
|
|
112
78
|
};
|
|
113
79
|
}
|
|
114
80
|
function normalizeImageSources(images) {
|
|
@@ -117,128 +83,83 @@ function normalizeImageSources(images) {
|
|
|
117
83
|
data: image.data instanceof Uint8Array ? image.data : new Uint8Array(image.data)
|
|
118
84
|
}));
|
|
119
85
|
}
|
|
120
|
-
function lookupFontFamily(family, loadedFamilies) {
|
|
121
|
-
if (loadedFamilies.has(family))
|
|
122
|
-
return family;
|
|
123
|
-
for (const loaded of loadedFamilies) {
|
|
124
|
-
if (loaded.toLowerCase() === family.toLowerCase())
|
|
125
|
-
return loaded;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
function rewriteFontFamilies(node, loadedFamilies, subsetChains) {
|
|
129
|
-
if (node.style?.fontFamily) {
|
|
130
|
-
const families = node.style.fontFamily.split(",").map((f) => f.trim().replace(RE_QUOTES, ""));
|
|
131
|
-
const resolved = [];
|
|
132
|
-
const seen = /* @__PURE__ */ new Set();
|
|
133
|
-
const addUnique = (name) => {
|
|
134
|
-
if (!seen.has(name.toLowerCase())) {
|
|
135
|
-
resolved.push(name);
|
|
136
|
-
seen.add(name.toLowerCase());
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
for (const f of families) {
|
|
140
|
-
const chain = resolveSubsetChain(f, subsetChains);
|
|
141
|
-
if (chain) {
|
|
142
|
-
chain.forEach(addUnique);
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
addUnique(lookupFontFamily(f, loadedFamilies) || f);
|
|
146
|
-
}
|
|
147
|
-
for (const family of loadedFamilies)
|
|
148
|
-
addUnique(family);
|
|
149
|
-
node.style.fontFamily = resolved.map((f) => `"${f}"`).join(", ");
|
|
150
|
-
}
|
|
151
|
-
if ("children" in node && node.children) {
|
|
152
|
-
for (const child of node.children)
|
|
153
|
-
rewriteFontFamilies(child, loadedFamilies, subsetChains);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
86
|
async function createImage(event, format) {
|
|
157
87
|
const { options, timings } = event;
|
|
158
88
|
const { fontFamilyOverride, defaultFont } = getDefaultFontFamily(options);
|
|
159
89
|
const nodes = await createTakumiNodes(event);
|
|
160
|
-
const
|
|
161
|
-
const
|
|
162
|
-
const fontEntries = dedupeFontsByBinary(fonts);
|
|
163
|
-
const hookTimeout = event.runtimeConfig.security?.renderTimeout ?? 15e3;
|
|
90
|
+
const fontEntries = dedupeFontsByBinary(selectLazyFonts(event.e, { supportedFormats: TAKUMI_FORMATS, component: options.component, fontFamilyOverride: fontFamilyOverride || defaultFont }));
|
|
91
|
+
const renderTimeout = event.runtimeConfig.security?.renderTimeout ?? 15e3;
|
|
164
92
|
await withTimeout(
|
|
165
93
|
event._nitro.hooks.callHook("nuxt-og-image:takumi:nodes", nodes, event),
|
|
166
|
-
|
|
94
|
+
renderTimeout,
|
|
167
95
|
"nuxt-og-image:takumi:nodes hook"
|
|
168
96
|
);
|
|
169
|
-
const
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
const baseURL = event.runtimeConfig.app.baseURL;
|
|
174
|
-
const { images: optionImages, ...takumiOptions } = options.takumi || {};
|
|
175
|
-
const images = [
|
|
176
|
-
...normalizeImageSources(optionImages)
|
|
177
|
-
];
|
|
178
|
-
if (resourceUrls.length) {
|
|
179
|
-
const fetchTimeout = getFetchTimeout(event.runtimeConfig);
|
|
180
|
-
const headers = { "x-nuxt-og-image": "1" };
|
|
181
|
-
await timings.measure("resource-fetch", () => Promise.all(resourceUrls.map(async (src) => {
|
|
182
|
-
let data;
|
|
183
|
-
if (src.startsWith("/")) {
|
|
184
|
-
const path = withBase(src, baseURL);
|
|
185
|
-
data = await fetchLocalAsset(event.e, path, {
|
|
186
|
-
fetchTimeout,
|
|
187
|
-
headers,
|
|
188
|
-
includeExternalFallback: true
|
|
189
|
-
});
|
|
190
|
-
} else if (import.meta.dev) {
|
|
191
|
-
data = await $fetch(src, {
|
|
192
|
-
responseType: "arrayBuffer",
|
|
193
|
-
signal: AbortSignal.timeout(fetchTimeout),
|
|
194
|
-
timeout: fetchTimeout,
|
|
195
|
-
headers
|
|
196
|
-
}).catch((err) => {
|
|
197
|
-
void err;
|
|
198
|
-
return void 0;
|
|
199
|
-
});
|
|
200
|
-
} else if (!isBlockedUrl(src)) {
|
|
201
|
-
data = await fetchWithRedirectValidation(src, {
|
|
202
|
-
timeout: fetchTimeout,
|
|
203
|
-
headers
|
|
204
|
-
}) ?? void 0;
|
|
205
|
-
}
|
|
206
|
-
if (data)
|
|
207
|
-
images.push({ src, data: new Uint8Array(data) });
|
|
208
|
-
})));
|
|
97
|
+
const renderer = await timings.measure("takumi-init", () => getTakumiRenderer(event));
|
|
98
|
+
const loadedFamilies = new Set(fontEntries.map((entry) => entry.family));
|
|
99
|
+
if (fontFamilyOverride && loadedFamilies.has(fontFamilyOverride)) {
|
|
100
|
+
nodes.style = { ...nodes.style, fontFamily: fontFamilyOverride };
|
|
209
101
|
}
|
|
210
102
|
const maxDpr = event.runtimeConfig.security?.maxDpr || 2;
|
|
211
103
|
const maxDim = event.runtimeConfig.security?.maxDimension || 2048;
|
|
212
104
|
const dpr = Math.min(Math.max(1, options.takumi?.devicePixelRatio ?? 1), maxDpr);
|
|
213
|
-
const
|
|
105
|
+
const fetchTimeout = getFetchTimeout(event.runtimeConfig);
|
|
106
|
+
const failedFontKeys = /* @__PURE__ */ new Set();
|
|
107
|
+
const fontLoaders = fontEntries.map((entry) => createFontLoader(entry, failedFontKeys));
|
|
108
|
+
const signal = AbortSignal.timeout(renderTimeout);
|
|
109
|
+
const prepare = (loaders) => renderer.prepare(nodes, {
|
|
214
110
|
width: Math.min(Number(options.width) * dpr, maxDim),
|
|
215
111
|
height: Math.min(Number(options.height) * dpr, maxDim),
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
112
|
+
devicePixelRatio: dpr,
|
|
113
|
+
fonts: loaders,
|
|
114
|
+
fontFamilies: [...loadedFamilies],
|
|
115
|
+
images: {
|
|
116
|
+
sources: normalizeImageSources(options.takumi?.images),
|
|
117
|
+
fetch: (src) => fetchImage(event, src, fetchTimeout),
|
|
118
|
+
// Defense-in-depth: any URL surviving the imageSrc transformer is re-validated here.
|
|
119
|
+
allowUrl: (src) => src.startsWith("/") || (import.meta.dev ? RE_HTTP_URL.test(src) : !isBlockedUrl(src)),
|
|
120
|
+
timeout: fetchTimeout,
|
|
121
|
+
throwOnError: false
|
|
122
|
+
},
|
|
123
|
+
signal
|
|
219
124
|
});
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
const loadedFamilies = new Set(fontEntries.map((entry) => entry.family));
|
|
226
|
-
renderOptions.fonts = fontEntries.map((entry) => createFontDescriptor(entry));
|
|
227
|
-
renderOptions.fontFamilies = [...loadedFamilies];
|
|
228
|
-
const rootStyle = nodes.style ?? {};
|
|
229
|
-
if (fontFamilyOverride) {
|
|
230
|
-
const chain = subsetChains.get(fontFamilyOverride);
|
|
231
|
-
if (chain) {
|
|
232
|
-
rootStyle.fontFamily = chain.map((f) => `"${f}"`).join(", ");
|
|
233
|
-
} else if (loadedFamilies.has(fontFamilyOverride)) {
|
|
234
|
-
rootStyle.fontFamily = fontFamilyOverride;
|
|
125
|
+
return await withTimeout((async () => {
|
|
126
|
+
try {
|
|
127
|
+
const prepared = await timings.measure("takumi-prepare", () => prepare(fontLoaders).catch((err) => {
|
|
128
|
+
if (failedFontKeys.size === 0) {
|
|
129
|
+
throw err;
|
|
235
130
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
131
|
+
return prepare(fontLoaders.filter((loader) => !failedFontKeys.has(loader.key)));
|
|
132
|
+
}));
|
|
133
|
+
return await timings.measure("render-takumi", () => renderer.render(prepared, { format: options.takumi?.format ?? format, signal }));
|
|
134
|
+
} catch (err) {
|
|
135
|
+
delete event._nitro._takumiRenderer;
|
|
136
|
+
throw err;
|
|
137
|
+
}
|
|
138
|
+
})(), renderTimeout, "takumi render");
|
|
139
|
+
}
|
|
140
|
+
async function fetchImage(event, src, fetchTimeout) {
|
|
141
|
+
const headers = { "x-nuxt-og-image": "1" };
|
|
142
|
+
let data;
|
|
143
|
+
if (src.startsWith("/")) {
|
|
144
|
+
data = await fetchLocalAsset(event.e, withBase(src, event.runtimeConfig.app.baseURL), {
|
|
145
|
+
fetchTimeout,
|
|
146
|
+
headers,
|
|
147
|
+
includeExternalFallback: true
|
|
148
|
+
});
|
|
149
|
+
} else if (import.meta.dev) {
|
|
150
|
+
data = await $fetch(src, {
|
|
151
|
+
responseType: "arrayBuffer",
|
|
152
|
+
signal: AbortSignal.timeout(fetchTimeout),
|
|
153
|
+
timeout: fetchTimeout,
|
|
154
|
+
headers
|
|
240
155
|
});
|
|
241
|
-
}
|
|
156
|
+
} else {
|
|
157
|
+
data = await fetchWithRedirectValidation(src, { timeout: fetchTimeout, headers });
|
|
158
|
+
}
|
|
159
|
+
if (!data) {
|
|
160
|
+
throw new Error(`Failed to fetch image ${src}`);
|
|
161
|
+
}
|
|
162
|
+
return new Response(data);
|
|
242
163
|
}
|
|
243
164
|
const TakumiRenderer = {
|
|
244
165
|
name: "takumi",
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { createHead, renderSSRHead } from "@unhead/vue/server";
|
|
2
2
|
import { createError } from "#nuxtseo/h3";
|
|
3
|
-
import
|
|
3
|
+
import { getFonts } from "#og-image/fonts";
|
|
4
4
|
import { fetchIsland } from "../../util/kit.js";
|
|
5
5
|
import { applyEmojis } from "../core/transforms/emojis/index.js";
|
|
6
6
|
const RE_SCRIPT_TAG = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
|
|
7
7
|
export async function html(ctx) {
|
|
8
8
|
const { options } = ctx;
|
|
9
|
-
const fonts = resolvedFonts;
|
|
10
9
|
if (!options.component) {
|
|
11
10
|
throw createError({
|
|
12
11
|
statusCode: 500,
|
|
@@ -15,6 +14,7 @@ export async function html(ctx) {
|
|
|
15
14
|
}
|
|
16
15
|
const islandTimeout = ctx.runtimeConfig.security?.renderTimeout ?? 15e3;
|
|
17
16
|
const island = await fetchIsland(ctx.e, ctx.options.component, typeof ctx.options.props !== "undefined" ? ctx.options.props : ctx.options, islandTimeout);
|
|
17
|
+
const fonts = getFonts();
|
|
18
18
|
const head = createHead();
|
|
19
19
|
head.push(island.head);
|
|
20
20
|
let defaultFontFamily = "sans-serif";
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { defineEventHandler } from "nuxt/server";
|
|
2
2
|
import { componentNames } from "#og-image-virtual/component-names.mjs";
|
|
3
3
|
import compatibility from "#og-image/compatibility";
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import
|
|
4
|
+
import { getFontRequirements } from "#og-image/font-requirements";
|
|
5
|
+
import { getFonts } from "#og-image/fonts";
|
|
6
|
+
import { getAvailableFonts } from "#og-image/fonts-available";
|
|
7
7
|
import { getSiteConfig } from "#site-config/server/composables/getSiteConfig";
|
|
8
8
|
import { getOgImageOrigin } from "../util/origin.js";
|
|
9
9
|
import { useOgImageRuntimeConfig } from "../utils.js";
|
|
@@ -16,8 +16,8 @@ export default defineEventHandler(async (e) => {
|
|
|
16
16
|
componentNames,
|
|
17
17
|
runtimeConfig,
|
|
18
18
|
compatibility,
|
|
19
|
-
resolvedFonts,
|
|
20
|
-
availableFonts,
|
|
21
|
-
fontRequirements
|
|
19
|
+
resolvedFonts: getFonts(),
|
|
20
|
+
availableFonts: getAvailableFonts(),
|
|
21
|
+
fontRequirements: getFontRequirements()
|
|
22
22
|
};
|
|
23
23
|
});
|
|
@@ -3,11 +3,11 @@ import { createError, defineEventHandler, getRequestURL, useRuntimeConfig } from
|
|
|
3
3
|
import { join } from "pathe";
|
|
4
4
|
import { withoutBase } from "ufo";
|
|
5
5
|
import { staticFontCacheDir } from "#og-image-virtual/build-dir.mjs";
|
|
6
|
-
import
|
|
7
|
-
import
|
|
6
|
+
import { getFonts } from "#og-image/fonts";
|
|
7
|
+
import { getAvailableFonts } from "#og-image/fonts-available";
|
|
8
8
|
export default defineEventHandler(async (event) => {
|
|
9
9
|
const path = withoutBase(getRequestURL(event).pathname, useRuntimeConfig().app.baseURL);
|
|
10
|
-
const font = [...
|
|
10
|
+
const font = [...getFonts(), ...getAvailableFonts()].find((face) => face.src === path || face.satoriSrc === path);
|
|
11
11
|
if (!font)
|
|
12
12
|
throw createError({ status: 404, statusText: "Not Found" });
|
|
13
13
|
const file = path === font.src && font.absolutePath ? font.absolutePath : join(staticFontCacheDir, path.slice("/_og-static-fonts/".length));
|
|
@@ -3,6 +3,7 @@ import { joinURL } from "ufo";
|
|
|
3
3
|
import { getCloudflareEnv } from "./util/cloudflare.js";
|
|
4
4
|
export const OG_IMAGE_SECRET_PURPOSE = "nuxt-og-image:url-signing";
|
|
5
5
|
let observed;
|
|
6
|
+
let derivation;
|
|
6
7
|
let warnedLegacy = false;
|
|
7
8
|
export async function initializeOgImageSigning(event) {
|
|
8
9
|
const config = useRuntimeConfig();
|
|
@@ -17,7 +18,7 @@ export async function initializeOgImageSigning(event) {
|
|
|
17
18
|
cloudflareEnv.NUXT_APP_SECRET === void 0 ? configRoot : cloudflareEnv.NUXT_APP_SECRET,
|
|
18
19
|
cloudflareEnv.NUXT_OG_IMAGE_SECRET
|
|
19
20
|
);
|
|
20
|
-
const secret = observed !== void 0 && observed.root === root2 ? observed.secret : root2 === configRoot ?
|
|
21
|
+
const secret = observed !== void 0 && observed.root === root2 ? observed.secret : await getSigningSecret(root2, root2 === configRoot ? "nuxt" : "explicit");
|
|
21
22
|
observed = { root: root2, configRoot, processRoot, secret };
|
|
22
23
|
event.context._ogImageSigningSecret = secret;
|
|
23
24
|
return;
|
|
@@ -27,11 +28,22 @@ export async function initializeOgImageSigning(event) {
|
|
|
27
28
|
return;
|
|
28
29
|
}
|
|
29
30
|
const root = resolveLegacyRoot(configRoot, processRoot);
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
event.context._ogImageSigningSecret = await getSigningSecret(root, root === configRoot ? "nuxt" : "explicit");
|
|
32
|
+
}
|
|
33
|
+
function getSigningSecret(root, mode) {
|
|
34
|
+
if (typeof root !== "string" || root.length < 32)
|
|
35
|
+
return mode === "nuxt" ? deriveSecret(OG_IMAGE_SECRET_PURPOSE) : deriveRootSecret(root);
|
|
36
|
+
if (derivation?.root === root && derivation.mode === mode)
|
|
37
|
+
return derivation.promise;
|
|
38
|
+
const promise = mode === "nuxt" ? deriveSecret(OG_IMAGE_SECRET_PURPOSE) : deriveRootSecret(root);
|
|
39
|
+
const entry = { root, mode, promise };
|
|
40
|
+
derivation = entry;
|
|
41
|
+
entry.promise = promise.catch((error) => {
|
|
42
|
+
if (derivation === entry)
|
|
43
|
+
derivation = void 0;
|
|
44
|
+
throw error;
|
|
45
|
+
});
|
|
46
|
+
return entry.promise;
|
|
35
47
|
}
|
|
36
48
|
export function isOgImageEndpointPath(pathname, baseURL) {
|
|
37
49
|
return pathname.startsWith("/_og/") || pathname.startsWith(joinURL(baseURL, "_og/"));
|
|
@@ -53,7 +65,7 @@ function resolveLegacyRoot(root, legacy) {
|
|
|
53
65
|
if ((root === void 0 || root === "") && legacy !== void 0 && legacy !== "") {
|
|
54
66
|
if (!warnedLegacy) {
|
|
55
67
|
warnedLegacy = true;
|
|
56
|
-
console.warn("[nuxt-og-image] NUXT_OG_IMAGE_SECRET is deprecated. Rename it to NUXT_APP_SECRET
|
|
68
|
+
console.warn("[nuxt-og-image] NUXT_OG_IMAGE_SECRET is deprecated. Rename it to NUXT_APP_SECRET and keep its value.");
|
|
57
69
|
}
|
|
58
70
|
return legacy;
|
|
59
71
|
}
|