From e673c143a9b63bae52689f719d6b363f66eb7a59 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E3=81=8B=E3=81=A3=E3=81=93=E3=81=8B=E3=82=8A?=
 <67428053+kakkokari-gtyih@users.noreply.github.com>
Date: Mon, 23 Sep 2024 21:43:48 +0900
Subject: [PATCH] =?UTF-8?q?fix(backend):=20happy-dom=E3=82=92=E4=BD=BF?=
 =?UTF-8?q?=E7=94=A8=E5=BE=8C=E3=81=ABclose=E3=81=99=E3=82=8B=E3=82=88?=
 =?UTF-8?q?=E3=81=86=E3=81=AB=20(#14615)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Add `DetachedWindowAPI.close` calls to `MfmService`

(cherry picked from commit ceaec3324925e53ca3f467b0438a98f1108eed0f)

* fix

* update changelog

* fix

---------

Co-authored-by: Julia Johannesen <julia@insertdomain.name>
---
 CHANGELOG.md                                              | 2 ++
 packages/backend/src/core/MfmService.ts                   | 8 ++++++--
 packages/backend/src/core/activitypub/ApRequestService.ts | 4 ++--
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 78b2b3fa4f..3e28c6bf01 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,8 @@
   (Cherry-picked from https://activitypub.software/TransFem-org/Sharkey/-/commit/26e0412fbb91447c37e8fb06ffb0487346063bb8)
 - Fix: `Retry-After`ヘッダーが送信されなかった問題を修正
   (Cherry-picked from https://activitypub.software/TransFem-org/Sharkey/-/commit/8a982c61c01909e7540ff1be9f019df07c3f0624)
+- Fix: サーバーサイドのDOM解析完了時にリソースを開放するように
+	(Cherry-picked from https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/634)
 
 ## 2024.8.0
 
diff --git a/packages/backend/src/core/MfmService.ts b/packages/backend/src/core/MfmService.ts
index 74536c68f5..d33b228c3d 100644
--- a/packages/backend/src/core/MfmService.ts
+++ b/packages/backend/src/core/MfmService.ts
@@ -239,7 +239,7 @@ export class MfmService {
 			return null;
 		}
 
-		const { window } = new Window();
+		const { happyDOM, window } = new Window();
 
 		const doc = window.document;
 
@@ -457,6 +457,10 @@ export class MfmService {
 
 		appendChildren(nodes, body);
 
-		return new XMLSerializer().serializeToString(body);
+		const serialized = new XMLSerializer().serializeToString(body);
+
+		happyDOM.close().catch(err => {});
+
+		return serialized;
 	}
 }
diff --git a/packages/backend/src/core/activitypub/ApRequestService.ts b/packages/backend/src/core/activitypub/ApRequestService.ts
index 805280db36..7c78f3319b 100644
--- a/packages/backend/src/core/activitypub/ApRequestService.ts
+++ b/packages/backend/src/core/activitypub/ApRequestService.ts
@@ -207,7 +207,7 @@ export class ApRequestService {
 
 		if ((contentType ?? '').split(';')[0].trimEnd().toLowerCase() === 'text/html' && _followAlternate === true) {
 			const html = await res.text();
-			const window = new Window({
+			const { window, happyDOM } = new Window({
 				settings: {
 					disableJavaScriptEvaluation: true,
 					disableJavaScriptFileLoading: true,
@@ -241,7 +241,7 @@ export class ApRequestService {
 			} catch (e) {
 				// something went wrong parsing the HTML, ignore the whole thing
 			} finally {
-				window.close();
+				happyDOM.close().catch(err => {});
 			}
 		}
 		//#endregion