@ethercorps/sveltekit-og 4.2.0 → 4.3.0-beta.1

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.
Files changed (50) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +39 -193
  3. package/dist/fonts.d.ts +5 -13
  4. package/dist/fonts.js +28 -24
  5. package/dist/helpers/create.d.ts +7 -5
  6. package/dist/helpers/create.js +38 -35
  7. package/dist/helpers/defaults.d.ts +4 -4
  8. package/dist/helpers/defaults.js +28 -18
  9. package/dist/helpers/emoji.d.ts +1 -1
  10. package/dist/helpers/emoji.js +31 -21
  11. package/dist/helpers/error-handler.d.ts +37 -0
  12. package/dist/helpers/error-handler.js +75 -0
  13. package/dist/helpers/logger.d.ts +7 -0
  14. package/dist/helpers/logger.js +21 -0
  15. package/dist/helpers/response.d.ts +30 -0
  16. package/dist/helpers/response.js +52 -0
  17. package/dist/helpers/to-html.d.ts +10 -0
  18. package/dist/helpers/to-html.js +10 -0
  19. package/dist/helpers/toJSX.d.ts +3 -0
  20. package/dist/helpers/toJSX.js +11 -0
  21. package/dist/helpers/utils.d.ts +2 -0
  22. package/dist/helpers/utils.js +25 -0
  23. package/dist/image-response.d.ts +2 -2
  24. package/dist/image-response.js +13 -21
  25. package/dist/plugin.js +6 -6
  26. package/dist/providers/instances.d.ts +3 -3
  27. package/dist/providers/instances.js +43 -15
  28. package/dist/providers/resvg/edge.d.ts +1 -1
  29. package/dist/providers/resvg/edge.js +10 -6
  30. package/dist/providers/resvg/node.d.ts +1 -1
  31. package/dist/providers/resvg/node.js +18 -9
  32. package/dist/providers/satori/edge.d.ts +6 -0
  33. package/dist/providers/satori/edge.js +12 -0
  34. package/dist/providers/satori/node.d.ts +1 -1
  35. package/dist/providers/satori/node.js +4 -4
  36. package/dist/takumi/fonts.d.ts +16 -0
  37. package/dist/takumi/fonts.js +15 -0
  38. package/dist/takumi/image-response.d.ts +6 -0
  39. package/dist/takumi/image-response.js +27 -0
  40. package/dist/takumi/index.d.ts +5 -0
  41. package/dist/takumi/index.js +4 -0
  42. package/dist/takumi/render.d.ts +4 -0
  43. package/dist/takumi/render.js +23 -0
  44. package/dist/takumi/renderer.d.ts +7 -0
  45. package/dist/takumi/renderer.js +39 -0
  46. package/dist/takumi/types.d.ts +64 -0
  47. package/dist/takumi/types.js +1 -0
  48. package/dist/types.d.ts +10 -14
  49. package/package.json +73 -17
  50. package/dist/providers/resvg/resvg.wasm +0 -0
