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 generating the latest resources.txt file to the default adblock extension #73

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Packages component and theme extensions used in the Brave browser",
"dependencies": {
"ad-block": "brave/ad-block",
"adblock-rs": "^0.1.28",
"adblock-rs": "^0.1.32",
"ajv": "^6.10.0",
"autoplay-whitelist": "github:brave/autoplay-whitelist",
"aws-sdk": "^2.449.0",
Expand Down
53 changes: 37 additions & 16 deletions scripts/generateAdBlockRustDataFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { Engine, lists } = require('adblock-rs')
const path = require('path')
const fs = require('fs')
const request = require('request')
const uBlockResources = 'https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/resources.txt'

/**
* Returns a promise that which resolves with the list data
Expand All @@ -14,7 +15,7 @@ const request = require('request')
* @param filter The filter function to apply to the body
* @return a promise that resolves with the content of the list or rejects with an error message.
*/
const getListBufferFromURL = (listURL, filter) => {
const getBufferFromURL = (listURL, filter) => {
return new Promise((resolve, reject) => {
request.get(listURL, function (error, response, body) {
if (error) {
Expand Down Expand Up @@ -47,6 +48,25 @@ const getListFilterFunction = (uuid) => {
return undefined
}

/**
* Obtains the output path to store a file given the specied name and subdir
*/
const getOutPath = (outputFilename, outSubdir) => {
let outPath = path.join('build')
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath)
}
outPath = path.join(outPath, 'ad-block-updater')
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath)
}
outPath = path.join(outPath, outSubdir)
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath)
}
return path.join(outPath, outputFilename)
}

/**
* Parses the passed in filter rule data and serializes a data file to disk.
*
Expand All @@ -62,23 +82,22 @@ const generateDataFileFromString = (filterRuleData, outputDATFilename, outSubdir
}
const client = new Engine(rules.split('\n'))
const arrayBuffer = client.serialize()
let outPath = path.join('build')
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath)
}
outPath = path.join(outPath, 'ad-block-updater')
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath)
}
outPath = path.join(outPath, outSubdir)
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath)
}
fs.writeFileSync(path.join(outPath, outputDATFilename), Buffer.from(arrayBuffer))
const outPath = getOutPath(outputDATFilename, outSubdir)
fs.writeFileSync(outPath, Buffer.from(arrayBuffer))
}

/**
* Generates a reosources.txt file for the specified buffer and subdir
*
* @param resourcesData The data to store in the resources.txt file
*/
const generateResourcesFileFromString = (resourcesData) => {
const outPath = getOutPath('resources.txt', 'default')
fs.writeFileSync(outPath, resourcesData, 'utf8')
}

/**
* Convenience function that uses getListBufferFromURL and generateDataFileFromString
* Convenience function that uses getBufferFromURL and generateDataFileFromString
* to construct a DAT file from a URL while applying a specific filter.
*
* @param listURL the URL of the list to fetch.
Expand Down Expand Up @@ -128,12 +147,14 @@ const generateDataFilesForList = (lists, filename) => {
lists.forEach((l) => {
console.log(`${l.url}...`)
const filterFn = getListFilterFunction(l.uuid)
promises.push(getListBufferFromURL(l.url, filterFn))
promises.push(getBufferFromURL(l.url, filterFn))
})
let p = Promise.all(promises)
p = p.then((listBuffers) => {
generateDataFileFromString(listBuffers, filename, 'default')
})
p = p.then(getBufferFromURL.bind(null, uBlockResources))
.then(generateResourcesFileFromString)
return p
}

Expand Down