{"version":3,"file":"response.cjs","names":["render","CONTAINER"],"sources":["../../src/core/response.ts"],"sourcesContent":["import type { ReactNode } from 'react';\nimport { CONTAINER } from './constants';\nimport { type RenderOptions, render } from './render';\nimport type { VNode } from './satori';\nimport type { MayBePromise, OnlyProps } from './types';\n\nexport interface RenderAsResponseOptions\n\textends OnlyProps<ResponseInit, 'headers' | 'status' | 'statusText' | 'cf'> {\n\tformat?: 'svg' | 'png';\n}\n\nexport function renderAsResponse(\n\tinput: () => MayBePromise<[ReactNode | VNode, RenderOptions | undefined]>,\n\toptions?: RenderAsResponseOptions,\n) {\n\tconst isSvg = options?.format === 'svg';\n\n\tconst headers = new Headers(options?.headers);\n\tif (!headers.has('Cache-Control')) {\n\t\tif (\n\t\t\ttypeof process === 'object' &&\n\t\t\tprocess !== null &&\n\t\t\ttypeof process.env === 'object' &&\n\t\t\tprocess.env !== null &&\n\t\t\tprocess.env.NODE_ENV === 'development'\n\t\t) {\n\t\t\theaders.set('Cache-Control', 'no-cache, no-store');\n\t\t} else {\n\t\t\theaders.set(\n\t\t\t\t'Cache-Control',\n\t\t\t\t'public, immutable, no-transform, max-age=31536000',\n\t\t\t);\n\t\t}\n\t}\n\theaders.set('Content-Type', isSvg ? 'image/svg+xml' : 'image/png');\n\tconst init: ResponseInit = {\n\t\theaders,\n\t\tstatus: options?.status,\n\t\tstatusText: options?.statusText,\n\t};\n\t// Add `cf` options if provided\n\tif (options && typeof options === 'object' && 'cf' in options) {\n\t\tObject.assign(init, { cf: options.cf });\n\t}\n\n\tconst body = async (): Promise<string | Uint8Array<ArrayBuffer>> => {\n\t\tconst [element, renderOptions] = await input();\n\t\tconst renderer = render(element, renderOptions);\n\t\tconst { image } = isSvg ? await renderer.asSvg() : await renderer.asPng();\n\t\treturn image;\n\t};\n\n\tconst stream = (): ReadableStream => {\n\t\treturn new ReadableStream({\n\t\t\tstart(controller) {\n\t\t\t\tbody()\n\t\t\t\t\t.then((body) => {\n\t\t\t\t\t\tconst bytes =\n\t\t\t\t\t\t\ttypeof body === 'string' ? CONTAINER.encoder.encode(body) : body;\n\t\t\t\t\t\tcontroller.enqueue(bytes);\n\t\t\t\t\t\tcontroller.close();\n\t\t\t\t\t})\n\t\t\t\t\t.catch((e) => {\n\t\t\t\t\t\tcontroller.error(e);\n\t\t\t\t\t});\n\t\t\t},\n\t\t});\n\t};\n\n\treturn {\n\t\tbody,\n\t\tstream,\n\t\tinit,\n\t};\n}\n\n/** An interface representing options for ImageResponse */\nexport interface ImageResponseOptions\n\textends RenderOptions,\n\t\tRenderAsResponseOptions {}\n\n/** A class for rendering {@link ReactNode} to image as {@link Response} */\nexport class ImageResponse extends Response {\n\t/**\n\t * Creates an instance of {@link ImageResponse}\n\t *\n\t * @param element The {@link ReactNode}\n\t * @param options The {@link ImageResponseOptions}\n\t */\n\tconstructor(element: ReactNode | VNode, options?: ImageResponseOptions) {\n\t\tconst { stream, init } = renderAsResponse(\n\t\t\t() => [element, options],\n\t\t\toptions,\n\t\t);\n\n\t\tsuper(stream(), init);\n\t}\n\n\tstatic async async(\n\t\telement: ReactNode | VNode,\n\t\toptions?: ImageResponseOptions,\n\t): Promise<Response> {\n\t\tconst { body, init } = renderAsResponse(() => [element, options], options);\n\n\t\treturn new Response(await body(), init);\n\t}\n}\n"],"mappings":";;;AAWA,SAAgB,iBACf,OACA,SACC;CACD,MAAM,QAAQ,SAAS,WAAW;CAElC,MAAM,UAAU,IAAI,QAAQ,SAAS,OAAO;CAC5C,IAAI,CAAC,QAAQ,IAAI,eAAe,GAC/B,IACC,OAAO,YAAY,YACnB,YAAY,QACZ,OAAO,QAAQ,QAAQ,YACvB,QAAQ,QAAQ,QAChB,QAAQ,IAAI,aAAa,eAEzB,QAAQ,IAAI,iBAAiB,oBAAoB;MAEjD,QAAQ,IACP,iBACA,mDACD;CAGF,QAAQ,IAAI,gBAAgB,QAAQ,kBAAkB,WAAW;CACjE,MAAM,OAAqB;EAC1B;EACA,QAAQ,SAAS;EACjB,YAAY,SAAS;CAC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