From 44908dbb553e1d0d8ec85de768fc9ffef4ca2a1b Mon Sep 17 00:00:00 2001 From: danpeen Date: Mon, 9 Oct 2023 20:37:32 +0800 Subject: [PATCH 1/2] fix(sandbox): fix the XMLHttpRequest object memory leak (#642) --- packages/browser-vm/src/modules/eventListener.ts | 9 +++++++-- .../browser-vm/src/modules/mutationObserver.ts | 4 +++- packages/browser-vm/src/modules/network.ts | 15 ++++++++++----- packages/browser-vm/src/modules/timer.ts | 15 +++++++++++---- packages/browser-vm/src/pluginify.ts | 3 ++- packages/browser-vm/src/sandbox.ts | 1 + packages/browser-vm/src/types.ts | 1 + 7 files changed, 35 insertions(+), 13 deletions(-) diff --git a/packages/browser-vm/src/modules/eventListener.ts b/packages/browser-vm/src/modules/eventListener.ts index ed52e3472..44162ebc7 100644 --- a/packages/browser-vm/src/modules/eventListener.ts +++ b/packages/browser-vm/src/modules/eventListener.ts @@ -15,8 +15,11 @@ export function listenerModule(_sandbox: Sandbox) { listener: Listener, options?: Opts, ) { + const curListeners = listeners.get(type) || []; - listeners.set(type, [...curListeners, listener]); + if (!_sandbox.options.disableCollect) { + listeners.set(type, [...curListeners, listener]); + } // This has been revised rawAddEventListener.call( @@ -43,7 +46,9 @@ export function listenerModule(_sandbox: Sandbox) { if (idx !== -1) { curListeners.splice(idx, 1); } - listeners.set(type, [...curListeners]); + if (!_sandbox.options.disableCollect) { + listeners.set(type, [...curListeners]); + } rawRemoveEventListener.call(this, type, listener, options); } diff --git a/packages/browser-vm/src/modules/mutationObserver.ts b/packages/browser-vm/src/modules/mutationObserver.ts index d08075a62..5b3be5598 100644 --- a/packages/browser-vm/src/modules/mutationObserver.ts +++ b/packages/browser-vm/src/modules/mutationObserver.ts @@ -6,7 +6,9 @@ export function observerModule(_sandbox: Sandbox) { class ProxyMutationObserver extends MutationObserver { constructor(cb: MutationCallback) { super(cb); - observerSet.add(this); + if (!_sandbox.options.disableCollect) { + observerSet.add(this); + } } } diff --git a/packages/browser-vm/src/modules/network.ts b/packages/browser-vm/src/modules/network.ts index 3d44e4134..de4b18a46 100644 --- a/packages/browser-vm/src/modules/network.ts +++ b/packages/browser-vm/src/modules/network.ts @@ -17,15 +17,16 @@ export function networkModule(sandbox: Sandbox) { baseUrl && typeof url === 'string' && !isAbsolute(url); - class fakeXMLHttpRequest extends XMLHttpRequest { constructor() { super(); - xhrSet.add(this); + if (!sandbox.options.disableCollect) { + xhrSet.add(this); + } } open() { - // Async request + // sync request if (arguments[2] === false) { xhrSet.delete(this); } @@ -59,7 +60,9 @@ export function networkModule(sandbox: Sandbox) { url = transformUrl(baseWsUrl, arguments[1]); } super(url, protocols); - wsSet.add(this); + if (!sandbox.options.disableCollect) { + wsSet.add(this); + } } close() { @@ -82,7 +85,9 @@ export function networkModule(sandbox: Sandbox) { let controller; if (!hasOwn(options, 'signal') && window.AbortController) { controller = new window.AbortController(); - fetchSet.add(controller); + if (!sandbox.options.disableCollect) { + fetchSet.add(controller); + } options.signal = controller.signal; } const result = window.fetch(input, options); diff --git a/packages/browser-vm/src/modules/timer.ts b/packages/browser-vm/src/modules/timer.ts index 7c29c38ff..edd97b6cb 100644 --- a/packages/browser-vm/src/modules/timer.ts +++ b/packages/browser-vm/src/modules/timer.ts @@ -1,14 +1,18 @@ +import { Sandbox } from '../sandbox'; const rawSetTimeout = window.setTimeout; const rawClearTimeout = window.clearTimeout; const rawSetInterval = window.setInterval; const rawClearInterval = window.clearInterval; -export const timeoutModule = () => { +export const timeoutModule = (sandbox: Sandbox) => { const timeout = new Set(); const setTimeout = (handler: TimerHandler, ms?: number, ...args: any[]) => { const timeoutId = rawSetTimeout(handler, ms, ...args); - timeout.add(timeoutId); + + if (!sandbox.options.disableCollect) { + timeout.add(timeoutId); + } return timeoutId; }; @@ -32,7 +36,7 @@ export const timeoutModule = () => { }; }; -export const intervalModule = () => { +export const intervalModule = (sandbox: Sandbox) => { const timeout = new Set(); const setInterval = ( @@ -41,7 +45,10 @@ export const intervalModule = () => { ...args: any[] ) => { const intervalId = rawSetInterval(callback, ms, ...args); - timeout.add(intervalId); + + if (!sandbox.options.disableCollect) { + timeout.add(intervalId); + } return intervalId; }; diff --git a/packages/browser-vm/src/pluginify.ts b/packages/browser-vm/src/pluginify.ts index e2a235702..4a3762340 100644 --- a/packages/browser-vm/src/pluginify.ts +++ b/packages/browser-vm/src/pluginify.ts @@ -133,7 +133,8 @@ function createOptions(Garfish: interfaces.Garfish) { disableWith: Boolean(appInfo.sandbox?.disableWith), disableElementtiming: Boolean(appInfo.sandbox?.disableElementtiming), strictIsolation: Boolean(appInfo.sandbox?.strictIsolation), - + // 缓存模式,不收集副作用 + disableCollect: Boolean(appInfo.cache || true), el: () => appInstance.htmlNode, styleScopeId: () => appInstance.appContainer.id, protectVariable: () => appInfo.protectVariable || [], diff --git a/packages/browser-vm/src/sandbox.ts b/packages/browser-vm/src/sandbox.ts index 2219b4683..026605be3 100644 --- a/packages/browser-vm/src/sandbox.ts +++ b/packages/browser-vm/src/sandbox.ts @@ -97,6 +97,7 @@ export class Sandbox { fixStaticResourceBaseUrl: true, disableWith: false, strictIsolation: false, + disableCollect: false, el: () => null, styleScopeId: () => '', protectVariable: () => [], diff --git a/packages/browser-vm/src/types.ts b/packages/browser-vm/src/types.ts index c8747d6a8..fbbb528a1 100644 --- a/packages/browser-vm/src/types.ts +++ b/packages/browser-vm/src/types.ts @@ -26,6 +26,7 @@ export interface SandboxOptions { disableWith?: boolean; strictIsolation?: boolean; disableElementtiming?: boolean; + disableCollect?: boolean; modules?: Array; addSourceList?: ( sourceInfo: From 4cdd9933d179f348ff6037d8c5c7dcff44846e99 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 9 Oct 2023 12:43:27 +0000 Subject: [PATCH 2/2] release: v1.17.0 --- package.json | 2 +- packages/bridge-react-v18/package.json | 2 +- packages/bridge-react/package.json | 2 +- packages/bridge-vue-v2/package.json | 2 +- packages/bridge-vue-v3/package.json | 2 +- packages/browser-snapshot/package.json | 2 +- packages/browser-vm/package.json | 2 +- packages/core/package.json | 2 +- packages/css-scope/package.json | 2 +- packages/es-module/package.json | 2 +- packages/garfish/package.json | 2 +- packages/hooks/package.json | 2 +- packages/loader/package.json | 2 +- packages/remote-module/package.json | 2 +- packages/router/package.json | 2 +- packages/test-suite/package.json | 2 +- packages/utils/package.json | 2 +- 17 files changed, 17 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index a2f0ca66e..eff935318 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,7 @@ "workspace-tools": "0.16.2", "zx": "4.2.0" }, - "version": "1.16.3", + "version": "1.17.0", "packageManager": "pnpm@7.6.0", "engines": { "node": ">=16.14.0" diff --git a/packages/bridge-react-v18/package.json b/packages/bridge-react-v18/package.json index 607d117c3..b2181c7bd 100644 --- a/packages/bridge-react-v18/package.json +++ b/packages/bridge-react-v18/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/bridge-react-v18", - "version": "1.16.3", + "version": "1.17.0", "description": "garfish module.", "keywords": [ "garfish", diff --git a/packages/bridge-react/package.json b/packages/bridge-react/package.json index afecdf696..169b5e25c 100644 --- a/packages/bridge-react/package.json +++ b/packages/bridge-react/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/bridge-react", - "version": "1.16.3", + "version": "1.17.0", "description": "garfish module.", "keywords": [ "garfish", diff --git a/packages/bridge-vue-v2/package.json b/packages/bridge-vue-v2/package.json index db7c678cf..9ce62df4c 100644 --- a/packages/bridge-vue-v2/package.json +++ b/packages/bridge-vue-v2/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/bridge-vue-v2", - "version": "1.16.3", + "version": "1.17.0", "description": "garfish vue bridge for v2.", "keywords": [ "garfish", diff --git a/packages/bridge-vue-v3/package.json b/packages/bridge-vue-v3/package.json index 439369375..41092be94 100644 --- a/packages/bridge-vue-v3/package.json +++ b/packages/bridge-vue-v3/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/bridge-vue-v3", - "version": "1.16.3", + "version": "1.17.0", "description": "garfish vue bridge for v3.", "keywords": [ "garfish", diff --git a/packages/browser-snapshot/package.json b/packages/browser-snapshot/package.json index dcb035772..78a3c991f 100644 --- a/packages/browser-snapshot/package.json +++ b/packages/browser-snapshot/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/browser-snapshot", - "version": "1.16.3", + "version": "1.17.0", "description": "browser-snapshot module.", "keywords": [ "garfish", diff --git a/packages/browser-vm/package.json b/packages/browser-vm/package.json index 90b20896a..2648623f7 100644 --- a/packages/browser-vm/package.json +++ b/packages/browser-vm/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/browser-vm", - "version": "1.16.3", + "version": "1.17.0", "description": "vm-sandbox module.", "keywords": [ "garfish", diff --git a/packages/core/package.json b/packages/core/package.json index f9e721659..a4f506f02 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/core", - "version": "1.16.3", + "version": "1.17.0", "description": "core module.", "keywords": [ "garfish", diff --git a/packages/css-scope/package.json b/packages/css-scope/package.json index 4dabb62b0..45693e78a 100644 --- a/packages/css-scope/package.json +++ b/packages/css-scope/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/css-scope", - "version": "1.16.3", + "version": "1.17.0", "description": "css scope", "keywords": [ "garfish", diff --git a/packages/es-module/package.json b/packages/es-module/package.json index 0690e836b..c0f200915 100644 --- a/packages/es-module/package.json +++ b/packages/es-module/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/es-module", - "version": "1.16.3", + "version": "1.17.0", "description": "es module polyfill", "keywords": [ "garfish", diff --git a/packages/garfish/package.json b/packages/garfish/package.json index 35b374150..9775dcc47 100644 --- a/packages/garfish/package.json +++ b/packages/garfish/package.json @@ -1,6 +1,6 @@ { "name": "garfish", - "version": "1.16.3", + "version": "1.17.0", "description": "garfish module.", "keywords": [ "garfish", diff --git a/packages/hooks/package.json b/packages/hooks/package.json index 7a7b847e5..2547d2241 100644 --- a/packages/hooks/package.json +++ b/packages/hooks/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/hooks", - "version": "1.16.3", + "version": "1.17.0", "description": "hooks module.", "keywords": [ "garfish", diff --git a/packages/loader/package.json b/packages/loader/package.json index e253d03c9..cf5180ecc 100644 --- a/packages/loader/package.json +++ b/packages/loader/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/loader", - "version": "1.16.3", + "version": "1.17.0", "description": "loader module.", "keywords": [ "garfish", diff --git a/packages/remote-module/package.json b/packages/remote-module/package.json index 26892f162..178b93f41 100644 --- a/packages/remote-module/package.json +++ b/packages/remote-module/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/remote-module", - "version": "1.16.3", + "version": "1.17.0", "description": "remote-module module.", "keywords": [ "garfish", diff --git a/packages/router/package.json b/packages/router/package.json index da4f58b0a..529080508 100644 --- a/packages/router/package.json +++ b/packages/router/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/router", - "version": "1.16.3", + "version": "1.17.0", "description": "router module.", "keywords": [ "garfish", diff --git a/packages/test-suite/package.json b/packages/test-suite/package.json index a7848045a..166c34603 100644 --- a/packages/test-suite/package.json +++ b/packages/test-suite/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/test-suite", - "version": "1.16.3", + "version": "1.17.0", "description": "garfish test suite.", "keywords": [ "garfish", diff --git a/packages/utils/package.json b/packages/utils/package.json index 3cb521872..a7274c658 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,6 +1,6 @@ { "name": "@garfish/utils", - "version": "1.16.3", + "version": "1.17.0", "description": "utils module.", "keywords": [ "garfish",