nuxt-og-image 2.2.4 → 3.0.0-beta.0

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.
Files changed (173) hide show
  1. package/README.md +2 -2
  2. package/dist/client/200.html +8 -8
  3. package/dist/client/404.html +8 -8
  4. package/dist/client/_nuxt/IconCSS.2211d324.js +1 -0
  5. package/dist/client/_nuxt/IconCSS.8f429b14.css +1 -0
  6. package/dist/client/_nuxt/builds/latest.json +1 -1
  7. package/dist/client/_nuxt/builds/meta/f5ab7749-50de-4d6e-a4e4-00f34f3fe2c6.json +1 -0
  8. package/dist/client/_nuxt/entry.434c2c45.css +1 -0
  9. package/dist/client/_nuxt/entry.e6eb9476.js +137 -0
  10. package/dist/client/_nuxt/{error-404.407d76a3.js → error-404.cd9bab3b.js} +1 -1
  11. package/dist/client/_nuxt/{error-500.531c4147.js → error-500.6f73f34f.js} +1 -1
  12. package/dist/client/grid.png +0 -0
  13. package/dist/client/index.html +8 -8
  14. package/dist/module.d.mts +43 -39
  15. package/dist/module.d.ts +43 -39
  16. package/dist/module.json +1 -1
  17. package/dist/module.mjs +341 -667
  18. package/dist/runtime/cache.d.ts +4 -4
  19. package/dist/runtime/cache.mjs +2 -1
  20. package/dist/runtime/components/OgImage/Cached.mjs +1 -1
  21. package/dist/runtime/components/OgImage/Dynamic.mjs +1 -1
  22. package/dist/runtime/components/OgImage/Screenshot.mjs +1 -1
  23. package/dist/runtime/components/OgImage/Static.mjs +1 -1
  24. package/dist/runtime/components/OgImage/WithoutCache.mjs +1 -1
  25. package/dist/runtime/components/OgImage/index.mjs +1 -1
  26. package/dist/runtime/components/Templates/Community/Nuxt.vue +183 -0
  27. package/dist/runtime/components/Templates/Official/BrandedLogo.vue +28 -0
  28. package/dist/runtime/components/Templates/Official/Fallback.vue +147 -0
  29. package/dist/runtime/components/Templates/Official/SimpleBlog.vue +33 -0
  30. package/dist/runtime/components/Templates/Official/Wave.vue +33 -0
  31. package/dist/runtime/components/Templates/Official/WithEmoji.vue +27 -0
  32. package/dist/runtime/composables/defineOgImage.d.ts +10 -6
  33. package/dist/runtime/composables/defineOgImage.mjs +21 -9
  34. package/dist/runtime/core/bindings/chromium/node.d.ts +2 -0
  35. package/dist/runtime/{nitro/providers/browser/universal.mjs → core/bindings/chromium/node.mjs} +3 -3
  36. package/dist/runtime/core/bindings/resvg/node.d.ts +6 -0
  37. package/dist/runtime/core/bindings/resvg/node.mjs +5 -0
  38. package/dist/runtime/core/bindings/resvg/wasm.d.ts +40 -0
  39. package/dist/runtime/core/bindings/resvg/wasm.mjs +7 -0
  40. package/dist/runtime/core/bindings/satori/node.d.ts +6 -0
  41. package/dist/runtime/core/bindings/satori/node.mjs +5 -0
  42. package/dist/runtime/core/bindings/satori/yoga-wasm.d.ts +6 -0
  43. package/dist/runtime/core/bindings/satori/yoga-wasm.mjs +7 -0
  44. package/dist/runtime/core/bindings/sharp/node.d.ts +2 -0
  45. package/dist/runtime/core/bindings/sharp/node.mjs +2 -0
  46. package/dist/runtime/core/bindings/sharp/wasm.d.ts +2 -0
  47. package/dist/runtime/core/bindings/sharp/wasm.mjs +2 -0
  48. package/dist/runtime/core/cache/prerender.d.ts +6 -0
  49. package/dist/runtime/core/cache/prerender.mjs +6 -0
  50. package/dist/runtime/core/env/assets.d.ts +2 -0
  51. package/dist/runtime/core/env/assets.mjs +15 -0
  52. package/dist/runtime/core/font/cache.d.ts +1 -0
  53. package/dist/runtime/core/font/cache.mjs +1 -0
  54. package/dist/runtime/core/font/fetch.d.ts +3 -0
  55. package/dist/runtime/core/font/fetch.mjs +29 -0
  56. package/dist/runtime/core/html/fetch.d.ts +3 -0
  57. package/dist/runtime/core/html/fetch.mjs +117 -0
  58. package/dist/runtime/core/options/extract.d.ts +3 -0
  59. package/dist/runtime/{nitro/utils-pure.mjs → core/options/extract.mjs} +23 -21
  60. package/dist/runtime/core/options/fetch.d.ts +3 -0
  61. package/dist/runtime/core/options/fetch.mjs +33 -0
  62. package/dist/runtime/core/options/normalise.d.ts +2 -0
  63. package/dist/runtime/{composables/util.mjs → core/options/normalise.mjs} +9 -6
  64. package/dist/runtime/core/renderers/chromium/index.d.ts +3 -0
  65. package/dist/runtime/core/renderers/chromium/index.mjs +26 -0
  66. package/dist/runtime/core/renderers/chromium/screenshot.d.ts +6 -0
  67. package/dist/runtime/core/renderers/chromium/screenshot.mjs +47 -0
  68. package/dist/runtime/core/renderers/satori/fonts.d.ts +3 -0
  69. package/dist/runtime/core/renderers/satori/fonts.mjs +8 -0
  70. package/dist/runtime/core/renderers/satori/index.d.ts +5 -0
  71. package/dist/runtime/core/renderers/satori/index.mjs +53 -0
  72. package/dist/runtime/core/renderers/satori/instances.d.ts +39 -0
  73. package/dist/runtime/core/renderers/satori/instances.mjs +17 -0
  74. package/dist/runtime/{nitro → core}/renderers/satori/plugins/encoding.mjs +1 -1
  75. package/dist/runtime/{nitro → core}/renderers/satori/plugins/imageSrc.mjs +8 -14
  76. package/dist/runtime/{nitro → core}/renderers/satori/plugins/twClasses.mjs +1 -0
  77. package/dist/runtime/core/renderers/satori/utils.d.ts +4 -0
  78. package/dist/runtime/core/renderers/satori/utils.mjs +20 -0
  79. package/dist/runtime/core/renderers/satori/vnodes.d.ts +3 -0
  80. package/dist/runtime/core/renderers/satori/vnodes.mjs +21 -0
  81. package/dist/runtime/core/utils/resolveRendererContext.d.ts +7 -0
  82. package/dist/runtime/core/utils/resolveRendererContext.mjs +76 -0
  83. package/dist/runtime/nitro/plugins/nuxt-content.d.ts +2 -0
  84. package/dist/runtime/nitro/plugins/nuxt-content.mjs +50 -0
  85. package/dist/runtime/nitro/plugins/prerender.d.ts +2 -3
  86. package/dist/runtime/nitro/plugins/prerender.mjs +24 -33
  87. package/dist/runtime/nuxt/plugins/nuxt-content-canonical-urls.mjs +29 -0
  88. package/dist/runtime/nuxt/plugins/route-rule-og-image.server.d.ts +2 -0
  89. package/dist/runtime/nuxt/plugins/route-rule-og-image.server.mjs +72 -0
  90. package/dist/runtime/{nitro/routes/debug.d.ts → server/routes/__og-image__/debug.json.d.ts} +1 -1
  91. package/dist/runtime/{nitro/routes/debug.mjs → server/routes/__og-image__/debug.json.mjs} +3 -2
  92. package/dist/runtime/server/routes/__og-image__/font-[name]-[weight].[extension].mjs +30 -0
  93. package/dist/runtime/server/routes/__og-image__/image-[path]-og.[extension].mjs +44 -0
  94. package/dist/runtime/types.d.ts +29 -24
  95. package/dist/runtime/utilts.d.ts +2 -0
  96. package/dist/runtime/utilts.mjs +8 -0
  97. package/dist/types.d.mts +3 -2
  98. package/dist/types.d.ts +3 -2
  99. package/package.json +37 -22
  100. package/dist/client/_nuxt/IconCSS.4a9d43d0.css +0 -1
  101. package/dist/client/_nuxt/IconCSS.9c30257a.js +0 -1
  102. package/dist/client/_nuxt/ImageLoader.752b0c7a.js +0 -1
  103. package/dist/client/_nuxt/ImageLoader.7571516f.css +0 -1
  104. package/dist/client/_nuxt/builds/meta/bb64bb30-cf6f-4625-97ba-06e6a0d3f8d1.json +0 -1
  105. package/dist/client/_nuxt/entry.39e39f51.css +0 -1
  106. package/dist/client/_nuxt/entry.ac864471.js +0 -135
  107. package/dist/client/_nuxt/index.dc1538d5.js +0 -1
  108. package/dist/client/_nuxt/index.ffbea0a9.css +0 -1
  109. package/dist/client/_nuxt/options.a77f5921.js +0 -1
  110. package/dist/client/_nuxt/png.41e0b446.js +0 -1
  111. package/dist/client/_nuxt/shiki.d4e62362.js +0 -7
  112. package/dist/client/_nuxt/svg.b8198280.js +0 -1
  113. package/dist/client/_nuxt/vnodes.67720126.js +0 -1
  114. package/dist/client/options/index.html +0 -15
  115. package/dist/client/png/index.html +0 -15
  116. package/dist/client/svg/index.html +0 -15
  117. package/dist/client/vnodes/index.html +0 -15
  118. package/dist/runtime/browserUtil.d.ts +0 -5
  119. package/dist/runtime/browserUtil.mjs +0 -41
  120. package/dist/runtime/components/OgImageTemplate/Fallback.vue +0 -161
  121. package/dist/runtime/composables/util.d.ts +0 -2
  122. package/dist/runtime/nitro/middleware/og.png.mjs +0 -69
  123. package/dist/runtime/nitro/middleware/playground.d.ts +0 -2
  124. package/dist/runtime/nitro/middleware/playground.mjs +0 -27
  125. package/dist/runtime/nitro/providers/browser/lambda.d.ts +0 -1
  126. package/dist/runtime/nitro/providers/browser/lambda.mjs +0 -9
  127. package/dist/runtime/nitro/providers/browser/playwright.d.ts +0 -1
  128. package/dist/runtime/nitro/providers/browser/playwright.mjs +0 -22
  129. package/dist/runtime/nitro/providers/browser/universal.d.ts +0 -2
  130. package/dist/runtime/nitro/providers/png/resvg-node.d.ts +0 -4
  131. package/dist/runtime/nitro/providers/png/resvg-node.mjs +0 -6
  132. package/dist/runtime/nitro/providers/png/resvg-wasm.d.ts +0 -3
  133. package/dist/runtime/nitro/providers/png/resvg-wasm.mjs +0 -11
  134. package/dist/runtime/nitro/providers/png/svg2png.d.ts +0 -3
  135. package/dist/runtime/nitro/providers/png/svg2png.mjs +0 -11
  136. package/dist/runtime/nitro/providers/satori/default.d.ts +0 -2
  137. package/dist/runtime/nitro/providers/satori/default.mjs +0 -4
  138. package/dist/runtime/nitro/providers/satori/yoga-wasm.d.ts +0 -3
  139. package/dist/runtime/nitro/providers/satori/yoga-wasm.mjs +0 -10
  140. package/dist/runtime/nitro/renderers/browser.d.ts +0 -3
  141. package/dist/runtime/nitro/renderers/browser.mjs +0 -36
  142. package/dist/runtime/nitro/renderers/satori/index.d.ts +0 -3
  143. package/dist/runtime/nitro/renderers/satori/index.mjs +0 -58
  144. package/dist/runtime/nitro/renderers/satori/utils.d.ts +0 -4
  145. package/dist/runtime/nitro/renderers/satori/utils.mjs +0 -60
  146. package/dist/runtime/nitro/routes/font.mjs +0 -22
  147. package/dist/runtime/nitro/routes/html.d.ts +0 -2
  148. package/dist/runtime/nitro/routes/html.mjs +0 -178
  149. package/dist/runtime/nitro/routes/options.d.ts +0 -3
  150. package/dist/runtime/nitro/routes/options.mjs +0 -35
  151. package/dist/runtime/nitro/routes/svg.mjs +0 -19
  152. package/dist/runtime/nitro/routes/vnode.d.ts +0 -2
  153. package/dist/runtime/nitro/routes/vnode.mjs +0 -19
  154. package/dist/runtime/nitro/utils-pure.d.ts +0 -3
  155. package/dist/runtime/nitro/utils.d.ts +0 -18
  156. package/dist/runtime/nitro/utils.mjs +0 -108
  157. package/dist/runtime/public-assets-optional/resvg/resvg.wasm +0 -0
  158. package/dist/runtime/public-assets-optional/svg2png/svg2png.wasm +0 -0
  159. package/dist/runtime/public-assets-optional/yoga/yoga.wasm +0 -0
  160. /package/dist/runtime/{nitro/providers → core/bindings}/css-inline/mock.d.ts +0 -0
  161. /package/dist/runtime/{nitro/providers → core/bindings}/css-inline/mock.mjs +0 -0
  162. /package/dist/runtime/{nitro/providers/css-inline/css-inline.d.ts → core/bindings/css-inline/node.d.ts} +0 -0
  163. /package/dist/runtime/{nitro/providers/css-inline/css-inline.mjs → core/bindings/css-inline/node.mjs} +0 -0
  164. /package/dist/runtime/{nitro → core}/renderers/satori/plugins/emojis.d.ts +0 -0
  165. /package/dist/runtime/{nitro → core}/renderers/satori/plugins/emojis.mjs +0 -0
  166. /package/dist/runtime/{nitro → core}/renderers/satori/plugins/encoding.d.ts +0 -0
  167. /package/dist/runtime/{nitro → core}/renderers/satori/plugins/flex.d.ts +0 -0
  168. /package/dist/runtime/{nitro → core}/renderers/satori/plugins/flex.mjs +0 -0
  169. /package/dist/runtime/{nitro → core}/renderers/satori/plugins/imageSrc.d.ts +0 -0
  170. /package/dist/runtime/{nitro → core}/renderers/satori/plugins/twClasses.d.ts +0 -0
  171. /package/dist/runtime/{nitro/routes/font.d.ts → nuxt/plugins/nuxt-content-canonical-urls.d.ts} +0 -0
  172. /package/dist/runtime/{nitro/middleware/og.png.d.ts → server/routes/__og-image__/font-[name]-[weight].[extension].d.ts} +0 -0
  173. /package/dist/runtime/{nitro/routes/svg.d.ts → server/routes/__og-image__/image-[path]-og.[extension].d.ts} +0 -0
