@ethercorps/sveltekit-og 2.0.1 → 3.0.0-alpha.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.
Binary file
@@ -0,0 +1,11 @@
1
+ import type { SatoriOptions } from 'satori/wasm';
2
+ export interface ImageResponseOptions {
3
+ width?: number;
4
+ height?: number;
5
+ fonts?: SatoriOptions['fonts'];
6
+ debug?: boolean;
7
+ status?: number;
8
+ statusText?: string;
9
+ headers?: Record<string, string>;
10
+ format?: 'svg' | 'png';
11
+ }
Binary file
package/package.json CHANGED
@@ -1,38 +1,39 @@
1
1
  {
2
2
  "name": "@ethercorps/sveltekit-og",
3
- "version": "2.0.1",
3
+ "version": "3.0.0-alpha.1",
4
4
  "private": false,
5
+ "description": "Generate Open Graph Images dynamically from HTML/CSS without a browser in SvelteKit",
6
+ "type": "module",
7
+ "files": [
8
+ "dist"
9
+ ],
5
10
  "devDependencies": {
6
- "@playwright/test": "^1.35.0",
7
- "@sveltejs/adapter-auto": "next",
8
- "@sveltejs/adapter-vercel": "^2.4.3",
9
- "@sveltejs/kit": "1.10.0",
10
- "@sveltejs/package": "next",
11
- "@typescript-eslint/eslint-plugin": "^5.59.9",
12
- "@typescript-eslint/parser": "^5.59.9",
13
- "autoprefixer": "^10.4.14",
14
- "brace": "^0.11.1",
15
- "eslint": "^8.42.0",
16
- "eslint-config-prettier": "^8.8.0",
17
- "eslint-plugin-svelte3": "^4.0.0",
18
- "postcss": "^8.4.24",
19
- "prettier": "^2.8.8",
20
- "prettier-plugin-svelte": "^2.10.1",
21
- "prism-svelte": "^0.5.0",
22
- "prismjs": "^1.29.0",
23
- "svelte": "^3.59.1",
24
- "svelte-check": "^2.10.3",
25
- "svelte-preprocess": "^4.10.7",
26
- "tailwindcss": "^3.3.2",
27
- "tslib": "^2.5.3",
28
- "typescript": "^4.9.5",
29
- "vite": "^4.3.9"
11
+ "svelte": "^4.2.2",
12
+ "@types/node": "^20.5.1",
13
+ "esbuild": "^0.18.20",
14
+ "esbuild-plugin-d.ts": "^1.1.0",
15
+ "typescript": "^5.1.6"
30
16
  },
31
- "type": "module",
32
17
  "dependencies": {
33
- "satori": "^0.10.1",
34
- "svg2png-wasm": "^1.4.0"
18
+ "@ethercorps/svelte-h2j": "link:../svelte-h2j",
19
+ "@resvg/resvg-wasm": "^2.4.1",
20
+ "just-camel-case": "^6.2.0",
21
+ "satori": "^0.10.3",
22
+ "yoga-wasm-web": "^0.3.3"
23
+ },
24
+ "main": "./dist/index.js",
25
+ "module": "./dist/index.js",
26
+ "exports": {
27
+ ".": {
28
+ "import": "./dist/index.js",
29
+ "require": "./dist/index.js"
30
+ }
35
31
  },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/etherCorps/sveltekit-og.git"
35
+ },
36
+ "homepage": "https://github.com/etherCorps/sveltekit-og#readme",
36
37
  "keywords": [
37
38
  "open graph image",
38
39
  "open graph",
@@ -44,12 +45,13 @@
44
45
  "sveltekit-og",
45
46
  "@ethercorps/sveltekit-og"
46
47
  ],
48
+ "author": "theetherGit",
47
49
  "license": "MIT",
