diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..9d02bbbc --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "submodules/uBlock"] + path = submodules/uBlock + url = https://github.com/brave/uBlock diff --git a/scripts/generateAdBlockRustDataFiles.js b/scripts/generateAdBlockRustDataFiles.js index 2d3f2b95..f6af0b5c 100644 --- a/scripts/generateAdBlockRustDataFiles.js +++ b/scripts/generateAdBlockRustDataFiles.js @@ -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 * @@ -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. * @@ -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)) } /** @@ -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 = [] @@ -134,6 +162,7 @@ const generateDataFilesForList = (lists, filename) => { p = p.then((listBuffers) => { generateDataFileFromString(listBuffers, filename, 'default') }) + p = p.then(generateResourcesFile) return p } diff --git a/submodules/uBlock b/submodules/uBlock new file mode 160000 index 00000000..fcfa83df --- /dev/null +++ b/submodules/uBlock @@ -0,0 +1 @@ +Subproject commit fcfa83dfc645a28a23bf48970bbfa009bad136c9