Skip to content

Commit

Permalink
fix: memory leak due to adblocker (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
Doridian authored Sep 20, 2020
1 parent 7fc1e77 commit 0f6e570
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/adblocker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Page} from 'puppeteer';
import {PuppeteerExtraPluginAdblocker} from 'puppeteer-extra-plugin-adblocker';

export const adBlocker = new PuppeteerExtraPluginAdblocker({
blockTrackers: true
});

export async function disableBlockerInPage(page: Page) {
const blockerObject = await adBlocker.getBlocker();
await blockerObject.disableBlockingInPage(page);
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import puppeteer from 'puppeteer-extra';
import stealthPlugin from 'puppeteer-extra-plugin-stealth';
import adblockerPlugin from 'puppeteer-extra-plugin-adblocker';
import {Config} from './config';
import {Stores} from './store/model';
import {Logger} from './logger';
import {tryLookupAndLoop} from './store';
import {getSleepTime} from './util';
import {adBlocker} from './adblocker';

puppeteer.use(stealthPlugin());
puppeteer.use(adblockerPlugin({blockTrackers: true}));
puppeteer.use(adBlocker);

/**
* Starts the bot.
Expand Down
6 changes: 3 additions & 3 deletions src/store/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import open from 'open';
import {Store} from './model';
import {sendNotification} from '../notification';
import {includesLabels} from './includes-labels';
import {delay, getSleepTime} from '../util';
import {closePage, delay, getSleepTime} from '../util';

/**
* Returns true if the brand should be checked for stock
Expand Down Expand Up @@ -62,7 +62,7 @@ async function lookup(browser: Browser, store: Store) {
response = await page.goto(link.url, {waitUntil: 'networkidle0'});
} catch {
Logger.error(`✖ [${store.name}] ${graphicsCard} skipping; timed out`);
await page.close();
await closePage(page);
continue;
}

Expand Down Expand Up @@ -96,7 +96,7 @@ async function lookup(browser: Browser, store: Store) {
sendNotification(givenUrl, link);
}

await page.close();
await closePage(page);
}
/* eslint-enable no-await-in-loop */
}
Expand Down
7 changes: 7 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {Page} from 'puppeteer';
import {Config} from './config';
import {disableBlockerInPage} from './adblocker';

export function getSleepTime() {
return Config.browser.minSleep + (Math.random() * (Config.browser.maxSleep - Config.browser.minSleep));
Expand All @@ -9,3 +11,8 @@ export async function delay(ms: number) {
setTimeout(resolve, ms);
});
}

export async function closePage(page: Page) {
await disableBlockerInPage(page);
await page.close();
}

0 comments on commit 0f6e570

Please sign in to comment.