@@ -1,178 +0,0 @@
1
- import { withBase } from "ufo";
2
- import { renderSSRHead } from "@unhead/ssr";
3
- import { createHeadCore } from "@unhead/vue";
4
- import { createError, defineEventHandler, getQuery, sendRedirect } from "h3";
5
- import { hash } from "ohash";
6
- import twemoji from "twemoji";
7
- import { createDefu } from "defu";
8
- import { fetchOptionsCached } from "../utils.mjs";
9
- import { useNitroOrigin, useRuntimeConfig } from "#imports";
10
- import loadCSSInline from "#nuxt-og-image/css-inline";
11
- export default defineEventHandler(async (e) => {
12
- const { fonts, satoriOptions } = useRuntimeConfig()["nuxt-og-image"];
13
- const query = getQuery(e);
14
- const path = withBase(query.path || "/", useRuntimeConfig().app.baseURL);
15
- const scale = query.scale;
16
- const mode = query.mode || "light";
17
- const nitroOrigin = useNitroOrigin(e);
18
- let queryOptions;
19
- if (query.options) {
20
- try {
21
- queryOptions = JSON.parse(query.options);
22
- } catch {
23
- }
24
- }
25
- let options = await fetchOptionsCached(e, path);
26
- const merger = createDefu((object, key, value) => {
27
- if (Array.isArray(value))
28
- return value;
29
- });
30
- if (queryOptions)
31
- options = merger(queryOptions, options);
32
- if (options.provider === "browser" && options.component === "PageScreenshot") {
33
- const pathWithoutBase = path.replace(new RegExp(`^${useRuntimeConfig().app.baseURL}`), "");
34
- return sendRedirect(e, withBase(pathWithoutBase, nitroOrigin));
35
- }
36
- if (!options.component) {
37
- throw createError({
38
- statusCode: 500,
39
- statusMessage: `Nuxt OG Image trying to render an invalid component. Received options ${JSON.stringify(options)}`
40
- });
41
- }
42
- const hashId = hash([options.component, options]);
43
- const island = await $fetch(`/__nuxt_island/${options.component}_${hashId}`, {
44
- params: {
45
- props: JSON.stringify(options)
46
- }
47
- });
48
- const head = createHeadCore();
49
- head.push(island.head);
50
- let defaultFontFamily = "sans-serif";
51
- const firstFont = fonts[0];
52
- if (firstFont)
53
- defaultFontFamily = firstFont.name;
54
- let html = island.html;
55
- try {
56
- html = twemoji.parse(html, {
57
- folder: "svg",
58
- ext: ".svg"
59
- });
60
- } catch (e2) {
61
- }
62
- const googleFonts = {};
63
- fonts.filter((font) => !font.path).forEach((font) => {
64
- if (!googleFonts[font.name])
65
- googleFonts[font.name] = [];
66
- googleFonts[font.name].push(font);
67
- });
68
- head.push({
69
- style: [
70
- {
71
- // default font is the first font family
72
- innerHTML: `body { font-family: '${defaultFontFamily.replace("+", " ")}', sans-serif; }`
73
- },
74
- {
75
- innerHTML: `body {
76
- transform: scale(${scale || 1});
77
- transform-origin: top left;
78
- max-height: 100vh;
79
- position: relative;
80
- width: ${options.width}px;
81
- height: ${options.height}px;
82
- overflow: hidden;
83
- background-color: ${mode === "dark" ? "#1b1b1b" : "#fff"};
84
- }
85
- img.emoji {
86
- height: 1em;
87
- width: 1em;
88
- margin: 0 .05em 0 .1em;
89
- vertical-align: -0.1em;
90
- }`
91
- },
92
- ...fonts.filter((font) => font.path).map((font) => {
93
- return `
94
- @font-face {
95
- font-family: '${font.name}';
96
- font-style: normal;
97
- font-weight: ${font.weight};
98
- src: url('${font.path}') format('truetype');
99
- }
100
- `;
101
- })
102
- ],
103
- meta: [
104
- {
105
- charset: "utf-8"
106
- }
107
- ],
108
- script: [
109
- {
110
- src: "https://cdn.tailwindcss.com"
111
- },
112
- {
113
- innerHTML: `tailwind.config = {
114
- corePlugins: {
115
- preflight: false,
116
- },
117
- theme: ${JSON.stringify(satoriOptions?.tailwindConfig?.theme || {})}
118
- }`
119
- }
120
- ],
121
- link: [
122
- {
123
- // reset css to match svg output
124
- href: "https://cdn.jsdelivr.net/npm/gardevoir",
125
- rel: "stylesheet"
126
- },
127
- // have to add each weight as their own stylesheet
128
- ...Object.entries(googleFonts).map(([name, fonts2]) => {
129
- return {
130
- href: `https://fonts.googleapis.com/css2?family=${name}:wght@${fonts2.map((f) => f.weight).join(";")}&display=swap`,
131
- rel: "stylesheet"
132
- };
133
- })
134
- ]
135
- });
136
- html = html.replaceAll(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "");
137
- const headChunk = await renderSSRHead(head);
138
- let htmlTemplate = `<!DOCTYPE html>
139
- <html ${headChunk.htmlAttrs}>
140
- <head>${headChunk.headTags}</head>
141
- <body ${headChunk.bodyAttrs}>${headChunk.bodyTagsOpen}<div style="position: relative; display: flex; margin: 0 auto; width: ${options.width}px; height: ${options.height}px; overflow: hidden;">${html}</div>${headChunk.bodyTags}</body>
142
- </html>`;
143
- const cssInline = loadCSSInline();
144
- if (!cssInline.__mock) {
145
- let hasInlineStyles = false;
146
- const stylesheets = htmlTemplate.match(/<link rel="stylesheet" href=".*?">/g);
147
- if (stylesheets) {
148
- for (const stylesheet of stylesheets) {
149
- if (!stylesheet.includes(`${options.component.replace("OgImageTemplate", "").replace("OgImage", "")}.vue`)) {
150
- htmlTemplate = htmlTemplate.replace(stylesheet, "");
151
- } else {
152
- const href = stylesheet.match(/href="(.*?)"/)[1];
153
- try {
154
- let css = await (await $fetch(href, {
155
- baseURL: nitroOrigin
156
- })).text();
157
- if (css.includes("const __vite__css =")) {
158
- css = css.match(/const __vite__css = "(.*)"/)[1].replace(/\\n/g, "\n");
159
- }
160
- css = css.replace(/\/\*# sourceMappingURL=.*?\*\//g, "").replaceAll("! important", "").replaceAll("!important");
161
- htmlTemplate = htmlTemplate.replace(stylesheet, `<style>${css}</style>`);
162
- hasInlineStyles = true;
163
- } catch {
164
- }
165
- }
166
- }
167
- }
168
- if (hasInlineStyles) {
169
- try {
170
- htmlTemplate = await cssInline(htmlTemplate, {
171
- url: nitroOrigin
172
- });
173
- } catch {
174
- }
175
- }
176
- }
177
- return htmlTemplate;
178
- });
@@ -1,3 +0,0 @@
1
- import type { OgImageOptions } from '../../types';
2
- declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<false | OgImageOptions>>;
3
- export default _default;
@@ -1,35 +0,0 @@
1
- import { createError, defineEventHandler, getQuery } from "h3";
2
- import { withoutBase } from "ufo";
3
- import { extractAndNormaliseOgImageOptions } from "../utils.mjs";
4
- import { getRouteRules } from "#internal/nitro";
5
- import { useRuntimeConfig } from "#imports";
6
- export default defineEventHandler(async (e) => {
7
- const query = getQuery(e);
8
- const path = withoutBase(query.path || "/", useRuntimeConfig().app.baseURL);
9
- let html;
10
- try {
11
- html = await globalThis.$fetch(path);
12
- } catch (err) {
13
- throw createError({
14
- statusCode: 500,
15
- statusMessage: `Failed to read the path ${path} for og-image extraction. ${err.message}.`
16
- });
17
- }
18
- e.node.req.url = path;
19
- const oldRouteRules = e.context._nitro.routeRules;
20
- e.context._nitro.routeRules = void 0;
21
- const routeRules = getRouteRules(e)?.ogImage || {};
22
- e.context._nitro.routeRules = oldRouteRules;
23
- e.node.req.url = e.path;
24
- if (routeRules === false)
25
- return false;
26
- const { defaults } = useRuntimeConfig()["nuxt-og-image"];
27
- const payload = extractAndNormaliseOgImageOptions(path, html, routeRules, defaults);
28
- if (!payload) {
29
- throw createError({
30
- statusCode: 500,
31
- statusMessage: `The path ${path} is missing the og-image payload.`
32
- });
33
- }
34
- return payload;
35
- });
@@ -1,19 +0,0 @@
1
- import { createError, defineEventHandler, getQuery, setHeader } from "h3";
2
- import { withBase } from "ufo";
3
- import { fetchOptionsCached } from "../utils.mjs";
4
- import { useProvider } from "#nuxt-og-image/provider";
5
- import { useRuntimeConfig } from "#imports";
6
- export default defineEventHandler(async (e) => {
7
- const query = getQuery(e);
8
- const path = withBase(query.path || "/", useRuntimeConfig().app.baseURL);
9
- const options = await fetchOptionsCached(e, path);
10
- setHeader(e, "Content-Type", "image/svg+xml");
11
- const provider = await useProvider(options.provider);
12
- if (!provider) {
13
- throw createError({
14
- statusCode: 500,
15
- statusMessage: `Provider ${options.provider} is missing.`
16
- });
17
- }
18
- return provider.createSvg(options);
19
- });
@@ -1,2 +0,0 @@
1
- declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<any>>;
2
- export default _default;
@@ -1,19 +0,0 @@
1
- import { createError, defineEventHandler, getQuery, setHeader } from "h3";
2
- import { withBase } from "ufo";
3
- import { fetchOptionsCached } from "../utils.mjs";
4
- import { useProvider } from "#nuxt-og-image/provider";
5
- import { useRuntimeConfig } from "#imports";
6
- export default defineEventHandler(async (e) => {
7
- const query = getQuery(e);
8
- const path = withBase(query.path || "/", useRuntimeConfig().app.baseURL);
9
- const options = await fetchOptionsCached(e, path);
10
- setHeader(e, "Content-Type", "application/json");
11
- const provider = await useProvider(options.provider);
12
- if (!provider) {
13
- throw createError({
14
- statusCode: 500,
15
- statusMessage: `Provider ${options.provider} is missing.`
16
- });
17
- }
18
- return provider.createVNode(options);
19
- });
@@ -1,3 +0,0 @@
1
- import type { OgImageOptions } from '../types';
2
- export declare function decodeHtml(html: string): string;
3
- export declare function extractAndNormaliseOgImageOptions(path: string, html: string, routeRules: OgImageOptions, defaults: OgImageOptions): OgImageOptions | false;
@@ -1,18 +0,0 @@
1
- /// <reference types="node" />
2
- import { Buffer } from 'node:buffer';
3
- import type { H3Event } from 'h3';
4
- import type { RuntimeOgImageOptions } from '../types';
5
- export declare function wasmLoader(asyncModuleLoad: Promise<any> | Buffer | string, fallback: string): {
6
- load(options: RuntimeOgImageOptions): Promise<any>;
7
- };
8
- export declare function fetchOptionsCached(e: H3Event, path: string): Promise<RuntimeOgImageOptions>;
9
- export declare function fetchOptions(e: H3Event, path: string): Promise<RuntimeOgImageOptions>;
10
- export declare function base64ToArrayBuffer(base64: string): ArrayBuffer;
11
- export declare function readPublicAsset(file: string, encoding?: BufferEncoding): Promise<string | Buffer | undefined>;
12
- export declare function readPublicAssetBase64(file: string): Promise<{
13
- src: string;
14
- width?: number;
15
- height?: number;
16
- } | undefined>;
17
- export declare function toBase64Image(fileName: string, data: string | ArrayBuffer): string;
18
- export * from './utils-pure';
@@ -1,108 +0,0 @@
1
- import { existsSync, promises as fsp } from "node:fs";
2
- import { Buffer } from "node:buffer";
3
- import { getQuery } from "h3";
4
- import { join } from "pathe";
5
- import sizeOf from "image-size";
6
- import { defu } from "defu";
7
- import { withoutLeadingSlash } from "ufo";
8
- import { useNitroCache } from "../cache.mjs";
9
- import { useNitroOrigin, useRuntimeConfig } from "#imports";
10
- export function wasmLoader(asyncModuleLoad, fallback) {
11
- let promise;
12
- let wasm;
13
- return {
14
- async load(options) {
15
- if (typeof promise !== "undefined")
16
- return promise;
17
- if (wasm)
18
- return wasm;
19
- promise = promise || new Promise(async (resolve) => {
20
- try {
21
- wasm = await asyncModuleLoad;
22
- if (typeof wasm === "string")
23
- wasm = void 0;
24
- } catch (e) {
25
- }
26
- if (!wasm) {
27
- wasm = await readPublicAsset(fallback, "base64");
28
- if (wasm)
29
- wasm = Buffer.from(wasm, "base64");
30
- }
31
- if (!wasm) {
32
- wasm = await (await globalThis.$fetch(fallback, { baseURL: options.requestOrigin })).arrayBuffer();
33
- wasm = Buffer.from(wasm);
34
- }
35
- resolve(wasm);
36
- });
37
- return promise;
38
- }
39
- };
40
- }
41
- export async function fetchOptionsCached(e, path) {
42
- const key = [
43
- withoutLeadingSlash(path === "/" || !path ? "index" : path).replaceAll("/", "-"),
44
- "options"
45
- ].join(":");
46
- const { cachedItem, update } = await useNitroCache(e, "nuxt-og-image", {
47
- key,
48
- // allow internal requests to be cached for 5 seconds
49
- cacheTtl: 5 * 1e3,
50
- cache: !process.dev,
51
- headers: false
52
- });
53
- if (cachedItem)
54
- return cachedItem;
55
- const options = await fetchOptions(e, path);
56
- await update(options);
57
- return options;
58
- }
59
- export async function fetchOptions(e, path) {
60
- const options = await globalThis.$fetch("/api/og-image-options", {
61
- query: {
62
- path
63
- },
64
- responseType: "json"
65
- });
66
- return defu(
67
- { requestOrigin: useNitroOrigin(e) },
68
- options,
69
- // use query data
70
- getQuery(e)
71
- );
72
- }
73
- export function base64ToArrayBuffer(base64) {
74
- const buffer = Buffer.from(base64, "base64");
75
- return new Uint8Array(buffer).buffer;
76
- }
77
- function r(base, key) {
78
- return join(base, key.replace(/:/g, "/"));
79
- }
80
- export async function readPublicAsset(file, encoding) {
81
- const { assetDirs } = useRuntimeConfig()["nuxt-og-image"];
82
- for (const assetDir of assetDirs) {
83
- const path = r(assetDir, file);
84
- if (existsSync(path))
85
- return await fsp.readFile(path, { encoding });
86
- }
87
- }
88
- export async function readPublicAssetBase64(file) {
89
- const base64 = await readPublicAsset(file, "base64");
90
- if (base64) {
91
- const dimensions = await sizeOf(Buffer.from(base64, "base64"));
92
- return {
93
- src: toBase64Image(file, base64),
94
- ...dimensions
95
- };
96
- }
97
- }
98
- export function toBase64Image(fileName, data) {
99
- const base64 = typeof data === "string" ? data : Buffer.from(data).toString("base64");
100
- let type = "image/jpeg";
101
- const ext = fileName.split(".").pop();
102
- if (ext === "svg")
103
- type = "image/svg+xml";
104
- else if (ext === "png")
105
- type = "image/png";
106
- return `data:${type};base64,${base64}`;
107
- }
108
- export * from "./utils-pure.mjs";