48
- "repository": "https://github.com/etherCorps/sveltekit-og",
49
- "homepage": "https://github.com/etherCorps/sveltekit-og#readme",
50
- "exports": {
51
- "./package.json": "./package.json",
52
- ".": "./index.js",
53
- "./toReactElement": "./toReactElement.js"
50
+ "bugs": {
51
+ "url": "https://github.com/etherCorps/sveltekit-og/issues"
52
+ },
53
+ "scripts": {
54
+ "build": "node bin/esbuild.js",
55
+ "ts": "tsc --noEmit"
54
56
  }
55
57
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Ether Corps
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
package/index.d.ts DELETED
@@ -1,17 +0,0 @@
1
- import { type SatoriOptions } from 'satori';
2
- import type { SvelteComponent } from 'svelte';
3
- import toReactElement from './toReactElement.js';
4
- declare const ImageResponse: (htmlTemplate: string, optionsByUser: ImageResponseOptions) => Promise<Response>;
5
- declare const componentToImageResponse: (component: typeof SvelteComponent, props: {} | undefined, optionsByUser: ImageResponseOptions) => Promise<Response>;
6
- type ImageResponseOptions = ConstructorParameters<typeof Response>[1] & ImageOptions;
7
- type ImageOptions = {
8
- width?: number;
9
- height?: number;
10
- debug?: boolean;
11
- fonts?: SatoriOptions['fonts'];
12
- backgroundColor?: string;
13
- graphemeImages?: Record<string, string>;
14
- loadAdditionalAsset?: (languageCode: string, segment: string) => Promise<SatoriOptions['fonts'] | string | undefined>;
15
- };
16
- export type ImageResponseType = typeof ImageResponse;
17
- export { componentToImageResponse, ImageResponse, toReactElement };
package/index.js DELETED
@@ -1,62 +0,0 @@
1
- import satori, {} from 'satori';
2
- import toReactElement from './toReactElement.js';
3
- import { svg2png, initialize } from 'svg2png-wasm';
4
- let initialized = false;
5
- const fontFile = await fetch('https://sveltekit-og.ethercorps.io/noto-sans.ttf');
6
- const fontData = await fontFile.arrayBuffer();
7
- const indexWasmRes = await fetch('https://unpkg.com/svg2png-wasm/svg2png_wasm_bg.wasm');
8
- const svg2PngWasmBuffer = await indexWasmRes.arrayBuffer();
9
- const initSvgToPng = async () => {
10
- await initialize(svg2PngWasmBuffer).catch((e) => console.log(e));
11
- initialized = true;
12
- };
13
- const ImageResponse = async (htmlTemplate, optionsByUser) => {
14
- const options = Object.assign({ width: 1200, height: 630, debug: !1 }, optionsByUser);
15
- const svg = await satori(toReactElement(htmlTemplate), {
16
- width: options.width,
17
- height: options.height,
18
- debug: options.debug,
19
- fonts: options.fonts || [
20
- {
21
- name: 'sans serif',
22
- data: fontData,
23
- style: 'normal',
24
- weight: 700
25
- }
26
- ]
27
- });
28
- if (!initialized) {
29
- await initSvgToPng();
30
- initialized = true;
31
- }
32
- const defaultConfig = {
33
- width: options.width,
34
- height: options.height // optional
35
- };
36
- if (Object.hasOwn(options, 'backgroundColor')) {
37
- defaultConfig.backgroundColor = options.backgroundColor;
38
- }
39
- const png = await svg2png(svg, defaultConfig);
40
- return new Response(png, {
41
- headers: {
42
- 'Content-Type': 'image/png',
43
- 'cache-control': 'public, immutable, no-transform, max-age=31536000',
44
- ...options.headers
45
- },
46
- status: options.status,
47
- statusText: options.statusText
48
- });
49
- };
50
- const componentToImageResponse = (component, props = {}, optionsByUser) => {
51
- const htmlTemplate = componentToMarkup(component, props);
52
- return ImageResponse(htmlTemplate, optionsByUser);
53
- };
54
- const componentToMarkup = (component, props = {}) => {
55
- const SvelteRenderedMarkup = component.render(props);
56
- let htmlTemplate = `${SvelteRenderedMarkup.html}`;
57
- if (SvelteRenderedMarkup && SvelteRenderedMarkup.css && SvelteRenderedMarkup.css.code) {
58
- htmlTemplate = `${SvelteRenderedMarkup.html}<style>${SvelteRenderedMarkup.css.code}</style>`;
59
- }
60
- return htmlTemplate;
61
- };
62
- export { componentToImageResponse, ImageResponse, toReactElement };
@@ -1,10 +0,0 @@
1
- interface VNode {
2
- type: string;
3
- props: {
4
- style?: Record<string, any>;
5
- children?: string | VNode | VNode[];
6
- [prop: string]: any;
7
- };
8
- }
9
- export declare const toReactElement: (htmlString: string) => VNode;
10
- export default toReactElement;
package/toReactElement.js DELETED
@@ -1,100 +0,0 @@
1
- import { parse, walk } from 'svelte/compiler';
2
- /* Start of code from satori-html for cssToObject converter*/
3
- const camelize = (ident) => ident.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
4
- const cssToObject = (str) => {
5
- const obj = {};
6
- let t = 0;
7
- let pair = ['', ''];
8
- const flags = {};
9
- for (const c of str) {
10
- if (!flags['('] && c === ':') {
11
- t = 1;
12
- }
13
- else if (c === ';') {
14
- const [decl = '', value = ''] = pair;
15
- obj[camelize(decl.trim())] = value.trim();
16
- t = 0;
17
- pair = ['', ''];
18
- }
19
- else {
20
- pair[t] += c;
21
- switch (c) {
22
- case '(': {
23
- flags[c]++;
24
- break;
25
- }
26
- case ')': {
27
- flags['(']--;
28
- break;
29
- }
30
- }
31
- }
32
- }
33
- const [decl = '', value = ''] = pair;
34
- if (decl.trim() && value.trim()) {
35
- obj[camelize(decl.trim())] = value.trim();
36
- }
37
- return obj;
38
- };
39
- const nodeMap = new WeakMap();
40
- const root = {
41
- type: 'div',
42
- props: {
43
- style: {
44
- display: 'flex',
45
- flexDirection: 'column',
46
- width: '100%',
47
- height: '100%'
48
- },
49
- children: []
50
- }
51
- };
52
- /* End of satori-html */
53
- export const toReactElement = (htmlString) => {
54
- const svelteAST = parse(htmlString);
55
- walk(svelteAST, {
56
- enter(node, parent, prop, index) {
57
- let newNode = {};
58
- if (node.type === 'Fragment') {
59
- nodeMap.set(node, root);
60
- }
61
- else if (node.type === 'Element') {
62
- newNode.type = node.name;
63
- const { ...props } = node.attributes;
64
- if (node.attributes.length > 0) {
65
- node.attributes.forEach((attribute) => {
66
- if (attribute.name === 'style') {
67
- props['style'] = cssToObject(attribute.value[0].data);
68
- }
69
- else
70
- props[attribute.name] = attribute.value[0].data;
71
- });
72
- delete props[0];
73
- }
74
- props.children = [];
75
- Object.assign(newNode, { props });
76
- nodeMap.set(node, newNode);
77
- if (parent) {
78
- const newParent = nodeMap.get(parent);
79
- newParent.props.children[index] = newNode;
80
- }
81
- }
82
- else if (node.type === 'Text') {
83
- newNode = node.data.trim();
84
- if (newNode) {
85
- if (parent && parent.type !== 'Attribute') {
86
- const newParent = nodeMap.get(parent);
87
- if (parent.children.length === 1) {
88
- newParent.props.children = newNode;
89
- }
90
- else {
91
- newParent.props.children[index] = newNode;
92
- }
93
- }
94
- }
95
- }
96
- }
97
- });
98
- return root;
99
- };
100
- export default toReactElement;