nuxt-og-image 7.0.7 → 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/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/shared/{nuxt-og-image.CS4ArVpM.mjs → nuxt-og-image.Dr6blNqj.mjs} +225 -99
- 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
package/dist/chunks/tw4.mjs
CHANGED
|
@@ -2,9 +2,8 @@ import { readFile } from 'node:fs/promises';
|
|
|
2
2
|
import { pathToFileURL } from 'node:url';
|
|
3
3
|
import { resolveModulePath } from 'exsolve';
|
|
4
4
|
import { dirname, join } from 'pathe';
|
|
5
|
-
import { e as extractVariantBaseClasses, r as resolveVariantPrefixes, h as resolveExtractedVars, l as resolveVarsDeep, s as simplifyCss, p as postProcessStyles, m as loadLightningCss, a as extractCssVars, c as extractUniversalVars, d as extractPerClassVars, g as extractVarsFromCss, f as extractClassStyles } from '../shared/nuxt-og-image.
|
|
5
|
+
import { e as extractVariantBaseClasses, r as resolveVariantPrefixes, h as resolveExtractedVars, l as resolveVarsDeep, s as simplifyCss, p as postProcessStyles, m as loadLightningCss, a as extractCssVars, c as extractUniversalVars, d as extractPerClassVars, g as extractVarsFromCss, f as extractClassStyles } from '../shared/nuxt-og-image.Dr6blNqj.mjs';
|
|
6
6
|
import 'node:fs';
|
|
7
|
-
import 'node:module';
|
|
8
7
|
import '@nuxt/kit';
|
|
9
8
|
import 'defu';
|
|
10
9
|
import 'fnv1a-64';
|
package/dist/chunks/uno.mjs
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
2
|
import { defu } from 'defu';
|
|
3
3
|
import { logger } from '../../dist/runtime/logger.js';
|
|
4
|
-
import { e as extractVariantBaseClasses, s as simplifyCss, a as extractCssVars, b as extractPropertyInitialValues, c as extractUniversalVars, d as extractPerClassVars, f as extractClassStyles, p as postProcessStyles, r as resolveVariantPrefixes, i as importOptionalPeer, g as extractVarsFromCss, h as resolveExtractedVars } from '../shared/nuxt-og-image.
|
|
5
|
-
export { j as importResolvedModule, k as resolveOptionalModulePath } from '../shared/nuxt-og-image.
|
|
4
|
+
import { e as extractVariantBaseClasses, s as simplifyCss, a as extractCssVars, b as extractPropertyInitialValues, c as extractUniversalVars, d as extractPerClassVars, f as extractClassStyles, p as postProcessStyles, r as resolveVariantPrefixes, i as importOptionalPeer, g as extractVarsFromCss, h as resolveExtractedVars } from '../shared/nuxt-og-image.Dr6blNqj.mjs';
|
|
5
|
+
export { j as importResolvedModule, k as resolveOptionalModulePath } from '../shared/nuxt-og-image.Dr6blNqj.mjs';
|
|
6
6
|
import 'node:fs';
|
|
7
|
-
import 'node:module';
|
|
8
7
|
import 'node:url';
|
|
9
8
|
import '@nuxt/kit';
|
|
10
9
|
import 'fnv1a-64';
|
|
@@ -159,7 +158,10 @@ function createUnoProvider(options) {
|
|
|
159
158
|
const perClassVars = extractPerClassVars(simplifiedCss);
|
|
160
159
|
const classMap = extractClassStyles(simplifiedCss, { collectVars: vars, merge: true });
|
|
161
160
|
const resolvedBaseStyles = /* @__PURE__ */ new Map();
|
|
161
|
+
const requested = new Set(classesToGenerate);
|
|
162
162
|
for (const [className, rawStyles] of classMap) {
|
|
163
|
+
if (!requested.has(className))
|
|
164
|
+
continue;
|
|
163
165
|
const classVars = perClassVars.get(className);
|
|
164
166
|
const mergedVars = classVars ? new Map([...vars, ...classVars]) : vars;
|
|
165
167
|
const styles = await postProcessStyles(rawStyles, mergedVars, void 0, context);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
|
|
3
|
+
declare function defineOgImageSchema(): v.OptionalSchema<v.ObjectSchema<{
|
|
4
|
+
readonly url: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
5
|
+
readonly component: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
6
|
+
readonly props: v.RecordSchema<v.StringSchema<undefined>, v.UnknownSchema, undefined>;
|
|
7
|
+
}, undefined>, undefined>;
|
|
8
|
+
|
|
9
|
+
export { defineOgImageSchema };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
|
|
3
|
+
declare function defineOgImageSchema(): v.OptionalSchema<v.ObjectSchema<{
|
|
4
|
+
readonly url: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
5
|
+
readonly component: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
6
|
+
readonly props: v.RecordSchema<v.StringSchema<undefined>, v.UnknownSchema, undefined>;
|
|
7
|
+
}, undefined>, undefined>;
|
|
8
|
+
|
|
9
|
+
export { defineOgImageSchema };
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import 'node:fs';
|
|
2
2
|
import 'node:fs/promises';
|
|
3
|
-
import 'node:module';
|
|
4
3
|
import 'node:url';
|
|
5
4
|
import '@nuxt/kit';
|
|
6
5
|
import 'defu';
|
|
7
6
|
import 'fnv1a-64';
|
|
8
7
|
import 'nuxtseo-shared/kit';
|
|
9
8
|
import 'pathe';
|
|
10
|
-
export { n as default } from './shared/nuxt-og-image.
|
|
9
|
+
export { n as default } from './shared/nuxt-og-image.Dr6blNqj.mjs';
|
|
11
10
|
import '../dist/runtime/logger.js';
|
|
12
11
|
import '../dist/runtime/shared/publish.js';
|
|
13
12
|
import 'node:crypto';
|
|
@@ -15,4 +15,4 @@ import type { DefineOgImageInput } from '../types.js';
|
|
|
15
15
|
* page relies on useSeoMeta auto-injection the rebuilt URL won't include those
|
|
16
16
|
* values; the resolver can't help without a server.
|
|
17
17
|
*/
|
|
18
|
-
export declare function clientProcessOgImageOptions(input: DefineOgImageInput | DefineOgImageInput[], route: RouteLocationNormalizedLoaded, basePath: string): string[];
|
|
18
|
+
export declare function clientProcessOgImageOptions(input: DefineOgImageInput | DefineOgImageInput[], route: RouteLocationNormalizedLoaded, basePath: string, isHydrating?: boolean): string[];
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { defu } from "defu";
|
|
2
|
-
import { injectHead, useHead, useRuntimeConfig } from "nuxt/app";
|
|
2
|
+
import { injectHead, useHead, useRuntimeConfig, useState } from "nuxt/app";
|
|
3
3
|
import { joinURL, withQuery } from "ufo";
|
|
4
4
|
import { toValue } from "vue";
|
|
5
5
|
import { hasServerRuntime } from "#build/nuxt-og-image/client-config.mjs";
|
|
6
|
-
import {
|
|
6
|
+
import { componentHashes, defaultComponentName } from "#build/nuxt-og-image/component-hashes.mjs";
|
|
7
7
|
import { createSitePathResolver } from "#site-config/app";
|
|
8
8
|
import { buildOgImageUrl, generateMeta, separateProps } from "../shared.js";
|
|
9
9
|
const clientEntriesByHead = /* @__PURE__ */ new WeakMap();
|
|
10
|
+
const HYDRATION_KEY = Symbol("og-image:hydration");
|
|
10
11
|
function registerClientOgHead(ogKey, input, options) {
|
|
11
12
|
let entries;
|
|
12
13
|
try {
|
|
@@ -20,6 +21,10 @@ function registerClientOgHead(ogKey, input, options) {
|
|
|
20
21
|
}
|
|
21
22
|
} catch {
|
|
22
23
|
}
|
|
24
|
+
if (ogKey !== HYDRATION_KEY) {
|
|
25
|
+
entries?.get(HYDRATION_KEY)?.dispose();
|
|
26
|
+
entries?.delete(HYDRATION_KEY);
|
|
27
|
+
}
|
|
23
28
|
entries?.get(ogKey)?.dispose();
|
|
24
29
|
const entry = useHead(input, options);
|
|
25
30
|
if (entry && entries)
|
|
@@ -59,13 +64,26 @@ function buildResolverUrl(baseURL, basePath, ogKey, query) {
|
|
|
59
64
|
queryObj._og_key = "twitter";
|
|
60
65
|
return Object.keys(queryObj).length ? withQuery(resolverUrl, queryObj) : resolverUrl;
|
|
61
66
|
}
|
|
62
|
-
export function clientProcessOgImageOptions(input, route, basePath) {
|
|
67
|
+
export function clientProcessOgImageOptions(input, route, basePath, isHydrating = false) {
|
|
63
68
|
const inputs = Array.isArray(input) ? input : [input];
|
|
64
69
|
const rc = useRuntimeConfig();
|
|
65
|
-
const baseURL = rc.app.baseURL;
|
|
66
|
-
const resolveImageUrl = createSitePathResolver({ absolute: true, withBase: true, canonical: false });
|
|
67
70
|
const publicCfg = rc.public?.["nuxt-og-image"] || {};
|
|
68
71
|
const defaults = publicCfg.defaults || {};
|
|
72
|
+
if (isHydrating) {
|
|
73
|
+
const meta = useState("og-image:meta", () => ({}));
|
|
74
|
+
const hasImage = inputs.some((rawInput) => {
|
|
75
|
+
const options = toValue(rawInput);
|
|
76
|
+
if (options === false)
|
|
77
|
+
return false;
|
|
78
|
+
const key = options.key || defaults.key || "og";
|
|
79
|
+
return !!meta.value[key]?.length;
|
|
80
|
+
});
|
|
81
|
+
if (hasImage)
|
|
82
|
+
registerClientOgHead(HYDRATION_KEY, { meta: Object.values(meta.value).flat() }, { processTemplateParams: true });
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
const baseURL = rc.app.baseURL;
|
|
86
|
+
const resolveImageUrl = createSitePathResolver({ absolute: true, withBase: true, canonical: false });
|
|
69
87
|
const metaOptions = { includeTwitter: publicCfg.includeTwitter };
|
|
70
88
|
const paths = [];
|
|
71
89
|
for (const rawInput of inputs) {
|
|
@@ -96,10 +114,10 @@ export function clientProcessOgImageOptions(input, route, basePath) {
|
|
|
96
114
|
const opts = separateProps(defu(validOptions, defaults));
|
|
97
115
|
const extension = opts.extension || defaults?.extension || "png";
|
|
98
116
|
const urlOpts = { ...opts, _path: basePath };
|
|
99
|
-
const componentName = opts.component ||
|
|
100
|
-
const
|
|
101
|
-
if (
|
|
102
|
-
urlOpts._componentHash =
|
|
117
|
+
const componentName = opts.component || defaultComponentName;
|
|
118
|
+
const componentHash = componentName ? componentHashes[componentName] : void 0;
|
|
119
|
+
if (componentHash)
|
|
120
|
+
urlOpts._componentHash = componentHash;
|
|
103
121
|
const result = buildOgImageUrl(urlOpts, extension, true, defaults, void 0);
|
|
104
122
|
const resolvedUrl = joinURL("/", baseURL, result.url);
|
|
105
123
|
const query = opts._query && Object.keys(opts._query).length ? { _query: opts._query } : {};
|
|
@@ -16,9 +16,7 @@ export function defineOgImageRaw(_options = {}) {
|
|
|
16
16
|
throw createError({ message: "You are using a defineOgImage() function in a client-only context. You must call this function within your root component setup, see https://github.com/nuxt-modules/og-image/pull/293." });
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
|
|
20
|
-
return [];
|
|
21
|
-
return clientProcessOgImageOptions(_options, route, basePath);
|
|
19
|
+
return clientProcessOgImageOptions(_options, route, basePath, nuxtApp.isHydrating);
|
|
22
20
|
}
|
|
23
21
|
const paths = [];
|
|
24
22
|
if (Array.isArray(_options)) {
|
|
@@ -45,6 +43,7 @@ function useProcessOgImageOptions(_options, nuxtApp, route, basePath) {
|
|
|
45
43
|
nuxtApp.ssrContext._ogImageDevtoolsInstance?.dispose();
|
|
46
44
|
nuxtApp.ssrContext._ogImageInstance = void 0;
|
|
47
45
|
nuxtApp.ssrContext._ogImagePayloads = [];
|
|
46
|
+
useState("og-image:meta", () => ({})).value = {};
|
|
48
47
|
if (import.meta.prerender) {
|
|
49
48
|
const event = useRequestEvent(nuxtApp);
|
|
50
49
|
if (event?.context._ogImagePrerenderPaths)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defu } from "defu";
|
|
2
2
|
import { stringify } from "devalue";
|
|
3
|
-
import { useHead, useRequestEvent, useRuntimeConfig } from "nuxt/app";
|
|
3
|
+
import { useHead, useNuxtApp, useRequestEvent, useRuntimeConfig, useState } from "nuxt/app";
|
|
4
4
|
import { joinURL, withQuery } from "ufo";
|
|
5
5
|
import { isRef, toValue } from "vue";
|
|
6
6
|
import { componentNames } from "#build/nuxt-og-image/components.mjs";
|
|
@@ -58,6 +58,7 @@ export function setHeadOgImagePrebuilt(input) {
|
|
|
58
58
|
const urlValue = toValue(url);
|
|
59
59
|
const absoluteUrl = /^https?:\/\//.test(urlValue) ? urlValue : toValue(withSiteUrl(urlValue, { withBase: true, canonical: !import.meta.dev }));
|
|
60
60
|
const meta = generateMeta(absoluteUrl, input, { includeTwitter });
|
|
61
|
+
useState("og-image:meta", () => ({})).value[input.key || "og"] = resolveUnrefHeadInput(meta);
|
|
61
62
|
useHead({ meta });
|
|
62
63
|
}
|
|
63
64
|
export function createOgImageMeta(src, input, ssrContext, pagePath, head) {
|
|
@@ -81,8 +82,8 @@ export function createOgImageMeta(src, input, ssrContext, pagePath, head) {
|
|
|
81
82
|
}
|
|
82
83
|
const baseURL = useRuntimeConfig().app.baseURL;
|
|
83
84
|
const resolveImageUrl = createSitePathResolver({ absolute: true, withBase: true, canonical: !import.meta.dev });
|
|
84
|
-
|
|
85
|
-
|
|
85
|
+
const hydrationMeta = useState("og-image:meta", () => ({}));
|
|
86
|
+
const headInput = {
|
|
86
87
|
// Meta is generated lazily so that title/description from useSeoMeta / useHead
|
|
87
88
|
// are available regardless of call ordering (all component setups have completed
|
|
88
89
|
// by the time Unhead resolves tags).
|
|
@@ -122,10 +123,17 @@ export function createOgImageMeta(src, input, ssrContext, pagePath, head) {
|
|
|
122
123
|
prerenderPaths.set(ogKey, (finalUrl.split("?")[0] || finalUrl).replace(/,/g, "%2C"));
|
|
123
124
|
}
|
|
124
125
|
}
|
|
125
|
-
|
|
126
|
+
const meta = generateMeta(toValue(resolveImageUrl(finalUrl)), opts, { includeTwitter: ogImageConfig.includeTwitter });
|
|
127
|
+
hydrationMeta.value[opts.key || "og"] = resolveUnrefHeadInput(meta);
|
|
128
|
+
return meta;
|
|
126
129
|
});
|
|
127
130
|
}
|
|
128
|
-
}
|
|
131
|
+
};
|
|
132
|
+
useNuxtApp().hook("app:rendered", () => {
|
|
133
|
+
headInput.meta();
|
|
134
|
+
});
|
|
135
|
+
ssrContext._ogImageInstance?.dispose();
|
|
136
|
+
ssrContext._ogImageInstance = useHead(headInput, {
|
|
129
137
|
processTemplateParams: true
|
|
130
138
|
});
|
|
131
139
|
if (import.meta.dev || import.meta.prerender) {
|
|
@@ -3,7 +3,7 @@ import { join } from "pathe";
|
|
|
3
3
|
import { withBase } from "ufo";
|
|
4
4
|
import { getRequestURL } from "#nuxtseo/h3";
|
|
5
5
|
import { fetchWithEvent, useRuntimeConfig } from "#nuxtseo/nitro";
|
|
6
|
-
import {
|
|
6
|
+
import { getResolvedFontPaths, rootDir, staticFontCacheDir } from "#og-image-virtual/build-dir.mjs";
|
|
7
7
|
import { getSiteConfig } from "#site-config/server/composables";
|
|
8
8
|
import { getFetchTimeout } from "../../../util/fetchTimeout.js";
|
|
9
9
|
import { fetchWithRedirectValidation } from "../../../util/ssrf.js";
|
|
@@ -27,8 +27,9 @@ export async function resolve(event, font) {
|
|
|
27
27
|
}
|
|
28
28
|
if (path && (isDataFontUrl(path) || isExternalFontUrl(path)))
|
|
29
29
|
return fetchSpecialFontUrl(path, getSiteConfig(event).url, timeout);
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
const resolvedFontPath = getResolvedFontPaths()[path];
|
|
31
|
+
if (resolvedFontPath)
|
|
32
|
+
return readFile(resolvedFontPath);
|
|
32
33
|
if (import.meta.prerender) {
|
|
33
34
|
if (path.startsWith("/_og-static-fonts/")) {
|
|
34
35
|
const filename = path.slice("/_og-static-fonts/".length);
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
declare function extractResourceUrls(nodes: any): Promise<string[]>;
|
|
2
1
|
declare const _default: {
|
|
3
2
|
initWasmPromise: Promise<void>;
|
|
4
3
|
Renderer: typeof import("@takumi-rs/core").Renderer;
|
|
5
|
-
extractResourceUrls: typeof extractResourceUrls;
|
|
6
4
|
};
|
|
7
5
|
export default _default;
|
|
@@ -1,37 +1,58 @@
|
|
|
1
1
|
import { Worker } from "node:worker_threads";
|
|
2
2
|
import { createWorkerQueue, parseWorkerTimeout } from "../worker-queue.js";
|
|
3
|
-
import { extractResourceUrls as extractTakumiResourceUrls } from "./resource-urls.js";
|
|
4
3
|
const workerCode = `
|
|
5
4
|
const { createRequire } = require('node:module')
|
|
6
5
|
const _require = createRequire(process.cwd() + '/')
|
|
7
6
|
const { parentPort } = require('node:worker_threads')
|
|
8
7
|
const { Renderer } = _require('@takumi-rs/core')
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
const renderer = new Renderer()
|
|
10
|
+
const pendingReads = new Map()
|
|
11
|
+
let readId = 0
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
function readFromMain(id, kind, key) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
pendingReads.set(++readId, { resolve, reject })
|
|
16
|
+
parentPort.postMessage({ _tag: 'read', id, readId, kind, key })
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
parentPort.on('message', async (message) => {
|
|
21
|
+
if (message.type === 'read-result') {
|
|
22
|
+
const pending = pendingReads.get(message.readId)
|
|
23
|
+
pendingReads.delete(message.readId)
|
|
24
|
+
if (message.error)
|
|
25
|
+
pending.reject(new Error(message.error))
|
|
26
|
+
else
|
|
27
|
+
pending.resolve(message.data)
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
const { id, nodes, layout, fonts, images, format } = message
|
|
13
31
|
try {
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
const failedFonts = new Set()
|
|
33
|
+
const loaders = fonts.map(font => ({
|
|
34
|
+
...font,
|
|
35
|
+
data: () => readFromMain(id, 'font', font.key).catch((err) => {
|
|
36
|
+
failedFonts.add(font.key)
|
|
37
|
+
throw err
|
|
38
|
+
}),
|
|
39
|
+
}))
|
|
40
|
+
const prepare = fonts => renderer.prepare(nodes, {
|
|
41
|
+
...layout,
|
|
42
|
+
fonts,
|
|
43
|
+
images: { sources: images, fetch: async src => new Response(await readFromMain(id, 'image', src)), throwOnError: false },
|
|
44
|
+
})
|
|
45
|
+
// A font that fails to load rejects prepare, so render without it instead
|
|
46
|
+
const prepared = await prepare(loaders).catch((err) => {
|
|
47
|
+
if (failedFonts.size === 0)
|
|
48
|
+
throw err
|
|
49
|
+
return prepare(loaders.filter(font => !failedFonts.has(font.key)))
|
|
50
|
+
})
|
|
51
|
+
const image = await renderer.render(prepared, { format })
|
|
31
52
|
// Always slice to create a standard ArrayBuffer \u2014 native addon buffers
|
|
32
53
|
// use external memory that can't be transferred via postMessage
|
|
33
54
|
const ab = image.buffer.slice(image.byteOffset, image.byteOffset + image.byteLength)
|
|
34
|
-
parentPort.postMessage({ _tag: 'response', id, payload: { _tag: 'success', image: ab
|
|
55
|
+
parentPort.postMessage({ _tag: 'response', id, payload: { _tag: 'success', image: ab } }, [ab])
|
|
35
56
|
} catch (err) {
|
|
36
57
|
parentPort.postMessage({ _tag: 'response', id, payload: { _tag: 'failure', error: err?.message || String(err) } })
|
|
37
58
|
}
|
|
@@ -39,8 +60,32 @@ parentPort.on('message', async ({ id, type, newFonts, nodes, options }) => {
|
|
|
39
60
|
parentPort.postMessage({ _tag: 'ready' })
|
|
40
61
|
`;
|
|
41
62
|
const workerTimeout = parseWorkerTimeout(process.env.NUXT_OG_IMAGE_WORKER_TIMEOUT);
|
|
63
|
+
const activeJobs = /* @__PURE__ */ new Map();
|
|
42
64
|
const workerQueue = createWorkerQueue({
|
|
43
|
-
createWorker: () =>
|
|
65
|
+
createWorker: () => {
|
|
66
|
+
const worker = new Worker(workerCode, { eval: true });
|
|
67
|
+
worker.on("message", async (message) => {
|
|
68
|
+
if (message._tag !== "read")
|
|
69
|
+
return;
|
|
70
|
+
const { id, readId, kind, key } = message;
|
|
71
|
+
try {
|
|
72
|
+
const options = activeJobs.get(id);
|
|
73
|
+
if (kind === "font") {
|
|
74
|
+
const data2 = await options?.fonts.find((font) => font.key === key)?.data();
|
|
75
|
+
worker.postMessage({ type: "read-result", readId, data: data2 });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (!options?.images.allowUrl(key)) {
|
|
79
|
+
throw new Error(`Image ${key} is not allowed`);
|
|
80
|
+
}
|
|
81
|
+
const data = await (await options.images.fetch(key)).arrayBuffer();
|
|
82
|
+
worker.postMessage({ type: "read-result", readId, data }, [data]);
|
|
83
|
+
} catch (err) {
|
|
84
|
+
worker.postMessage({ type: "read-result", readId, error: err.message });
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
return worker;
|
|
88
|
+
},
|
|
44
89
|
executionTimeout: workerTimeout,
|
|
45
90
|
startupTimeout: workerTimeout,
|
|
46
91
|
label: "takumi"
|
|
@@ -50,49 +95,29 @@ if (!globalThis[signalKey]) {
|
|
|
50
95
|
globalThis[signalKey] = true;
|
|
51
96
|
process.on("exit", workerQueue.shutdown);
|
|
52
97
|
}
|
|
53
|
-
function extractResourceUrls(nodes) {
|
|
54
|
-
return Promise.resolve(extractTakumiResourceUrls(nodes));
|
|
55
|
-
}
|
|
56
98
|
class RendererWorkerProxy {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
pendingFonts = [];
|
|
60
|
-
syncedGeneration = -1;
|
|
61
|
-
loadFont(font) {
|
|
62
|
-
const key = `${font.name}|${font.weight || 400}|${font.style || "normal"}`;
|
|
63
|
-
if (this.allFontKeys.has(key))
|
|
64
|
-
return;
|
|
65
|
-
this.allFontKeys.add(key);
|
|
66
|
-
this.allFonts.push(font);
|
|
67
|
-
this.pendingFonts.push(font);
|
|
99
|
+
async prepare(nodes, options) {
|
|
100
|
+
return { nodes, options };
|
|
68
101
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
fontsToSend = this.pendingFonts.splice(0);
|
|
80
|
-
}
|
|
81
|
-
this.syncedGeneration = generation;
|
|
82
|
-
return { id, type: "render", newFonts: fontsToSend, nodes, options };
|
|
83
|
-
}).then((result) => {
|
|
84
|
-
if (result._tag === "failure")
|
|
85
|
-
throw new Error(result.error);
|
|
86
|
-
if (result.fontWarnings?.length) {
|
|
87
|
-
for (const w of result.fontWarnings)
|
|
88
|
-
console.warn(`[nuxt-og-image] Failed to load font "${w.name}" (weight: ${w.weight}) into takumi renderer: ${w.error}`);
|
|
102
|
+
async render({ nodes, options }, { format }) {
|
|
103
|
+
const { fonts, images, signal: _signal, ...layout } = options;
|
|
104
|
+
let jobId;
|
|
105
|
+
const result = await workerQueue.enqueue(({ id }) => {
|
|
106
|
+
jobId = id;
|
|
107
|
+
activeJobs.set(id, options);
|
|
108
|
+
return { id, nodes, layout, fonts: fonts.map(({ data, ...font }) => font), images: images.sources, format };
|
|
109
|
+
}).finally(() => {
|
|
110
|
+
if (jobId !== void 0) {
|
|
111
|
+
activeJobs.delete(jobId);
|
|
89
112
|
}
|
|
90
|
-
return Buffer.from(result.image);
|
|
91
113
|
});
|
|
114
|
+
if (result._tag === "failure") {
|
|
115
|
+
throw new Error(result.error);
|
|
116
|
+
}
|
|
117
|
+
return Buffer.from(result.image);
|
|
92
118
|
}
|
|
93
119
|
}
|
|
94
120
|
export default {
|
|
95
121
|
initWasmPromise: Promise.resolve(),
|
|
96
|
-
Renderer: RendererWorkerProxy
|
|
97
|
-
extractResourceUrls
|
|
122
|
+
Renderer: RendererWorkerProxy
|
|
98
123
|
};
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { Renderer } from '@takumi-rs/core';
|
|
2
|
-
import { extractResourceUrls } from './resource-urls.js';
|
|
3
2
|
declare const _default: {
|
|
4
3
|
initWasmPromise: Promise<void>;
|
|
5
4
|
Renderer: typeof Renderer;
|
|
6
|
-
extractResourceUrls: typeof extractResourceUrls;
|
|
7
5
|
};
|
|
8
6
|
export default _default;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { Renderer } from '@takumi-rs/wasm/no-bundler';
|
|
2
|
-
import { extractResourceUrls } from './resource-urls.js';
|
|
3
2
|
declare const _default: {
|
|
4
3
|
initWasmPromise: Promise<import("@takumi-rs/wasm").InitOutput>;
|
|
5
4
|
Renderer: typeof Renderer;
|
|
6
|
-
extractResourceUrls: typeof extractResourceUrls;
|
|
7
5
|
};
|
|
8
6
|
export default _default;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import init, { Renderer } from "@takumi-rs/wasm/no-bundler";
|
|
2
|
-
import { extractResourceUrls } from "./resource-urls.js";
|
|
3
2
|
const wasmBinary = import("@takumi-rs/wasm/takumi_wasm_bg.wasm?module").then((m) => m.default || m);
|
|
4
3
|
export default {
|
|
5
4
|
initWasmPromise: wasmBinary.then((wasm) => init({ module_or_path: wasm })),
|
|
6
|
-
Renderer
|
|
7
|
-
extractResourceUrls
|
|
5
|
+
Renderer
|
|
8
6
|
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface DevFontState {
|
|
2
|
+
fonts: unknown[];
|
|
3
|
+
available: unknown[];
|
|
4
|
+
resolvedFontPaths: Record<string, string>;
|
|
5
|
+
requirements: unknown;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Development only. Components are analysed lazily, so the font list can change after Nitro is built;
|
|
9
|
+
* the module rewrites this file instead of rebuilding Nitro. Parsed again only when the file changes.
|
|
10
|
+
*/
|
|
11
|
+
export declare function readDevFontState(path: string): DevFontState | undefined;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2
|
+
let cachedPath;
|
|
3
|
+
let cachedVersion;
|
|
4
|
+
let cached;
|
|
5
|
+
export function readDevFontState(path) {
|
|
6
|
+
try {
|
|
7
|
+
const { mtimeMs, size } = statSync(path);
|
|
8
|
+
const version = `${mtimeMs}:${size}`;
|
|
9
|
+
if (path !== cachedPath || version !== cachedVersion) {
|
|
10
|
+
cached = JSON.parse(readFileSync(path, "utf-8"));
|
|
11
|
+
cachedPath = path;
|
|
12
|
+
cachedVersion = version;
|
|
13
|
+
}
|
|
14
|
+
return cached;
|
|
15
|
+
} catch {
|
|
16
|
+
return void 0;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { parse } from "devalue";
|
|
2
2
|
import { createError } from "#nuxtseo/h3";
|
|
3
|
+
import { fetchRawWithEvent } from "#nuxtseo/nitro";
|
|
3
4
|
import { htmlPayloadCache } from "#og-image-cache";
|
|
4
5
|
import { extractSocialPreviewTags } from "../../pure.js";
|
|
5
6
|
import { logger } from "../util/logger.js";
|
|
@@ -42,7 +43,7 @@ export async function fetchPathHtmlAndExtractOptions(e, path, key) {
|
|
|
42
43
|
const cachedHtmlPayload = await htmlPayloadCache.getItem(cacheKey);
|
|
43
44
|
if (!import.meta.dev && cachedHtmlPayload && cachedHtmlPayload.expiresAt > Date.now())
|
|
44
45
|
return cachedHtmlPayload.value;
|
|
45
|
-
let [html, err] = await doFetchWithErrorHandling(
|
|
46
|
+
let [html, err] = await doFetchWithErrorHandling(fetchRawWithEvent.bind(null, e), path);
|
|
46
47
|
if (err) {
|
|
47
48
|
logger.warn(err);
|
|
48
49
|
}
|
|
@@ -32,6 +32,15 @@ export declare function loadAllFontsDebug(component?: string): {
|
|
|
32
32
|
resolvedFontFamilies: string[];
|
|
33
33
|
};
|
|
34
34
|
export declare function loadAllFonts(event: H3Event, options: LoadFontsOptions): Promise<RuntimeFontConfig[]>;
|
|
35
|
+
export interface LazyRuntimeFontConfig extends Omit<RuntimeFontConfig, 'data'> {
|
|
36
|
+
/** Reads the font bytes on demand. Rejects when the file cannot be loaded. */
|
|
37
|
+
data: () => Promise<Uint8Array>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Select fonts like `loadAllFonts`, but read each file only when the renderer asks for it.
|
|
41
|
+
* Takumi picks unicode-range subsets from the rendered text, so unused CJK subsets never load.
|
|
42
|
+
*/
|
|
43
|
+
export declare function selectLazyFonts(event: H3Event, options: LoadFontsOptions): LazyRuntimeFontConfig[];
|
|
35
44
|
/**
|
|
36
45
|
* Load additional fonts specified via defineOgImage({ fonts: [...] }).
|
|
37
46
|
* Only object format ({ name, weight, path }) with a path is supported.
|
|
@@ -48,12 +57,6 @@ export interface LoadFontsForRendererOptions extends LoadFontsOptions {
|
|
|
48
57
|
/**
|
|
49
58
|
* Shared font loading sequence used by both renderers.
|
|
50
59
|
* Loads base fonts via loadAllFonts, then appends any custom font definitions.
|
|
51
|
-
*
|
|
52
|
-
* When a font family has multiple unicode-range subsets loaded (e.g., CJK fonts
|
|
53
|
-
* split into many small chunks), renames each subset to a unique family name
|
|
54
|
-
* (e.g., "Noto Sans SC__<stable hash per binary>") so renderers use font-family
|
|
55
|
-
* fallback chains for per-character glyph coverage. Without this, both Satori
|
|
56
|
-
* and Takumi pick the first font file and show .notdef for characters in other subsets.
|
|
57
60
|
*/
|
|
58
61
|
export declare function loadFontsForRenderer(event: OgImageRenderEventContext, options: LoadFontsForRendererOptions): Promise<RuntimeFontConfig[]>;
|
|
59
62
|
/** Get the default font family override from options or first resolved font */
|