forked from microlinkhq/browserless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postinstall.js
33 lines (26 loc) · 1.02 KB
/
postinstall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict'
const { PuppeteerBlocker } = require('@cliqz/adblocker-puppeteer')
const { promisify } = require('util')
const got = require('got')
const fs = require('fs')
const writeFile = promisify(fs.writeFile)
const OUTPUT_FILENAME = 'src/engine.bin'
// 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))
// 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.fromPrebuiltFull(fetch)
await writeFile(OUTPUT_FILENAME, engine.serialize())
}
main()
.catch(err => console.error(err) && process.exit(1))
.then(process.exit)