Skip to content

Commit

Permalink
feat: worker support (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom authored Feb 21, 2024
1 parent f7544a4 commit 5248d06
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 218 deletions.
Binary file modified bun.lockb
Binary file not shown.
177 changes: 1 addition & 176 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
/** @jsxFrag */

import { describe, expect, test } from "vitest";
import {
ImageResponse,
toReactNode,
unstable_createNodejsStream,
} from "./index.js";
import { ImageResponse, unstable_createNodejsStream } from "./index.js";

describe("ImageResponse", () => {
test("default", () => {
Expand All @@ -24,174 +20,3 @@ describe("unstable_createNodejsStream", () => {
).toBeDefined();
});
});

describe("toReactNode", () => {
test("primitive", () => {
expect(toReactNode("hello")).toMatchInlineSnapshot(`
"hello"
`);
expect(toReactNode(1)).toMatchInlineSnapshot("1");
expect(toReactNode([1, "hello"])).toMatchInlineSnapshot(`
[
1,
"hello",
]
`);
});

test("empty", () => {
expect(toReactNode(<div />)).toMatchInlineSnapshot(`
{
"key": null,
"props": {
"children": [],
},
"type": "div",
}
`);
});

test("children", () => {
expect(toReactNode(<div>hello</div>)).toMatchInlineSnapshot(`
{
"key": null,
"props": {
"children": "hello",
},
"type": "div",
}
`);
});

test("JSX children", () => {
expect(
toReactNode(
<div>
<div>hello</div>
<div>world</div>
<div>{69}</div>
</div>,
),
).toMatchInlineSnapshot(`
{
"key": null,
"props": {
"children": [
{
"key": null,
"props": {
"children": "hello",
},
"type": "div",
},
{
"key": null,
"props": {
"children": "world",
},
"type": "div",
},
{
"key": null,
"props": {
"children": 69,
},
"type": "div",
},
],
},
"type": "div",
}
`);
});

test("fragment", () => {
expect(
toReactNode(
<div>
<>
<div>hello</div>
<div>world</div>
</>
</div>,
),
).toMatchInlineSnapshot(`
{
"key": null,
"props": {
"children": [
{
"key": null,
"props": {
"children": "hello",
},
"type": "div",
},
{
"key": null,
"props": {
"children": "world",
},
"type": "div",
},
],
},
"type": "div",
}
`);
});

test("array", () => {
expect(
toReactNode([<div>hello</div>, <div>world</div>]),
).toMatchInlineSnapshot(`
[
{
"key": null,
"props": {
"children": "hello",
},
"type": "div",
},
{
"key": null,
"props": {
"children": "world",
},
"type": "div",
},
]
`);
});

test("props", () => {
expect(
toReactNode(
<div
className="lol"
foo="bar"
baz={{ barry: true }}
style={{ backgroundColor: "red" }}
>
ok
</div>,
),
).toMatchInlineSnapshot(`
{
"key": null,
"props": {
"baz": {
"barry": true,
},
"children": "ok",
"className": "lol",
"foo": "bar",
"style": {
"backgroundColor": "red",
},
},
"type": "div",
}
`);
});
});
42 changes: 1 addition & 41 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import * as Og from "@wevm/vercel-og";
import type { Child } from "hono/jsx";
import type { HtmlEscapedString } from "hono/utils/html";

type HonoElement = HtmlEscapedString | Promise<HtmlEscapedString>;
type ReactElement = {
type: string;
props: object;
key: string | null;
};
import { type HonoElement, toReactNode } from "./utils.js";

export class ImageResponse extends Og.ImageResponse {
constructor(
Expand All @@ -22,35 +14,3 @@ export const unstable_createNodejsStream = (
element: HonoElement,
options?: Parameters<typeof Og.unstable_createNodejsStream>[1],
) => Og.unstable_createNodejsStream(toReactNode(element), options);

export function toReactNode<
const jsx extends Child,
returnType = jsx extends HonoElement[]
? ReactElement[]
: jsx extends HonoElement
? ReactElement
: jsx,
>(jsx_: jsx): returnType {
const jsx = jsx_ as Exclude<Child, Promise<string>>;

if (Array.isArray(jsx))
return jsx.map((child) => toReactNode(child)) as returnType;
if (typeof jsx === "string") return jsx as returnType;
if (typeof jsx === "number") return jsx as returnType;
if (typeof jsx.tag === "function") return toReactNode(jsx.children); // fragment

const { tag, props } = jsx;

const children = jsx.children?.map((child) =>
toReactNode(child as HonoElement),
);

return {
type: tag,
key: null,
props: {
...props,
children: children.length === 1 ? children[0] : children,
},
} as returnType;
}
4 changes: 3 additions & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"exports": {
".": {
"types": "./lib/index.d.ts",
"worker": "./lib/worker.js",
"default": "./lib/index.js"
}
},
Expand All @@ -16,6 +17,7 @@
"hono": ">=3"
},
"dependencies": {
"@wevm/vercel-og": "~0.6.10"
"@wevm/vercel-og": "~0.6.10",
"workers-og": "~0.0.20"
}
}
Loading

0 comments on commit 5248d06

Please sign in to comment.