@@ -0,0 +1,16 @@
1
+ import type { FontDetails } from "takumi-js/node";
2
+ import { BaseFont } from "../fonts.js";
3
+ import type { MayBePromise } from "../types.js";
4
+ type ByteBuf = Uint8Array | ArrayBuffer | Buffer;
5
+ /** Takumi-native font descriptor; data is the bytes or a lazy loader, like takumi-js wants. */
6
+ export interface TakumiFontDescriptor {
7
+ name?: string;
8
+ data: ByteBuf | (() => MayBePromise<ByteBuf>);
9
+ weight?: number;
10
+ style?: FontDetails["style"];
11
+ }
12
+ /** What the takumi path accepts: our GoogleFont/CustomFont, or a raw takumi descriptor. */
13
+ export type TakumiFontInput = BaseFont | TakumiFontDescriptor;
14
+ /** Normalize mixed font inputs to FontDetails for registerFont, loaders run in parallel. */
15
+ export declare function resolveTakumiFonts(fonts: TakumiFontInput[]): Promise<FontDetails[]>;
16
+ export {};
@@ -0,0 +1,15 @@
1
+ import { BaseFont } from "../fonts.js";
2
+ import { handleAsync, ErrorCodes } from "../helpers/error-handler.js";
3
+ async function normalizeFont(font) {
4
+ if (font instanceof BaseFont) {
5
+ // our font classes lazily load + cache through the data getter
6
+ const data = (await font.data);
7
+ return { name: font.name, data, weight: font.weight, style: font.style };
8
+ }
9
+ const data = typeof font.data === "function" ? await font.data() : await font.data;
10
+ return { name: font.name, data, weight: font.weight, style: font.style };
11
+ }
12
+ /** Normalize mixed font inputs to FontDetails for registerFont, loaders run in parallel. */
13
+ export async function resolveTakumiFonts(fonts) {
14
+ return handleAsync(() => Promise.all(fonts.map(normalizeFont)), ErrorCodes.FONT_LOAD_FAILED, "Failed to resolve fonts for Takumi");
15
+ }
@@ -0,0 +1,6 @@
1
+ import type { Component, ComponentProps } from "svelte";
2
+ import type { TakumiImageResponseOptions } from "./types.js";
3
+ /** OG image rendered by Takumi. Takes an HTML string or a Svelte component. */
4
+ export declare class ImageResponse<T extends string | Component<any>> extends Response {
5
+ constructor(element: T, options?: TakumiImageResponseOptions, props?: T extends Component<any> ? ComponentProps<T> : never);
6
+ }
@@ -0,0 +1,27 @@
1
+ import { createTakumiImage } from "./render.js";
2
+ import { DEFAULT_WIDTH, DEFAULT_HEIGHT } from "../helpers/defaults.js";
3
+ import { buildImageResponse, CONTENT_TYPES as SHARED_CONTENT_TYPES } from "../helpers/response.js";
4
+ const DEFAULT_OPTIONS = {
5
+ width: DEFAULT_WIDTH,
6
+ height: DEFAULT_HEIGHT,
7
+ format: "png",
8
+ emoji: "twemoji",
9
+ debug: false,
10
+ };
11
+ // Verifies the shared map covers every takumi format
12
+ const CONTENT_TYPES = SHARED_CONTENT_TYPES;
13
+ /** OG image rendered by Takumi. Takes an HTML string or a Svelte component. */
14
+ export class ImageResponse extends Response {
15
+ constructor(element, options, props) {
16
+ const opts = { ...DEFAULT_OPTIONS, ...options };
17
+ const { body, init } = buildImageResponse(() => createTakumiImage(element, opts, props), {
18
+ label: opts.format.toUpperCase(),
19
+ contentType: CONTENT_TYPES[opts.format],
20
+ debug: opts.debug,
21
+ headers: opts.headers,
22
+ status: opts.status,
23
+ statusText: opts.statusText,
24
+ });
25
+ super(body, init);
26
+ }
27
+ }
@@ -0,0 +1,5 @@
1
+ export { ImageResponse } from "./image-response.js";
2
+ export type { TakumiImageResponseOptions, TakumiImageOptions, TakumiResponseOptions, TakumiFormat, } from "./types.js";
3
+ export type { TakumiFontInput, TakumiFontDescriptor } from "./fonts.js";
4
+ export { resolveTakumiFonts } from "./fonts.js";
5
+ export { GoogleFont, CustomFont, loadGoogleFont } from "../fonts.js";
@@ -0,0 +1,4 @@
1
+ export { ImageResponse } from "./image-response.js";
2
+ export { resolveTakumiFonts } from "./fonts.js";
3
+ // re-export the font helpers so the takumi path works from one import
4
+ export { GoogleFont, CustomFont, loadGoogleFont } from "../fonts.js";
@@ -0,0 +1,4 @@
1
+ import type { Component } from "svelte";
2
+ import type { TakumiImageOptions } from "./types.js";
3
+ /** Render an HTML string or Svelte component to image bytes (or an svg string). */
4
+ export declare function createTakumiImage(element: string | Component<any>, options: TakumiImageOptions, props?: Record<string, unknown>): Promise<Uint8Array | string>;
@@ -0,0 +1,23 @@
1
+ import { render as takumiRender, renderSvg } from "takumi-js";
2
+ import { useTakumiRenderer, registerTakumiFonts } from "./renderer.js";
3
+ import { resolveTakumiFonts } from "./fonts.js";
4
+ import { createLogger } from "../helpers/logger.js";
5
+ import { handleAsync, ErrorCodes, handleSync } from "../helpers/error-handler.js";
6
+ import { createVNode } from "../helpers/toJSX.js";
7
+ /** Render an HTML string or Svelte component to image bytes (or an svg string). */
8
+ export async function createTakumiImage(element, options, props) {
9
+ const log = createLogger(options.debug ?? false);
10
+ const vNode = handleSync(() => createVNode(element, { props }), ErrorCodes.VNODE_CREATION_FAILED, "Failed to create HTML for Takumi");
11
+ const renderer = await useTakumiRenderer();
12
+ if (options.fonts?.length) {
13
+ const fonts = await resolveTakumiFonts(options.fonts);
14
+ await registerTakumiFonts(renderer, fonts);
15
+ }
16
+ const { width, height, format = "png", quality, stylesheets, emoji } = options;
17
+ const shared = { renderer: renderer, width, height, stylesheets, emoji };
18
+ log.debug(`Rendering ${format.toUpperCase()} with Takumi`);
19
+ if (format === "svg") {
20
+ return handleAsync(() => renderSvg(vNode, shared), ErrorCodes.TAKUMI_RENDER_FAILED, "Failed to render SVG with Takumi");
21
+ }
22
+ return handleAsync(() => takumiRender(vNode, { ...shared, format, quality }), ErrorCodes.TAKUMI_RENDER_FAILED, "Failed to render image with Takumi");
23
+ }
@@ -0,0 +1,7 @@
1
+ import type { Renderer as NodeRenderer, FontDetails } from "takumi-js/node";
2
+ export type TakumiRenderer = NodeRenderer;
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>;
6
+ /** Register each font once, keyed by name/weight/style. */
7
+ export declare function registerTakumiFonts(renderer: TakumiRenderer, fonts: FontDetails[]): Promise<void>;
@@ -0,0 +1,39 @@
1
+ import autoModule, { init as initTakumiWasm, Renderer } from "takumi-js/wasm";
2
+ import { handleAsync, ErrorCodes } from "../helpers/error-handler.js";
3
+ // keep one renderer alive across requests, same as the satori/resvg instances.
4
+ // fonts registered on it stick around, so we dedupe by key.
5
+ let rendererPromise;
6
+ const registeredFontKeys = new Set();
7
+ async function initRenderer() {
8
+ await handleAsync(async () => {
9
+ // reuse the wasm that ships with takumi-js instead of vendoring our own.
10
+ // @takumi-rs/wasm/auto picks the right binary per runtime (workerd, edge,
11
+ // node, ?module) via export conditions. this is the same dance takumi-js
12
+ // does internally.
13
+ const resolved = typeof autoModule === "function" ? await autoModule() : await autoModule;
14
+ const input = resolved && typeof resolved === "object" && "default" in resolved
15
+ ? resolved.default
16
+ : resolved;
17
+ await initTakumiWasm(input ? { module_or_path: input } : undefined);
18
+ }, ErrorCodes.TAKUMI_INIT_FAILED, "Failed to initialize Takumi WASM");
19
+ return new Renderer();
20
+ }
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
+ });
28
+ return rendererPromise;
29
+ }
30
+ /** Register each font once, keyed by name/weight/style. */
31
+ export async function registerTakumiFonts(renderer, fonts) {
32
+ for (const font of fonts) {
33
+ const key = `${font.name ?? "unnamed"}-${font.weight ?? "auto"}-${font.style ?? "normal"}`;
34
+ if (registeredFontKeys.has(key))
35
+ continue;
36
+ await handleAsync(() => renderer.registerFont(font), ErrorCodes.FONT_LOAD_FAILED, `Failed to register Takumi font: ${key}`);
37
+ registeredFontKeys.add(key);
38
+ }
39
+ }
@@ -0,0 +1,64 @@
1
+ import type { EmojiType } from "takumi-js/helpers/emoji";
2
+ import type { OutputFormat } from "takumi-js/node";
3
+ import type { TakumiFontInput } from "./fonts.js";
4
+ /** Static output formats Takumi can encode, plus vector `svg`. */
5
+ export type TakumiFormat = OutputFormat | "svg";
6
+ export type TakumiImageOptions = {
7
+ /**
8
+ * Width of the image.
9
+ * @default 1200
10
+ * */
11
+ width?: number;
12
+ /**
13
+ * Height of the image.
14
+ * @default 630
15
+ * */
16
+ height?: number;
17
+ /**
18
+ * Output format.
19
+ * @default png
20
+ * */
21
+ format?: TakumiFormat;
22
+ /**
23
+ * Quality for lossy formats (jpeg, lossy webp), 0-100.
24
+ * */
25
+ quality?: number;
26
+ /**
27
+ * Fonts to register. Accepts this library's `GoogleFont`/`CustomFont`
28
+ * helpers or raw Takumi font descriptors. If omitted, Takumi's built-in
29
+ * sans-serif is used.
30
+ * */
31
+ fonts?: TakumiFontInput[];
32
+ /**
33
+ * Extra CSS stylesheets applied before rendering.
34
+ * */
35
+ stylesheets?: string[];
36
+ /**
37
+ * Emoji provider, or `"from-font"` to source emoji glyphs from loaded fonts.
38
+ * @default twemoji
39
+ * */
40
+ emoji?: EmojiType | "from-font";
41
+ /**
42
+ * Enable debug logging.
43
+ * @default false
44
+ * */
45
+ debug?: boolean;
46
+ };
47
+ export type TakumiResponseOptions = {
48
+ /**
49
+ * Response status code.
50
+ * @default 200
51
+ * */
52
+ status?: number;
53
+ /**
54
+ * Response status text.
55
+ * @default Success
56
+ * */
57
+ statusText?: string;
58
+ /**
59
+ * Response headers.
60
+ * */
61
+ headers?: Record<string, string>;
62
+ };
63
+ /** Options for the Takumi `ImageResponse`. */
64
+ export type TakumiImageResponseOptions = TakumiImageOptions & TakumiResponseOptions;
@@ -0,0 +1 @@
1
+ export {};
package/dist/types.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import type { SatoriOptions } from 'satori/wasm';
2
- import type { EmojiType } from './helpers/emoji.js';
3
- export type Font = SatoriOptions['fonts'][number];
1
+ import type { SatoriOptions } from "satori";
2
+ import type { EmojiType } from "./helpers/emoji.js";
3
+ export type Font = SatoriOptions["fonts"][number];
4
4
  export type Fonts = Font[];
