@ethercorps/sveltekit-og 4.2.1 → 4.3.0-next.10
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/README.md +27 -193
- package/dist/fonts.d.ts +5 -13
- package/dist/fonts.js +26 -24
- package/dist/helpers/create.d.ts +2 -2
- package/dist/helpers/create.js +29 -27
- package/dist/helpers/defaults.d.ts +4 -4
- package/dist/helpers/defaults.js +18 -17
- package/dist/helpers/emoji.js +30 -15
- package/dist/helpers/error-handler.d.ts +37 -0
- package/dist/helpers/error-handler.js +75 -0
- package/dist/helpers/logger.d.ts +7 -0
- package/dist/helpers/logger.js +21 -0
- package/dist/helpers/response.d.ts +20 -0
- package/dist/helpers/response.js +39 -0
- package/dist/helpers/to-html.d.ts +10 -0
- package/dist/helpers/to-html.js +10 -0
- package/dist/helpers/toJSX.d.ts +2 -2
- package/dist/helpers/toJSX.js +10 -5
- package/dist/helpers/utils.d.ts +2 -0
- package/dist/helpers/utils.js +25 -0
- package/dist/image-response.d.ts +2 -2
- package/dist/image-response.js +14 -21
- package/dist/plugin.js +6 -6
- package/dist/providers/instances.d.ts +3 -3
- package/dist/providers/instances.js +37 -10
- package/dist/providers/resvg/edge.d.ts +1 -1
- package/dist/providers/resvg/edge.js +10 -6
- package/dist/providers/resvg/node.d.ts +1 -1
- package/dist/providers/resvg/node.js +18 -9
- package/dist/providers/satori/edge.d.ts +6 -0
- package/dist/providers/satori/edge.js +12 -0
- package/dist/providers/satori/node.d.ts +1 -1
- package/dist/providers/satori/node.js +4 -4
- package/dist/takumi/fonts.d.ts +16 -0
- package/dist/takumi/fonts.js +15 -0
- package/dist/takumi/image-response.d.ts +6 -0
- package/dist/takumi/image-response.js +33 -0
- package/dist/takumi/index.d.ts +5 -0
- package/dist/takumi/index.js +4 -0
- package/dist/takumi/render.d.ts +4 -0
- package/dist/takumi/render.js +30 -0
- package/dist/takumi/renderer.d.ts +6 -0
- package/dist/takumi/renderer.js +35 -0
- package/dist/takumi/types.d.ts +64 -0
- package/dist/takumi/types.js +1 -0
- package/dist/types.d.ts +10 -10
- package/package.json +130 -81
package/README.md
CHANGED
|
@@ -6,8 +6,24 @@
|
|
|
6
6
|
|
|
7
7
|
Dynamically generate Open Graph images from an HTML+CSS template or Svelte component using fast and efficient conversion from HTML > SVG > PNG. Based on [Satori](https://github.com/vercel/satori#documentation). No headless browser required.
|
|
8
8
|
|
|
9
|
+
## Table of Contents
|
|
10
|
+
|
|
11
|
+
- [SvelteKit Open Graph Image Generation](#sveltekit-open-graph-image-generation)
|
|
12
|
+
- [Table of Contents](#table-of-contents)
|
|
13
|
+
- [Docs](#docs)
|
|
14
|
+
- [Installation](#installation)
|
|
15
|
+
- [Usage](#usage)
|
|
16
|
+
- [Examples](#examples)
|
|
17
|
+
- [Contributing](#contributing)
|
|
18
|
+
- [Changelog](#changelog)
|
|
19
|
+
- [License](#license)
|
|
20
|
+
- [Acknowledgements](#acknowledgements)
|
|
21
|
+
- [Authors](#authors)
|
|
22
|
+
- [Contributors](#contributors)
|
|
23
|
+
|
|
9
24
|
## Docs
|
|
10
|
-
|
|
25
|
+
|
|
26
|
+
For more detailed information and advanced usage, please refer to the [official documentation](https://sveltekit-og.dev).
|
|
11
27
|
|
|
12
28
|
## Installation
|
|
13
29
|
|
|
@@ -17,210 +33,28 @@ pnpm install @ethercorps/sveltekit-og
|
|
|
17
33
|
|
|
18
34
|
## Usage
|
|
19
35
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
- Add vite plugin
|
|
23
|
-
|
|
24
|
-
```typescript title="vite.cofig.js"
|
|
25
|
-
import { sveltekit } from '@sveltejs/kit/vite';
|
|
26
|
-
import { sveltekitOG } from '@ethercorps/sveltekit-og/plugin';
|
|
27
|
-
const config = {
|
|
28
|
-
plugins: [sveltekit(), sveltekitOG()]
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export default config;
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
### Rollup (will be deprecated in v5)
|
|
35
|
-
- Add `rollupWasm` to `build.rollupOptions.plugins` in `vite.cofig.js` file.
|
|
36
|
-
- For more information, check [docs](https://sveltekit-og.dev/docs/getting-started)
|
|
37
|
-
|
|
38
|
-
```ts title="vite.cofig.js"
|
|
39
|
-
import { sveltekit } from '@sveltejs/kit/vite';
|
|
40
|
-
import { defineConfig } from 'vitest/config';
|
|
41
|
-
import { rollupWasm } from '@ethercorps/sveltekit-og/plugin';
|
|
42
|
-
|
|
43
|
-
export default defineConfig({
|
|
44
|
-
plugins: [sveltekit()],
|
|
45
|
-
build: {
|
|
46
|
-
rollupOptions: {
|
|
47
|
-
plugins: [rollupWasm()],
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
- For node adapter update config with `rollupWasm`
|
|
54
|
-
- Check node runtime [docs](https://sveltekit-og.dev/docs/runtime/node)
|
|
55
|
-
|
|
56
|
-
```ts title="vite.cofig.js"
|
|
57
|
-
import { sveltekit } from '@sveltejs/kit/vite';
|
|
58
|
-
import { defineConfig } from 'vitest/config';
|
|
59
|
-
import { rollupWasm } from '@ethercorps/sveltekit-og/plugin';
|
|
60
|
-
|
|
61
|
-
export default defineConfig({
|
|
62
|
-
plugins: [sveltekit()],
|
|
63
|
-
build: {
|
|
64
|
-
rollupOptions: {
|
|
65
|
-
plugins: [
|
|
66
|
-
rollupWasm({ esmImport: false })
|
|
67
|
-
],
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
- Create a file at `/src/routes/og/+server.ts`. Alternatively, you can use JavaScript by removing the types from this example.
|
|
74
|
-
|
|
75
|
-
```typescript
|
|
76
|
-
// src/routes/og/+server.ts
|
|
77
|
-
import { ImageResponse } from '@ethercorps/sveltekit-og';
|
|
78
|
-
import { RequestHandler } from './$types';
|
|
79
|
-
|
|
80
|
-
const template = `
|
|
81
|
-
<div tw="bg-gray-50 flex w-full h-full items-center justify-center">
|
|
82
|
-
<div tw="flex flex-col md:flex-row w-full py-12 px-4 md:items-center justify-between p-8">
|
|
83
|
-
<h2 tw="flex flex-col text-3xl sm:text-4xl font-bold tracking-tight text-gray-900 text-left">
|
|
84
|
-
<span>Ready to dive in?</span>
|
|
85
|
-
<span tw="text-indigo-600">Start your free trial today.</span>
|
|
86
|
-
</h2>
|
|
87
|
-
<div tw="mt-8 flex md:mt-0">
|
|
88
|
-
<div tw="flex rounded-md shadow">
|
|
89
|
-
<a href="#" tw="flex items-center justify-center rounded-md border border-transparent bg-indigo-600 px-5 py-3 text-base font-medium text-white">Get started</a>
|
|
90
|
-
</div>
|
|
91
|
-
<div tw="ml-3 flex rounded-md shadow">
|
|
92
|
-
<a href="#" tw="flex items-center justify-center rounded-md border border-transparent bg-white px-5 py-3 text-base font-medium text-indigo-600">Learn more</a>
|
|
93
|
-
</div>
|
|
94
|
-
</div>
|
|
95
|
-
</div>
|
|
96
|
-
</div>
|
|
97
|
-
`;
|
|
98
|
-
|
|
99
|
-
const fontFile = await fetch('https://og-playground.vercel.app/inter-latin-ext-400-normal.woff');
|
|
100
|
-
const fontData: ArrayBuffer = await fontFile.arrayBuffer();
|
|
101
|
-
|
|
102
|
-
export const GET: RequestHandler = async () => {
|
|
103
|
-
return await new ImageResponse(template, {
|
|
104
|
-
height: 630,
|
|
105
|
-
width: 1200,
|
|
106
|
-
fonts: [
|
|
107
|
-
{
|
|
108
|
-
name: 'Inter Latin',
|
|
109
|
-
data: fontData,
|
|
110
|
-
weight: 400
|
|
111
|
-
}
|
|
112
|
-
]
|
|
113
|
-
});
|
|
114
|
-
};
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
Then run `npm dev` and visit `localhost:5173/og` to view your generated PNG. Remember that hot module reloading does not work with server routes, so if you change your HTML or CSS, hard refresh the route to see changes.
|
|
118
|
-
|
|
119
|
-
## Example Output
|
|
120
|
-
|
|
121
|
-

|
|
122
|
-
|
|
123
|
-
## Headers
|
|
124
|
-
|
|
125
|
-
When run in development, image headers contain `cache-control: no-cache, no-store`. In production, image headers contain `'cache-control': 'public, immutable, no-transform, max-age=31536000'`, which caches the image for 1 year. In both cases, the `'content-type': 'image/png'` is used.
|
|
126
|
-
|
|
127
|
-
## Styling
|
|
128
|
-
|
|
129
|
-
Notice that our example uses TailwindCSS classes (e.g. `tw="bg-gray-50"`). Alternatively, your HTML can contain style attributes using any of [the subset of CSS supported by Satori](https://github.com/vercel/satori#css).
|
|
130
|
-
|
|
131
|
-
Satori supports only a subset of HTML and CSS. For full details, see [Satori’s documentation](https://github.com/vercel/satori#documentation). Notably, Satori only supports flex-based layouts.
|
|
132
|
-
|
|
133
|
-
## Fonts
|
|
134
|
-
|
|
135
|
-
Satori supports `ttf`, `otf`, and `woff` font formats; `woff2` is not supported. To maximize the font parsing speed, `ttf` or `otf` are recommended over `woff`.
|
|
136
|
-
|
|
137
|
-
By default, `@ethercorps/sveltekit-og` includes only 'Noto Sans' font. If you need to use other fonts, you can specify them as shown in the example. Notably, you can also import a font file that is stored locally within your project and are not required to use fetch.
|
|
36
|
+
For detailed usage instructions, please see the [Getting Started](https://sveltekit-og.dev/docs/getting-started) section of our documentation.
|
|
138
37
|
|
|
139
38
|
## Examples
|
|
140
39
|
|
|
141
|
-
-
|
|
142
|
-
-
|
|
143
|
-
|
|
144
|
-
## API Reference
|
|
145
|
-
|
|
146
|
-
The package exposes an `ImageResponse` constructors, with the following options available:
|
|
147
|
-
|
|
148
|
-
```typescript
|
|
149
|
-
import {ImageResponse} from '@ethercorps/sveltekit-og'
|
|
150
|
-
import {SvelteComponent} from "svelte";
|
|
151
|
-
|
|
152
|
-
ImageResponse(
|
|
153
|
-
element : string | Component,
|
|
154
|
-
options : {
|
|
155
|
-
width ? : number = 1200
|
|
156
|
-
height ? : number = 630,
|
|
157
|
-
backgroundColor ? : string = "#fff"
|
|
158
|
-
fonts ? : {
|
|
159
|
-
name: string,
|
|
160
|
-
data: ArrayBuffer,
|
|
161
|
-
weight: number,
|
|
162
|
-
style: 'normal' | 'italic'
|
|
163
|
-
}[]
|
|
164
|
-
debug ? : boolean = false
|
|
165
|
-
// Options that will be passed to the HTTP response
|
|
166
|
-
status ? : number = 200
|
|
167
|
-
statusText ? : string
|
|
168
|
-
headers ? : Record<string, string>
|
|
169
|
-
},
|
|
170
|
-
// Component props if components.
|
|
171
|
-
ComponentProps<Component>
|
|
172
|
-
)
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
## Changelog
|
|
176
|
-
|
|
177
|
-
### v4.0.0 (Breaking Changes)
|
|
178
|
-
|
|
179
|
-
> Just install @ethercorps/sveltekit-og
|
|
180
|
-
|
|
181
|
-
> Support for NodeJS, Deno, Cloudflare Pages, Cloudflare Workers, Vercel and Netlify.
|
|
182
|
-
|
|
183
|
-
> No support for Bun tried and failed.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
### v3.0.0 (Breaking Changes)
|
|
187
|
-
|
|
188
|
-
> Just install @ethercorps/sveltekit-og
|
|
189
|
-
> No wasm as of now, only support for nodejs based runtime.
|
|
40
|
+
- **ImageResponse**: [_source_](/src/routes/+server.ts) · [_demo_](https://vercel.sveltekit-og.dev)
|
|
41
|
+
- **Component Rendering**: [_source_](/src/routes/sc/+server.ts) · [_demo_](https://vercel.sveltekit-og.dev/sc)
|
|
190
42
|
|
|
191
|
-
|
|
43
|
+
## Contributing
|
|
192
44
|
|
|
193
|
-
|
|
45
|
+
Contributions are welcome! Please read our [contributing guidelines](CONTRIBUTING.md) to get started.
|
|
194
46
|
|
|
195
|
-
|
|
196
|
-
npm i @resvg/resvg-js
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
```
|
|
200
|
-
npm i satori
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
> From now on their will be no issues related to build, and soon this library going to have its own documentation.
|
|
204
|
-
|
|
205
|
-
### v1.2.2 Update (Breaking Change)
|
|
206
|
-
|
|
207
|
-
- We don't provide access to satori from `@ethercorps/sveltekit-og`.
|
|
208
|
-
|
|
209
|
-
### v1.0.0 Update (Breaking Changes)
|
|
47
|
+
## Changelog
|
|
210
48
|
|
|
211
|
-
|
|
212
|
-
Now you can use `{ toReactElement }` with `"@ethercorps/sveltekit-og"` like:
|
|
49
|
+
All notable changes to this project are documented in the [changelog](CHANGELOG.md).
|
|
213
50
|
|
|
214
|
-
|
|
215
|
-
- Removed `@resvg/resvg-wasm` with `@resvg/resvg-js` because of internal errors.
|
|
216
|
-
- Removed `satori-html` because now we have `toReactElement` out of the box with svelte compiler.
|
|
217
|
-
> If you find a problem related to undefined a please check [_vite.config.js_](/vite.config.ts) and add ` define: { _a: 'undefined' } in config.`
|
|
51
|
+
## License
|
|
218
52
|
|
|
219
|
-
|
|
53
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
220
54
|
|
|
221
55
|
## Acknowledgements
|
|
222
56
|
|
|
223
|
-
This project
|
|
57
|
+
This project would not be possible without the following projects:
|
|
224
58
|
|
|
225
59
|
- [Satori & @vercel/og](https://github.com/vercel/satori)
|
|
226
60
|
- [Noto by Google Fonts](https://fonts.google.com/noto)
|
package/dist/fonts.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FinalFontOptions, FontStyle, FontWeight, MayBePromise } from
|
|
1
|
+
import type { FinalFontOptions, FontStyle, FontWeight, MayBePromise } from "./types.js";
|
|
2
2
|
interface BaseFontOptions {
|
|
3
3
|
weight?: FontWeight;
|
|
4
4
|
style?: FontStyle;
|
|
@@ -7,32 +7,24 @@ interface BaseFontOptions {
|
|
|
7
7
|
* All font types inherit from this class.
|
|
8
8
|
*/
|
|
9
9
|
export declare class BaseFont {
|
|
10
|
-
protected input: any;
|
|
11
10
|
name: string;
|
|
12
11
|
style: FontStyle;
|
|
13
12
|
weight: FontWeight;
|
|
14
|
-
constructor(name: string,
|
|
13
|
+
constructor(name: string, { weight, style }?: BaseFontOptions);
|
|
15
14
|
/** * Abstract getter that returns the promised ArrayBuffer.
|
|
16
15
|
* Overridden by CustomFont and GoogleFont for lazy loading.
|
|
17
16
|
*/
|
|
18
17
|
get data(): MayBePromise<Buffer | ArrayBuffer>;
|
|
19
18
|
}
|
|
20
|
-
/** * A helper class to load Custom fonts, typically from local files.
|
|
21
|
-
* The input must be a function provided by the user (e.g., using $app/server/read).
|
|
22
|
-
*/
|
|
23
19
|
export declare class CustomFont extends BaseFont {
|
|
20
|
+
protected input: MayBePromise<Buffer | ArrayBuffer> | (() => MayBePromise<Buffer | ArrayBuffer>);
|
|
24
21
|
private promise?;
|
|
25
|
-
/**
|
|
26
|
-
* Creates an instance of CustomFont.
|
|
27
|
-
* @param name The name of the font (for CSS font-family).
|
|
28
|
-
* @param input Font data as ArrayBuffer or a function that resolves to ArrayBuffer (user must provide the loading logic).
|
|
29
|
-
*/
|
|
30
22
|
constructor(name: string, input: MayBePromise<Buffer | ArrayBuffer> | (() => MayBePromise<Buffer | ArrayBuffer>), options?: BaseFontOptions);
|
|
31
|
-
/** A promise which resolves to font data as `ArrayBuffer` (Lazy load) */
|
|
23
|
+
/** A promise which resolves to font data as `ArrayBuffer` (Lazy load and CACHED) */
|
|
32
24
|
get data(): Promise<Buffer | ArrayBuffer>;
|
|
33
25
|
}
|
|
34
26
|
/** Loads Google font ArrayBuffer with caching. */
|
|
35
|
-
export declare const loadGoogleFont: (family: string, { text, weight, style, display }?: {
|
|
27
|
+
export declare const loadGoogleFont: (family: string, { text, weight, style, display, }?: {
|
|
36
28
|
text?: string;
|
|
37
29
|
weight?: string | number;
|
|
38
30
|
style?: FontStyle;
|
package/dist/fonts.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
import { FONT_CACHE_MAP } from
|
|
1
|
+
import { FONT_CACHE_MAP } from "./helpers/cache.js";
|
|
2
2
|
/** * Base font class defining the structure required by Satori.
|
|
3
3
|
* All font types inherit from this class.
|
|
4
4
|
*/
|
|
5
5
|
export class BaseFont {
|
|
6
|
-
input;
|
|
7
6
|
name;
|
|
8
7
|
style;
|
|
9
8
|
weight;
|
|
10
|
-
constructor(name,
|
|
11
|
-
this.input = input;
|
|
9
|
+
constructor(name, { weight = 400, style = "normal" } = {}) {
|
|
12
10
|
this.name = name;
|
|
13
11
|
this.style = style;
|
|
14
12
|
this.weight = weight;
|
|
@@ -17,43 +15,47 @@ export class BaseFont {
|
|
|
17
15
|
* Overridden by CustomFont and GoogleFont for lazy loading.
|
|
18
16
|
*/
|
|
19
17
|
get data() {
|
|
20
|
-
|
|
18
|
+
throw new Error("The 'data' getter must be implemented by subclasses of BaseFont.");
|
|
21
19
|
}
|
|
22
20
|
}
|
|
23
|
-
/** * A helper class to load Custom fonts, typically from local files.
|
|
24
|
-
* The input must be a function provided by the user (e.g., using $app/server/read).
|
|
25
|
-
*/
|
|
26
21
|
export class CustomFont extends BaseFont {
|
|
22
|
+
input;
|
|
27
23
|
promise;
|
|
28
|
-
/**
|
|
29
|
-
* Creates an instance of CustomFont.
|
|
30
|
-
* @param name The name of the font (for CSS font-family).
|
|
31
|
-
* @param input Font data as ArrayBuffer or a function that resolves to ArrayBuffer (user must provide the loading logic).
|
|
32
|
-
*/
|
|
33
24
|
constructor(name, input, options) {
|
|
34
|
-
super(name,
|
|
25
|
+
super(name, options);
|
|
26
|
+
this.input = input;
|
|
35
27
|
}
|
|
36
|
-
/** A promise which resolves to font data as `ArrayBuffer` (Lazy load) */
|
|
28
|
+
/** A promise which resolves to font data as `ArrayBuffer` (Lazy load and CACHED) */
|
|
37
29
|
get data() {
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
30
|
+
const cacheKey = `${this.name}-${this.weight}-${this.style}`;
|
|
31
|
+
const cachedData = FONT_CACHE_MAP.get(cacheKey);
|
|
32
|
+
if (cachedData) {
|
|
33
|
+
return Promise.resolve(cachedData);
|
|
34
|
+
}
|
|
35
|
+
const fallback = async () => {
|
|
36
|
+
const buffer = typeof this.input === "function" ? this.input() : this.input;
|
|
37
|
+
const resolvedBuffer = await buffer;
|
|
38
|
+
FONT_CACHE_MAP.set(cacheKey, resolvedBuffer);
|
|
39
|
+
return resolvedBuffer;
|
|
40
|
+
};
|
|
41
41
|
this.promise = this.promise?.then(null, fallback) ?? fallback();
|
|
42
42
|
return this.promise;
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
/** Constructs Google font css url */
|
|
46
|
-
const constructGoogleFontCssUrl = (family, { text, weight = 400, style =
|
|
46
|
+
const constructGoogleFontCssUrl = (family, { text, weight = 400, style = "normal", display, } = {}) => {
|
|
47
47
|
// Logic to build the URL (e.g., https://fonts.googleapis.com/css2?family=...wght@...)
|
|
48
48
|
const params = {
|
|
49
|
-
family: `${family.replaceAll(
|
|
49
|
+
family: `${family.replaceAll(" ", "+")}:${style === "italic" ? "ital," : ""}wght@${style === "italic" ? "1," : ""}${weight}`,
|
|
50
50
|
};
|
|
51
51
|
if (text)
|
|
52
52
|
params.text = encodeURIComponent(text);
|
|
53
|
-
return `https://fonts.googleapis.com/css2?${Object.keys(params)
|
|
53
|
+
return `https://fonts.googleapis.com/css2?${Object.keys(params)
|
|
54
|
+
.map((key) => `${key}=${params[key]}`)
|
|
55
|
+
.join("&")}`;
|
|
54
56
|
};
|
|
55
57
|
/** Loads Google font ArrayBuffer with caching. */
|
|
56
|
-
export const loadGoogleFont = async (family, { text, weight = 400, style =
|
|
58
|
+
export const loadGoogleFont = async (family, { text, weight = 400, style = "normal", display, } = {}) => {
|
|
57
59
|
const cssUrl = constructGoogleFontCssUrl(family, { text, weight, display, style });
|
|
58
60
|
const fromMap = FONT_CACHE_MAP.get(cssUrl);
|
|
59
61
|
if (fromMap) {
|
|
@@ -84,7 +86,7 @@ export class GoogleFont extends BaseFont {
|
|
|
84
86
|
text;
|
|
85
87
|
promise;
|
|
86
88
|
constructor(family, options = {}) {
|
|
87
|
-
super(options.name || family,
|
|
89
|
+
super(options.name || family, options);
|
|
88
90
|
this.family = family;
|
|
89
91
|
this.text = options.text;
|
|
90
92
|
}
|
|
@@ -108,5 +110,5 @@ export async function resolveFonts(fontClasses) {
|
|
|
108
110
|
style: fontClass.style,
|
|
109
111
|
};
|
|
110
112
|
}));
|
|
111
|
-
return resolvedFonts.filter(font => font !== null);
|
|
113
|
+
return resolvedFonts.filter((font) => font !== null);
|
|
112
114
|
}
|
package/dist/helpers/create.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Component } from
|
|
2
|
-
import type { ComponentOptions, ImageOptions } from
|
|
1
|
+
import type { Component } from "svelte";
|
|
2
|
+
import type { ComponentOptions, ImageOptions } from "../types.js";
|
|
3
3
|
export declare function createSvg(element: string | Component, imageOptions: ImageOptions, componentOptions?: ComponentOptions): Promise<string>;
|
|
4
4
|
export declare function createPng(element: string | Component, imageOptions: ImageOptions, componentOptions?: ComponentOptions): Promise<Uint8Array<ArrayBufferLike>>;
|
package/dist/helpers/create.js
CHANGED
|
@@ -1,38 +1,40 @@
|
|
|
1
|
-
import { loadDynamicAsset } from
|
|
2
|
-
import { default_fonts, DEFAULT_WIDTH } from
|
|
3
|
-
import { useResvg, useSatori } from
|
|
4
|
-
import { createVNode } from
|
|
1
|
+
import { loadDynamicAsset } from "./emoji.js";
|
|
2
|
+
import { default_fonts, DEFAULT_WIDTH } from "../helpers/defaults.js";
|
|
3
|
+
import { useResvg, useSatori } from "../providers/instances.js";
|
|
4
|
+
import { createVNode } from "./toJSX.js";
|
|
5
|
+
import { createLogger } from "./logger.js";
|
|
6
|
+
import { handleAsyncAll, handleAsync, ErrorCodes } from "./error-handler.js";
|
|
5
7
|
export async function createSvg(element, imageOptions, componentOptions) {
|
|
6
|
-
const
|
|
8
|
+
const log = createLogger(imageOptions.debug ?? false);
|
|
9
|
+
const [satori, vnodes] = await handleAsyncAll([() => useSatori(imageOptions.debug), () => Promise.resolve(createVNode(element, componentOptions))], ErrorCodes.SATORI_RENDER_FAILED, "Failed to initialize Satori or create VNode");
|
|
7
10
|
const satoriOptions = structuredClone(imageOptions);
|
|
8
|
-
if (!Object.hasOwn(satoriOptions,
|
|
9
|
-
satoriOptions[
|
|
11
|
+
if (!Object.hasOwn(satoriOptions, "fonts")) {
|
|
12
|
+
satoriOptions["fonts"] = await handleAsync(() => default_fonts(), ErrorCodes.FONT_LOAD_FAILED, "Failed to load default fonts for Satori");
|
|
10
13
|
}
|
|
11
|
-
satoriOptions[
|
|
12
|
-
emoji: imageOptions.emoji
|
|
14
|
+
satoriOptions["loadAdditionalAsset"] = loadDynamicAsset({
|
|
15
|
+
emoji: imageOptions.emoji,
|
|
13
16
|
});
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return satori(vnodes, satoriOptions);
|
|
17
|
+
log.debug("Generating SVG with Satori");
|
|
18
|
+
log.info("VNode provided to satori:", JSON.stringify(vnodes, null, 2), "\n");
|
|
19
|
+
log.info("Options provided to satori:", imageOptions);
|
|
20
|
+
return handleAsync(() => satori(vnodes, satoriOptions), ErrorCodes.SATORI_RENDER_FAILED, "Failed to render SVG with Satori");
|
|
19
21
|
}
|
|
20
22
|
export async function createPng(element, imageOptions, componentOptions) {
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const resvg_instance = await useResvg();
|
|
23
|
+
const log = createLogger(imageOptions.debug ?? false);
|
|
24
|
+
const svg = await handleAsync(() => createSvg(element, imageOptions, componentOptions), ErrorCodes.SATORI_RENDER_FAILED, "Failed to create SVG for PNG rendering");
|
|
25
|
+
log.debug("SVG generated by satori for ReSVG: \n", svg, "\n");
|
|
26
|
+
const resvg_instance = await handleAsync(() => useResvg(imageOptions.debug), ErrorCodes.RESVG_INIT_FAILED, "Failed to initialize ReSVG");
|
|
26
27
|
const resvg_options = {
|
|
27
28
|
fitTo: {
|
|
28
|
-
mode:
|
|
29
|
+
mode: "width",
|
|
29
30
|
value: imageOptions.width || DEFAULT_WIDTH,
|
|
30
|
-
}
|
|
31
|
+
},
|
|
31
32
|
};
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
log.debug("Rendering PNG with ReSVG");
|
|
34
|
+
log.info("Options provided to ReSVG:", resvg_options, "\n");
|
|
35
|
+
return handleAsync(async () => {
|
|
36
|
+
const resvg = new resvg_instance(svg, resvg_options);
|
|
37
|
+
const png_data = resvg.render();
|
|
38
|
+
return png_data.asPng();
|
|
39
|
+
}, ErrorCodes.RESVG_RENDER_FAILED, "Failed to render PNG with ReSVG");
|
|
38
40
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { SatoriOptions } from
|
|
2
|
-
import type { ImageOptions } from
|
|
3
|
-
import type { EmojiType } from
|
|
4
|
-
export declare function default_fonts(): Promise<SatoriOptions[
|
|
1
|
+
import type { SatoriOptions } from "satori";
|
|
2
|
+
import type { ImageOptions } from "../types.js";
|
|
3
|
+
import type { EmojiType } from "./emoji.js";
|
|
4
|
+
export declare function default_fonts(): Promise<SatoriOptions["fonts"]>;
|
|
5
5
|
export declare const DEFAULT_FORMAT = "png";
|
|
6
6
|
export declare const DEFAULT_WIDTH = 1200;
|
|
7
7
|
export declare const DEFAULT_HEIGHT = 630;
|
package/dist/helpers/defaults.js
CHANGED
|
@@ -1,37 +1,38 @@
|
|
|
1
|
+
import { handleAsyncAll, validateResponse, ErrorCodes } from "./error-handler.js";
|
|
1
2
|
export async function default_fonts() {
|
|
2
|
-
const [noto_sans_regular_font_resp, noto_sans_bold_font_reps] = await
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
const [noto_sans_regular_font_resp, noto_sans_bold_font_reps] = await handleAsyncAll([
|
|
4
|
+
() => fetch("https://cdn-sveltekit-og.ethercorps.io/NotoSans-Regular.ttf"),
|
|
5
|
+
() => fetch("https://cdn-sveltekit-og.ethercorps.io/NotoSans-Bold.ttf"),
|
|
6
|
+
], ErrorCodes.FONT_LOAD_FAILED, "Failed to fetch default fonts");
|
|
7
|
+
const [noto_sans_regular_font, noto_sans_bold_font] = await handleAsyncAll([
|
|
8
|
+
() => validateResponse(noto_sans_regular_font_resp, ErrorCodes.FONT_LOAD_FAILED, "Failed to validate regular font response"),
|
|
9
|
+
() => validateResponse(noto_sans_bold_font_reps, ErrorCodes.FONT_LOAD_FAILED, "Failed to validate bold font response"),
|
|
10
|
+
], ErrorCodes.FONT_LOAD_FAILED, "Failed to process font responses");
|
|
10
11
|
return [
|
|
11
12
|
{
|
|
12
13
|
data: noto_sans_regular_font,
|
|
13
|
-
name:
|
|
14
|
+
name: "Inter",
|
|
14
15
|
weight: 400,
|
|
15
|
-
style:
|
|
16
|
+
style: "normal",
|
|
16
17
|
},
|
|
17
18
|
{
|
|
18
19
|
data: noto_sans_bold_font,
|
|
19
|
-
name:
|
|
20
|
+
name: "Inter",
|
|
20
21
|
weight: 700,
|
|
21
|
-
style:
|
|
22
|
-
}
|
|
22
|
+
style: "normal",
|
|
23
|
+
},
|
|
23
24
|
];
|
|
24
25
|
}
|
|
25
|
-
export const DEFAULT_FORMAT =
|
|
26
|
+
export const DEFAULT_FORMAT = "png";
|
|
26
27
|
export const DEFAULT_WIDTH = 1200;
|
|
27
28
|
export const DEFAULT_HEIGHT = 630;
|
|
28
|
-
export const DEFAULT_EMOJI_PROVIDER =
|
|
29
|
+
export const DEFAULT_EMOJI_PROVIDER = "twemoji";
|
|
29
30
|
export const DEFAULT_STATUS_CODE = 200;
|
|
30
|
-
export const DEFAULT_STATUS_TEXT =
|
|
31
|
+
export const DEFAULT_STATUS_TEXT = "Success";
|
|
31
32
|
export const DEFAULT_OPTIONS = {
|
|
32
33
|
height: DEFAULT_HEIGHT,
|
|
33
34
|
width: DEFAULT_WIDTH,
|
|
34
35
|
debug: false,
|
|
35
36
|
format: DEFAULT_FORMAT,
|
|
36
|
-
emoji:
|
|
37
|
+
emoji: "twemoji",
|
|
37
38
|
};
|
package/dist/helpers/emoji.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { DEFAULT_EMOJI_PROVIDER } from
|
|
1
|
+
import { DEFAULT_EMOJI_PROVIDER } from "../helpers/defaults.js";
|
|
2
|
+
import { handleAsync, ErrorCodes } from "./error-handler.js";
|
|
2
3
|
// Code stolen from @vercel/og and https://github.com/fineshopdesign/cf-wasm
|
|
3
4
|
const U200D = String.fromCharCode(8205);
|
|
4
5
|
const UFE0Fg = /\uFE0F/g;
|
|
@@ -11,7 +12,7 @@ function toCodePoint(unicodeSurrogates) {
|
|
|
11
12
|
while (i < unicodeSurrogates.length) {
|
|
12
13
|
c = unicodeSurrogates.charCodeAt(i++);
|
|
13
14
|
if (p) {
|
|
14
|
-
r.push((65536 + (p - 55296 << 10) + (c - 56320)).toString(16));
|
|
15
|
+
r.push((65536 + ((p - 55296) << 10) + (c - 56320)).toString(16));
|
|
15
16
|
p = 0;
|
|
16
17
|
}
|
|
17
18
|
else if (55296 <= c && c <= 56319) {
|
|
@@ -24,27 +25,41 @@ function toCodePoint(unicodeSurrogates) {
|
|
|
24
25
|
return r.join("-");
|
|
25
26
|
}
|
|
26
27
|
const emoji_apis = {
|
|
27
|
-
twemoji: (code) => "https://cdn.jsdelivr.net/gh/twitter/twemoji@14.0.2/assets/svg/" +
|
|
28
|
+
twemoji: (code) => "https://cdn.jsdelivr.net/gh/twitter/twemoji@14.0.2/assets/svg/" +
|
|
29
|
+
code.toLowerCase() +
|
|
30
|
+
".svg",
|
|
28
31
|
openmoji: "https://cdn.jsdelivr.net/npm/@svgmoji/openmoji@2.0.0/svg/",
|
|
29
32
|
blobmoji: "https://cdn.jsdelivr.net/npm/@svgmoji/blob@2.0.0/svg/",
|
|
30
33
|
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/" +
|
|
32
|
-
|
|
34
|
+
fluent: (code) => "https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/" +
|
|
35
|
+
code.toLowerCase() +
|
|
36
|
+
"_color.svg",
|
|
37
|
+
fluentFlat: (code) => "https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/" +
|
|
38
|
+
code.toLowerCase() +
|
|
39
|
+
"_flat.svg",
|
|
33
40
|
};
|
|
34
|
-
function loadEmoji(code, type) {
|
|
35
|
-
|
|
36
|
-
type
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
async function loadEmoji(code, type) {
|
|
42
|
+
return handleAsync(async () => {
|
|
43
|
+
if (!type || !emoji_apis[type]) {
|
|
44
|
+
type = DEFAULT_EMOJI_PROVIDER;
|
|
45
|
+
}
|
|
46
|
+
const api = emoji_apis[type];
|
|
47
|
+
if (typeof api === "function") {
|
|
48
|
+
return fetch(api(code));
|
|
49
|
+
}
|
|
50
|
+
return fetch(`${api}${code.toUpperCase()}.svg`);
|
|
51
|
+
}, ErrorCodes.EMOJI_LOAD_FAILED, `Failed to load emoji for code: ${code}`);
|
|
43
52
|
}
|
|
44
53
|
export const loadDynamicAsset = ({ emoji }) => {
|
|
45
54
|
const fn = async (code, text) => {
|
|
46
55
|
if (code === "emoji") {
|
|
47
|
-
return
|
|
56
|
+
return handleAsync(async () => {
|
|
57
|
+
const iconCode = getIconCode(text);
|
|
58
|
+
const emojiResponse = await loadEmoji(iconCode, emoji);
|
|
59
|
+
const svgText = await emojiResponse.text();
|
|
60
|
+
const base64Data = btoa(svgText);
|
|
61
|
+
return `data:image/svg+xml;base64,` + base64Data;
|
|
62
|
+
}, ErrorCodes.EMOJI_LOAD_FAILED, `Failed to process emoji: ${text}`);
|
|
48
63
|
}
|
|
49
64
|
};
|
|
50
65
|
return async (...args) => {
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export declare class ImageResponseError extends Error {
|
|
2
|
+
code: string;
|
|
3
|
+
originalError?: Error | undefined;
|
|
4
|
+
constructor(message: string, code: string, originalError?: Error | undefined);
|
|
5
|
+
}
|
|
6
|
+
/** Coerce anything thrown into an ImageResponseError, keeping existing ones as-is. */
|
|
7
|
+
export declare function toImageResponseError(error: unknown): ImageResponseError;
|
|
8
|
+
export declare const ErrorCodes: {
|
|
9
|
+
readonly FONT_LOAD_FAILED: "FONT_LOAD_FAILED";
|
|
10
|
+
readonly VNODE_CREATION_FAILED: "VNODE_CREATION_FAILED";
|
|
11
|
+
readonly SATORI_RENDER_FAILED: "SATORI_RENDER_FAILED";
|
|
12
|
+
readonly RESVG_INIT_FAILED: "RESVG_INIT_FAILED";
|
|
13
|
+
readonly RESVG_RENDER_FAILED: "RESVG_RENDER_FAILED";
|
|
14
|
+
readonly SATORI_INIT_FAILED: "SATORI_INIT_FAILED";
|
|
15
|
+
readonly EMOJI_LOAD_FAILED: "EMOJI_LOAD_FAILED";
|
|
16
|
+
readonly TAKUMI_INIT_FAILED: "TAKUMI_INIT_FAILED";
|
|
17
|
+
readonly TAKUMI_RENDER_FAILED: "TAKUMI_RENDER_FAILED";
|
|
18
|
+
readonly UNKNOWN_ERROR: "UNKNOWN_ERROR";
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Wraps an async operation with error handling and logging
|
|
22
|
+
*/
|
|
23
|
+
export declare function handleAsync<T = unknown>(operation: () => Promise<T>, errorCode: string, errorMessage: string): Promise<T>;
|
|
24
|
+
/**
|
|
25
|
+
* Wraps a sync operation with error handling and logging
|
|
26
|
+
*/
|
|
27
|
+
export declare function handleSync<T>(operation: () => T, errorCode: string, errorMessage: string): T;
|
|
28
|
+
/**
|
|
29
|
+
* Wraps multiple async operations with error handling
|
|
30
|
+
*/
|
|
31
|
+
export declare function handleAsyncAll<T extends readonly unknown[]>(operations: {
|
|
32
|
+
readonly [K in keyof T]: () => Promise<T[K]>;
|
|
33
|
+
}, errorCode: string, errorMessage: string): Promise<T>;
|
|
34
|
+
/**
|
|
35
|
+
* Validates a response and throws if not ok
|
|
36
|
+
*/
|
|
37
|
+
export declare function validateResponse(response: Response, errorCode: string, errorMessage: string): Promise<ArrayBuffer>;
|