Skip to content

Commit

Permalink
JSON adblock resources support
Browse files Browse the repository at this point in the history
  • Loading branch information
antonok-edm committed Nov 7, 2019
1 parent 192a41b commit 6e114ab
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "submodules/uBlock"]
path = submodules/uBlock
url = https://github.com/brave/uBlock
59 changes: 44 additions & 15 deletions scripts/generateAdBlockRustDataFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const { Engine, lists } = require('adblock-rs')
const { Engine, lists, uBlockResources } = require('adblock-rs')
const path = require('path')
const fs = require('fs')
const request = require('request')

const uBlockLocalRoot = 'submodules/uBlock'
const uBlockWebAccessibleResources = path.join(uBlockLocalRoot, 'src/web_accessible_resources')
const uBlockRedirectEngine = path.join(uBlockLocalRoot, 'src/js/redirect-engine.js')
const uBlockScriptlets = path.join(uBlockLocalRoot, 'assets/resources/scriptlets.js')

/**
* Returns a promise that generates a resources file from the uBlock Origin
* repo hosted on GitHub
*/
const generateResourcesFile = (uBlockArchiveZip) => {
return new Promise((resolve, reject) => {
const jsonData = JSON.stringify(uBlockResources(
uBlockWebAccessibleResources,
uBlockRedirectEngine,
uBlockScriptlets
))
fs.writeFileSync(getOutPath('resources.json', 'default'), jsonData, 'utf8')
})
}

/**
* Returns a promise that which resolves with the list data
*
Expand Down Expand Up @@ -47,6 +67,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,19 +101,8 @@ 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))
}

/**
Expand Down Expand Up @@ -121,7 +149,7 @@ const generateDataFilesForAllRegions = () => {
}

/**
* Convenience function that generates a DAT file for the default list
* Convenience function that generates a DAT file and resources file for the default list
*/
const generateDataFilesForList = (lists, filename) => {
let promises = []
Expand All @@ -134,6 +162,7 @@ const generateDataFilesForList = (lists, filename) => {
p = p.then((listBuffers) => {
generateDataFileFromString(listBuffers, filename, 'default')
})
p = p.then(generateResourcesFile)
return p
}

Expand Down
1 change: 1 addition & 0 deletions submodules/uBlock
Submodule uBlock added at fcfa83

0 comments on commit 6e114ab

Please sign in to comment.