From d73accf5c4ff770c0f9235b0fd5b7f43d63d18ea Mon Sep 17 00:00:00 2001 From: jake champion Date: Fri, 22 Nov 2024 22:21:36 +0000 Subject: [PATCH 1/3] fix: use version of htmlrewriter which does not make use of asyncify, which looks to have a potential memory leak under high load we noticed the memory issue with Netlify's CSP plugin which used the same htmlrewriter library. We've built a new htmlrewriter library which uses the latest version of lol-html and removes the ability to use async-handlers, which is what required asyncify to be included. --- deno.json | 3 ++- edge-runtime/lib/response.ts | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/deno.json b/deno.json index 28e8ea6c3c..561c910ed5 100644 --- a/deno.json +++ b/deno.json @@ -6,5 +6,6 @@ }, "imports": { "@netlify/edge-functions": "https://edge.netlify.com/v1/index.ts" - } + }, + "importMap": "./edge-runtime/vendor/import_map.json" } diff --git a/edge-runtime/lib/response.ts b/edge-runtime/lib/response.ts index 3fd82c5ab0..8c5ddea80e 100644 --- a/edge-runtime/lib/response.ts +++ b/edge-runtime/lib/response.ts @@ -1,5 +1,5 @@ import type { Context } from '@netlify/edge-functions' -import { HTMLRewriter } from '../vendor/deno.land/x/html_rewriter@v0.1.0-pre.17/index.ts' +import { HTMLRewriter, init, type TextChunk } from 'https://deno.land/x/htmlrewriter@v1.0.0/src/index.ts' import { updateModifiedHeaders } from './headers.ts' import type { StructuredLogger } from './logging.ts' @@ -13,6 +13,8 @@ import { relativizeURL, } from './util.ts' +await init(); + export interface FetchEventResult { response: Response waitUntil: Promise @@ -79,7 +81,7 @@ export const buildResponse = async ({ if (response.dataTransforms.length > 0) { rewriter.on('script[id="__NEXT_DATA__"]', { - text(textChunk) { + text(textChunk: TextChunk) { // Grab all the chunks in the Next data script tag buffer += textChunk.text if (textChunk.lastInTextNode) { From b76dfb1a9e283ce2b3a27029e42aebdafad6dceb Mon Sep 17 00:00:00 2001 From: pieh Date: Mon, 9 Dec 2024 16:39:02 +0100 Subject: [PATCH 2/3] chore: vendor updated htmlrewriter --- edge-runtime/lib/response.ts | 8 ++++++-- edge-runtime/vendor.ts | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/edge-runtime/lib/response.ts b/edge-runtime/lib/response.ts index 8c5ddea80e..b09ea896a9 100644 --- a/edge-runtime/lib/response.ts +++ b/edge-runtime/lib/response.ts @@ -1,5 +1,9 @@ import type { Context } from '@netlify/edge-functions' -import { HTMLRewriter, init, type TextChunk } from 'https://deno.land/x/htmlrewriter@v1.0.0/src/index.ts' +import { + HTMLRewriter, + init, + type TextChunk, +} from '../vendor/deno.land/x/htmlrewriter@v1.0.0/src/index.ts' import { updateModifiedHeaders } from './headers.ts' import type { StructuredLogger } from './logging.ts' @@ -13,7 +17,7 @@ import { relativizeURL, } from './util.ts' -await init(); +await init() export interface FetchEventResult { response: Response diff --git a/edge-runtime/vendor.ts b/edge-runtime/vendor.ts index e8c6720d4d..df153912c3 100644 --- a/edge-runtime/vendor.ts +++ b/edge-runtime/vendor.ts @@ -12,6 +12,6 @@ import 'https://deno.land/std@0.175.0/node/util.ts' import 'https://deno.land/std@0.175.0/path/mod.ts' import 'https://deno.land/x/path_to_regexp@v6.2.1/index.ts' -import 'https://deno.land/x/html_rewriter@v0.1.0-pre.17/index.ts' +import 'https://deno.land/x/htmlrewriter@v1.0.0/src/index.ts' import 'https://v1-7-0--edge-utils.netlify.app/logger/mod.ts' From 2d968b45c46df8564fc7519f119705ddc2f93107 Mon Sep 17 00:00:00 2001 From: pieh Date: Mon, 9 Dec 2024 17:00:02 +0100 Subject: [PATCH 3/3] fix: update remaining htmlrewriter import --- edge-runtime/lib/middleware.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/edge-runtime/lib/middleware.ts b/edge-runtime/lib/middleware.ts index 46bbef6888..2ae25c1d34 100644 --- a/edge-runtime/lib/middleware.ts +++ b/edge-runtime/lib/middleware.ts @@ -1,6 +1,6 @@ import type { Context } from '@netlify/edge-functions' -import { ElementHandlers } from '../vendor/deno.land/x/html_rewriter@v0.1.0-pre.17/index.ts' +import type { ElementHandlers } from '../vendor/deno.land/x/htmlrewriter@v1.0.0/src/index.ts' type NextDataTransform = (data: T) => T