Skip to content

Commit

Permalink
fix(sandbox): fix the XMLHttpRequest object memory leak (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
danpeen authored Oct 9, 2023
1 parent 987acc5 commit 44908db
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 13 deletions.
9 changes: 7 additions & 2 deletions packages/browser-vm/src/modules/eventListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/browser-vm/src/modules/mutationObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
15 changes: 10 additions & 5 deletions packages/browser-vm/src/modules/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand Down
15 changes: 11 additions & 4 deletions packages/browser-vm/src/modules/timer.ts
Original file line number Diff line number Diff line change
@@ -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<number>();

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;
};

Expand All @@ -32,7 +36,7 @@ export const timeoutModule = () => {
};
};

export const intervalModule = () => {
export const intervalModule = (sandbox: Sandbox) => {
const timeout = new Set<number>();

const setInterval = (
Expand All @@ -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;
};

Expand Down
3 changes: 2 additions & 1 deletion packages/browser-vm/src/pluginify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || [],
Expand Down
1 change: 1 addition & 0 deletions packages/browser-vm/src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class Sandbox {
fixStaticResourceBaseUrl: true,
disableWith: false,
strictIsolation: false,
disableCollect: false,
el: () => null,
styleScopeId: () => '',
protectVariable: () => [],
Expand Down
1 change: 1 addition & 0 deletions packages/browser-vm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface SandboxOptions {
disableWith?: boolean;
strictIsolation?: boolean;
disableElementtiming?: boolean;
disableCollect?: boolean;
modules?: Array<Module>;
addSourceList?: (
sourceInfo:
Expand Down

1 comment on commit 44908db

@vercel
Copy link

@vercel vercel bot commented on 44908db Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.