-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
12 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,22 @@ | ||
import type { ImageResponse as ImageResponseType } from "@vercel/og"; | ||
import * as Og from "@vercel/og"; | ||
import { type HonoElement, toReactNode } from "./utils.js"; | ||
|
||
// Determine what instance of the module to import based on the runtime | ||
// (Next.js has it's own compiled version of @vercel/og). | ||
function importModule(): Promise<typeof import("@vercel/og")> { | ||
return import( | ||
typeof process.env.NEXT_RUNTIME !== "undefined" | ||
? process.env.NEXT_RUNTIME === "edge" | ||
? "./next/dist/compiled/@vercel/og/index.edge.js" | ||
: "./next/dist/compiled/@vercel/og/index.node.js" | ||
: "@vercel/og" | ||
); | ||
} | ||
const pkg = Og; | ||
|
||
export type ImageResponseOptions = ConstructorParameters< | ||
typeof ImageResponseType | ||
typeof Og.ImageResponse | ||
>[1]; | ||
|
||
export class ImageResponse extends Response { | ||
public static displayName = "ImageResponse"; | ||
constructor(element: HonoElement, options: ImageResponseOptions = {}) { | ||
const readable = new ReadableStream({ | ||
async start(controller) { | ||
const OGImageResponse = (await importModule()).ImageResponse; | ||
const imageResponse = new OGImageResponse( | ||
toReactNode(element), | ||
options, | ||
) as Response; | ||
|
||
if (!imageResponse.body) return controller.close(); | ||
|
||
const reader = imageResponse.body!.getReader(); | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
return controller.close(); | ||
} | ||
controller.enqueue(value); | ||
} | ||
}, | ||
}); | ||
|
||
super(readable, { | ||
headers: { | ||
"content-type": "image/png", | ||
"cache-control": | ||
process.env.NODE_ENV === "development" | ||
? "no-cache, no-store" | ||
: "public, immutable, no-transform, max-age=31536000", | ||
...options.headers, | ||
}, | ||
status: options.status, | ||
statusText: options.statusText, | ||
}); | ||
export class ImageResponse extends Og.ImageResponse { | ||
constructor(element: HonoElement, options?: ImageResponseOptions) { | ||
super(toReactNode(element), options); | ||
} | ||
} | ||
|
||
export const unstable_createNodejsStream = async ( | ||
export const unstable_createNodejsStream = ( | ||
element: HonoElement, | ||
options?: Parameters< | ||
typeof import("@vercel/og").unstable_createNodejsStream | ||
>[1], | ||
) => { | ||
const mod = await importModule(); | ||
return "unstable_createNodejsStream" in mod | ||
? mod.unstable_createNodejsStream(toReactNode(element), options) | ||
: undefined; | ||
}; | ||
options?: Parameters<typeof Og.unstable_createNodejsStream>[1], | ||
) => | ||
"unstable_createNodejsStream" in pkg | ||
? pkg.unstable_createNodejsStream(toReactNode(element), options) | ||
: undefined; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters