takumi-js 1.0.8 → 1.0.10

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/dist/index.cjs CHANGED
@@ -1,3 +1,3 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_render = require("./render-BzJnfhcB.cjs");
2
+ const require_render = require("./render-Cwo-VZ54.cjs");
3
3
  exports.render = require_render.render;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { n as render, t as RenderOptions } from "./render-B4oZcaAM.cjs";
1
+ import { n as render, t as RenderOptions } from "./render-mfKqlvP8.cjs";
2
2
  import { ContainerNode, FetchResourcesOptions, ImageNode, Node, NodeAttributes, NodeMetadata, ReactElementLike, TextNode } from "@takumi-rs/helpers";
3
3
  import { AnimationFrameSource, AnimationOutputFormat, AnimationSceneSource, ConstructRendererOptions, DitheringAlgorithm, EncodeFramesOptions, Font, FontDetails, ImageSource, Keyframes, KeyframesMap, KeyframesRuleList, MeasuredNode, MeasuredTextRun, OutputFormat, RenderAnimationOptions } from "@takumi-rs/core";
4
4
 
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { n as render, t as RenderOptions } from "./render-JdCdrWfJ.mjs";
1
+ import { n as render, t as RenderOptions } from "./render-D_xiyqZH.mjs";
2
2
  import { ContainerNode, FetchResourcesOptions, ImageNode, Node, NodeAttributes, NodeMetadata, ReactElementLike, TextNode } from "@takumi-rs/helpers";
3
3
  import { AnimationFrameSource, AnimationOutputFormat, AnimationSceneSource, ConstructRendererOptions, DitheringAlgorithm, EncodeFramesOptions, Font, FontDetails, ImageSource, Keyframes, KeyframesMap, KeyframesRuleList, MeasuredNode, MeasuredTextRun, OutputFormat, RenderAnimationOptions } from "@takumi-rs/core";
4
4
 
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { t as render } from "./render-EhI8Ytcl.mjs";
1
+ import { t as render } from "./render-M8h1fYtV.mjs";
2
2
  export { render };
@@ -86,6 +86,26 @@ function transformElement(element, options) {
86
86
  if (typeof element === "string") return (0, _takumi_rs_helpers_html.fromHtml)(element);
87
87
  return (0, _takumi_rs_helpers_jsx.fromJsx)(element, options?.jsx);
88
88
  }
89
+ /**
90
+ * Renders a React element, HTML string, or Takumi node tree into an image.
91
+ *
92
+ * This function automatically detects the best renderer for your environment (native Rust on Node.js,
93
+ * WASM on Edge/Workers) and handles resource fetching (fonts, images) and emoji extraction.
94
+ *
95
+ * @example
96
+ * ```tsx
97
+ * import { render } from "takumi-js";
98
+ *
99
+ * const buffer = await render(
100
+ * <div tw="bg-blue-500 text-white p-4">Hello World</div>,
101
+ * { width: 1200, height: 630 }
102
+ * );
103
+ * ```
104
+ *
105
+ * @param element - The content to render. Can be a JSX element (React-like), an HTML string, or a pre-constructed node tree.
106
+ * @param options - Configuration for rendering, including dimensions, format, fonts, and more.
107
+ * @returns A promise that resolves to the rendered image data (Buffer/Uint8Array).
108
+ */
89
109
  async function render(element, options) {
90
110
  const imports = await getImports(options && "module" in options ? options.module : void 0);
91
111
  const isExternalRenderer = options && "renderer" in options;
@@ -37,6 +37,26 @@ type RenderOptionsWithRenderer = InnerRenderOptions & {
37
37
  };
38
38
  type RenderOptionsWithoutRenderer = Omit<RenderOptionsWithRenderer, "renderer"> & ManagedRendererOptions;
39
39
  type RenderOptions = RenderOptionsWithRenderer | RenderOptionsWithoutRenderer;
40
+ /**
41
+ * Renders a React element, HTML string, or Takumi node tree into an image.
42
+ *
43
+ * This function automatically detects the best renderer for your environment (native Rust on Node.js,
44
+ * WASM on Edge/Workers) and handles resource fetching (fonts, images) and emoji extraction.
45
+ *
46
+ * @example
47
+ * ```tsx
48
+ * import { render } from "takumi-js";
49
+ *
50
+ * const buffer = await render(
51
+ * <div tw="bg-blue-500 text-white p-4">Hello World</div>,
52
+ * { width: 1200, height: 630 }
53
+ * );
54
+ * ```
55
+ *
56
+ * @param element - The content to render. Can be a JSX element (React-like), an HTML string, or a pre-constructed node tree.
57
+ * @param options - Configuration for rendering, including dimensions, format, fonts, and more.
58
+ * @returns A promise that resolves to the rendered image data (Buffer/Uint8Array).
59
+ */
40
60
  declare function render(element: ReactNode | ReactElementLike | string, options?: RenderOptions): Promise<Uint8Array<ArrayBufferLike> | Buffer<ArrayBufferLike>>;
41
61
  //#endregion
42
62
  export { render as n, RenderOptions as t };
@@ -85,6 +85,26 @@ function transformElement(element, options) {
85
85
  if (typeof element === "string") return fromHtml(element);
86
86
  return fromJsx(element, options?.jsx);
87
87
  }
88
+ /**
89
+ * Renders a React element, HTML string, or Takumi node tree into an image.
90
+ *
91
+ * This function automatically detects the best renderer for your environment (native Rust on Node.js,
92
+ * WASM on Edge/Workers) and handles resource fetching (fonts, images) and emoji extraction.
93
+ *
94
+ * @example
95
+ * ```tsx
96
+ * import { render } from "takumi-js";
97
+ *
98
+ * const buffer = await render(
99
+ * <div tw="bg-blue-500 text-white p-4">Hello World</div>,
100
+ * { width: 1200, height: 630 }
101
+ * );
102
+ * ```
103
+ *
104
+ * @param element - The content to render. Can be a JSX element (React-like), an HTML string, or a pre-constructed node tree.
105
+ * @param options - Configuration for rendering, including dimensions, format, fonts, and more.
106
+ * @returns A promise that resolves to the rendered image data (Buffer/Uint8Array).
107
+ */
88
108
  async function render(element, options) {
89
109
  const imports = await getImports(options && "module" in options ? options.module : void 0);
90
110
  const isExternalRenderer = options && "renderer" in options;
@@ -37,6 +37,26 @@ type RenderOptionsWithRenderer = InnerRenderOptions & {
37
37
  };
38
38
  type RenderOptionsWithoutRenderer = Omit<RenderOptionsWithRenderer, "renderer"> & ManagedRendererOptions;
39
39
  type RenderOptions = RenderOptionsWithRenderer | RenderOptionsWithoutRenderer;
40
+ /**
41
+ * Renders a React element, HTML string, or Takumi node tree into an image.
42
+ *
43
+ * This function automatically detects the best renderer for your environment (native Rust on Node.js,
44
+ * WASM on Edge/Workers) and handles resource fetching (fonts, images) and emoji extraction.
45
+ *
46
+ * @example
47
+ * ```tsx
48
+ * import { render } from "takumi-js";
49
+ *
50
+ * const buffer = await render(
51
+ * <div tw="bg-blue-500 text-white p-4">Hello World</div>,
52
+ * { width: 1200, height: 630 }
53
+ * );
54
+ * ```
55
+ *
56
+ * @param element - The content to render. Can be a JSX element (React-like), an HTML string, or a pre-constructed node tree.
57
+ * @param options - Configuration for rendering, including dimensions, format, fonts, and more.
58
+ * @returns A promise that resolves to the rendered image data (Buffer/Uint8Array).
59
+ */
40
60
  declare function render(element: ReactNode | ReactElementLike | string, options?: RenderOptions): Promise<Uint8Array<ArrayBufferLike> | Buffer<ArrayBufferLike>>;
41
61
  //#endregion
42
62
  export { render as n, RenderOptions as t };
package/dist/response.cjs CHANGED
@@ -2,7 +2,7 @@ Object.defineProperties(exports, {
2
2
  __esModule: { value: true },
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
- const require_render = require("./render-BzJnfhcB.cjs");
5
+ const require_render = require("./render-Cwo-VZ54.cjs");
6
6
  //#region src/response/index.ts
7
7
  function mergeOptions(defaultOptions, options) {
8
8
  if (!defaultOptions) return options;
@@ -29,6 +29,26 @@ function defaultErrorHandler(error) {
29
29
  console.error("Failed to render image.");
30
30
  console.error(error);
31
31
  }
32
+ /**
33
+ * Creates a reusable ImageResponse factory with shared configuration.
34
+ *
35
+ * Use this to avoid repeating configuration like fonts, stylesheets, or cache headers
36
+ * across multiple API routes.
37
+ *
38
+ * @example
39
+ * ```tsx
40
+ * const myImageResponse = createImageResponse({
41
+ * fonts: [{ name: "Inter", data: fontBuffer }],
42
+ * headers: { "Cache-Control": "public, max-age=31536000" }
43
+ * });
44
+ *
45
+ * export function GET() {
46
+ * return myImageResponse(<div>Custom Og Image</div>);
47
+ * }
48
+ * ```
49
+ *
50
+ * @param defaultOptions - Options that will be applied to every response created by this factory.
51
+ */
32
52
  function createImageResponse(defaultOptions) {
33
53
  return function imageResponse(element, options) {
34
54
  const mergedOptions = mergeOptions(defaultOptions, options);
@@ -63,6 +83,29 @@ function createImageResponse(defaultOptions) {
63
83
  };
64
84
  }
65
85
  let defaultImageResponse;
86
+ /**
87
+ * A universal ImageResponse class for generating images in API routes.
88
+ *
89
+ * Drop-in compatible with `next/og`'s `ImageResponse`. It supports React elements,
90
+ * custom fonts, Tailwind CSS (via `tw` prop), and various image formats.
91
+ *
92
+ * @example
93
+ * ```tsx
94
+ * import { ImageResponse } from "takumi-js/response";
95
+ *
96
+ * export function GET() {
97
+ * return new ImageResponse(
98
+ * <div tw="flex h-full w-full items-center justify-center bg-white">
99
+ * <h1 tw="text-6xl font-bold">Hello World</h1>
100
+ * </div>,
101
+ * { width: 1200, height: 630 }
102
+ * );
103
+ * }
104
+ * ```
105
+ *
106
+ * @param component - The JSX element to render.
107
+ * @param options - Rendering and response options.
108
+ */
66
109
  var ImageResponse = class extends Response {
67
110
  ready;
68
111
  constructor(component, options) {
@@ -1,4 +1,4 @@
1
- import { t as RenderOptions } from "./render-B4oZcaAM.cjs";
1
+ import { t as RenderOptions } from "./render-mfKqlvP8.cjs";
2
2
  import { ReactNode } from "react";
3
3
 
4
4
  //#region src/response/index.d.ts
@@ -9,7 +9,50 @@ type ImageResponseOptions = RenderOptions & ResponseInit & {
9
9
  onError?: (error: unknown) => void | Promise<void>;
10
10
  };
11
11
  type ImageResponseFactory = (component: ReactNode, options?: ImageResponseOptions) => ImageResponseResult;
12
+ /**
13
+ * Creates a reusable ImageResponse factory with shared configuration.
14
+ *
15
+ * Use this to avoid repeating configuration like fonts, stylesheets, or cache headers
16
+ * across multiple API routes.
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * const myImageResponse = createImageResponse({
21
+ * fonts: [{ name: "Inter", data: fontBuffer }],
22
+ * headers: { "Cache-Control": "public, max-age=31536000" }
23
+ * });
24
+ *
25
+ * export function GET() {
26
+ * return myImageResponse(<div>Custom Og Image</div>);
27
+ * }
28
+ * ```
29
+ *
30
+ * @param defaultOptions - Options that will be applied to every response created by this factory.
31
+ */
12
32
  declare function createImageResponse(defaultOptions?: ImageResponseOptions): ImageResponseFactory;
33
+ /**
34
+ * A universal ImageResponse class for generating images in API routes.
35
+ *
36
+ * Drop-in compatible with `next/og`'s `ImageResponse`. It supports React elements,
37
+ * custom fonts, Tailwind CSS (via `tw` prop), and various image formats.
38
+ *
39
+ * @example
40
+ * ```tsx
41
+ * import { ImageResponse } from "takumi-js/response";
42
+ *
43
+ * export function GET() {
44
+ * return new ImageResponse(
45
+ * <div tw="flex h-full w-full items-center justify-center bg-white">
46
+ * <h1 tw="text-6xl font-bold">Hello World</h1>
47
+ * </div>,
48
+ * { width: 1200, height: 630 }
49
+ * );
50
+ * }
51
+ * ```
52
+ *
53
+ * @param component - The JSX element to render.
54
+ * @param options - Rendering and response options.
55
+ */
13
56
  declare class ImageResponse extends Response {
14
57
  readonly ready: Promise<void>;
15
58
  constructor(component: ReactNode, options?: ImageResponseOptions);
@@ -1,4 +1,4 @@
1
- import { t as RenderOptions } from "./render-JdCdrWfJ.mjs";
1
+ import { t as RenderOptions } from "./render-D_xiyqZH.mjs";
2
2
  import { ReactNode } from "react";
3
3
 
4
4
  //#region src/response/index.d.ts
@@ -9,7 +9,50 @@ type ImageResponseOptions = RenderOptions & ResponseInit & {
9
9
  onError?: (error: unknown) => void | Promise<void>;
10
10
  };
11
11
  type ImageResponseFactory = (component: ReactNode, options?: ImageResponseOptions) => ImageResponseResult;
12
+ /**
13
+ * Creates a reusable ImageResponse factory with shared configuration.
14
+ *
15
+ * Use this to avoid repeating configuration like fonts, stylesheets, or cache headers
16
+ * across multiple API routes.
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * const myImageResponse = createImageResponse({
21
+ * fonts: [{ name: "Inter", data: fontBuffer }],
22
+ * headers: { "Cache-Control": "public, max-age=31536000" }
23
+ * });
24
+ *
25
+ * export function GET() {
26
+ * return myImageResponse(<div>Custom Og Image</div>);
27
+ * }
28
+ * ```
29
+ *
30
+ * @param defaultOptions - Options that will be applied to every response created by this factory.
31
+ */
12
32
  declare function createImageResponse(defaultOptions?: ImageResponseOptions): ImageResponseFactory;
33
+ /**
34
+ * A universal ImageResponse class for generating images in API routes.
35
+ *
36
+ * Drop-in compatible with `next/og`'s `ImageResponse`. It supports React elements,
37
+ * custom fonts, Tailwind CSS (via `tw` prop), and various image formats.
38
+ *
39
+ * @example
40
+ * ```tsx
41
+ * import { ImageResponse } from "takumi-js/response";
42
+ *
43
+ * export function GET() {
44
+ * return new ImageResponse(
45
+ * <div tw="flex h-full w-full items-center justify-center bg-white">
46
+ * <h1 tw="text-6xl font-bold">Hello World</h1>
47
+ * </div>,
48
+ * { width: 1200, height: 630 }
49
+ * );
50
+ * }
51
+ * ```
52
+ *
53
+ * @param component - The JSX element to render.
54
+ * @param options - Rendering and response options.
55
+ */
13
56
  declare class ImageResponse extends Response {
14
57
  readonly ready: Promise<void>;
15
58
  constructor(component: ReactNode, options?: ImageResponseOptions);
package/dist/response.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as render } from "./render-EhI8Ytcl.mjs";
1
+ import { t as render } from "./render-M8h1fYtV.mjs";
2
2
  //#region src/response/index.ts
3
3
  function mergeOptions(defaultOptions, options) {
4
4
  if (!defaultOptions) return options;
@@ -25,6 +25,26 @@ function defaultErrorHandler(error) {
25
25
  console.error("Failed to render image.");
26
26
  console.error(error);
27
27
  }
28
+ /**
29
+ * Creates a reusable ImageResponse factory with shared configuration.
30
+ *
31
+ * Use this to avoid repeating configuration like fonts, stylesheets, or cache headers
32
+ * across multiple API routes.
33
+ *
34
+ * @example
35
+ * ```tsx
36
+ * const myImageResponse = createImageResponse({
37
+ * fonts: [{ name: "Inter", data: fontBuffer }],
38
+ * headers: { "Cache-Control": "public, max-age=31536000" }
39
+ * });
40
+ *
41
+ * export function GET() {
42
+ * return myImageResponse(<div>Custom Og Image</div>);
43
+ * }
44
+ * ```
45
+ *
46
+ * @param defaultOptions - Options that will be applied to every response created by this factory.
47
+ */
28
48
  function createImageResponse(defaultOptions) {
29
49
  return function imageResponse(element, options) {
30
50
  const mergedOptions = mergeOptions(defaultOptions, options);
@@ -59,6 +79,29 @@ function createImageResponse(defaultOptions) {
59
79
  };
60
80
  }
61
81
  let defaultImageResponse;
82
+ /**
83
+ * A universal ImageResponse class for generating images in API routes.
84
+ *
85
+ * Drop-in compatible with `next/og`'s `ImageResponse`. It supports React elements,
86
+ * custom fonts, Tailwind CSS (via `tw` prop), and various image formats.
87
+ *
88
+ * @example
89
+ * ```tsx
90
+ * import { ImageResponse } from "takumi-js/response";
91
+ *
92
+ * export function GET() {
93
+ * return new ImageResponse(
94
+ * <div tw="flex h-full w-full items-center justify-center bg-white">
95
+ * <h1 tw="text-6xl font-bold">Hello World</h1>
96
+ * </div>,
97
+ * { width: 1200, height: 630 }
98
+ * );
99
+ * }
100
+ * ```
101
+ *
102
+ * @param component - The JSX element to render.
103
+ * @param options - Rendering and response options.
104
+ */
62
105
  var ImageResponse = class extends Response {
63
106
  ready;
64
107
  constructor(component, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "takumi-js",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "All-in-one Takumi package for Node.js and WebAssembly runtimes.",
5
5
  "license": "(MIT OR Apache-2.0)",
6
6
  "author": {
@@ -69,9 +69,9 @@
69
69
  "test:bundlers": "bun test tests/bundlers.test.ts"
70
70
  },
71
71
  "dependencies": {
72
- "@takumi-rs/core": "1.0.8",
73
- "@takumi-rs/helpers": "1.0.8",
74
- "@takumi-rs/wasm": "1.0.8"
72
+ "@takumi-rs/core": "1.0.10",
73
+ "@takumi-rs/helpers": "1.0.10",
74
+ "@takumi-rs/wasm": "1.0.10"
75
75
  },
76
76
  "devDependencies": {
77
77
  "@types/bun": "catalog:",