Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Mar 7, 2024
1 parent 7a151f7 commit 7e384c4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 60 deletions.
71 changes: 12 additions & 59 deletions src/index.ts
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;
1 change: 0 additions & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"dependencies": {
"@wevm/vercel-og": "~0.6.11",
"@vercel/og": "~0.6.2",
"next": "^14.1.3",
"workers-og": "~0.0.20"
}
}

0 comments on commit 7e384c4

Please sign in to comment.