@junejs/og 0.0.6 → 0.0.7-dev.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/dist/edge.d.ts +3 -3
- package/dist/edge.js +2 -5
- package/dist/fonts-Bx7XuIfO.d.ts +35 -0
- package/dist/fonts.d.ts +1 -28
- package/dist/fonts.js +7 -1
- package/dist/headers.d.ts +15 -0
- package/dist/headers.js +21 -0
- package/dist/node.d.ts +3 -3
- package/dist/node.js +2 -5
- package/dist/types.d.ts +3 -4
- package/dist/workerd.d.ts +8 -3
- package/dist/workerd.js +31 -1
- package/package.json +3 -3
- package/src/edge.ts +3 -7
- package/src/fonts.ts +7 -1
- package/src/headers.ts +20 -0
- package/src/node.ts +3 -7
- package/src/workerd.ts +49 -1
package/dist/edge.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as loadGoogleFont, i as loadDefaultFonts, n as OgFont, r as hasCJK, t as OG_HEADERS } from "./fonts-Bx7XuIfO.js";
|
|
2
2
|
import { ImageResponseOptions } from "./types.js";
|
|
3
3
|
import { ReactElement } from "react";
|
|
4
4
|
//#region src/edge.d.ts
|
|
5
|
-
declare class ImageResponse extends Response {
|
|
5
|
+
export declare class ImageResponse extends Response {
|
|
6
6
|
constructor(element: ReactElement, options?: ImageResponseOptions);
|
|
7
7
|
}
|
|
8
8
|
//#endregion
|
|
9
|
-
export {
|
|
9
|
+
export { type ImageResponseOptions, OG_HEADERS, type OgFont, hasCJK, loadDefaultFonts, loadGoogleFont };
|
package/dist/edge.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { OG_HEADERS, hasCJK, loadDefaultFonts, loadGoogleFont } from "./fonts.js";
|
|
2
|
+
import { ogResponseHeaders } from "./headers.js";
|
|
2
3
|
//#region src/edge.ts
|
|
3
4
|
const _import = new Function("m", "return import(m)");
|
|
4
5
|
var ImageResponse = class extends Response {
|
|
@@ -6,11 +7,7 @@ var ImageResponse = class extends Response {
|
|
|
6
7
|
const { readable, writable } = new TransformStream();
|
|
7
8
|
super(readable, {
|
|
8
9
|
status: options.status ?? 200,
|
|
9
|
-
headers:
|
|
10
|
-
"cache-control": "public, max-age=86400, stale-while-revalidate=604800",
|
|
11
|
-
...options.headers,
|
|
12
|
-
"content-type": "image/png"
|
|
13
|
-
}
|
|
10
|
+
headers: ogResponseHeaders(options)
|
|
14
11
|
});
|
|
15
12
|
_import("@vercel/og").then(({ ImageResponse: VercelOg }) => {
|
|
16
13
|
const rendered = new VercelOg(element, {
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//#region src/fonts.d.ts
|
|
2
|
+
interface OgFont {
|
|
3
|
+
name: string;
|
|
4
|
+
data: ArrayBuffer;
|
|
5
|
+
weight: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
|
|
6
|
+
style: "normal" | "italic";
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Load a Google Font subset for the given text. Only the glyphs that appear in
|
|
10
|
+
* `text` are fetched (the `text=` query parameter asks Google to subset the font),
|
|
11
|
+
* so even a full CJK face downloads just a few KB per title.
|
|
12
|
+
*
|
|
13
|
+
* Cache strategy: Cache API (available on Cloudflare Workers and Vercel Edge) first,
|
|
14
|
+
* then in-memory (survives the dev process lifetime).
|
|
15
|
+
*/
|
|
16
|
+
declare function loadGoogleFont(family: string, weight: number, text: string): Promise<ArrayBuffer>;
|
|
17
|
+
/**
|
|
18
|
+
* True when the string contains CJK ideographs or kana (Chinese, Japanese).
|
|
19
|
+
* Korean Hangul (U+AC00–U+D7AF) is NOT detected — loadDefaultFonts ships
|
|
20
|
+
* Noto Sans TC, which has no Hangul glyphs, so detecting Korean here would
|
|
21
|
+
* swap a readable Inter fallback for tofu. Proper Korean support means a
|
|
22
|
+
* Noto Sans KR branch; until then this stays honestly Chinese/Japanese.
|
|
23
|
+
*/
|
|
24
|
+
declare const hasCJK: (s: string) => boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Convenience: load Inter (always) + Noto Sans TC (when the title contains CJK).
|
|
27
|
+
* Pass `text` as all the strings that will be rendered so the subset is complete.
|
|
28
|
+
*/
|
|
29
|
+
declare function loadDefaultFonts(text: string): Promise<OgFont[]>;
|
|
30
|
+
declare const OG_HEADERS: {
|
|
31
|
+
readonly "content-type": "image/png";
|
|
32
|
+
readonly "cache-control": "public, max-age=86400, stale-while-revalidate=604800";
|
|
33
|
+
};
|
|
34
|
+
//#endregion
|
|
35
|
+
export { loadGoogleFont as a, loadDefaultFonts as i, OgFont as n, hasCJK as r, OG_HEADERS as t };
|
package/dist/fonts.d.ts
CHANGED
|
@@ -1,29 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
interface OgFont {
|
|
3
|
-
name: string;
|
|
4
|
-
data: ArrayBuffer;
|
|
5
|
-
weight: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
|
|
6
|
-
style: "normal" | "italic";
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Load a Google Font subset for the given text. Only the glyphs that appear in
|
|
10
|
-
* `text` are fetched (the `text=` query parameter asks Google to subset the font),
|
|
11
|
-
* so even a full CJK face downloads just a few KB per title.
|
|
12
|
-
*
|
|
13
|
-
* Cache strategy: Cache API (available on Cloudflare Workers and Vercel Edge) first,
|
|
14
|
-
* then in-memory (survives the dev process lifetime).
|
|
15
|
-
*/
|
|
16
|
-
declare function loadGoogleFont(family: string, weight: number, text: string): Promise<ArrayBuffer>;
|
|
17
|
-
/** True when the string contains CJK characters (Chinese, Japanese, Korean). */
|
|
18
|
-
declare const hasCJK: (s: string) => boolean;
|
|
19
|
-
/**
|
|
20
|
-
* Convenience: load Inter (always) + Noto Sans TC (when the title contains CJK).
|
|
21
|
-
* Pass `text` as all the strings that will be rendered so the subset is complete.
|
|
22
|
-
*/
|
|
23
|
-
declare function loadDefaultFonts(text: string): Promise<OgFont[]>;
|
|
24
|
-
declare const OG_HEADERS: {
|
|
25
|
-
readonly "content-type": "image/png";
|
|
26
|
-
readonly "cache-control": "public, max-age=86400, stale-while-revalidate=604800";
|
|
27
|
-
};
|
|
28
|
-
//#endregion
|
|
1
|
+
import { a as loadGoogleFont, i as loadDefaultFonts, n as OgFont, r as hasCJK, t as OG_HEADERS } from "./fonts-Bx7XuIfO.js";
|
|
29
2
|
export { OG_HEADERS, OgFont, hasCJK, loadDefaultFonts, loadGoogleFont };
|
package/dist/fonts.js
CHANGED
|
@@ -25,7 +25,13 @@ async function _fetchFont(cssUrl, family, cache) {
|
|
|
25
25
|
memoryCache.set(cssUrl, buf);
|
|
26
26
|
return buf;
|
|
27
27
|
}
|
|
28
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* True when the string contains CJK ideographs or kana (Chinese, Japanese).
|
|
30
|
+
* Korean Hangul (U+AC00–U+D7AF) is NOT detected — loadDefaultFonts ships
|
|
31
|
+
* Noto Sans TC, which has no Hangul glyphs, so detecting Korean here would
|
|
32
|
+
* swap a readable Inter fallback for tofu. Proper Korean support means a
|
|
33
|
+
* Noto Sans KR branch; until then this stays honestly Chinese/Japanese.
|
|
34
|
+
*/
|
|
29
35
|
const hasCJK = (s) => /[ -鿿豈-]/.test(s);
|
|
30
36
|
/**
|
|
31
37
|
* Convenience: load Inter (always) + Noto Sans TC (when the title contains CJK).
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ImageResponseOptions } from "./types.js";
|
|
2
|
+
//#region src/headers.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* The response headers every backend's ImageResponse answers with.
|
|
5
|
+
*
|
|
6
|
+
* Merged through `Headers.set`, which is case-insensitive: a caller's
|
|
7
|
+
* `Cache-Control` replaces the default instead of joining it. An object spread
|
|
8
|
+
* (`{ "cache-control": …, ...options.headers }`) keeps both spellings as
|
|
9
|
+
* separate keys, and `new Headers(init)` then appends their values.
|
|
10
|
+
*
|
|
11
|
+
* Contract (types.ts): callers may merge or override any header EXCEPT
|
|
12
|
+
* content-type, which is set last so the body is always served as a PNG.
|
|
13
|
+
*/
|
|
14
|
+
export declare function ogResponseHeaders(options: ImageResponseOptions): Headers;
|
|
15
|
+
//#endregion
|
package/dist/headers.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { OG_HEADERS } from "./fonts.js";
|
|
2
|
+
//#region src/headers.ts
|
|
3
|
+
/**
|
|
4
|
+
* The response headers every backend's ImageResponse answers with.
|
|
5
|
+
*
|
|
6
|
+
* Merged through `Headers.set`, which is case-insensitive: a caller's
|
|
7
|
+
* `Cache-Control` replaces the default instead of joining it. An object spread
|
|
8
|
+
* (`{ "cache-control": …, ...options.headers }`) keeps both spellings as
|
|
9
|
+
* separate keys, and `new Headers(init)` then appends their values.
|
|
10
|
+
*
|
|
11
|
+
* Contract (types.ts): callers may merge or override any header EXCEPT
|
|
12
|
+
* content-type, which is set last so the body is always served as a PNG.
|
|
13
|
+
*/
|
|
14
|
+
function ogResponseHeaders(options) {
|
|
15
|
+
const headers = new Headers({ "cache-control": OG_HEADERS["cache-control"] });
|
|
16
|
+
for (const [name, value] of Object.entries(options.headers ?? {})) headers.set(name, value);
|
|
17
|
+
headers.set("content-type", "image/png");
|
|
18
|
+
return headers;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { ogResponseHeaders };
|
package/dist/node.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as loadGoogleFont, i as loadDefaultFonts, n as OgFont, r as hasCJK, t as OG_HEADERS } from "./fonts-Bx7XuIfO.js";
|
|
2
2
|
import { ImageResponseOptions } from "./types.js";
|
|
3
3
|
import { ReactElement } from "react";
|
|
4
4
|
//#region src/node.d.ts
|
|
5
|
-
declare class ImageResponse extends Response {
|
|
5
|
+
export declare class ImageResponse extends Response {
|
|
6
6
|
constructor(element: ReactElement, options?: ImageResponseOptions);
|
|
7
7
|
}
|
|
8
8
|
//#endregion
|
|
9
|
-
export {
|
|
9
|
+
export { type ImageResponseOptions, OG_HEADERS, type OgFont, hasCJK, loadDefaultFonts, loadGoogleFont };
|
package/dist/node.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { OG_HEADERS, hasCJK, loadDefaultFonts, loadGoogleFont } from "./fonts.js";
|
|
2
|
+
import { ogResponseHeaders } from "./headers.js";
|
|
2
3
|
//#region src/node.ts
|
|
3
4
|
const _import = new Function("m", "return import(m)");
|
|
4
5
|
var ImageResponse = class extends Response {
|
|
@@ -6,11 +7,7 @@ var ImageResponse = class extends Response {
|
|
|
6
7
|
const { readable, writable } = new TransformStream();
|
|
7
8
|
super(readable, {
|
|
8
9
|
status: options.status ?? 200,
|
|
9
|
-
headers:
|
|
10
|
-
"cache-control": "public, max-age=86400, stale-while-revalidate=604800",
|
|
11
|
-
...options.headers,
|
|
12
|
-
"content-type": "image/png"
|
|
13
|
-
}
|
|
10
|
+
headers: ogResponseHeaders(options)
|
|
14
11
|
});
|
|
15
12
|
const writer = writable.getWriter();
|
|
16
13
|
Promise.all([_import("satori"), _import("@resvg/resvg-js")]).then(([{ default: satori }, { Resvg }]) => satori(element, {
|
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { OgFont } from "./fonts.js";
|
|
1
|
+
import { n as OgFont } from "./fonts-Bx7XuIfO.js";
|
|
2
2
|
//#region src/types.d.ts
|
|
3
3
|
/** Options accepted by ImageResponse across all backends. */
|
|
4
|
-
interface ImageResponseOptions {
|
|
4
|
+
export interface ImageResponseOptions {
|
|
5
5
|
width?: number;
|
|
6
6
|
height?: number;
|
|
7
7
|
/** Fonts to load for satori — use {@link loadGoogleFont} or {@link loadDefaultFonts}. */
|
|
@@ -15,5 +15,4 @@ interface ImageResponseOptions {
|
|
|
15
15
|
/** Render debug outlines (satori option). */
|
|
16
16
|
debug?: boolean;
|
|
17
17
|
}
|
|
18
|
-
//#endregion
|
|
19
|
-
export { ImageResponseOptions };
|
|
18
|
+
//#endregion
|
package/dist/workerd.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as loadGoogleFont, i as loadDefaultFonts, n as OgFont, r as hasCJK, t as OG_HEADERS } from "./fonts-Bx7XuIfO.js";
|
|
2
2
|
import { ImageResponseOptions } from "./types.js";
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import { ReactElement } from "react";
|
|
4
|
+
//#region src/workerd.d.ts
|
|
5
|
+
export declare class ImageResponse extends Response {
|
|
6
|
+
constructor(element: ReactElement, options?: ImageResponseOptions);
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { type ImageResponseOptions, OG_HEADERS, type OgFont, hasCJK, loadDefaultFonts, loadGoogleFont };
|
package/dist/workerd.js
CHANGED
|
@@ -1,3 +1,33 @@
|
|
|
1
1
|
import { OG_HEADERS, hasCJK, loadDefaultFonts, loadGoogleFont } from "./fonts.js";
|
|
2
|
-
import {
|
|
2
|
+
import { ogResponseHeaders } from "./headers.js";
|
|
3
|
+
import { ImageResponse as ImageResponse$1 } from "workers-og";
|
|
4
|
+
//#region src/workerd.ts
|
|
5
|
+
var ImageResponse = class extends Response {
|
|
6
|
+
constructor(element, options = {}) {
|
|
7
|
+
let cancelled = false;
|
|
8
|
+
const body = new ReadableStream({
|
|
9
|
+
async pull(controller) {
|
|
10
|
+
const rendered = new ImageResponse$1(element, {
|
|
11
|
+
width: options.width,
|
|
12
|
+
height: options.height,
|
|
13
|
+
fonts: options.fonts,
|
|
14
|
+
emoji: options.emoji,
|
|
15
|
+
debug: options.debug
|
|
16
|
+
});
|
|
17
|
+
const bytes = new Uint8Array(await rendered.arrayBuffer());
|
|
18
|
+
if (cancelled) return;
|
|
19
|
+
controller.enqueue(bytes);
|
|
20
|
+
controller.close();
|
|
21
|
+
},
|
|
22
|
+
cancel() {
|
|
23
|
+
cancelled = true;
|
|
24
|
+
}
|
|
25
|
+
}, { highWaterMark: 0 });
|
|
26
|
+
super(body, {
|
|
27
|
+
status: options.status ?? 200,
|
|
28
|
+
headers: ogResponseHeaders(options)
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
//#endregion
|
|
3
33
|
export { ImageResponse, OG_HEADERS, hasCJK, loadDefaultFonts, loadGoogleFont };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@junejs/og",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7-dev.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "June — cross-platform OG image generation. Cloudflare Workers, Vercel Edge, and Node.js dev all share one import; the right backend is selected at build time via export conditions.",
|
|
6
6
|
"exports": {
|
|
@@ -55,10 +55,10 @@
|
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@resvg/resvg-js": "^2.6.2",
|
|
57
57
|
"@vercel/og": "^0.11.1",
|
|
58
|
-
"@types/react": "^19.
|
|
58
|
+
"@types/react": "^19.3.0",
|
|
59
59
|
"react": "^19.2.0",
|
|
60
60
|
"satori": "^0.26.0",
|
|
61
|
-
"typescript": "^
|
|
61
|
+
"typescript": "^7.0.0",
|
|
62
62
|
"workers-og": "^0.0.27"
|
|
63
63
|
},
|
|
64
64
|
"author": "Lawrence Lin",
|
package/src/edge.ts
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
// load regardless of use). Lazy-loading defers it to first render — edge/Node OG works at runtime
|
|
11
11
|
// (the consumer installs @vercel/og), and a static-prerendered route, never invoked, pulls nothing.
|
|
12
12
|
import type { ReactElement } from "react";
|
|
13
|
+
|
|
14
|
+
import { ogResponseHeaders } from "./headers";
|
|
13
15
|
export type { ImageResponseOptions } from "./types";
|
|
14
16
|
export { loadGoogleFont, loadDefaultFonts, hasCJK, OG_HEADERS } from "./fonts";
|
|
15
17
|
export type { OgFont } from "./fonts";
|
|
@@ -24,13 +26,7 @@ export class ImageResponse extends Response {
|
|
|
24
26
|
const { readable, writable } = new TransformStream<Uint8Array, Uint8Array>();
|
|
25
27
|
super(readable, {
|
|
26
28
|
status: options.status ?? 200,
|
|
27
|
-
headers:
|
|
28
|
-
"cache-control": "public, max-age=86400, stale-while-revalidate=604800",
|
|
29
|
-
...options.headers,
|
|
30
|
-
// Contract (types.ts): callers may merge/override any header EXCEPT content-type
|
|
31
|
-
// — set it last so it always wins and the body is always served as a PNG.
|
|
32
|
-
"content-type": "image/png",
|
|
33
|
-
},
|
|
29
|
+
headers: ogResponseHeaders(options),
|
|
34
30
|
});
|
|
35
31
|
(_import("@vercel/og") as Promise<{ ImageResponse: VercelImageResponse }>)
|
|
36
32
|
.then(({ ImageResponse: VercelOg }) => {
|
package/src/fonts.ts
CHANGED
|
@@ -59,7 +59,13 @@ async function _fetchFont(cssUrl: string, family: string, cache?: Cache): Promis
|
|
|
59
59
|
return buf;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
/**
|
|
62
|
+
/**
|
|
63
|
+
* True when the string contains CJK ideographs or kana (Chinese, Japanese).
|
|
64
|
+
* Korean Hangul (U+AC00–U+D7AF) is NOT detected — loadDefaultFonts ships
|
|
65
|
+
* Noto Sans TC, which has no Hangul glyphs, so detecting Korean here would
|
|
66
|
+
* swap a readable Inter fallback for tofu. Proper Korean support means a
|
|
67
|
+
* Noto Sans KR branch; until then this stays honestly Chinese/Japanese.
|
|
68
|
+
*/
|
|
63
69
|
export const hasCJK = (s: string): boolean => /[ -鿿豈-]/.test(s);
|
|
64
70
|
|
|
65
71
|
/**
|
package/src/headers.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { OG_HEADERS } from "./fonts";
|
|
2
|
+
import type { ImageResponseOptions } from "./types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The response headers every backend's ImageResponse answers with.
|
|
6
|
+
*
|
|
7
|
+
* Merged through `Headers.set`, which is case-insensitive: a caller's
|
|
8
|
+
* `Cache-Control` replaces the default instead of joining it. An object spread
|
|
9
|
+
* (`{ "cache-control": …, ...options.headers }`) keeps both spellings as
|
|
10
|
+
* separate keys, and `new Headers(init)` then appends their values.
|
|
11
|
+
*
|
|
12
|
+
* Contract (types.ts): callers may merge or override any header EXCEPT
|
|
13
|
+
* content-type, which is set last so the body is always served as a PNG.
|
|
14
|
+
*/
|
|
15
|
+
export function ogResponseHeaders(options: ImageResponseOptions): Headers {
|
|
16
|
+
const headers = new Headers({ "cache-control": OG_HEADERS["cache-control"] });
|
|
17
|
+
for (const [name, value] of Object.entries(options.headers ?? {})) headers.set(name, value);
|
|
18
|
+
headers.set("content-type", "image/png");
|
|
19
|
+
return headers;
|
|
20
|
+
}
|
package/src/node.ts
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
// attempt to resolve them at compile time — satori and @resvg/resvg-js are optional
|
|
7
7
|
// devDependencies of @junejs/og and are never installed by downstream consumers.
|
|
8
8
|
import type { ReactElement } from "react";
|
|
9
|
+
|
|
10
|
+
import { ogResponseHeaders } from "./headers";
|
|
9
11
|
export { loadGoogleFont, loadDefaultFonts, hasCJK, OG_HEADERS } from "./fonts";
|
|
10
12
|
export type { OgFont } from "./fonts";
|
|
11
13
|
export type { ImageResponseOptions } from "./types";
|
|
@@ -21,13 +23,7 @@ export class ImageResponse extends Response {
|
|
|
21
23
|
const { readable, writable } = new TransformStream<Uint8Array, Uint8Array>();
|
|
22
24
|
super(readable, {
|
|
23
25
|
status: options.status ?? 200,
|
|
24
|
-
headers:
|
|
25
|
-
"cache-control": "public, max-age=86400, stale-while-revalidate=604800",
|
|
26
|
-
...options.headers,
|
|
27
|
-
// Contract (types.ts): callers may merge/override any header EXCEPT content-type
|
|
28
|
-
// — set it last so it always wins and the body is always served as a PNG.
|
|
29
|
-
"content-type": "image/png",
|
|
30
|
-
},
|
|
26
|
+
headers: ogResponseHeaders(options),
|
|
31
27
|
});
|
|
32
28
|
const writer = writable.getWriter();
|
|
33
29
|
Promise.all([
|
package/src/workerd.ts
CHANGED
|
@@ -2,7 +2,55 @@
|
|
|
2
2
|
// workers-og wraps satori + a Workers-compatible resvg WASM loader; mark it as buildExternal
|
|
3
3
|
// in june.config.ts so wrangler's own WASM rules can handle its .wasm assets:
|
|
4
4
|
// build: { external: ["workers-og"] }
|
|
5
|
-
|
|
5
|
+
//
|
|
6
|
+
// Do not re-export workers-og's ImageResponse. It builds
|
|
7
|
+
// { "Content-Type": "image/png", "Cache-Control": "<immutable, max-age=31536000>", ...options.headers }
|
|
8
|
+
// with title-case keys. OG_HEADERS (and node.ts / edge.ts) use lowercase keys, so both
|
|
9
|
+
// spellings survive the spread and `new Headers` appends them:
|
|
10
|
+
// content-type: image/png, image/png
|
|
11
|
+
// cache-control: public, immutable, no-transform, max-age=31536000, public, max-age=86400, ...
|
|
12
|
+
// Own the response the way node.ts and edge.ts do: the envelope comes from
|
|
13
|
+
// ogResponseHeaders(), and workers-og renders the body only. The static import stays —
|
|
14
|
+
// a lazy `import()` would drop workers-og from the bundle graph, so wrangler would never
|
|
15
|
+
// see the WASM. The render starts when the body is read, not when the response is built,
|
|
16
|
+
// so the envelope (status/headers) is synchronous and does not require the workerd runtime.
|
|
17
|
+
import type { ReactElement } from "react";
|
|
18
|
+
import { ImageResponse as WorkersOgImageResponse } from "workers-og";
|
|
19
|
+
|
|
20
|
+
import { ogResponseHeaders } from "./headers";
|
|
21
|
+
|
|
6
22
|
export type { ImageResponseOptions } from "./types";
|
|
7
23
|
export { loadGoogleFont, loadDefaultFonts, hasCJK, OG_HEADERS } from "./fonts";
|
|
8
24
|
export type { OgFont } from "./fonts";
|
|
25
|
+
|
|
26
|
+
export class ImageResponse extends Response {
|
|
27
|
+
constructor(element: ReactElement, options: import("./types").ImageResponseOptions = {}) {
|
|
28
|
+
let cancelled = false;
|
|
29
|
+
const body = new ReadableStream<Uint8Array>(
|
|
30
|
+
{
|
|
31
|
+
async pull(controller) {
|
|
32
|
+
// Never forward options.headers — workers-og spreads them onto title-case keys.
|
|
33
|
+
const rendered = new WorkersOgImageResponse(element, {
|
|
34
|
+
width: options.width,
|
|
35
|
+
height: options.height,
|
|
36
|
+
fonts: options.fonts,
|
|
37
|
+
emoji: options.emoji,
|
|
38
|
+
debug: options.debug,
|
|
39
|
+
});
|
|
40
|
+
const bytes = new Uint8Array(await rendered.arrayBuffer());
|
|
41
|
+
// The reader cancelled while the render ran: the stream is closed, drop the bytes.
|
|
42
|
+
if (cancelled) return;
|
|
43
|
+
controller.enqueue(bytes);
|
|
44
|
+
controller.close();
|
|
45
|
+
},
|
|
46
|
+
cancel() {
|
|
47
|
+
cancelled = true;
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
// highWaterMark 0: pull() only answers a read. The default (1) pulls once start()
|
|
51
|
+
// settles to fill the queue, which would start the WASM render with no reader.
|
|
52
|
+
{ highWaterMark: 0 },
|
|
53
|
+
);
|
|
54
|
+
super(body, { status: options.status ?? 200, headers: ogResponseHeaders(options) });
|
|
55
|
+
}
|
|
56
|
+
}
|