cf-workers-og 1.0.1 → 2.0.0
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 +76 -84
- package/dist/compat.d.ts +11 -0
- package/dist/compat.js +20 -0
- package/dist/compat.js.map +1 -0
- package/dist/compat.node.d.ts +9 -0
- package/dist/compat.node.js +20 -0
- package/dist/compat.node.js.map +1 -0
- package/dist/core/image-response.d.ts +34 -0
- package/dist/fonts.d.ts +1 -51
- package/dist/fonts.node.d.ts +1 -51
- package/dist/fonts.shared-RU0Lt2Ku.js +135 -0
- package/dist/fonts.shared-RU0Lt2Ku.js.map +1 -0
- package/dist/fonts.shared.d.ts +17 -0
- package/dist/html.d.ts +11 -0
- package/dist/html.js +19 -0
- package/dist/html.js.map +1 -0
- package/dist/html.node.d.ts +9 -0
- package/dist/html.node.js +19 -0
- package/dist/html.node.js.map +1 -0
- package/dist/image-response.d.ts +1 -62
- package/dist/image-response.node.d.ts +1 -62
- package/dist/index.d.ts +0 -1
- package/dist/index.js +7 -122
- package/dist/index.js.map +1 -1
- package/dist/index.node.d.ts +0 -1
- package/dist/index.node.js +7 -122
- package/dist/index.node.js.map +1 -1
- package/dist/types.d.ts +17 -3
- package/package.json +53 -1
package/dist/html.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"html.js","sources":["../src/html.ts"],"sourcesContent":["/**\n * cf-workers-og (HTML entry point)\n *\n * Includes HTML string parsing support without legacy constructor behavior.\n */\n\nimport { ImageResponse as CfImageResponse } from \"@cf-wasm/og/workerd\";\nimport type { ReactNode } from \"react\";\nimport { createImageResponseClass } from \"./core/image-response\";\nimport { parseHtml } from \"./html-parser\";\n\nexport { cache } from \"@cf-wasm/og/workerd\";\n\nexport const ImageResponse = createImageResponseClass<ReactNode | string>({\n cfImageResponse: CfImageResponse,\n parseHtml,\n});\n\nexport { parseHtml } from \"./html-parser\";\n\nexport { GoogleFont, CustomFont, loadGoogleFont, createFontConfig } from \"./fonts\";\n\nexport type {\n ImageResponseOptions,\n FontConfig,\n FontWeight,\n FontStyle,\n EmojiType,\n GoogleFontOptions,\n} from \"./types\";\n"],"names":["CfImageResponse"],"mappings":";;;;;AAaO,MAAM,gBAAgB,yBAA6C;AAAA,EACxE,iBAAiBA;AAAAA,EACjB;AACF,CAAC;"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cf-workers-og (Node.js HTML entry point)
|
|
3
|
+
*/
|
|
4
|
+
import type { ReactNode } from "react";
|
|
5
|
+
export { cache } from "@cf-wasm/og/node";
|
|
6
|
+
export declare const ImageResponse: import("./core/image-response").ImageResponseClass<ReactNode>;
|
|
7
|
+
export { parseHtml } from "./html-parser";
|
|
8
|
+
export { GoogleFont, CustomFont, loadGoogleFont, createFontConfig } from "./fonts.node";
|
|
9
|
+
export type { ImageResponseOptions, FontConfig, FontWeight, FontStyle, EmojiType, GoogleFontOptions, } from "./types";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ImageResponse as ImageResponse$1 } from "@cf-wasm/og/node";
|
|
2
|
+
import { CustomFont, GoogleFont, cache } from "@cf-wasm/og/node";
|
|
3
|
+
import { c as createImageResponseClass } from "./fonts.shared-RU0Lt2Ku.js";
|
|
4
|
+
import { a, l } from "./fonts.shared-RU0Lt2Ku.js";
|
|
5
|
+
import { p as parseHtml } from "./html-parser-DRzlsDtB.js";
|
|
6
|
+
const ImageResponse = createImageResponseClass({
|
|
7
|
+
cfImageResponse: ImageResponse$1,
|
|
8
|
+
parseHtml
|
|
9
|
+
});
|
|
10
|
+
export {
|
|
11
|
+
CustomFont,
|
|
12
|
+
GoogleFont,
|
|
13
|
+
ImageResponse,
|
|
14
|
+
cache,
|
|
15
|
+
a as createFontConfig,
|
|
16
|
+
l as loadGoogleFont,
|
|
17
|
+
parseHtml
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=html.node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"html.node.js","sources":["../src/html.node.ts"],"sourcesContent":["/**\n * cf-workers-og (Node.js HTML entry point)\n */\n\nimport { ImageResponse as CfImageResponse } from \"@cf-wasm/og/node\";\nimport type { ReactNode } from \"react\";\nimport { createImageResponseClass } from \"./core/image-response\";\nimport { parseHtml } from \"./html-parser\";\n\nexport { cache } from \"@cf-wasm/og/node\";\n\nexport const ImageResponse = createImageResponseClass<ReactNode | string>({\n cfImageResponse: CfImageResponse,\n parseHtml,\n});\n\nexport { parseHtml } from \"./html-parser\";\n\nexport { GoogleFont, CustomFont, loadGoogleFont, createFontConfig } from \"./fonts.node\";\n\nexport type {\n ImageResponseOptions,\n FontConfig,\n FontWeight,\n FontStyle,\n EmojiType,\n GoogleFontOptions,\n} from \"./types\";\n"],"names":["CfImageResponse"],"mappings":";;;;;AAWO,MAAM,gBAAgB,yBAA6C;AAAA,EACxE,iBAAiBA;AAAAA,EACjB;AACF,CAAC;"}
|
package/dist/image-response.d.ts
CHANGED
|
@@ -1,64 +1,3 @@
|
|
|
1
1
|
import type { ReactNode } from "react";
|
|
2
|
-
import type { ImageResponseOptions } from "./types";
|
|
3
2
|
export { cache } from "@cf-wasm/og/workerd";
|
|
4
|
-
|
|
5
|
-
* Generate an OG image Response from a React element or HTML string.
|
|
6
|
-
*
|
|
7
|
-
* This is a wrapper around @cf-wasm/og that provides:
|
|
8
|
-
* - Backwards compatibility with workers-og API
|
|
9
|
-
* - HTML string parsing support
|
|
10
|
-
* - Simplified options interface
|
|
11
|
-
*
|
|
12
|
-
* @example JSX usage (recommended):
|
|
13
|
-
* ```tsx
|
|
14
|
-
* import { ImageResponse, cache } from 'cf-workers-og';
|
|
15
|
-
*
|
|
16
|
-
* export default {
|
|
17
|
-
* async fetch(request, env, ctx) {
|
|
18
|
-
* cache.setExecutionContext(ctx);
|
|
19
|
-
*
|
|
20
|
-
* return ImageResponse.create(
|
|
21
|
-
* <div style={{ display: 'flex', background: '#000' }}>
|
|
22
|
-
* <h1 style={{ color: 'white' }}>Hello World</h1>
|
|
23
|
-
* </div>,
|
|
24
|
-
* { width: 1200, height: 630 }
|
|
25
|
-
* );
|
|
26
|
-
* }
|
|
27
|
-
* };
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @example HTML string usage:
|
|
31
|
-
* ```typescript
|
|
32
|
-
* import { ImageResponse, parseHtml } from 'cf-workers-og';
|
|
33
|
-
*
|
|
34
|
-
* const html = '<div style="display: flex;"><h1>Hello</h1></div>';
|
|
35
|
-
* return ImageResponse.create(parseHtml(html), options);
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
export declare class ImageResponse extends Response {
|
|
39
|
-
/**
|
|
40
|
-
* Create an OG image Response (async, recommended).
|
|
41
|
-
*
|
|
42
|
-
* @param element - React element or HTML string to render
|
|
43
|
-
* @param options - Image generation options
|
|
44
|
-
* @returns Promise<Response> with the generated image
|
|
45
|
-
*/
|
|
46
|
-
static create(element: ReactNode | string, options?: ImageResponseOptions): Promise<Response>;
|
|
47
|
-
/**
|
|
48
|
-
* Constructor for backwards compatibility with workers-og.
|
|
49
|
-
*
|
|
50
|
-
* Note: This returns a Promise, not an ImageResponse instance.
|
|
51
|
-
* For TypeScript, use `ImageResponse.create()` instead.
|
|
52
|
-
*
|
|
53
|
-
* @param element - React element or HTML string to render
|
|
54
|
-
* @param options - Image generation options
|
|
55
|
-
* @returns Response (via Promise trick for constructor)
|
|
56
|
-
*
|
|
57
|
-
* @example
|
|
58
|
-
* ```typescript
|
|
59
|
-
* // Works like old workers-og
|
|
60
|
-
* return new ImageResponse(element, options);
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
constructor(element: ReactNode | string, options?: ImageResponseOptions);
|
|
64
|
-
}
|
|
3
|
+
export declare const ImageResponse: import("./core/image-response").ImageResponseClass<ReactNode>;
|
|
@@ -1,64 +1,3 @@
|
|
|
1
1
|
import type { ReactNode } from "react";
|
|
2
|
-
import type { ImageResponseOptions } from "./types";
|
|
3
2
|
export { cache } from "@cf-wasm/og/node";
|
|
4
|
-
|
|
5
|
-
* Generate an OG image Response from a React element or HTML string.
|
|
6
|
-
*
|
|
7
|
-
* This is a wrapper around @cf-wasm/og that provides:
|
|
8
|
-
* - Backwards compatibility with workers-og API
|
|
9
|
-
* - HTML string parsing support
|
|
10
|
-
* - Simplified options interface
|
|
11
|
-
*
|
|
12
|
-
* @example JSX usage (recommended):
|
|
13
|
-
* ```tsx
|
|
14
|
-
* import { ImageResponse, cache } from 'cf-workers-og';
|
|
15
|
-
*
|
|
16
|
-
* export default {
|
|
17
|
-
* async fetch(request, env, ctx) {
|
|
18
|
-
* cache.setExecutionContext(ctx);
|
|
19
|
-
*
|
|
20
|
-
* return ImageResponse.create(
|
|
21
|
-
* <div style={{ display: 'flex', background: '#000' }}>
|
|
22
|
-
* <h1 style={{ color: 'white' }}>Hello World</h1>
|
|
23
|
-
* </div>,
|
|
24
|
-
* { width: 1200, height: 630 }
|
|
25
|
-
* );
|
|
26
|
-
* }
|
|
27
|
-
* };
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @example HTML string usage:
|
|
31
|
-
* ```typescript
|
|
32
|
-
* import { ImageResponse, parseHtml } from 'cf-workers-og';
|
|
33
|
-
*
|
|
34
|
-
* const html = '<div style="display: flex;"><h1>Hello</h1></div>';
|
|
35
|
-
* return ImageResponse.create(parseHtml(html), options);
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
export declare class ImageResponse extends Response {
|
|
39
|
-
/**
|
|
40
|
-
* Create an OG image Response (async, recommended).
|
|
41
|
-
*
|
|
42
|
-
* @param element - React element or HTML string to render
|
|
43
|
-
* @param options - Image generation options
|
|
44
|
-
* @returns Promise<Response> with the generated image
|
|
45
|
-
*/
|
|
46
|
-
static create(element: ReactNode | string, options?: ImageResponseOptions): Promise<Response>;
|
|
47
|
-
/**
|
|
48
|
-
* Constructor for backwards compatibility with workers-og.
|
|
49
|
-
*
|
|
50
|
-
* Note: This returns a Promise, not an ImageResponse instance.
|
|
51
|
-
* For TypeScript, use `ImageResponse.create()` instead.
|
|
52
|
-
*
|
|
53
|
-
* @param element - React element or HTML string to render
|
|
54
|
-
* @param options - Image generation options
|
|
55
|
-
* @returns Response (via Promise trick for constructor)
|
|
56
|
-
*
|
|
57
|
-
* @example
|
|
58
|
-
* ```typescript
|
|
59
|
-
* // Works like old workers-og
|
|
60
|
-
* return new ImageResponse(element, options);
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
constructor(element: ReactNode | string, options?: ImageResponseOptions);
|
|
64
|
-
}
|
|
3
|
+
export declare const ImageResponse: import("./core/image-response").ImageResponseClass<ReactNode>;
|
package/dist/index.d.ts
CHANGED
|
@@ -28,6 +28,5 @@
|
|
|
28
28
|
* @packageDocumentation
|
|
29
29
|
*/
|
|
30
30
|
export { ImageResponse, cache } from "./image-response";
|
|
31
|
-
export { parseHtml } from "./html-parser";
|
|
32
31
|
export { GoogleFont, CustomFont, loadGoogleFont, createFontConfig } from "./fonts";
|
|
33
32
|
export type { ImageResponseOptions, FontConfig, FontWeight, FontStyle, EmojiType, GoogleFontOptions, } from "./types";
|
package/dist/index.js
CHANGED
|
@@ -1,131 +1,16 @@
|
|
|
1
1
|
import { ImageResponse as ImageResponse$1 } from "@cf-wasm/og/workerd";
|
|
2
2
|
import { CustomFont, GoogleFont, cache } from "@cf-wasm/og/workerd";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* @param element - React element or HTML string to render
|
|
9
|
-
* @param options - Image generation options
|
|
10
|
-
* @returns Promise<Response> with the generated image
|
|
11
|
-
*/
|
|
12
|
-
static async create(element, options = {}) {
|
|
13
|
-
const reactElement = typeof element === "string" ? parseHtml(element) : element;
|
|
14
|
-
const {
|
|
15
|
-
width = 1200,
|
|
16
|
-
height = 630,
|
|
17
|
-
format = "png",
|
|
18
|
-
fonts,
|
|
19
|
-
emoji,
|
|
20
|
-
debug = false,
|
|
21
|
-
headers = {},
|
|
22
|
-
status = 200,
|
|
23
|
-
statusText
|
|
24
|
-
} = options;
|
|
25
|
-
const response = await ImageResponse$1.async(reactElement, {
|
|
26
|
-
width,
|
|
27
|
-
height,
|
|
28
|
-
format,
|
|
29
|
-
fonts: fonts ?? [],
|
|
30
|
-
emoji
|
|
31
|
-
});
|
|
32
|
-
const responseHeaders = new Headers(response.headers);
|
|
33
|
-
responseHeaders.set(
|
|
34
|
-
"Content-Type",
|
|
35
|
-
format === "svg" ? "image/svg+xml" : "image/png"
|
|
36
|
-
);
|
|
37
|
-
responseHeaders.set(
|
|
38
|
-
"Cache-Control",
|
|
39
|
-
debug ? "no-cache, no-store" : "public, immutable, no-transform, max-age=31536000"
|
|
40
|
-
);
|
|
41
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
42
|
-
responseHeaders.set(key, value);
|
|
43
|
-
}
|
|
44
|
-
return new Response(response.body, {
|
|
45
|
-
headers: responseHeaders,
|
|
46
|
-
status,
|
|
47
|
-
statusText
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Constructor for backwards compatibility with workers-og.
|
|
52
|
-
*
|
|
53
|
-
* Note: This returns a Promise, not an ImageResponse instance.
|
|
54
|
-
* For TypeScript, use `ImageResponse.create()` instead.
|
|
55
|
-
*
|
|
56
|
-
* @param element - React element or HTML string to render
|
|
57
|
-
* @param options - Image generation options
|
|
58
|
-
* @returns Response (via Promise trick for constructor)
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* ```typescript
|
|
62
|
-
* // Works like old workers-og
|
|
63
|
-
* return new ImageResponse(element, options);
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
constructor(element, options = {}) {
|
|
67
|
-
super(null);
|
|
68
|
-
return ImageResponse.create(element, options);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
async function loadGoogleFont(options) {
|
|
72
|
-
const { family, weight, text } = options;
|
|
73
|
-
const params = {
|
|
74
|
-
family: `${encodeURIComponent(family)}${weight ? `:wght@${weight}` : ""}`
|
|
75
|
-
};
|
|
76
|
-
if (text) {
|
|
77
|
-
params.text = text;
|
|
78
|
-
} else {
|
|
79
|
-
params.subset = "latin";
|
|
80
|
-
}
|
|
81
|
-
const cssUrl = `https://fonts.googleapis.com/css2?${Object.keys(params).map((key) => `${key}=${params[key]}`).join("&")}`;
|
|
82
|
-
const cfCache = typeof caches !== "undefined" ? caches.default : void 0;
|
|
83
|
-
let cssResponse;
|
|
84
|
-
if (cfCache) {
|
|
85
|
-
cssResponse = await cfCache.match(cssUrl);
|
|
86
|
-
}
|
|
87
|
-
if (!cssResponse) {
|
|
88
|
-
cssResponse = await fetch(cssUrl, {
|
|
89
|
-
headers: {
|
|
90
|
-
// Request TTF format (works better with Satori)
|
|
91
|
-
"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"
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
if (cfCache) {
|
|
95
|
-
const cacheResponse = new Response(cssResponse.body, cssResponse);
|
|
96
|
-
cacheResponse.headers.set("Cache-Control", "s-maxage=3600");
|
|
97
|
-
await cfCache.put(cssUrl, cacheResponse.clone());
|
|
98
|
-
cssResponse = cacheResponse;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
const css = await cssResponse.text();
|
|
102
|
-
const fontUrlMatch = css.match(
|
|
103
|
-
/src: url\(([^)]+)\) format\(['"]?(opentype|truetype)['"]?\)/
|
|
104
|
-
);
|
|
105
|
-
if (!fontUrlMatch?.[1]) {
|
|
106
|
-
throw new Error(
|
|
107
|
-
`Could not find font URL for "${family}" (weight: ${weight ?? "default"})`
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
const fontUrl = fontUrlMatch[1];
|
|
111
|
-
const fontResponse = await fetch(fontUrl);
|
|
112
|
-
return fontResponse.arrayBuffer();
|
|
113
|
-
}
|
|
114
|
-
function createFontConfig(name, data, weight = 400, style = "normal") {
|
|
115
|
-
return {
|
|
116
|
-
name,
|
|
117
|
-
data,
|
|
118
|
-
weight,
|
|
119
|
-
style
|
|
120
|
-
};
|
|
121
|
-
}
|
|
3
|
+
import { c as createImageResponseClass } from "./fonts.shared-RU0Lt2Ku.js";
|
|
4
|
+
import { a, l } from "./fonts.shared-RU0Lt2Ku.js";
|
|
5
|
+
const ImageResponse = createImageResponseClass({
|
|
6
|
+
cfImageResponse: ImageResponse$1
|
|
7
|
+
});
|
|
122
8
|
export {
|
|
123
9
|
CustomFont,
|
|
124
10
|
GoogleFont,
|
|
125
11
|
ImageResponse,
|
|
126
12
|
cache,
|
|
127
|
-
createFontConfig,
|
|
128
|
-
loadGoogleFont
|
|
129
|
-
parseHtml
|
|
13
|
+
a as createFontConfig,
|
|
14
|
+
l as loadGoogleFont
|
|
130
15
|
};
|
|
131
16
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/image-response.ts"
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/image-response.ts"],"sourcesContent":["import { ImageResponse as CfImageResponse } from \"@cf-wasm/og/workerd\";\nimport type { ReactNode } from \"react\";\nimport { createImageResponseClass } from \"./core/image-response\";\n\nexport { cache } from \"@cf-wasm/og/workerd\";\n\nexport const ImageResponse = createImageResponseClass<ReactNode>({\n cfImageResponse: CfImageResponse,\n});\n"],"names":["CfImageResponse"],"mappings":";;;;AAMO,MAAM,gBAAgB,yBAAoC;AAAA,EAC/D,iBAAiBA;AACnB,CAAC;"}
|
package/dist/index.node.d.ts
CHANGED
|
@@ -7,6 +7,5 @@
|
|
|
7
7
|
* @packageDocumentation
|
|
8
8
|
*/
|
|
9
9
|
export { ImageResponse, cache } from "./image-response.node";
|
|
10
|
-
export { parseHtml } from "./html-parser";
|
|
11
10
|
export { GoogleFont, CustomFont, loadGoogleFont, createFontConfig } from "./fonts.node";
|
|
12
11
|
export type { ImageResponseOptions, FontConfig, FontWeight, FontStyle, EmojiType, GoogleFontOptions, } from "./types";
|
package/dist/index.node.js
CHANGED
|
@@ -1,131 +1,16 @@
|
|
|
1
1
|
import { ImageResponse as ImageResponse$1 } from "@cf-wasm/og/node";
|
|
2
2
|
import { CustomFont, GoogleFont, cache } from "@cf-wasm/og/node";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* @param element - React element or HTML string to render
|
|
9
|
-
* @param options - Image generation options
|
|
10
|
-
* @returns Promise<Response> with the generated image
|
|
11
|
-
*/
|
|
12
|
-
static async create(element, options = {}) {
|
|
13
|
-
const reactElement = typeof element === "string" ? parseHtml(element) : element;
|
|
14
|
-
const {
|
|
15
|
-
width = 1200,
|
|
16
|
-
height = 630,
|
|
17
|
-
format = "png",
|
|
18
|
-
fonts,
|
|
19
|
-
emoji,
|
|
20
|
-
debug = false,
|
|
21
|
-
headers = {},
|
|
22
|
-
status = 200,
|
|
23
|
-
statusText
|
|
24
|
-
} = options;
|
|
25
|
-
const response = await ImageResponse$1.async(reactElement, {
|
|
26
|
-
width,
|
|
27
|
-
height,
|
|
28
|
-
format,
|
|
29
|
-
fonts: fonts ?? [],
|
|
30
|
-
emoji
|
|
31
|
-
});
|
|
32
|
-
const responseHeaders = new Headers(response.headers);
|
|
33
|
-
responseHeaders.set(
|
|
34
|
-
"Content-Type",
|
|
35
|
-
format === "svg" ? "image/svg+xml" : "image/png"
|
|
36
|
-
);
|
|
37
|
-
responseHeaders.set(
|
|
38
|
-
"Cache-Control",
|
|
39
|
-
debug ? "no-cache, no-store" : "public, immutable, no-transform, max-age=31536000"
|
|
40
|
-
);
|
|
41
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
42
|
-
responseHeaders.set(key, value);
|
|
43
|
-
}
|
|
44
|
-
return new Response(response.body, {
|
|
45
|
-
headers: responseHeaders,
|
|
46
|
-
status,
|
|
47
|
-
statusText
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Constructor for backwards compatibility with workers-og.
|
|
52
|
-
*
|
|
53
|
-
* Note: This returns a Promise, not an ImageResponse instance.
|
|
54
|
-
* For TypeScript, use `ImageResponse.create()` instead.
|
|
55
|
-
*
|
|
56
|
-
* @param element - React element or HTML string to render
|
|
57
|
-
* @param options - Image generation options
|
|
58
|
-
* @returns Response (via Promise trick for constructor)
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* ```typescript
|
|
62
|
-
* // Works like old workers-og
|
|
63
|
-
* return new ImageResponse(element, options);
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
constructor(element, options = {}) {
|
|
67
|
-
super(null);
|
|
68
|
-
return ImageResponse.create(element, options);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
async function loadGoogleFont(options) {
|
|
72
|
-
const { family, weight, text } = options;
|
|
73
|
-
const params = {
|
|
74
|
-
family: `${encodeURIComponent(family)}${weight ? `:wght@${weight}` : ""}`
|
|
75
|
-
};
|
|
76
|
-
if (text) {
|
|
77
|
-
params.text = text;
|
|
78
|
-
} else {
|
|
79
|
-
params.subset = "latin";
|
|
80
|
-
}
|
|
81
|
-
const cssUrl = `https://fonts.googleapis.com/css2?${Object.keys(params).map((key) => `${key}=${params[key]}`).join("&")}`;
|
|
82
|
-
const cfCache = typeof caches !== "undefined" ? caches.default : void 0;
|
|
83
|
-
let cssResponse;
|
|
84
|
-
if (cfCache) {
|
|
85
|
-
cssResponse = await cfCache.match(cssUrl);
|
|
86
|
-
}
|
|
87
|
-
if (!cssResponse) {
|
|
88
|
-
cssResponse = await fetch(cssUrl, {
|
|
89
|
-
headers: {
|
|
90
|
-
// Request TTF format (works better with Satori)
|
|
91
|
-
"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"
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
if (cfCache) {
|
|
95
|
-
const cacheResponse = new Response(cssResponse.body, cssResponse);
|
|
96
|
-
cacheResponse.headers.set("Cache-Control", "s-maxage=3600");
|
|
97
|
-
await cfCache.put(cssUrl, cacheResponse.clone());
|
|
98
|
-
cssResponse = cacheResponse;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
const css = await cssResponse.text();
|
|
102
|
-
const fontUrlMatch = css.match(
|
|
103
|
-
/src: url\(([^)]+)\) format\(['"]?(opentype|truetype)['"]?\)/
|
|
104
|
-
);
|
|
105
|
-
if (!fontUrlMatch?.[1]) {
|
|
106
|
-
throw new Error(
|
|
107
|
-
`Could not find font URL for "${family}" (weight: ${weight ?? "default"})`
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
const fontUrl = fontUrlMatch[1];
|
|
111
|
-
const fontResponse = await fetch(fontUrl);
|
|
112
|
-
return fontResponse.arrayBuffer();
|
|
113
|
-
}
|
|
114
|
-
function createFontConfig(name, data, weight = 400, style = "normal") {
|
|
115
|
-
return {
|
|
116
|
-
name,
|
|
117
|
-
data,
|
|
118
|
-
weight,
|
|
119
|
-
style
|
|
120
|
-
};
|
|
121
|
-
}
|
|
3
|
+
import { c as createImageResponseClass } from "./fonts.shared-RU0Lt2Ku.js";
|
|
4
|
+
import { a, l } from "./fonts.shared-RU0Lt2Ku.js";
|
|
5
|
+
const ImageResponse = createImageResponseClass({
|
|
6
|
+
cfImageResponse: ImageResponse$1
|
|
7
|
+
});
|
|
122
8
|
export {
|
|
123
9
|
CustomFont,
|
|
124
10
|
GoogleFont,
|
|
125
11
|
ImageResponse,
|
|
126
12
|
cache,
|
|
127
|
-
createFontConfig,
|
|
128
|
-
loadGoogleFont
|
|
129
|
-
parseHtml
|
|
13
|
+
a as createFontConfig,
|
|
14
|
+
l as loadGoogleFont
|
|
130
15
|
};
|
|
131
16
|
//# sourceMappingURL=index.node.js.map
|
package/dist/index.node.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.node.js","sources":["../src/image-response.node.ts"
|
|
1
|
+
{"version":3,"file":"index.node.js","sources":["../src/image-response.node.ts"],"sourcesContent":["import { ImageResponse as CfImageResponse } from \"@cf-wasm/og/node\";\nimport type { ReactNode } from \"react\";\nimport { createImageResponseClass } from \"./core/image-response\";\n\nexport { cache } from \"@cf-wasm/og/node\";\n\nexport const ImageResponse = createImageResponseClass<ReactNode>({\n cfImageResponse: CfImageResponse,\n});\n"],"names":["CfImageResponse"],"mappings":";;;;AAMO,MAAM,gBAAgB,yBAAoC;AAAA,EAC/D,iBAAiBA;AACnB,CAAC;"}
|
package/dist/types.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export type FontWeight = 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
|
|
|
11
11
|
*/
|
|
12
12
|
export type FontStyle = "normal" | "italic";
|
|
13
13
|
/**
|
|
14
|
-
* Font configuration for
|
|
14
|
+
* Font configuration for manually loaded fonts
|
|
15
15
|
*/
|
|
16
16
|
export interface FontConfig {
|
|
17
17
|
name: string;
|
|
@@ -19,6 +19,19 @@ export interface FontConfig {
|
|
|
19
19
|
weight?: FontWeight;
|
|
20
20
|
style?: FontStyle;
|
|
21
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Font configuration with async data loading (used by GoogleFont/CustomFont classes)
|
|
24
|
+
*/
|
|
25
|
+
export interface AsyncFontConfig {
|
|
26
|
+
name: string;
|
|
27
|
+
data: ArrayBuffer | Promise<ArrayBuffer>;
|
|
28
|
+
weight?: FontWeight;
|
|
29
|
+
style?: FontStyle;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Union type for all supported font configurations
|
|
33
|
+
*/
|
|
34
|
+
export type FontInput = FontConfig | AsyncFontConfig;
|
|
22
35
|
/**
|
|
23
36
|
* Options for ImageResponse
|
|
24
37
|
*/
|
|
@@ -39,9 +52,10 @@ export interface ImageResponseOptions {
|
|
|
39
52
|
*/
|
|
40
53
|
format?: "png" | "svg";
|
|
41
54
|
/**
|
|
42
|
-
* Fonts to use for rendering text
|
|
55
|
+
* Fonts to use for rendering text.
|
|
56
|
+
* Accepts FontConfig (manual loading) or GoogleFont/CustomFont classes.
|
|
43
57
|
*/
|
|
44
|
-
fonts?:
|
|
58
|
+
fonts?: FontInput[];
|
|
45
59
|
/**
|
|
46
60
|
* Emoji provider for rendering emoji
|
|
47
61
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cf-workers-og",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Generate Open Graph images on Cloudflare Workers with Vite support",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -20,6 +20,58 @@
|
|
|
20
20
|
"types": "./dist/index.d.ts",
|
|
21
21
|
"import": "./dist/index.js"
|
|
22
22
|
}
|
|
23
|
+
},
|
|
24
|
+
"./node": {
|
|
25
|
+
"types": "./dist/index.node.d.ts",
|
|
26
|
+
"import": "./dist/index.node.js"
|
|
27
|
+
},
|
|
28
|
+
"./workerd": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./html": {
|
|
33
|
+
"node": {
|
|
34
|
+
"types": "./dist/html.node.d.ts",
|
|
35
|
+
"import": "./dist/html.node.js"
|
|
36
|
+
},
|
|
37
|
+
"workerd": {
|
|
38
|
+
"types": "./dist/html.d.ts",
|
|
39
|
+
"import": "./dist/html.js"
|
|
40
|
+
},
|
|
41
|
+
"default": {
|
|
42
|
+
"types": "./dist/html.d.ts",
|
|
43
|
+
"import": "./dist/html.js"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"./node/html": {
|
|
47
|
+
"types": "./dist/html.node.d.ts",
|
|
48
|
+
"import": "./dist/html.node.js"
|
|
49
|
+
},
|
|
50
|
+
"./workerd/html": {
|
|
51
|
+
"types": "./dist/html.d.ts",
|
|
52
|
+
"import": "./dist/html.js"
|
|
53
|
+
},
|
|
54
|
+
"./compat": {
|
|
55
|
+
"node": {
|
|
56
|
+
"types": "./dist/compat.node.d.ts",
|
|
57
|
+
"import": "./dist/compat.node.js"
|
|
58
|
+
},
|
|
59
|
+
"workerd": {
|
|
60
|
+
"types": "./dist/compat.d.ts",
|
|
61
|
+
"import": "./dist/compat.js"
|
|
62
|
+
},
|
|
63
|
+
"default": {
|
|
64
|
+
"types": "./dist/compat.d.ts",
|
|
65
|
+
"import": "./dist/compat.js"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"./node/compat": {
|
|
69
|
+
"types": "./dist/compat.node.d.ts",
|
|
70
|
+
"import": "./dist/compat.node.js"
|
|
71
|
+
},
|
|
72
|
+
"./workerd/compat": {
|
|
73
|
+
"types": "./dist/compat.d.ts",
|
|
74
|
+
"import": "./dist/compat.js"
|
|
23
75
|
}
|
|
24
76
|
},
|
|
25
77
|
"files": [
|