Skip to content

Commit

Permalink
chore: update adblocker and use pre-built engine from CDN (#134)
Browse files Browse the repository at this point in the history
* chore: update adblocker and use pre-built engine from CDN

* Fix fetch abstraction on top of 'got'
* Update 'got' to latest to get 'text', 'json', and 'buffer' helpers
* Make use of prebuilt engine from CDN whenever possible

* Revert update of 'got'
  • Loading branch information
remusao authored and Kikobeats committed Jan 17, 2020
1 parent 4eaced9 commit ba14926
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/goto/package.json
Expand Up @@ -29,7 +29,7 @@
],
"dependencies": {
"@browserless/devices": "^5.18.5",
"@cliqz/adblocker-puppeteer": "~1.4.1",
"@cliqz/adblocker-puppeteer": "~1.5.0",
"debug-logfmt": "~1.0.4",
"got": "~9.6.0",
"p-reflect": "~2.1.0",
Expand Down
24 changes: 13 additions & 11 deletions packages/goto/scripts/postinstall.js
@@ -1,6 +1,6 @@
'use strict'

const { PuppeteerBlocker, fullLists } = require('@cliqz/adblocker-puppeteer')
const { PuppeteerBlocker } = require('@cliqz/adblocker-puppeteer')
const { promisify } = require('util')
const got = require('got')
const fs = require('fs')
Expand All @@ -9,20 +9,22 @@ const writeFile = promisify(fs.writeFile)

const OUTPUT_FILENAME = 'src/engine.bin'

// Lightweight `fetch` polyfill on top of `got` to allow consumption by adblocker
const fetch = async url => {
const body = (await got(url)).body
// Small helpers to allow fetching different types of data with `got`
const fetchBuffer = async url => (await got(url, { encoding: null })).body
const fetchText = async url => Buffer.from(await fetchBuffer(url), 'ascii')
const fetchJson = async url => JSON.parse(await fetchText(url))

return {
text: () => body,
arrayBuffer: () => Buffer.from(body, 'ascii').buffer,
json: () => JSON.parse(body)
}
}
// Lightweight `fetch` polyfill on top of `got` to allow consumption by adblocker
const fetch = url =>
Promise.resolve({
text: () => fetchText(url),
arrayBuffer: () => fetchBuffer(url),
json: () => fetchJson(url)
})

const main = async () => {
// create a ad-blocker engine
const engine = await PuppeteerBlocker.fromLists(fetch, fullLists)
const engine = await PuppeteerBlocker.fromPrebuiltFull(fetch)
await writeFile(OUTPUT_FILENAME, engine.serialize())
}

Expand Down

0 comments on commit ba14926

Please sign in to comment.