Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper to cache adblocker engine #477

Merged
merged 1 commit into from
Feb 10, 2020

Conversation

remusao
Copy link
Collaborator

@remusao remusao commented Feb 10, 2020

This PR improves the following methods from adblocker engine/blocker:

  • fromLists,
  • fromPrebuiltAdsOnly
  • fromPrebuiltAdsAndTracking
  • fromPrebuiltFull

With an extra argument caching indicating how to cache the resulting engine for faster loading. By default no caching is performed, but if this argument is specified then the engine initialization will:

  1. check if engine is already cached, and use this if available (faster)
  2. if engine is not cached or loading fails, initializes from scratch (current behavior)
  3. new engine is serialized and written to cache for next time.

Example of how to use it with @cliqz/adblocker-puppeteer (API is the same for @cliqz/adblocker-electron and @cliqz/adblocker-webextension):

import puppeteer from 'puppeteer';
import { PuppeteerBlocker } from '@cliqz/adblocker-puppeteer';
import fetch from 'cross-fetch'; // required 'fetch'
import { promises as fs } from 'fs'; // used for caching

const browser = await puppeteer.launch();
const page = await browser.newPage();

PuppeteerBlocker.fromPrebuiltAdsAndTracking(fetch, {
  path: 'engine.bin',
  read: fs.readFile,
  write: fs.writeFile,
}).then((blocker) => {
  blocker.enableBlockingInPage(page);
});

@remusao remusao merged commit 8a218b2 into ghostery:master Feb 10, 2020
@remusao remusao added the PR: New Feature 🚀 Increment minor version when merged label Feb 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: New Feature 🚀 Increment minor version when merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant