@junejs/og 0.0.1 → 0.0.3
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 +1 -1
- package/src/node.ts +21 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@junejs/og",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
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": {
|
package/src/node.ts
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
// Uses satori (JSX → SVG) + @resvg/resvg-js (SVG → PNG, native binding).
|
|
3
3
|
// Native bindings are fine for Vercel Node.js functions too; only Edge functions need WASM.
|
|
4
4
|
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// runs in Node.js and the worker build resolves to workerd.ts / edge.ts via conditions.
|
|
5
|
+
// satori and @resvg/resvg-js are DYNAMIC imports (not static) so their type declarations
|
|
6
|
+
// are not required at compile time — downstream packages can typecheck without them installed.
|
|
8
7
|
import type { ReactElement } from "react";
|
|
9
|
-
import satori from "satori";
|
|
10
|
-
import { Resvg } from "@resvg/resvg-js";
|
|
11
8
|
export { loadGoogleFont, loadDefaultFonts, hasCJK, OG_HEADERS } from "./fonts";
|
|
12
9
|
export type { OgFont } from "./fonts";
|
|
13
10
|
export type { ImageResponseOptions } from "./types";
|
|
14
11
|
|
|
12
|
+
type Satori = (element: unknown, opts: unknown) => Promise<string>;
|
|
13
|
+
type ResvgClass = new (svg: string) => { render(): { asPng(): Uint8Array } };
|
|
14
|
+
|
|
15
15
|
export class ImageResponse extends Response {
|
|
16
16
|
constructor(element: ReactElement, options: import("./types").ImageResponseOptions = {}) {
|
|
17
17
|
const { readable, writable } = new TransformStream<Uint8Array, Uint8Array>();
|
|
@@ -24,16 +24,21 @@ export class ImageResponse extends Response {
|
|
|
24
24
|
},
|
|
25
25
|
});
|
|
26
26
|
const writer = writable.getWriter();
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
27
|
+
Promise.all([
|
|
28
|
+
import("satori") as Promise<{ default: Satori }>,
|
|
29
|
+
import("@resvg/resvg-js") as Promise<{ Resvg: ResvgClass }>,
|
|
30
|
+
])
|
|
31
|
+
.then(([{ default: satori }, { Resvg }]) =>
|
|
32
|
+
satori(element, {
|
|
33
|
+
width: options.width ?? 1200,
|
|
34
|
+
height: options.height ?? 630,
|
|
35
|
+
fonts: options.fonts ?? [],
|
|
36
|
+
}).then((svg) => {
|
|
37
|
+
const png = new Resvg(svg).render().asPng();
|
|
38
|
+
writer.write(new Uint8Array(png));
|
|
39
|
+
writer.close();
|
|
40
|
+
}),
|
|
41
|
+
)
|
|
42
|
+
.catch((err: unknown) => writer.abort(err));
|
|
38
43
|
}
|
|
39
44
|
}
|