5
- export type FontStyle = Font['style'];
6
- export type FontWeight = Font['weight'];
5
+ export type FontStyle = Font["style"];
6
+ export type FontWeight = Font["weight"];
7
7
  export type FinalFontOptions = NonNullable<Fonts>;
8
8
  export type ImageOptions = {
9
9
  /**
@@ -30,7 +30,7 @@ export type ImageOptions = {
30
30
  * Tailwind config
31
31
  * @default provided by satori
32
32
  * */
33
- tailwindConfig?: SatoriOptions['tailwindConfig'];
33
+ tailwindConfig?: SatoriOptions["tailwindConfig"];
34
34
  /**
35
35
  * Debug operations
36
36
  * @default false
@@ -40,7 +40,7 @@ export type ImageOptions = {
40
40
  * Image format
41
41
  * @default png
42
42
  * */
43
- format?: 'svg' | 'png';
43
+ format?: "svg" | "png";
44
44
  };
45
45
  export type ResponseImageOptions = {
46
46
  /**
@@ -70,7 +70,7 @@ export type ImageResponseOptions = ImageOptions & ResponseImageOptions;
70
70
  * Svelte Component props to render the component which dynamic content
71
71
  * */
72
72
  export type ComponentOptions = {
73
- props?: Record<string, any>;
73
+ props?: Record<string, unknown>;
74
74
  };
75
75
  /**
76
76
  * React virtual node, supported by satori as input (alternative to JSX input).
@@ -78,14 +78,10 @@ export type ComponentOptions = {
78
78
  export interface VNode {
79
79
  type: string;
80
80
  props: {
81
- style?: Record<string, any>;
81
+ style?: Record<string, unknown>;
82
82
  children?: string | VNode | VNode[];
83
- [prop: string]: any;
83
+ [prop: string]: unknown;
84
84
  };
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;
package/package.json CHANGED
@@ -1,6 +1,38 @@
1
1
  {
2
2
  "name": "@ethercorps/sveltekit-og",
3
- "version": "4.2.0",
3
+ "version": "4.3.0-beta.1",
4
+ "license": "MIT",
5
+ "homepage": "https://sveltekit-og.dev",
6
+ "repository": "github:ethercorps/sveltekit-og",
7
+ "funding": "https://github.com/sponsors/ethercorps",
8
+ "author": "Shivam Meena <https://github.com/theetherGit>",
9
+ "description": "Dynamically generate Open Graph images from an HTML, CSS template or Svelte component using fast and efficient conversion from HTML > SVG > PNG",
10
+ "contributors": [
11
+ {
12
+ "name": "Shivam Meena",
13
+ "github": "https://github.com/theetherGit"
14
+ },
15
+ {
16
+ "name": "Jason",
17
+ "github": "https://github.com/jasongitmail"
18
+ },
19
+ {
20
+ "name": "Mihkel Martin Kasterpalu",
21
+ "github": "https://github.com/MihkelMK"
22
+ },
23
+ {
24
+ "name": "Luke Parke",
25
+ "github": "https://github.com/LukasParke"
26
+ },
27
+ {
28
+ "name": "Willow (GHOST)",
29
+ "github": "https://github.com/ghostdevv"
30
+ },
31
+ {
32
+ "name": "Minseo Lee",
33
+ "github": "https://github.com/quiple"
34
+ }
35
+ ],
4
36
  "exports": {
5
37
  ".": {
6
38
  "types": "./dist/index.d.ts",
@@ -11,10 +43,16 @@
11
43
  "types": "./dist/fonts.d.ts",
12
44
  "import": "./dist/fonts.js"
13
45
  },
46
+ "./takumi": {
47
+ "types": "./dist/takumi/index.d.ts",
48
+ "svelte": "./dist/takumi/index.js",
49
+ "import": "./dist/takumi/index.js"
50
+ },
14
51
  "./plugin": {
15
52
  "types": "./dist/plugin.d.ts",
16
53
  "import": "./dist/plugin.js"
17
- }
54
+ },
55
+ "./package.json": "./package.json"
18
56
  },
19
57
  "files": [
20
58
  "dist",
@@ -22,26 +60,34 @@
22
60
  "!dist/**/*.spec.*"
23
61
  ],
