@junejs/og 0.0.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.
- package/package.json +52 -0
- package/src/edge.ts +6 -0
- package/src/fonts.ts +92 -0
- package/src/node.ts +39 -0
- package/src/types.ts +17 -0
- package/src/workerd.ts +8 -0
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@junejs/og",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
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
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"workerd": "./src/workerd.ts",
|
|
9
|
+
"edge-light": "./src/edge.ts",
|
|
10
|
+
"default": "./src/node.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"typecheck": "tsc --noEmit"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"june",
|
|
21
|
+
"og",
|
|
22
|
+
"og-image",
|
|
23
|
+
"open-graph",
|
|
24
|
+
"satori",
|
|
25
|
+
"image-response",
|
|
26
|
+
"cloudflare-workers",
|
|
27
|
+
"vercel"
|
|
28
|
+
],
|
|
29
|
+
"homepage": "https://june.build",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": "^19.2.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@resvg/resvg-js": "^2.6.2",
|
|
36
|
+
"@vercel/og": "^0.11.1",
|
|
37
|
+
"@types/react": "^19.2.0",
|
|
38
|
+
"react": "^19.2.0",
|
|
39
|
+
"satori": "^0.26.0",
|
|
40
|
+
"typescript": "^5.9.0",
|
|
41
|
+
"workers-og": "^0.0.27"
|
|
42
|
+
},
|
|
43
|
+
"author": "Lawrence Lin",
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git+https://github.com/junebuild/june.git",
|
|
50
|
+
"directory": "packages/og"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/edge.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Vercel Edge / edge-light backend — resolved when the build uses the "edge-light" condition.
|
|
2
|
+
// @vercel/og bundles its own resvg WASM compatible with Vercel's Edge Runtime.
|
|
3
|
+
export { ImageResponse } from "@vercel/og";
|
|
4
|
+
export type { ImageResponseOptions } from "./types";
|
|
5
|
+
export { loadGoogleFont, loadDefaultFonts, hasCJK, OG_HEADERS } from "./fonts";
|
|
6
|
+
export type { OgFont } from "./fonts";
|
package/src/fonts.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// Google Fonts subsetting + cache utilities shared across all OG backends.
|
|
2
|
+
// Works on Cloudflare Workers (Cache API) and Node.js/dev (in-memory Map).
|
|
3
|
+
|
|
4
|
+
export interface OgFont {
|
|
5
|
+
name: string;
|
|
6
|
+
data: ArrayBuffer;
|
|
7
|
+
weight: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
|
|
8
|
+
style: "normal" | "italic";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const memoryCache = new Map<string, ArrayBuffer>();
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Load a Google Font subset for the given text. Only the glyphs that appear in
|
|
15
|
+
* `text` are fetched (the `text=` query parameter asks Google to subset the font),
|
|
16
|
+
* so even a full CJK face downloads just a few KB per title.
|
|
17
|
+
*
|
|
18
|
+
* Cache strategy: Cache API (available on Cloudflare Workers and Vercel Edge) first,
|
|
19
|
+
* then in-memory (survives the dev process lifetime).
|
|
20
|
+
*/
|
|
21
|
+
export async function loadGoogleFont(
|
|
22
|
+
family: string,
|
|
23
|
+
weight: number,
|
|
24
|
+
text: string,
|
|
25
|
+
): Promise<ArrayBuffer> {
|
|
26
|
+
const cssUrl =
|
|
27
|
+
`https://fonts.googleapis.com/css2?family=${encodeURIComponent(family)}:wght@${weight}` +
|
|
28
|
+
`&text=${encodeURIComponent(text)}`;
|
|
29
|
+
|
|
30
|
+
const cache = (globalThis as unknown as { caches?: { default: Cache } }).caches?.default;
|
|
31
|
+
const cached = await cache?.match(cssUrl);
|
|
32
|
+
if (cached) return cached.arrayBuffer();
|
|
33
|
+
|
|
34
|
+
const memo = memoryCache.get(cssUrl);
|
|
35
|
+
if (memo) return memo;
|
|
36
|
+
|
|
37
|
+
return _fetchFont(cssUrl, family, cache);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function _fetchFont(cssUrl: string, family: string, cache?: Cache): Promise<ArrayBuffer> {
|
|
41
|
+
// A legacy Safari UA makes Google Fonts respond with TTF; satori requires TTF/OTF (not woff2).
|
|
42
|
+
const css = await fetch(cssUrl, {
|
|
43
|
+
headers: {
|
|
44
|
+
"User-Agent":
|
|
45
|
+
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1",
|
|
46
|
+
},
|
|
47
|
+
}).then((r) => r.text());
|
|
48
|
+
|
|
49
|
+
const url = css.match(/src: url\((.+?)\)/)?.[1];
|
|
50
|
+
if (!url) throw new Error(`@junejs/og: could not extract font URL for "${family}"`);
|
|
51
|
+
|
|
52
|
+
const buf = await fetch(url).then((r) => r.arrayBuffer());
|
|
53
|
+
|
|
54
|
+
await cache?.put(
|
|
55
|
+
cssUrl,
|
|
56
|
+
new Response(buf, { headers: { "cache-control": "public, max-age=604800" } }),
|
|
57
|
+
);
|
|
58
|
+
memoryCache.set(cssUrl, buf);
|
|
59
|
+
return buf;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** True when the string contains CJK characters (Chinese, Japanese, Korean). */
|
|
63
|
+
export const hasCJK = (s: string): boolean => /[ -鿿豈-]/.test(s);
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Convenience: load Inter (always) + Noto Sans TC (when the title contains CJK).
|
|
67
|
+
* Pass `text` as all the strings that will be rendered so the subset is complete.
|
|
68
|
+
*/
|
|
69
|
+
export async function loadDefaultFonts(text: string): Promise<OgFont[]> {
|
|
70
|
+
const fonts: OgFont[] = [
|
|
71
|
+
{
|
|
72
|
+
name: "Inter",
|
|
73
|
+
data: await loadGoogleFont("Inter", 600, text),
|
|
74
|
+
weight: 600,
|
|
75
|
+
style: "normal",
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
if (hasCJK(text)) {
|
|
79
|
+
fonts.push({
|
|
80
|
+
name: "Noto Sans TC",
|
|
81
|
+
data: await loadGoogleFont("Noto Sans TC", 600, text),
|
|
82
|
+
weight: 600,
|
|
83
|
+
style: "normal",
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return fonts;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export const OG_HEADERS = {
|
|
90
|
+
"content-type": "image/png",
|
|
91
|
+
"cache-control": "public, max-age=86400, stale-while-revalidate=604800",
|
|
92
|
+
} as const;
|
package/src/node.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Node.js / local dev backend — the "default" condition (no workerd / edge-light).
|
|
2
|
+
// Uses satori (JSX → SVG) + @resvg/resvg-js (SVG → PNG, native binding).
|
|
3
|
+
// Native bindings are fine for Vercel Node.js functions too; only Edge functions need WASM.
|
|
4
|
+
//
|
|
5
|
+
// This file must NOT be statically imported from inside app/ — that would pull it into the
|
|
6
|
+
// worker bundle graph. A June resource route can import it safely because the dev server
|
|
7
|
+
// runs in Node.js and the worker build resolves to workerd.ts / edge.ts via conditions.
|
|
8
|
+
import type { ReactElement } from "react";
|
|
9
|
+
import satori from "satori";
|
|
10
|
+
import { Resvg } from "@resvg/resvg-js";
|
|
11
|
+
export { loadGoogleFont, loadDefaultFonts, hasCJK, OG_HEADERS } from "./fonts";
|
|
12
|
+
export type { OgFont } from "./fonts";
|
|
13
|
+
export type { ImageResponseOptions } from "./types";
|
|
14
|
+
|
|
15
|
+
export class ImageResponse extends Response {
|
|
16
|
+
constructor(element: ReactElement, options: import("./types").ImageResponseOptions = {}) {
|
|
17
|
+
const { readable, writable } = new TransformStream<Uint8Array, Uint8Array>();
|
|
18
|
+
super(readable, {
|
|
19
|
+
status: options.status ?? 200,
|
|
20
|
+
headers: {
|
|
21
|
+
"content-type": "image/png",
|
|
22
|
+
"cache-control": "public, max-age=86400, stale-while-revalidate=604800",
|
|
23
|
+
...options.headers,
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
const writer = writable.getWriter();
|
|
27
|
+
satori(element as never, {
|
|
28
|
+
width: options.width ?? 1200,
|
|
29
|
+
height: options.height ?? 630,
|
|
30
|
+
fonts: (options.fonts ?? []) as never,
|
|
31
|
+
})
|
|
32
|
+
.then((svg) => {
|
|
33
|
+
const png = new Resvg(svg).render().asPng();
|
|
34
|
+
writer.write(new Uint8Array(png));
|
|
35
|
+
writer.close();
|
|
36
|
+
})
|
|
37
|
+
.catch((err) => writer.abort(err));
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { OgFont } from "./fonts.ts";
|
|
2
|
+
|
|
3
|
+
/** Options accepted by ImageResponse across all backends. */
|
|
4
|
+
export interface ImageResponseOptions {
|
|
5
|
+
width?: number;
|
|
6
|
+
height?: number;
|
|
7
|
+
/** Fonts to load for satori — use {@link loadGoogleFont} or {@link loadDefaultFonts}. */
|
|
8
|
+
fonts?: OgFont[];
|
|
9
|
+
/** HTTP status code. Default 200. */
|
|
10
|
+
status?: number;
|
|
11
|
+
/** Additional response headers (merged; do not override content-type). */
|
|
12
|
+
headers?: Record<string, string>;
|
|
13
|
+
/** Emoji rendering style (satori option). Default "twemoji". */
|
|
14
|
+
emoji?: "twemoji" | "blobmoji" | "noto" | "openmoji";
|
|
15
|
+
/** Render debug outlines (satori option). */
|
|
16
|
+
debug?: boolean;
|
|
17
|
+
}
|
package/src/workerd.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Cloudflare Workers backend — resolved when the build uses the "workerd" export condition.
|
|
2
|
+
// workers-og wraps satori + a Workers-compatible resvg WASM loader; mark it as buildExternal
|
|
3
|
+
// in june.config.ts so wrangler's own WASM rules can handle its .wasm assets:
|
|
4
|
+
// build: { external: ["workers-og"] }
|
|
5
|
+
export { ImageResponse } from "workers-og";
|
|
6
|
+
export type { ImageResponseOptions } from "./types";
|
|
7
|
+
export { loadGoogleFont, loadDefaultFonts, hasCJK, OG_HEADERS } from "./fonts";
|
|
8
|
+
export type { OgFont } from "./fonts";
|