@junejs/og 0.0.4 → 0.0.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@junejs/og",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
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/edge.ts CHANGED
@@ -1,6 +1,55 @@
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";
1
+ // Vercel / edge-light backend — resolved when the build uses the "edge-light" condition.
2
+ // @vercel/og renders OG images with its own resvg WASM (works on Vercel's Edge AND Node runtimes).
3
+ //
4
+ // @vercel/og is loaded LAZILY via `new Function("return import(m)")` (mirrors node.ts's satori/
5
+ // @resvg load) so the bundler never resolves it at build time. Its entry statically imports
6
+ // `./yoga.wasm?module`, which rolldown — June's worker bundler — can't bundle; a STATIC
7
+ // `export { ImageResponse } from "@vercel/og"` here dragged that WASM into EVERY worker carrying
8
+ // the OG route, breaking the build even when OG is prerendered to static files and @vercel/og is
9
+ // never called at runtime (externalizing doesn't help: an ESM static import is resolved at module
10
+ // load regardless of use). Lazy-loading defers it to first render — edge/Node OG works at runtime
11
+ // (the consumer installs @vercel/og), and a static-prerendered route, never invoked, pulls nothing.
12
+ import type { ReactElement } from "react";
4
13
  export type { ImageResponseOptions } from "./types";
5
14
  export { loadGoogleFont, loadDefaultFonts, hasCJK, OG_HEADERS } from "./fonts";
6
15
  export type { OgFont } from "./fonts";
16
+
17
+ type VercelImageResponse = new (element: ReactElement, options?: Record<string, unknown>) => Response;
18
+
19
+ // TypeScript cannot see through Function() — no static module resolution is attempted.
20
+ const _import = new Function("m", "return import(m)") as (m: string) => Promise<unknown>;
21
+
22
+ export class ImageResponse extends Response {
23
+ constructor(element: ReactElement, options: import("./types").ImageResponseOptions = {}) {
24
+ const { readable, writable } = new TransformStream<Uint8Array, Uint8Array>();
25
+ super(readable, {
26
+ 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
+ },
34
+ });
35
+ (_import("@vercel/og") as Promise<{ ImageResponse: VercelImageResponse }>)
36
+ .then(({ ImageResponse: VercelOg }) => {
37
+ // Delegate the actual render to @vercel/og, then pipe its PNG body into ours
38
+ // (so our status/headers above win, matching node.ts's response shape). pipeTo
39
+ // handles backpressure, completion, and error propagation (it aborts `writable`
40
+ // if the source errors), so there's no hand-managed writer to get wrong.
41
+ const rendered = new VercelOg(element, {
42
+ width: options.width ?? 1200,
43
+ height: options.height ?? 630,
44
+ fonts: options.fonts,
45
+ emoji: options.emoji,
46
+ debug: options.debug,
47
+ });
48
+ return rendered.body ? rendered.body.pipeTo(writable) : writable.close();
49
+ })
50
+ // The lazy import or the render setup failed (a streaming error already aborted
51
+ // `writable` via pipeTo — the redundant abort is swallowed). Abort so the response
52
+ // errors instead of hanging.
53
+ .catch((err: unknown) => writable.abort(err).catch(() => {}));
54
+ }
55
+ }
package/src/node.ts CHANGED
@@ -22,9 +22,11 @@ export class ImageResponse extends Response {
22
22
  super(readable, {
23
23
  status: options.status ?? 200,
24
24
  headers: {
25
- "content-type": "image/png",
26
25
  "cache-control": "public, max-age=86400, stale-while-revalidate=604800",
27
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",
28
30
  },
29
31
  });
30
32
  const writer = writable.getWriter();