24
62
  "devDependencies": {
25
- "@sveltejs/adapter-vercel": "^5.10.2",
26
- "@sveltejs/kit": "^2.48.5",
27
- "@sveltejs/package": "^2.5.4",
63
+ "@eslint/compat": "^2.0.3",
64
+ "@eslint/js": "^9.39.3",
65
+ "@sveltejs/adapter-auto": "^3.3.1",
66
+ "@sveltejs/kit": "^2.53.4",
67
+ "@sveltejs/package": "^2.5.7",
28
68
  "@sveltejs/vite-plugin-svelte": "^4.0.4",
29
- "@types/node": "^24.7.1",
69
+ "@types/node": "^24.11.0",
30
70
  "@typescript-eslint/eslint-plugin": "^5.62.0",
31
71
  "@typescript-eslint/parser": "^5.62.0",
32
72
  "css-tree": "^2.3.1",
33
73
  "eslint": "^8.57.1",
34
- "eslint-config-prettier": "^8.10.0",
74
+ "eslint-config-prettier": "^8.10.2",
75
+ "eslint-plugin-oxlint": "^1.55.0",
35
76
  "eslint-plugin-svelte": "^2.46.1",
36
- "prettier": "^3.6.2",
37
- "prettier-plugin-svelte": "^3.4.0",
77
+ "globals": "^16.5.0",
78
+ "oxlint": "^1.55.0",
79
+ "prettier": "^3.8.1",
80
+ "prettier-plugin-svelte": "^3.5.1",
81
+ "prettier-plugin-tailwindcss": "^0.6.14",
38
82
  "publint": "^0.1.16",
39
83
  "rollup-plugin-visualizer": "^5.14.0",
40
- "svelte": "^5.43.12",
41
- "svelte-check": "^4.3.2",
84
+ "svelte": "^5.53.10",
85
+ "svelte-check": "^4.4.5",
42
86
  "tslib": "^2.8.1",
43
87
  "typescript": "^5.9.3",
44
- "vite": "^5.4.19",
88
+ "typescript-eslint": "^8.57.0",
89
+ "vite": "^5.4.21",
90
+ "takumi-js": "2.0.1",
45
91
  "vitest": "^1.6.1"
46
92
  },
