@ethercorps/sveltekit-og 4.0.0-next.1 → 4.0.0-next.2
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/helpers/create.d.ts +5 -0
- package/dist/helpers/create.js +48 -0
- package/dist/helpers/defaults.d.ts +10 -0
- package/dist/helpers/defaults.js +37 -0
- package/dist/helpers/emoji.d.ts +3 -0
- package/dist/helpers/emoji.js +53 -0
- package/dist/image-response.d.ts +5 -0
- package/dist/image-response.js +26 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/plugin.d.ts +4 -0
- package/dist/plugin.js +9 -0
- package/dist/providers/instances.d.ts +38 -0
- package/dist/providers/instances.js +17 -0
- package/dist/providers/resvg/edge.d.ts +6 -0
- package/dist/providers/resvg/edge.js +6 -0
- package/dist/providers/resvg/node.d.ts +6 -0
- package/dist/providers/resvg/node.js +13 -0
- package/dist/providers/resvg/resvg.wasm +0 -0
- package/dist/providers/satori/node.d.ts +6 -0
- package/dist/providers/satori/node.js +6 -0
- package/dist/types.d.ts +68 -4
- package/package.json +33 -23
- package/dist/api.d.ts +0 -6
- package/dist/api.js +0 -65
- package/dist/font.d.ts +0 -5
- package/dist/font.js +0 -27
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Component } from 'svelte';
|
|
2
|
+
import type { ComponentOptions, ImageOptions, VNode } from '../types.js';
|
|
3
|
+
export declare function createVNode(element: string | Component, componentOptions?: ComponentOptions): Promise<VNode>;
|
|
4
|
+
export declare function createSvg(element: string | Component, imageOptions: ImageOptions, componentOptions?: ComponentOptions): Promise<string>;
|
|
5
|
+
export declare function createPng(element: string | Component, imageOptions: ImageOptions, componentOptions?: ComponentOptions): Promise<Uint8Array<ArrayBufferLike>>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { html } from 'satori-html';
|
|
2
|
+
import { loadDynamicAsset } from './emoji.js';
|
|
3
|
+
import { default_fonts, DEFAULT_WIDTH } from '../helpers/defaults.js';
|
|
4
|
+
import { useResvg, useSatori } from '../providers/instances.js';
|
|
5
|
+
import { render } from 'svelte/server';
|
|
6
|
+
function svelteComponentToJsx(component, props = {}) {
|
|
7
|
+
const { body, head } = render(component, { props });
|
|
8
|
+
return html(body + head);
|
|
9
|
+
}
|
|
10
|
+
export async function createVNode(element, componentOptions) {
|
|
11
|
+
return typeof element === 'string' ? html(element.replaceAll('\n', '').trim()) : svelteComponentToJsx(element, componentOptions?.props);
|
|
12
|
+
}
|
|
13
|
+
export async function createSvg(element, imageOptions, componentOptions) {
|
|
14
|
+
const [satori, vnodes] = await Promise.all([useSatori(), createVNode(element, componentOptions)]);
|
|
15
|
+
if (!Object.hasOwn(imageOptions, 'fonts')) {
|
|
16
|
+
imageOptions['fonts'] = await default_fonts();
|
|
17
|
+
}
|
|
18
|
+
// Add dynamic emoji loading handler
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
imageOptions['loadAdditionalAsset'] = loadDynamicAsset({
|
|
22
|
+
emoji: imageOptions.emoji
|
|
23
|
+
});
|
|
24
|
+
if (imageOptions.debug) {
|
|
25
|
+
console.info('VNode proivided to satori:', vnodes, '\n');
|
|
26
|
+
console.info('Options proivided to satori:', imageOptions, '\n');
|
|
27
|
+
}
|
|
28
|
+
return satori(vnodes, imageOptions);
|
|
29
|
+
}
|
|
30
|
+
export async function createPng(element, imageOptions, componentOptions) {
|
|
31
|
+
const svg = await createSvg(element, imageOptions, componentOptions);
|
|
32
|
+
if (imageOptions.debug) {
|
|
33
|
+
console.info('SVG generated by satori:', svg, '\n');
|
|
34
|
+
}
|
|
35
|
+
const resvg_instance = await useResvg();
|
|
36
|
+
const resvg_options = {
|
|
37
|
+
fitTo: {
|
|
38
|
+
mode: 'width',
|
|
39
|
+
value: imageOptions.width || DEFAULT_WIDTH,
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
if (imageOptions.debug) {
|
|
43
|
+
console.info('Options provided to ReSVG:', resvg_options, '\n');
|
|
44
|
+
}
|
|
45
|
+
const resvg = new resvg_instance(svg, resvg_options);
|
|
46
|
+
const png_data = resvg.render();
|
|
47
|
+
return png_data.asPng();
|
|
48
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { SatoriOptions } from 'satori';
|
|
2
|
+
import type { ImageOptions } from '../types.js';
|
|
3
|
+
export declare function default_fonts(): Promise<SatoriOptions['fonts']>;
|
|
4
|
+
export declare const DEFAULT_FORMAT = "png";
|
|
5
|
+
export declare const DEFAULT_WIDTH = 1200;
|
|
6
|
+
export declare const DEFAULT_HEIGHT = 630;
|
|
7
|
+
export declare const DEFAULT_EMOJI_PROVIDER = "twemoji";
|
|
8
|
+
export declare const DEFAULT_STATUS_CODE = 200;
|
|
9
|
+
export declare const DEFAULT_STATUS_TEXT = "Success";
|
|
10
|
+
export declare const DEFAULT_OPTIONS: ImageOptions;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export async function default_fonts() {
|
|
2
|
+
const [noto_sans_regular_font_resp, noto_sans_bold_font_reps] = await Promise.all([
|
|
3
|
+
fetch('https://cdn-sveltekit-og.ethercorps.io/NotoSans-Regular.ttf'), fetch('https://cdn-sveltekit-og.ethercorps.io/NotoSans-Bold.ttf')
|
|
4
|
+
]);
|
|
5
|
+
if (!(noto_sans_bold_font_reps.ok || noto_sans_bold_font_reps.ok)) {
|
|
6
|
+
console.error('Not able to load default fonts');
|
|
7
|
+
throw new Error('Not able to load default fonts');
|
|
8
|
+
}
|
|
9
|
+
const [noto_sans_regular_font, noto_sans_bold_font] = await Promise.all([noto_sans_regular_font_resp.arrayBuffer(), noto_sans_bold_font_reps.arrayBuffer()]);
|
|
10
|
+
return [
|
|
11
|
+
{
|
|
12
|
+
data: noto_sans_regular_font,
|
|
13
|
+
name: 'Inter',
|
|
14
|
+
weight: 400,
|
|
15
|
+
style: 'normal'
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
data: noto_sans_bold_font,
|
|
19
|
+
name: 'Inter',
|
|
20
|
+
weight: 700,
|
|
21
|
+
style: 'normal'
|
|
22
|
+
}
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
export const DEFAULT_FORMAT = 'png';
|
|
26
|
+
export const DEFAULT_WIDTH = 1200;
|
|
27
|
+
export const DEFAULT_HEIGHT = 630;
|
|
28
|
+
export const DEFAULT_EMOJI_PROVIDER = 'twemoji';
|
|
29
|
+
export const DEFAULT_STATUS_CODE = 200;
|
|
30
|
+
export const DEFAULT_STATUS_TEXT = 'Success';
|
|
31
|
+
export const DEFAULT_OPTIONS = {
|
|
32
|
+
height: DEFAULT_HEIGHT,
|
|
33
|
+
width: DEFAULT_WIDTH,
|
|
34
|
+
debug: false,
|
|
35
|
+
format: DEFAULT_FORMAT,
|
|
36
|
+
emoji: 'twemoji'
|
|
37
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { DEFAULT_EMOJI_PROVIDER } from '../helpers/defaults.js';
|
|
2
|
+
// Code stolen from @vercel/og
|
|
3
|
+
const U200D = String.fromCharCode(8205);
|
|
4
|
+
const UFE0Fg = /\uFE0F/g;
|
|
5
|
+
function getIconCode(char) {
|
|
6
|
+
return toCodePoint(char.indexOf(U200D) < 0 ? char.replace(UFE0Fg, "") : char);
|
|
7
|
+
}
|
|
8
|
+
function toCodePoint(unicodeSurrogates) {
|
|
9
|
+
const r = [];
|
|
10
|
+
let c = 0, p = 0, i = 0;
|
|
11
|
+
while (i < unicodeSurrogates.length) {
|
|
12
|
+
c = unicodeSurrogates.charCodeAt(i++);
|
|
13
|
+
if (p) {
|
|
14
|
+
r.push((65536 + (p - 55296 << 10) + (c - 56320)).toString(16));
|
|
15
|
+
p = 0;
|
|
16
|
+
}
|
|
17
|
+
else if (55296 <= c && c <= 56319) {
|
|
18
|
+
p = c;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
r.push(c.toString(16));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return r.join("-");
|
|
25
|
+
}
|
|
26
|
+
const emoji_apis = {
|
|
27
|
+
twemoji: (code) => "https://cdn.jsdelivr.net/gh/twitter/twemoji@14.0.2/assets/svg/" + code.toLowerCase() + ".svg",
|
|
28
|
+
openmoji: "https://cdn.jsdelivr.net/npm/@svgmoji/openmoji@2.0.0/svg/",
|
|
29
|
+
blobmoji: "https://cdn.jsdelivr.net/npm/@svgmoji/blob@2.0.0/svg/",
|
|
30
|
+
noto: "https://cdn.jsdelivr.net/gh/svgmoji/svgmoji/packages/svgmoji__noto/svg/",
|
|
31
|
+
fluent: (code) => "https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/" + code.toLowerCase() + "_color.svg",
|
|
32
|
+
fluentFlat: (code) => "https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/" + code.toLowerCase() + "_flat.svg"
|
|
33
|
+
};
|
|
34
|
+
function loadEmoji(code, type) {
|
|
35
|
+
if (!type || !apis[type]) {
|
|
36
|
+
type = DEFAULT_EMOJI_PROVIDER;
|
|
37
|
+
}
|
|
38
|
+
const api = apis[type];
|
|
39
|
+
if (typeof api === "function") {
|
|
40
|
+
return fetch(api(code));
|
|
41
|
+
}
|
|
42
|
+
return fetch(`${api}${code.toUpperCase()}.svg`);
|
|
43
|
+
}
|
|
44
|
+
export const loadDynamicAsset = ({ emoji }) => {
|
|
45
|
+
const fn = async (code, text) => {
|
|
46
|
+
if (code === "emoji") {
|
|
47
|
+
return `data:image/svg+xml;base64,` + btoa(await (await loadEmoji(getIconCode(text), emoji)).text());
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
return async (...args) => {
|
|
51
|
+
return await fn(...args);
|
|
52
|
+
};
|
|
53
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Component, ComponentProps } from 'svelte';
|
|
2
|
+
import type { ImageResponseOptions } from './types.js';
|
|
3
|
+
export declare class ImageResponse<T extends string | Component<any>> extends Response {
|
|
4
|
+
constructor(element: T, options?: ImageResponseOptions, props?: T extends Component<any> ? ComponentProps<T> : never);
|
|
5
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { DEFAULT_OPTIONS, DEFAULT_STATUS_CODE, DEFAULT_STATUS_TEXT } from './helpers/defaults.js';
|
|
2
|
+
import { createPng, createSvg } from './helpers/create.js';
|
|
3
|
+
export class ImageResponse extends Response {
|
|
4
|
+
constructor(element, options, props) {
|
|
5
|
+
const extended_options = Object.assign({ ...DEFAULT_OPTIONS }, options);
|
|
6
|
+
const create_image_function = extended_options.format === 'png' ? createPng : createSvg;
|
|
7
|
+
const body = new ReadableStream({
|
|
8
|
+
async start(controller) {
|
|
9
|
+
const buffer = await create_image_function(element, extended_options, { props });
|
|
10
|
+
controller.enqueue(buffer);
|
|
11
|
+
controller.close();
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
super(body, {
|
|
15
|
+
headers: {
|
|
16
|
+
'Content-Type': `image/${extended_options.format}${extended_options.format === 'svg' ? '+xml' : ''}`,
|
|
17
|
+
'Cache-Control': extended_options.debug
|
|
18
|
+
? 'no-cache, no-store'
|
|
19
|
+
: 'public, immutable, no-transform, max-age=31536000',
|
|
20
|
+
...extended_options.headers
|
|
21
|
+
},
|
|
22
|
+
status: extended_options.status || DEFAULT_STATUS_CODE,
|
|
23
|
+
statusText: extended_options.statusText || DEFAULT_STATUS_TEXT
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { ImageResponse } from "./
|
|
2
|
-
export type { ImageResponseOptions,
|
|
1
|
+
export { ImageResponse } from "./image-response.js";
|
|
2
|
+
export type { ImageResponseOptions, VNode } from "./types.js";
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { ImageResponse } from "./
|
|
1
|
+
export { ImageResponse } from "./image-response.js";
|
package/dist/plugin.d.ts
ADDED
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type _satori from 'satori';
|
|
2
|
+
export declare function useResvg(): Promise<new (svg: Uint8Array | string, options?: import("@resvg/resvg-wasm").ResvgRenderOptions) => {
|
|
3
|
+
free(): void;
|
|
4
|
+
render(): {
|
|
5
|
+
free(): void;
|
|
6
|
+
asPng(): Uint8Array;
|
|
7
|
+
readonly height: number;
|
|
8
|
+
readonly pixels: Uint8Array;
|
|
9
|
+
readonly width: number;
|
|
10
|
+
};
|
|
11
|
+
toString(): string;
|
|
12
|
+
innerBBox(): {
|
|
13
|
+
free(): void;
|
|
14
|
+
height: number;
|
|
15
|
+
width: number;
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
} | undefined;
|
|
19
|
+
getBBox(): {
|
|
20
|
+
free(): void;
|
|
21
|
+
height: number;
|
|
22
|
+
width: number;
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
} | undefined;
|
|
26
|
+
cropByBBox(bbox: {
|
|
27
|
+
free(): void;
|
|
28
|
+
height: number;
|
|
29
|
+
width: number;
|
|
30
|
+
x: number;
|
|
31
|
+
y: number;
|
|
32
|
+
}): void;
|
|
33
|
+
imagesToResolve(): any[];
|
|
34
|
+
resolveImage(href: string, buffer: Uint8Array): void;
|
|
35
|
+
readonly height: number;
|
|
36
|
+
readonly width: number;
|
|
37
|
+
}>;
|
|
38
|
+
export declare function useSatori(): Promise<typeof _satori>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { isEdgeLight, isWorkerd } from 'std-env';
|
|
2
|
+
// we keep instances alive to avoid re-importing them on every request, maybe not needed but
|
|
3
|
+
// also helps with type inference
|
|
4
|
+
// Code from vue-og-images
|
|
5
|
+
const resvgInstance = { instance: undefined };
|
|
6
|
+
const satoriInstance = { instance: undefined };
|
|
7
|
+
export async function useResvg() {
|
|
8
|
+
const moduleImport = isEdgeLight || isWorkerd ? import(`./resvg/edge.js`) : import('./resvg/node.js');
|
|
9
|
+
resvgInstance.instance = resvgInstance.instance || await moduleImport.then(m => m.default);
|
|
10
|
+
await resvgInstance.instance.initWasmPromise;
|
|
11
|
+
return resvgInstance.instance.Resvg;
|
|
12
|
+
}
|
|
13
|
+
export async function useSatori() {
|
|
14
|
+
satoriInstance.instance = satoriInstance.instance || await import(`./satori/node.js`).then(m => m.default);
|
|
15
|
+
await satoriInstance.instance.initWasmPromise;
|
|
16
|
+
return satoriInstance.instance.satori;
|
|
17
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Resvg as _Resvg, initWasm } from '@resvg/resvg-wasm';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fetch will be called only once whenever you load this file.
|
|
5
|
+
* In vercel serverless functions, fetch will run on cold start.
|
|
6
|
+
* In Node.js (Stateful e.g. Linux servers), Fetch will run once when you start your server.
|
|
7
|
+
* */
|
|
8
|
+
const resvgWasm = fetch('https://unpkg.com/@resvg/resvg-wasm/index_bg.wasm')
|
|
9
|
+
|
|
10
|
+
export default {
|
|
11
|
+
initWasmPromise: initWasm(resvgWasm),
|
|
12
|
+
Resvg: _Resvg,
|
|
13
|
+
}
|
|
Binary file
|
package/dist/types.d.ts
CHANGED
|
@@ -1,15 +1,79 @@
|
|
|
1
1
|
import type { SatoriOptions } from 'satori/wasm';
|
|
2
|
-
export
|
|
2
|
+
export type ImageOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* Width of the image
|
|
5
|
+
* @default 1200
|
|
6
|
+
* */
|
|
3
7
|
width?: number;
|
|
8
|
+
/**
|
|
9
|
+
* Height of the image
|
|
10
|
+
* @default 630
|
|
11
|
+
* */
|
|
4
12
|
height?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Emoji provider
|
|
15
|
+
* @default twemoji
|
|
16
|
+
* */
|
|
17
|
+
emoji?: 'twemoji' | 'blobmoji' | 'noto' | 'openmoji' | 'fluent' | 'fluentFlat';
|
|
18
|
+
/**
|
|
19
|
+
* Fonts used for your text
|
|
20
|
+
* @default `helpers/defaults.js`
|
|
21
|
+
* */
|
|
5
22
|
fonts?: SatoriOptions['fonts'];
|
|
23
|
+
/**
|
|
24
|
+
* Tailwind config
|
|
25
|
+
* @default provided by satori
|
|
26
|
+
* */
|
|
27
|
+
tailwindConfig?: SatoriOptions['tailwindConfig'];
|
|
28
|
+
/**
|
|
29
|
+
* Debug operations
|
|
30
|
+
* @default false
|
|
31
|
+
* */
|
|
6
32
|
debug?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Image format
|
|
35
|
+
* @default png
|
|
36
|
+
* */
|
|
37
|
+
format?: 'svg' | 'png';
|
|
38
|
+
};
|
|
39
|
+
export type ResponseImageOptions = {
|
|
40
|
+
/**
|
|
41
|
+
* Status code for response
|
|
42
|
+
* @default 200
|
|
43
|
+
* */
|
|
7
44
|
status?: number;
|
|
45
|
+
/**
|
|
46
|
+
* Status text for response
|
|
47
|
+
* @default Success
|
|
48
|
+
* */
|
|
8
49
|
statusText?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Response Headers
|
|
52
|
+
* @default {
|
|
53
|
+
* 'Content-Type': 'image/png',
|
|
54
|
+
* 'Cache-Control': options.debug ? 'no-cache, no-store' : 'public, immutable, no-transform, max-age=31536000'
|
|
55
|
+
* }
|
|
56
|
+
* */
|
|
9
57
|
headers?: Record<string, string>;
|
|
10
|
-
|
|
11
|
-
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Image response options type exposed to devs for ImageResponse Instance
|
|
61
|
+
* */
|
|
62
|
+
export type ImageResponseOptions = ImageOptions & ResponseImageOptions;
|
|
63
|
+
/**
|
|
64
|
+
* Svelte Component props to render the component which dynamic content
|
|
65
|
+
* */
|
|
12
66
|
export type ComponentOptions = {
|
|
13
67
|
props?: Record<string, any>;
|
|
14
|
-
style?: string;
|
|
15
68
|
};
|
|
69
|
+
/**
|
|
70
|
+
* React virtual node, supported by satori as input (alternative to JSX input).
|
|
71
|
+
* */
|
|
72
|
+
export interface VNode {
|
|
73
|
+
type: string;
|
|
74
|
+
props: {
|
|
75
|
+
style?: Record<string, any>;
|
|
76
|
+
children?: string | VNode | VNode[];
|
|
77
|
+
[prop: string]: any;
|
|
78
|
+
};
|
|
79
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ethercorps/sveltekit-og",
|
|
3
|
-
"version": "4.0.0-next.
|
|
3
|
+
"version": "4.0.0-next.2",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"dev": "vite dev",
|
|
6
6
|
"build": "vite build && npm run package",
|
|
@@ -11,13 +11,18 @@
|
|
|
11
11
|
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
|
12
12
|
"test": "vitest",
|
|
13
13
|
"lint": "prettier --plugin-search-dir . --check . && eslint .",
|
|
14
|
-
"format": "prettier --plugin-search-dir . --write ."
|
|
14
|
+
"format": "prettier --plugin-search-dir . --write .",
|
|
15
|
+
"publishBeta": "npm publish --tag beta"
|
|
15
16
|
},
|
|
16
17
|
"exports": {
|
|
17
18
|
".": {
|
|
18
19
|
"types": "./dist/index.d.ts",
|
|
19
20
|
"svelte": "./dist/index.js",
|
|
20
21
|
"import": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./plugin": {
|
|
24
|
+
"types": "./dist/plugin.d.ts",
|
|
25
|
+
"import": "./dist/plugin.js"
|
|
21
26
|
}
|
|
22
27
|
},
|
|
23
28
|
"files": [
|
|
@@ -25,36 +30,41 @@
|
|
|
25
30
|
"!dist/**/*.test.*",
|
|
26
31
|
"!dist/**/*.spec.*"
|
|
27
32
|
],
|
|
28
|
-
"peerDependencies": {
|
|
29
|
-
"svelte": "^5.0.0"
|
|
30
|
-
},
|
|
31
33
|
"devDependencies": {
|
|
32
|
-
"@sveltejs/adapter-
|
|
33
|
-
"@sveltejs/kit": "^2.
|
|
34
|
-
"@sveltejs/package": "^2.
|
|
35
|
-
"@sveltejs/vite-plugin-svelte": "^
|
|
34
|
+
"@sveltejs/adapter-vercel": "^5.10.2",
|
|
35
|
+
"@sveltejs/kit": "^2.48.5",
|
|
36
|
+
"@sveltejs/package": "^2.5.4",
|
|
37
|
+
"@sveltejs/vite-plugin-svelte": "^4.0.4",
|
|
36
38
|
"@typescript-eslint/eslint-plugin": "^5.62.0",
|
|
37
39
|
"@typescript-eslint/parser": "^5.62.0",
|
|
38
40
|
"css-tree": "^2.3.1",
|
|
39
|
-
"eslint": "^8.
|
|
41
|
+
"eslint": "^8.57.1",
|
|
40
42
|
"eslint-config-prettier": "^8.10.0",
|
|
41
|
-
"eslint-plugin-svelte": "^2.
|
|
42
|
-
"prettier": "^
|
|
43
|
-
"prettier-plugin-svelte": "^
|
|
43
|
+
"eslint-plugin-svelte": "^2.46.1",
|
|
44
|
+
"prettier": "^3.6.2",
|
|
45
|
+
"prettier-plugin-svelte": "^3.4.0",
|
|
44
46
|
"publint": "^0.1.16",
|
|
45
|
-
"
|
|
46
|
-
"svelte
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
47
|
+
"rollup-plugin-visualizer": "^5.14.0",
|
|
48
|
+
"svelte": "^5.43.12",
|
|
49
|
+
"svelte-check": "^4.3.2",
|
|
50
|
+
"tslib": "^2.8.1",
|
|
51
|
+
"typescript": "^5.9.3",
|
|
52
|
+
"vite": "^5.4.19",
|
|
53
|
+
"vitest": "^1.6.1"
|
|
51
54
|
},
|
|
55
|
+
"main": "./dist/index.js",
|
|
52
56
|
"svelte": "./dist/index.js",
|
|
53
57
|
"types": "./dist/index.d.ts",
|
|
54
58
|
"type": "module",
|
|
55
59
|
"dependencies": {
|
|
56
|
-
"@
|
|
57
|
-
"
|
|
58
|
-
"satori": "
|
|
59
|
-
|
|
60
|
+
"@resvg/resvg-wasm": "^2.6.2",
|
|
61
|
+
"satori": "^0.10.14",
|
|
62
|
+
"satori-html": "0.3.2",
|
|
63
|
+
"std-env": "^3.9.0",
|
|
64
|
+
"unwasm": "^0.5.0"
|
|
65
|
+
},
|
|
66
|
+
"peerDependencies": {
|
|
67
|
+
"@sveltejs/kit": ">=2.0.0"
|
|
68
|
+
},
|
|
69
|
+
"packageManager": "pnpm@10.12.1"
|
|
60
70
|
}
|
package/dist/api.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { ComponentOptions, ImageResponseOptions } from './types.js';
|
|
2
|
-
import { SvelteComponent } from "svelte";
|
|
3
|
-
export declare const og: (element: string | typeof SvelteComponent, options: ImageResponseOptions, componentOptions: ComponentOptions) => Promise<any>;
|
|
4
|
-
export declare class ImageResponse extends Response {
|
|
5
|
-
constructor(element: string | typeof SvelteComponent, options: ImageResponseOptions | undefined, { props, style }: ComponentOptions);
|
|
6
|
-
}
|
package/dist/api.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import satori from 'satori';
|
|
2
|
-
import { Resvg } from '@resvg/resvg-js';
|
|
3
|
-
import { loadGoogleFont } from './font.js';
|
|
4
|
-
import { toReactElement, svelteComponentToJsx } from "@ethercorps/svelte-h2j";
|
|
5
|
-
import { SvelteComponent } from "svelte";
|
|
6
|
-
export const og = async (element, options, componentOptions) => {
|
|
7
|
-
const reactElement = typeof element === 'string' ? toReactElement(element) : svelteComponentToJsx(element, componentOptions);
|
|
8
|
-
// render the React element-like object into an SVG
|
|
9
|
-
const svg = await satori(reactElement, {
|
|
10
|
-
width: options.width || 1200,
|
|
11
|
-
height: options.height || 630,
|
|
12
|
-
fonts: options.fonts?.length
|
|
13
|
-
? options.fonts
|
|
14
|
-
: [
|
|
15
|
-
{
|
|
16
|
-
name: 'Bitter',
|
|
17
|
-
data: await loadGoogleFont({ family: 'Bitter', weight: 600 }),
|
|
18
|
-
weight: 500,
|
|
19
|
-
style: 'normal'
|
|
20
|
-
}
|
|
21
|
-
]
|
|
22
|
-
});
|
|
23
|
-
const requestedFormat = options.format || 'png';
|
|
24
|
-
if (requestedFormat === 'svg') {
|
|
25
|
-
return svg;
|
|
26
|
-
}
|
|
27
|
-
// convert the SVG into a PNG
|
|
28
|
-
const opts = {
|
|
29
|
-
// background: "rgba(238, 235, 230, .9)",
|
|
30
|
-
fitTo: {
|
|
31
|
-
mode: 'width',
|
|
32
|
-
value: options.width || 1200
|
|
33
|
-
},
|
|
34
|
-
font: {
|
|
35
|
-
loadSystemFonts: false // It will be faster to disable loading system fonts.
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
const resvg = new Resvg(svg, opts);
|
|
39
|
-
const pngData = resvg.render();
|
|
40
|
-
const pngBuffer = pngData.asPng();
|
|
41
|
-
return pngBuffer;
|
|
42
|
-
};
|
|
43
|
-
export class ImageResponse extends Response {
|
|
44
|
-
constructor(element, options = {}, { props = {}, style = '' }) {
|
|
45
|
-
super();
|
|
46
|
-
const body = new ReadableStream({
|
|
47
|
-
async start(controller) {
|
|
48
|
-
const buffer = await og(element, options, { props, style });
|
|
49
|
-
controller.enqueue(buffer);
|
|
50
|
-
controller.close();
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
return new Response(body, {
|
|
54
|
-
headers: {
|
|
55
|
-
'Content-Type': 'image/png',
|
|
56
|
-
'Cache-Control': options.debug
|
|
57
|
-
? 'no-cache, no-store'
|
|
58
|
-
: 'public, immutable, no-transform, max-age=31536000',
|
|
59
|
-
...options.headers
|
|
60
|
-
},
|
|
61
|
-
status: options.status || 200,
|
|
62
|
-
statusText: options.statusText
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
}
|
package/dist/font.d.ts
DELETED
package/dist/font.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export async function loadGoogleFont({ family, weight, text }) {
|
|
2
|
-
const params = {
|
|
3
|
-
family: `${encodeURIComponent(family)}${weight ? `:wght@${weight}` : ''}`
|
|
4
|
-
};
|
|
5
|
-
if (text) {
|
|
6
|
-
params.text = text;
|
|
7
|
-
}
|
|
8
|
-
else {
|
|
9
|
-
params.subset = 'latin';
|
|
10
|
-
}
|
|
11
|
-
const url = `https://fonts.googleapis.com/css2?${Object.keys(params)
|
|
12
|
-
.map((key) => `${key}=${params[key]}`)
|
|
13
|
-
.join('&')}`;
|
|
14
|
-
const res = await fetch(`${url}`, {
|
|
15
|
-
headers: {
|
|
16
|
-
// construct user agent to get TTF font
|
|
17
|
-
'User-Agent': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1'
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
const body = await res.text();
|
|
21
|
-
// Get the font URL from the CSS text
|
|
22
|
-
const fontUrl = body.match(/src: url\((.+)\) format\('(opentype|truetype)'\)/)?.[1];
|
|
23
|
-
if (!fontUrl) {
|
|
24
|
-
throw new Error('Could not find font URL');
|
|
25
|
-
}
|
|
26
|
-
return fetch(fontUrl).then((res) => res.arrayBuffer());
|
|
27
|
-
}
|