From 49566f5a269ef355fb545d86042ed46e10422203 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 18 Sep 2024 15:09:24 +0200 Subject: [PATCH] Fix vercel og package memory leak (#70214) # Conflicts: # pnpm-lock.yaml --- package.json | 2 +- packages/next/src/compiled/@vercel/og/index.edge.js | 6 +++++- packages/next/src/compiled/@vercel/og/index.node.js | 6 +++++- packages/next/src/compiled/@vercel/og/og.d.ts | 2 +- packages/next/src/compiled/@vercel/og/package.json | 2 +- packages/next/taskfile.js | 7 +++---- pnpm-lock.yaml | 8 ++++---- 7 files changed, 20 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index d255933cf057a..72cdfffcdde1b 100644 --- a/package.json +++ b/package.json @@ -115,7 +115,7 @@ "@typescript-eslint/eslint-plugin": "6.14.0", "@typescript-eslint/parser": "6.14.0", "@vercel/fetch": "6.1.1", - "@vercel/og": "0.6.2", + "@vercel/og": "0.6.3", "abort-controller": "3.0.0", "alex": "9.1.0", "amphtml-validator": "1.0.35", diff --git a/packages/next/src/compiled/@vercel/og/index.edge.js b/packages/next/src/compiled/@vercel/og/index.edge.js index 089354ce4b62d..3a2f17f0356e8 100644 --- a/packages/next/src/compiled/@vercel/og/index.edge.js +++ b/packages/next/src/compiled/@vercel/og/index.edge.js @@ -18784,7 +18784,11 @@ async function render(satori, resvg, opts, defaultFonts, element) { value: options.width } }); - return resvgJS.render().asPng(); + const pngData = resvgJS.render(); + const pngBuffer = pngData.asPng(); + pngData.free(); + resvgJS.free(); + return pngBuffer; } // src/figma/index.tsx diff --git a/packages/next/src/compiled/@vercel/og/index.node.js b/packages/next/src/compiled/@vercel/og/index.node.js index c92983c67cbcc..a516f4fec131f 100644 --- a/packages/next/src/compiled/@vercel/og/index.node.js +++ b/packages/next/src/compiled/@vercel/og/index.node.js @@ -18786,7 +18786,11 @@ async function render(satori2, resvg, opts, defaultFonts, element) { value: options.width } }); - return resvgJS.render().asPng(); + const pngData = resvgJS.render(); + const pngBuffer = pngData.asPng(); + pngData.free(); + resvgJS.free(); + return pngBuffer; } // src/figma/index.tsx diff --git a/packages/next/src/compiled/@vercel/og/og.d.ts b/packages/next/src/compiled/@vercel/og/og.d.ts index 39519c965402f..f2751b99bb730 100644 --- a/packages/next/src/compiled/@vercel/og/og.d.ts +++ b/packages/next/src/compiled/@vercel/og/og.d.ts @@ -1 +1 @@ -export default function render(satori: any, resvg: any, opts: any, defaultFonts: any, element: any): Promise; +export default function render(satori: any, resvg: any, opts: any, defaultFonts: any, element: any): Promise; diff --git a/packages/next/src/compiled/@vercel/og/package.json b/packages/next/src/compiled/@vercel/og/package.json index c1ad8487bd81f..fec64327691cb 100644 --- a/packages/next/src/compiled/@vercel/og/package.json +++ b/packages/next/src/compiled/@vercel/og/package.json @@ -1,6 +1,6 @@ { "name": "@vercel/og", - "version": "0.6.2", + "version": "0.6.3", "LICENSE": "MLP-2.0", "type": "module", "main": "./index.node.js", diff --git a/packages/next/taskfile.js b/packages/next/taskfile.js index e8f536d2f57e0..ddbc3dcf8c911 100644 --- a/packages/next/taskfile.js +++ b/packages/next/taskfile.js @@ -210,10 +210,9 @@ export async function copy_vercel_og(task, opts) { .run({ every: true }, function* (file) { const source = file.data.toString() // Refers to copied satori types - file.data = source.replace( - /['"]satori['"]/g, - '"next/dist/compiled/@vercel/og/satori"' - ) + file.data = source + .replace(/['"]satori['"]/g, '"next/dist/compiled/@vercel/og/satori"') + .replace("typeof import('@resvg/resvg-wasm')", 'any') }) .target('src/compiled/@vercel/og') diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0f71f28aa2d3e..b94f72f443f99 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -186,8 +186,8 @@ importers: specifier: 6.1.1 version: 6.1.1(@types/node-fetch@2.6.1)(node-fetch@2.6.7) '@vercel/og': - specifier: 0.6.2 - version: 0.6.2 + specifier: 0.6.3 + version: 0.6.3 abort-controller: specifier: 3.0.0 version: 3.0.0 @@ -7599,8 +7599,8 @@ packages: - supports-color dev: true - /@vercel/og@0.6.2: - resolution: {integrity: sha512-OTe0KE37F5Y2eTys6eMnfopC+P4qr2ooXUTFyFPTplYSPwowmFk/HLD1FXtbKLjqsIH0SgekcJWad+C5uX4nkg==} + /@vercel/og@0.6.3: + resolution: {integrity: sha512-aoCrC9FqkeA+WEEb9CwSmjD0rGlFeNqbUsI41JPmKWR9Hx6FFn86tvH96O5HZMF6VAXTGHxa3nPH3BokROpdgA==} engines: {node: '>=16'} dependencies: '@resvg/resvg-wasm': 2.4.0