47
93
  "main": "./dist/index.js",
@@ -50,13 +96,19 @@
50
96
  "type": "module",
51
97
  "dependencies": {
52
98
  "@resvg/resvg-wasm": "^2.6.2",
53
- "satori": "^0.10.14",
99
+ "satori": "^0.25.0",
54
100
  "satori-html": "0.3.2",
55
- "std-env": "^3.9.0",
56
- "unwasm": "^0.5.0"
101
+ "std-env": "^4.1.0",
102
+ "unwasm": "^0.5.3"
57
103
  },
58
104
  "peerDependencies": {
59
- "@sveltejs/kit": ">=2.0.0"
105
+ "@sveltejs/kit": ">=2.0.0",
106
+ "takumi-js": "^2.0.1"
107
+ },
108
+ "peerDependenciesMeta": {
109
+ "takumi-js": {
110
+ "optional": true
111
+ }
60
112
  },
61
113
  "scripts": {
62
114
  "dev": "vite dev",
@@ -66,7 +118,11 @@
66
118
  "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
67
119
  "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
68
120
  "test": "vitest",
69
- "lint": "prettier --plugin-search-dir . --check . && eslint .",
121
+ "bench": "vitest bench --run",
122
+ "bench:save": "vitest bench --run --outputJson benchmarks/baseline.json",
123
+ "bench:compare": "vitest bench --run --compare benchmarks/baseline.json",
124
+ "bench:snapshot": "node scripts/bench-snapshot.mjs",
125
+ "lint": "oxlint . && eslint .",
70
126
  "format": "prettier --plugin-search-dir . --write .",
71
127
  "publishBeta": "npm publish --tag beta"
72
128
  }
Binary file