@vercel/og 0.4.0 → 0.4.1

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.
@@ -19,7 +19,7 @@ var _a, _b;
19
19
  var isDev = ((_b = (_a = globalThis == null ? void 0 : globalThis.process) == null ? void 0 : _a.env) == null ? void 0 : _b.NODE_ENV) === "development";
20
20
  var ImageResponse = class {
21
21
  constructor(element, options = {}) {
22
- if (!new Error().stack.includes("async_hooks") || typeof Response === "undefined" || typeof ReadableStream === "undefined") {
22
+ if (typeof Response === "undefined" || typeof ReadableStream === "undefined") {
23
23
  throw new Error("The `ImageResponse` API is not supported in this runtime, use the `unstable_createNodejsStream` API instead or switch to the Vercel Edge Runtime.");
24
24
  }
25
25
  const result = new ReadableStream({
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.node.ts"],"sourcesContent":["import type { ReactElement } from 'react'\nimport type { ImageResponseNodeOptions, ImageResponseOptions } from './types'\n\n// @ts-ignore\nimport satoriMod, { init as initSatori } from 'satori/wasm'\nimport initYoga from 'yoga-wasm-web'\nimport * as resvg from '@resvg/resvg-wasm'\n\nimport { Readable } from 'stream'\nimport fs from 'fs'\nimport { fileURLToPath } from 'url'\n\nimport render from './og'\n\nconst satori = satoriMod.default || satoriMod\n\nconst fontData = fs.readFileSync(\n fileURLToPath(`${import.meta.url}/../noto-sans-v27-latin-regular.ttf`)\n)\nconst yoga_wasm = fs.readFileSync(\n fileURLToPath(`${import.meta.url}/../yoga.wasm`)\n)\nconst resvg_wasm = fs.readFileSync(\n fileURLToPath(`${import.meta.url}/../resvg.wasm`)\n)\n\nconst initializedResvg = resvg.initWasm(resvg_wasm)\nconst initializedYoga = initYoga(yoga_wasm).then((yoga) => initSatori(yoga))\n\nconst isDev = globalThis?.process?.env?.NODE_ENV === 'development'\n\nexport class ImageResponse {\n constructor(element: ReactElement, options: ImageResponseOptions = {}) {\n // This is a workaround for Next.js to detect if the API is used in\n // pages/api or app API routes. When used in pages/api, we can't return\n // a Response instance and need another interface instead.\n // If the stack includes `async_hooks` we are almost sure that we are\n // in the app API routes.\n if (\n !new Error().stack.includes('async_hooks') ||\n typeof Response === 'undefined' ||\n typeof ReadableStream === 'undefined'\n ) {\n throw new Error(\n 'The `ImageResponse` API is not supported in this runtime, use the `unstable_createNodejsStream` API instead or switch to the Vercel Edge Runtime.'\n )\n }\n\n const result = new ReadableStream({\n async start(controller) {\n await initializedYoga\n await initializedResvg\n const fonts = [\n {\n name: 'sans serif',\n data: fontData,\n weight: 700,\n style: 'normal',\n },\n ]\n\n const result = await render(satori, resvg, options, fonts, element)\n\n controller.enqueue(result)\n controller.close()\n },\n })\n\n return new Response(result, {\n headers: {\n 'content-type': 'image/png',\n 'cache-control': isDev\n ? 'no-cache, no-store'\n : 'public, immutable, no-transform, max-age=31536000',\n ...options.headers,\n },\n status: options.status,\n statusText: options.statusText,\n })\n }\n}\n\n/**\n * Creates a pipeable stream of the rendered image in a lambda function.\n * All parameters are the same as `ImageResponse`.\n * @example\n * ```js\n * import { unstable_createNodejsStream } from '@vercel/og'\n *\n * export default async (req, res) => {\n * const stream = await unstable_createNodejsStream(<div>Hello World</div>, { ... })\n * res.setHeader('Content-Type', 'image/png')\n * res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')\n * res.statusCode = 200\n * res.statusMessage = 'OK'\n * stream.pipe(res)\n * }\n * ```\n */\nexport async function unstable_createNodejsStream(\n element: ReactElement,\n options: Omit<\n ImageResponseNodeOptions,\n 'status' | 'statusText' | 'headers'\n > = {}\n) {\n await initializedYoga\n await initializedResvg\n const fonts = [\n {\n name: 'sans serif',\n data: fontData,\n weight: 700,\n style: 'normal',\n },\n ]\n\n const result = await render(satori, resvg, options, fonts, element)\n return Readable.from(Buffer.from(result))\n}\n"],"mappings":";;;;;AAIA;AACA;AACA;AAEA;AACA;AACA;AAIA,IAAM,SAAS,UAAU,WAAW;AAEpC,IAAM,WAAW,GAAG,aAClB,cAAc,GAAG,YAAY,wCAAwC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R,OAAO;AAAA,IACT;AAAA,EACF;AAEA,QAAM,SAAS,MAAM,OAAO,QAAQ,OAAO,SAAS,OAAO,OAAO;AAClE,SAAO,SAAS,KAAK,OAAO,KAAK,MAAM,CAAC;AAC1C;","names":[]}
1
+ {"version":3,"sources":["../src/index.node.ts"],"sourcesContent":["import type { ReactElement } from 'react'\nimport type { ImageResponseNodeOptions, ImageResponseOptions } from './types'\n\n// @ts-ignore\nimport satoriMod, { init as initSatori } from 'satori/wasm'\nimport initYoga from 'yoga-wasm-web'\nimport * as resvg from '@resvg/resvg-wasm'\n\nimport { Readable } from 'stream'\nimport fs from 'fs'\nimport { fileURLToPath } from 'url'\n\nimport render from './og'\n\nconst satori = satoriMod.default || satoriMod\n\nconst fontData = fs.readFileSync(\n fileURLToPath(`${import.meta.url}/../noto-sans-v27-latin-regular.ttf`)\n)\nconst yoga_wasm = fs.readFileSync(\n fileURLToPath(`${import.meta.url}/../yoga.wasm`)\n)\nconst resvg_wasm = fs.readFileSync(\n fileURLToPath(`${import.meta.url}/../resvg.wasm`)\n)\n\nconst initializedResvg = resvg.initWasm(resvg_wasm)\nconst initializedYoga = initYoga(yoga_wasm).then((yoga) => initSatori(yoga))\n\nconst isDev = globalThis?.process?.env?.NODE_ENV === 'development'\n\nexport class ImageResponse {\n constructor(element: ReactElement, options: ImageResponseOptions = {}) {\n // This is a workaround for Next.js to detect if the API is used in\n // pages/api or app API routes. When used in pages/api, we can't return\n // a Response instance and need another interface instead.\n if (\n typeof Response === 'undefined' ||\n typeof ReadableStream === 'undefined'\n ) {\n throw new Error(\n 'The `ImageResponse` API is not supported in this runtime, use the `unstable_createNodejsStream` API instead or switch to the Vercel Edge Runtime.'\n )\n }\n\n const result = new ReadableStream({\n async start(controller) {\n await initializedYoga\n await initializedResvg\n const fonts = [\n {\n name: 'sans serif',\n data: fontData,\n weight: 700,\n style: 'normal',\n },\n ]\n\n const result = await render(satori, resvg, options, fonts, element)\n\n controller.enqueue(result)\n controller.close()\n },\n })\n\n return new Response(result, {\n headers: {\n 'content-type': 'image/png',\n 'cache-control': isDev\n ? 'no-cache, no-store'\n : 'public, immutable, no-transform, max-age=31536000',\n ...options.headers,\n },\n status: options.status,\n statusText: options.statusText,\n })\n }\n}\n\n/**\n * Creates a pipeable stream of the rendered image in a lambda function.\n * All parameters are the same as `ImageResponse`.\n * @example\n * ```js\n * import { unstable_createNodejsStream } from '@vercel/og'\n *\n * export default async (req, res) => {\n * const stream = await unstable_createNodejsStream(<div>Hello World</div>, { ... })\n * res.setHeader('Content-Type', 'image/png')\n * res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')\n * res.statusCode = 200\n * res.statusMessage = 'OK'\n * stream.pipe(res)\n * }\n * ```\n */\nexport async function unstable_createNodejsStream(\n element: ReactElement,\n options: Omit<\n ImageResponseNodeOptions,\n 'status' | 'statusText' | 'headers'\n > = {}\n) {\n await initializedYoga\n await initializedResvg\n const fonts = [\n {\n name: 'sans serif',\n data: fontData,\n weight: 700,\n style: 'normal',\n },\n ]\n\n const result = await render(satori, resvg, options, fonts, element)\n return Readable.from(Buffer.from(result))\n}\n"],"mappings":";;;;;AAIA;AACA;AACA;AAEA;AACA;AACA;AAIA,IAAM,SAAS,UAAU,WAAW;AAEpC,IAAM,WAAW,GAAG,aAClB,cAAc,GAAG,YAAY,wCAAwC,CACvE;AACA,IAAM,YAAY,GAAG,aACnB,cAAc,GAAG,YAAY,kBAAkB,CACjD;AACA,IAAM,aAAa,GAAG,aACpB,cAAc,GAAG,YAAY,mBAAmB,CAClD;AAEA,IAAM,mBAAmB,AAAM,eAAS,UAAU;AAClD,IAAM,kBAAkB,SAAS,SAAS,EAAE,KAAK,CAAC,SAAS,WAAW,IAAI,CAAC;AA3B3E;AA6BA,IAAM,QAAQ,sDAAY,YAAZ,mBAAqB,QAArB,mBAA0B,cAAa;AAE9C,IAAM,gBAAN,MAAoB;AAAA,EACzB,YAAY,SAAuB,UAAgC,CAAC,GAAG;AAIrE,QACE,OAAO,aAAa,eACpB,OAAO,mBAAmB,aAC1B;AACA,YAAM,IAAI,MACR,mJACF;AAAA,IACF;AAEA,UAAM,SAAS,IAAI,eAAe;AAAA,MAChC,MAAM,MAAM,YAAY;AACtB,cAAM;AACN,cAAM;AACN,cAAM,QAAQ;AAAA,UACZ;AAAA,YACE,MAAM;AAAA,YACN,MAAM;AAAA,YACN,QAAQ;AAAA,YACR,OAAO;AAAA,UACT;AAAA,QACF;AAEA,cAAM,UAAS,MAAM,OAAO,QAAQ,OAAO,SAAS,OAAO,OAAO;AAElE,mBAAW,QAAQ,OAAM;AACzB,mBAAW,MAAM;AAAA,MACnB;AAAA,IACF,CAAC;AAED,WAAO,IAAI,SAAS,QAAQ;AAAA,MAC1B,SAAS;AAAA,QACP,gBAAgB;AAAA,QAChB,iBAAiB,QACb,uBACA;AAAA,QACJ,GAAG,QAAQ;AAAA,MACb;AAAA,MACA,QAAQ,QAAQ;AAAA,MAChB,YAAY,QAAQ;AAAA,IACtB,CAAC;AAAA,EACH;AACF;AAmBA,2CACE,SACA,UAGI,CAAC,GACL;AACA,QAAM;AACN,QAAM;AACN,QAAM,QAAQ;AAAA,IACZ;AAAA,MACE,MAAM;AAAA,MACN,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO;AAAA,IACT;AAAA,EACF;AAEA,QAAM,SAAS,MAAM,OAAO,QAAQ,OAAO,SAAS,OAAO,OAAO;AAClE,SAAO,SAAS,KAAK,OAAO,KAAK,MAAM,CAAC;AAC1C;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/og",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Generate Open Graph Images dynamically from HTML/CSS without a browser",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",