@ethercorps/sveltekit-og 4.3.0-next.12 → 4.3.0-next.14
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/fonts.js +4 -2
- package/dist/helpers/create.d.ts +3 -0
- package/dist/helpers/create.js +15 -6
- package/dist/helpers/defaults.js +10 -1
- package/dist/helpers/emoji.d.ts +1 -1
- package/dist/helpers/emoji.js +1 -6
- package/dist/helpers/response.d.ts +10 -0
- package/dist/helpers/response.js +16 -3
- package/dist/helpers/toJSX.d.ts +1 -1
- package/dist/helpers/toJSX.js +5 -8
- package/dist/helpers/utils.js +1 -1
- package/dist/image-response.js +5 -6
- package/dist/providers/instances.js +38 -37
- package/dist/takumi/image-response.js +6 -12
- package/dist/takumi/render.js +3 -10
- package/dist/takumi/renderer.d.ts +3 -2
- package/dist/takumi/renderer.js +7 -3
- package/dist/types.d.ts +0 -4
- package/package.json +1 -1
package/dist/fonts.js
CHANGED
|
@@ -50,8 +50,10 @@ const constructGoogleFontCssUrl = (family, { text, weight = 400, style = "normal
|
|
|
50
50
|
};
|
|
51
51
|
if (text)
|
|
52
52
|
params.text = encodeURIComponent(text);
|
|
53
|
-
|
|
54
|
-
.
|
|
53
|
+
if (display)
|
|
54
|
+
params.display = display;
|
|
55
|
+
return `https://fonts.googleapis.com/css2?${Object.entries(params)
|
|
56
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
55
57
|
.join("&")}`;
|
|
56
58
|
};
|
|
57
59
|
/** Loads Google font ArrayBuffer with caching. */
|
package/dist/helpers/create.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import type { Component } from "svelte";
|
|
2
2
|
import type { ComponentOptions, ImageOptions } from "../types.js";
|
|
3
|
+
/** Single entry for the Satori and ReSVG engine */
|
|
4
|
+
export declare function createImage(element: string | Component, imageOptions: ImageOptions, componentOptions?: ComponentOptions): Promise<Uint8Array | string>;
|
|
5
|
+
/** Create an SVG string from a Svelte component or HTML string using Satori */
|
|
3
6
|
export declare function createSvg(element: string | Component, imageOptions: ImageOptions, componentOptions?: ComponentOptions): Promise<string>;
|
|
4
7
|
export declare function createPng(element: string | Component, imageOptions: ImageOptions, componentOptions?: ComponentOptions): Promise<Uint8Array<ArrayBufferLike>>;
|
package/dist/helpers/create.js
CHANGED
|
@@ -3,15 +3,23 @@ import { default_fonts, DEFAULT_WIDTH } from "../helpers/defaults.js";
|
|
|
3
3
|
import { useResvg, useSatori } from "../providers/instances.js";
|
|
4
4
|
import { createVNode } from "./toJSX.js";
|
|
5
5
|
import { createLogger } from "./logger.js";
|
|
6
|
-
import {
|
|
6
|
+
import { handleAsync, ErrorCodes } from "./error-handler.js";
|
|
7
|
+
/** Single entry for the Satori and ReSVG engine */
|
|
8
|
+
export function createImage(element, imageOptions, componentOptions) {
|
|
9
|
+
return imageOptions.format === "svg"
|
|
10
|
+
? createSvg(element, imageOptions, componentOptions)
|
|
11
|
+
: createPng(element, imageOptions, componentOptions);
|
|
12
|
+
}
|
|
13
|
+
/** Create an SVG string from a Svelte component or HTML string using Satori */
|
|
7
14
|
export async function createSvg(element, imageOptions, componentOptions) {
|
|
8
15
|
const log = createLogger(imageOptions.debug ?? false);
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
16
|
+
const vnodes = createVNode(element, componentOptions);
|
|
17
|
+
const satori = await useSatori(imageOptions.debug);
|
|
18
|
+
const satoriOptions = { ...imageOptions };
|
|
19
|
+
if (!satoriOptions.fonts) {
|
|
20
|
+
satoriOptions.fonts = await handleAsync(() => default_fonts(), ErrorCodes.FONT_LOAD_FAILED, "Failed to load default fonts for Satori");
|
|
13
21
|
}
|
|
14
|
-
satoriOptions
|
|
22
|
+
satoriOptions.loadAdditionalAsset = loadDynamicAsset({
|
|
15
23
|
emoji: imageOptions.emoji,
|
|
16
24
|
});
|
|
17
25
|
log.debug("Generating SVG with Satori");
|
|
@@ -19,6 +27,7 @@ export async function createSvg(element, imageOptions, componentOptions) {
|
|
|
19
27
|
log.info("Options provided to satori:", imageOptions);
|
|
20
28
|
return handleAsync(() => satori(vnodes, satoriOptions), ErrorCodes.SATORI_RENDER_FAILED, "Failed to render SVG with Satori");
|
|
21
29
|
}
|
|
30
|
+
/* Create a PNG image from a Svelte component or HTML string using Satori and ReSVG */
|
|
22
31
|
export async function createPng(element, imageOptions, componentOptions) {
|
|
23
32
|
const log = createLogger(imageOptions.debug ?? false);
|
|
24
33
|
const svg = await handleAsync(() => createSvg(element, imageOptions, componentOptions), ErrorCodes.SATORI_RENDER_FAILED, "Failed to create SVG for PNG rendering");
|
package/dist/helpers/defaults.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { handleAsyncAll, validateResponse, ErrorCodes } from "./error-handler.js";
|
|
2
|
-
|
|
2
|
+
// fetched once per process; a rejected fetch clears the cache so the next request retries
|
|
3
|
+
let defaultFontsPromise;
|
|
4
|
+
export function default_fonts() {
|
|
5
|
+
defaultFontsPromise ??= loadDefaultFonts().catch((error) => {
|
|
6
|
+
defaultFontsPromise = undefined;
|
|
7
|
+
throw error;
|
|
8
|
+
});
|
|
9
|
+
return defaultFontsPromise;
|
|
10
|
+
}
|
|
11
|
+
async function loadDefaultFonts() {
|
|
3
12
|
const [noto_sans_regular_font_resp, noto_sans_bold_font_reps] = await handleAsyncAll([
|
|
4
13
|
() => fetch("https://cdn-sveltekit-og.ethercorps.io/NotoSans-Regular.ttf"),
|
|
5
14
|
() => fetch("https://cdn-sveltekit-og.ethercorps.io/NotoSans-Bold.ttf"),
|
package/dist/helpers/emoji.d.ts
CHANGED
|
@@ -8,6 +8,6 @@ declare const emoji_apis: {
|
|
|
8
8
|
};
|
|
9
9
|
export declare const loadDynamicAsset: ({ emoji }: {
|
|
10
10
|
emoji: EmojiType;
|
|
11
|
-
}) => (
|
|
11
|
+
}) => (code: string, text: string) => Promise<string | undefined>;
|
|
12
12
|
export type EmojiType = keyof typeof emoji_apis;
|
|
13
13
|
export {};
|
package/dist/helpers/emoji.js
CHANGED
|
@@ -51,7 +51,7 @@ async function loadEmoji(code, type) {
|
|
|
51
51
|
}, ErrorCodes.EMOJI_LOAD_FAILED, `Failed to load emoji for code: ${code}`);
|
|
52
52
|
}
|
|
53
53
|
export const loadDynamicAsset = ({ emoji }) => {
|
|
54
|
-
|
|
54
|
+
return async (code, text) => {
|
|
55
55
|
if (code === "emoji") {
|
|
56
56
|
return handleAsync(async () => {
|
|
57
57
|
const iconCode = getIconCode(text);
|
|
@@ -62,9 +62,4 @@ export const loadDynamicAsset = ({ emoji }) => {
|
|
|
62
62
|
}, ErrorCodes.EMOJI_LOAD_FAILED, `Failed to process emoji: ${text}`);
|
|
63
63
|
}
|
|
64
64
|
};
|
|
65
|
-
return async (...args) => {
|
|
66
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
67
|
-
// @ts-ignore
|
|
68
|
-
return await fn(...args);
|
|
69
|
-
};
|
|
70
65
|
};
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/** format → content type for every format either engine can emit; satori uses the
|
|
2
|
+
* png/svg subset, takumi the whole map. */
|
|
3
|
+
export declare const CONTENT_TYPES: {
|
|
4
|
+
readonly png: "image/png";
|
|
5
|
+
readonly jpeg: "image/jpeg";
|
|
6
|
+
readonly webp: "image/webp";
|
|
7
|
+
readonly ico: "image/x-icon";
|
|
8
|
+
readonly raw: "application/octet-stream";
|
|
9
|
+
readonly svg: "image/svg+xml";
|
|
10
|
+
};
|
|
1
11
|
type ImageProducer = () => Promise<Uint8Array | string>;
|
|
2
12
|
interface BuildOptions {
|
|
3
13
|
/** uppercased format, used in logs e.g. "PNG" */
|
package/dist/helpers/response.js
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
import { createLogger } from "./logger.js";
|
|
2
2
|
import { handleAsync, toImageResponseError, ErrorCodes } from "./error-handler.js";
|
|
3
|
+
import { DEFAULT_STATUS_CODE, DEFAULT_STATUS_TEXT } from "./defaults.js";
|
|
3
4
|
import { formatBytes } from "./utils.js";
|
|
5
|
+
/** format → content type for every format either engine can emit; satori uses the
|
|
6
|
+
* png/svg subset, takumi the whole map. */
|
|
7
|
+
export const CONTENT_TYPES = {
|
|
8
|
+
png: "image/png",
|
|
9
|
+
jpeg: "image/jpeg",
|
|
10
|
+
webp: "image/webp",
|
|
11
|
+
ico: "image/x-icon",
|
|
12
|
+
raw: "application/octet-stream",
|
|
13
|
+
svg: "image/svg+xml",
|
|
14
|
+
};
|
|
15
|
+
// TextEncoder is stateless; one shared instance instead of one per svg response
|
|
16
|
+
const textEncoder = new TextEncoder();
|
|
4
17
|
/**
|
|
5
18
|
* Shared body + response init for both engines' ImageResponse. Streams the image
|
|
6
19
|
* out as bytes (a Response stream can't take a raw string), with consistent
|
|
@@ -12,7 +25,7 @@ export function buildImageResponse(produce, opts) {
|
|
|
12
25
|
async start(controller) {
|
|
13
26
|
try {
|
|
14
27
|
const out = await handleAsync(produce, ErrorCodes.UNKNOWN_ERROR, `Failed to generate ${opts.label}`);
|
|
15
|
-
const bytes = typeof out === "string" ?
|
|
28
|
+
const bytes = typeof out === "string" ? textEncoder.encode(out) : out;
|
|
16
29
|
log.info(`Generated ${opts.label}: ${formatBytes(bytes.byteLength)}`);
|
|
17
30
|
controller.enqueue(bytes);
|
|
18
31
|
controller.close();
|
|
@@ -32,8 +45,8 @@ export function buildImageResponse(produce, opts) {
|
|
|
32
45
|
: "public, immutable, no-transform, max-age=31536000",
|
|
33
46
|
...opts.headers,
|
|
34
47
|
},
|
|
35
|
-
status: opts.status ||
|
|
36
|
-
statusText: opts.statusText ||
|
|
48
|
+
status: opts.status || DEFAULT_STATUS_CODE,
|
|
49
|
+
statusText: opts.statusText || DEFAULT_STATUS_TEXT,
|
|
37
50
|
};
|
|
38
51
|
return { body, init };
|
|
39
52
|
}
|
package/dist/helpers/toJSX.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { Component } from "svelte";
|
|
2
2
|
import type { ComponentOptions, VNode } from "../types.js";
|
|
3
|
-
export declare function createVNode(element: string | Component,
|
|
3
|
+
export declare function createVNode(element: string | Component, componentProps?: ComponentOptions): VNode;
|
package/dist/helpers/toJSX.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import { html } from "satori-html";
|
|
2
2
|
import { handleSync, ErrorCodes } from "./error-handler.js";
|
|
3
3
|
import { renderComponentToHtml } from "./to-html.js";
|
|
4
|
-
function
|
|
4
|
+
export function createVNode(element, componentProps) {
|
|
5
5
|
return handleSync(() => {
|
|
6
|
-
|
|
6
|
+
if (typeof element === "string")
|
|
7
|
+
return html(element.replaceAll("\n", "").trim());
|
|
8
|
+
const { body, head } = renderComponentToHtml(element, componentProps?.props);
|
|
7
9
|
return html(body + head);
|
|
8
|
-
}, ErrorCodes.VNODE_CREATION_FAILED, "Failed to
|
|
9
|
-
}
|
|
10
|
-
export function createVNode(element, componentOptions) {
|
|
11
|
-
return handleSync(() => typeof element === "string"
|
|
12
|
-
? html(element.replaceAll("\n", "").trim())
|
|
13
|
-
: svelteComponentToHTML(element, componentOptions?.props), ErrorCodes.VNODE_CREATION_FAILED, "Failed to create VNode");
|
|
10
|
+
}, ErrorCodes.VNODE_CREATION_FAILED, "Failed to create VNode");
|
|
14
11
|
}
|
package/dist/helpers/utils.js
CHANGED
|
@@ -4,7 +4,7 @@ export const formatBytes = (bytes, decimals = 2) => {
|
|
|
4
4
|
if (bytes === 0)
|
|
5
5
|
return "0 Bytes";
|
|
6
6
|
const decimalPoint = decimals < 0 ? 0 : decimals;
|
|
7
|
-
const sizeIndex = Math.min(Math.floor(Math.log(bytes) / Math.log(kbSize)),
|
|
7
|
+
const sizeIndex = Math.min(Math.floor(Math.log(bytes) / Math.log(kbSize)), sizeFormats.length - 1);
|
|
8
8
|
return (parseFloat((bytes / Math.pow(kbSize, sizeIndex)).toFixed(decimalPoint)) +
|
|
9
9
|
" " +
|
|
10
10
|
sizeFormats[sizeIndex]);
|
package/dist/image-response.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { DEFAULT_OPTIONS } from "./helpers/defaults.js";
|
|
2
|
-
import {
|
|
3
|
-
import { buildImageResponse } from "./helpers/response.js";
|
|
2
|
+
import { createImage } from "./helpers/create.js";
|
|
3
|
+
import { buildImageResponse, CONTENT_TYPES } from "./helpers/response.js";
|
|
4
4
|
export class ImageResponse extends Response {
|
|
5
5
|
constructor(element, options, props) {
|
|
6
|
-
const opts =
|
|
6
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
7
7
|
const format = opts.format ?? "png";
|
|
8
|
-
const
|
|
9
|
-
const { body, init } = buildImageResponse(() => createImage(element, opts, { props }), {
|
|
8
|
+
const { body, init } = buildImageResponse(() => createImage(element, { ...opts, format }, { props }), {
|
|
10
9
|
label: format.toUpperCase(),
|
|
11
|
-
contentType:
|
|
10
|
+
contentType: CONTENT_TYPES[format],
|
|
12
11
|
debug: opts.debug ?? false,
|
|
13
12
|
headers: opts.headers,
|
|
14
13
|
status: opts.status,
|
|
@@ -1,44 +1,45 @@
|
|
|
1
1
|
import { isEdgeLight, isWorkerd } from "std-env";
|
|
2
2
|
import { createLogger } from "../helpers/logger.js";
|
|
3
3
|
import { handleAsync, ErrorCodes } from "../helpers/error-handler.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Lazy singleton around a node/edge wasm module pair. The edge/node choice is
|
|
6
|
+
* made per-runtime via std-env; both sides stay direct import() expressions so
|
|
7
|
+
* the bundler can emit the wasm chunk. Memoizes the init promise (like
|
|
8
|
+
* takumi/renderer.ts) so concurrent first requests share one init, and clears
|
|
9
|
+
* it on rejection so a failed init can be retried.
|
|
10
|
+
*/
|
|
11
|
+
function createProvider(label, errorCode, imports) {
|
|
12
|
+
let instancePromise;
|
|
13
|
+
const init = async (debug) => {
|
|
14
|
+
const log = createLogger(debug);
|
|
15
|
+
log.debug(`Initializing ${label}`);
|
|
16
|
+
const isWorkerLikeRuntime = isEdgeLight || isWorkerd;
|
|
17
|
+
log.info(`Detected runtime: ${isWorkerLikeRuntime ? "Edge Light or Workerd" : "Node.js"}`);
|
|
18
|
+
const moduleImport = isWorkerLikeRuntime ? imports.edge() : imports.node();
|
|
19
|
+
const loaded = await handleAsync(() => moduleImport.then((m) => m.default), errorCode, `Failed to import ${label} module`);
|
|
20
|
+
await handleAsync(() => loaded.initWasmPromise, errorCode, `Failed to initialize ${label} WASM`);
|
|
21
|
+
return loaded;
|
|
22
|
+
};
|
|
23
|
+
return (debug = false) => {
|
|
24
|
+
instancePromise ??= init(debug).catch((error) => {
|
|
25
|
+
instancePromise = undefined;
|
|
26
|
+
throw error;
|
|
27
|
+
});
|
|
28
|
+
return instancePromise;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const useResvgModule = createProvider("ReSVG", ErrorCodes.RESVG_INIT_FAILED, {
|
|
32
|
+
// workers take a precompiled `?module`; node takes raw wasm bytes
|
|
33
|
+
edge: () => import("./resvg/edge.js"),
|
|
34
|
+
node: () => import("./resvg/node.js"),
|
|
35
|
+
});
|
|
36
|
+
const useSatoriModule = createProvider("Satori", ErrorCodes.SATORI_INIT_FAILED, {
|
|
37
|
+
edge: () => import("./satori/edge.js"),
|
|
38
|
+
node: () => import("./satori/node.js"),
|
|
39
|
+
});
|
|
13
40
|
export async function useResvg(debug = false) {
|
|
14
|
-
|
|
15
|
-
if (resvgInstance.instance) {
|
|
16
|
-
return resvgInstance.instance.Resvg;
|
|
17
|
-
}
|
|
18
|
-
log.debug("Initializing ReSVG WASM");
|
|
19
|
-
const isWorkerLikeRuntime = isEdgeLight || isWorkerd;
|
|
20
|
-
log.info(`Detected runtime: ${isWorkerLikeRuntime ? "Edge Light or Workerd" : "Node.js"}`);
|
|
21
|
-
// workers take a precompiled `?module`; node takes raw wasm bytes. keep both
|
|
22
|
-
// as direct import() expressions so the bundler can emit the wasm chunk.
|
|
23
|
-
const moduleImport = isWorkerLikeRuntime
|
|
24
|
-
? import("./resvg/edge.js")
|
|
25
|
-
: import("./resvg/node.js");
|
|
26
|
-
resvgInstance.instance = await handleAsync(() => moduleImport.then((m) => m.default), ErrorCodes.RESVG_INIT_FAILED, "Failed to import ReSVG module");
|
|
27
|
-
await handleAsync(() => resvgInstance.instance.initWasmPromise, ErrorCodes.RESVG_INIT_FAILED, "Failed to initialize ReSVG WASM");
|
|
28
|
-
return resvgInstance.instance.Resvg;
|
|
41
|
+
return (await useResvgModule(debug)).Resvg;
|
|
29
42
|
}
|
|
30
43
|
export async function useSatori(debug = false) {
|
|
31
|
-
|
|
32
|
-
if (satoriInstance.instance) {
|
|
33
|
-
return satoriInstance.instance.satori;
|
|
34
|
-
}
|
|
35
|
-
log.debug("Initializing Satori");
|
|
36
|
-
const isWorkerLikeRuntime = isEdgeLight || isWorkerd;
|
|
37
|
-
log.info(`Detected runtime: ${isWorkerLikeRuntime ? "Edge Light or Workerd" : "Node.js"}`);
|
|
38
|
-
const moduleImport = isWorkerLikeRuntime
|
|
39
|
-
? import("./satori/edge.js")
|
|
40
|
-
: import("./satori/node.js");
|
|
41
|
-
satoriInstance.instance = await handleAsync(() => moduleImport.then((m) => m.default), ErrorCodes.SATORI_INIT_FAILED, "Failed to import Satori module");
|
|
42
|
-
await handleAsync(() => satoriInstance.instance.initWasmPromise, ErrorCodes.SATORI_INIT_FAILED, "Failed to initialize Satori WASM");
|
|
43
|
-
return satoriInstance.instance.satori;
|
|
44
|
+
return (await useSatoriModule(debug)).satori;
|
|
44
45
|
}
|
|
@@ -1,21 +1,15 @@
|
|
|
1
1
|
import { createTakumiImage } from "./render.js";
|
|
2
|
-
import {
|
|
2
|
+
import { DEFAULT_WIDTH, DEFAULT_HEIGHT } from "../helpers/defaults.js";
|
|
3
|
+
import { buildImageResponse, CONTENT_TYPES as SHARED_CONTENT_TYPES } from "../helpers/response.js";
|
|
3
4
|
const DEFAULT_OPTIONS = {
|
|
4
|
-
width:
|
|
5
|
-
height:
|
|
5
|
+
width: DEFAULT_WIDTH,
|
|
6
|
+
height: DEFAULT_HEIGHT,
|
|
6
7
|
format: "png",
|
|
7
8
|
emoji: "twemoji",
|
|
8
9
|
debug: false,
|
|
9
10
|
};
|
|
10
|
-
//
|
|
11
|
-
const CONTENT_TYPES =
|
|
12
|
-
png: "image/png",
|
|
13
|
-
jpeg: "image/jpeg",
|
|
14
|
-
webp: "image/webp",
|
|
15
|
-
ico: "image/x-icon",
|
|
16
|
-
raw: "application/octet-stream",
|
|
17
|
-
svg: "image/svg+xml",
|
|
18
|
-
};
|
|
11
|
+
// Verifies the shared map covers every takumi format
|
|
12
|
+
const CONTENT_TYPES = SHARED_CONTENT_TYPES;
|
|
19
13
|
/** OG image rendered by Takumi. Takes an HTML string or a Svelte component. */
|
|
20
14
|
export class ImageResponse extends Response {
|
|
21
15
|
constructor(element, options, props) {
|
package/dist/takumi/render.js
CHANGED
|
@@ -4,19 +4,12 @@ import { useTakumiRenderer, registerTakumiFonts } from "./renderer.js";
|
|
|
4
4
|
import { resolveTakumiFonts } from "./fonts.js";
|
|
5
5
|
import { createLogger } from "../helpers/logger.js";
|
|
6
6
|
import { handleAsync, handleSync, ErrorCodes } from "../helpers/error-handler.js";
|
|
7
|
-
import {
|
|
8
|
-
function elementToHtml(element, props) {
|
|
9
|
-
if (typeof element === "string")
|
|
10
|
-
return html(element.replaceAll("\n", "").trim());
|
|
11
|
-
// head carries css injected via <svelte:options css="injected" />, so it goes first
|
|
12
|
-
const { head, body } = renderComponentToHtml(element, props);
|
|
13
|
-
return html(head + body);
|
|
14
|
-
}
|
|
7
|
+
import { createVNode } from "../helpers/toJSX.js";
|
|
15
8
|
/** Render an HTML string or Svelte component to image bytes (or an svg string). */
|
|
16
9
|
export async function createTakumiImage(element, options, props) {
|
|
17
10
|
const log = createLogger(options.debug ?? false);
|
|
18
|
-
const vNode = handleSync(() =>
|
|
19
|
-
const renderer = await useTakumiRenderer(
|
|
11
|
+
const vNode = handleSync(() => createVNode(element, props), ErrorCodes.VNODE_CREATION_FAILED, "Failed to create HTML for Takumi");
|
|
12
|
+
const renderer = await useTakumiRenderer();
|
|
20
13
|
if (options.fonts?.length) {
|
|
21
14
|
const fonts = await resolveTakumiFonts(options.fonts);
|
|
22
15
|
await registerTakumiFonts(renderer, fonts);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Renderer as NodeRenderer, FontDetails } from "takumi-js/node";
|
|
2
2
|
export type TakumiRenderer = NodeRenderer;
|
|
3
|
-
/** Lazily creates and caches the Takumi renderer for the current runtime.
|
|
4
|
-
|
|
3
|
+
/** Lazily creates and caches the Takumi renderer for the current runtime.
|
|
4
|
+
* A rejected init clears the cache so the next request retries. */
|
|
5
|
+
export declare function useTakumiRenderer(): Promise<TakumiRenderer>;
|
|
5
6
|
/** Register each font once, keyed by name/weight/style. */
|
|
6
7
|
export declare function registerTakumiFonts(renderer: TakumiRenderer, fonts: FontDetails[]): Promise<void>;
|
package/dist/takumi/renderer.js
CHANGED
|
@@ -18,9 +18,13 @@ async function initRenderer() {
|
|
|
18
18
|
}, ErrorCodes.TAKUMI_INIT_FAILED, "Failed to initialize Takumi WASM");
|
|
19
19
|
return new Renderer();
|
|
20
20
|
}
|
|
21
|
-
/** Lazily creates and caches the Takumi renderer for the current runtime.
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
/** Lazily creates and caches the Takumi renderer for the current runtime.
|
|
22
|
+
* A rejected init clears the cache so the next request retries. */
|
|
23
|
+
export async function useTakumiRenderer() {
|
|
24
|
+
rendererPromise ??= initRenderer().catch((error) => {
|
|
25
|
+
rendererPromise = undefined;
|
|
26
|
+
throw error;
|
|
27
|
+
});
|
|
24
28
|
return rendererPromise;
|
|
25
29
|
}
|
|
26
30
|
/** Register each font once, keyed by name/weight/style. */
|
package/dist/types.d.ts
CHANGED
|
@@ -85,7 +85,3 @@ export interface VNode {
|
|
|
85
85
|
}
|
|
86
86
|
/** utils types */
|
|
87
87
|
export type MayBePromise<T> = T | Promise<T>;
|
|
88
|
-
export type OnlyProps<T, P> = {
|
|
89
|
-
[K in keyof T as K extends P ? K : never]: T[K];
|
|
90
|
-
};
|
|
91
|
-
export type StringWithSuggestions<S extends string> = (string & Record<never, never>) | S;
|