Skip to content

Commit

Permalink
Further modularize uBO's codebase
Browse files Browse the repository at this point in the history
Related issue:
- uBlockOrigin/uBlock-issues#1664

Modularization is a necessary step toward possibly publishing
a more complete nodejs package to allow using uBO's filtering
capabilities outside of the uBO extension.

Additionally, as per feedback, remove undue usage of console
output as per feedback:
- uBlockOrigin/uBlock-issues#1664 (comment)
  • Loading branch information
gorhill committed Jul 28, 2021
1 parent d7cd6d7 commit 62b6826
Show file tree
Hide file tree
Showing 34 changed files with 1,984 additions and 1,873 deletions.
41 changes: 18 additions & 23 deletions platform/nodejs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import './lib/punycode.js';
import './lib/publicsuffixlist/publicsuffixlist.js';

import globals from './js/globals.js';
import snfe from './js/static-net-filtering.js';
import { FilteringContext } from './js/filtering-context.js';
import { LineIterator } from './js/text-iterators.js';
import { StaticFilteringParser } from './js/static-filtering-parser.js';
import { staticNetFilteringEngine } from './js/static-net-filtering.js';

import {
CompiledListReader,
Expand All @@ -41,33 +41,31 @@ import {

/******************************************************************************/

function compileList(rawText, writer) {
function compileList(rawText, writer, options = {}) {
const lineIter = new LineIterator(rawText);
const parser = new StaticFilteringParser(true);
const events = Array.isArray(options.events) ? options.events : undefined;

parser.setMaxTokenLength(staticNetFilteringEngine.MAX_TOKEN_LENGTH);
parser.setMaxTokenLength(snfe.MAX_TOKEN_LENGTH);

while ( lineIter.eot() === false ) {
let line = lineIter.next();

while ( line.endsWith(' \\') ) {
if ( lineIter.peek(4) !== ' ' ) { break; }
line = line.slice(0, -2).trim() + lineIter.next().trim();
}
parser.analyze(line);

if ( parser.shouldIgnore() ) { continue; }
if ( parser.category !== parser.CATStaticNetFilter ) { continue; }
if ( parser.patternHasUnicode() && parser.toASCII() === false ) {
continue;
}
if ( staticNetFilteringEngine.compile(parser, writer) ) { continue; }
if ( staticNetFilteringEngine.error !== undefined ) {
console.info(JSON.stringify({
realm: 'message',
if ( snfe.compile(parser, writer) ) { continue; }
if ( snfe.error !== undefined && events !== undefined ) {
options.events.push({
type: 'error',
text: staticNetFilteringEngine.error
}));
text: snfe.error
});
}
}

Expand All @@ -79,13 +77,13 @@ function applyList(name, raw) {
writer.properties.set('name', name);
const compiled = compileList(raw, writer);
const reader = new CompiledListReader(compiled);
staticNetFilteringEngine.fromCompiled(reader);
snfe.fromCompiled(reader);
}

function enableWASM(path) {
return Promise.all([
globals.publicSuffixList.enableWASM(`${path}/lib/publicsuffixlist`),
staticNetFilteringEngine.enableWASM(`${path}/js`),
snfe.enableWASM(`${path}/js`),
]);
}

Expand All @@ -94,35 +92,32 @@ function pslInit(raw) {
const require = createRequire(import.meta.url); // jshint ignore:line
raw = require('./data/effective_tld_names.json');
if ( typeof raw !== 'string' || raw.trim() === '' ) {
console.info('Unable to populate public suffix list');
console.error('Unable to populate public suffix list');
return;
}
}
globals.publicSuffixList.parse(raw, globals.punycode.toASCII);
console.info('Public suffix list populated');
}

function restart(lists) {
function restart(lists, options = {}) {
// Remove all filters
reset();

if ( Array.isArray(lists) && lists.length !== 0 ) {
// Populate filtering engine with filter lists
for ( const { name, raw } of lists ) {
applyList(name, raw);
applyList(name, raw, options);
}
// Commit changes
staticNetFilteringEngine.freeze();
staticNetFilteringEngine.optimize();
snfe.freeze();
snfe.optimize();
}

console.info('Static network filtering engine populated');

return staticNetFilteringEngine;
return snfe;
}

function reset() {
staticNetFilteringEngine.reset();
snfe.reset();
}

export {
Expand Down
2 changes: 1 addition & 1 deletion platform/nodejs/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function fetch(listName) {
}
*/

pslInit();
await pslInit();

const snfe = await Promise.all([
fetch('easylist'),
Expand Down
20 changes: 0 additions & 20 deletions src/background.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,17 @@
<title>uBlock Origin</title>
</head>
<body>
<script src="js/console.js"></script>
<script src="lib/lz4/lz4-block-codec-any.js"></script>
<script src="js/webext.js"></script>
<script src="js/vapi.js"></script>
<script src="js/vapi-common.js"></script>
<script src="js/vapi-background.js" type="module"></script>
<script src="js/vapi-background-ext.js" type="module"></script><!-- platform-specific to extend common code paths -->
<script src="js/background.js" type="module"></script>
<script src="js/traffic.js" type="module"></script>
<script src="js/utils.js" type="module"></script>
<script src="js/lz4.js" type="module"></script>
<script src="js/cachestorage.js" type="module"></script>
<script src="js/assets.js" type="module"></script>
<script src="js/redirect-engine.js" type="module"></script>
<script src="js/dynamic-net-filtering.js" type="module"></script>
<script src="js/url-net-filtering.js" type="module"></script>
<script src="js/static-ext-filtering.js" type="module"></script>
<script src="js/cosmetic-filtering.js" type="module"></script>
<script src="js/scriptlet-filtering.js" type="module"></script>
<script src="js/html-filtering.js" type="module"></script>
<script src="js/httpheader-filtering.js" type="module"></script>
<script src="js/hnswitches.js" type="module"></script>
<script src="js/ublock.js" type="module"></script>
<script src="js/storage.js" type="module"></script>
<script src="js/logger.js" type="module"></script>
<script src="js/pagestore.js" type="module"></script>
<script src="js/tab.js" type="module"></script>
<script src="js/messaging.js" type="module"></script>
<script src="js/text-encode.js" type="module"></script>
<script src="js/contextmenu.js" type="module"></script>
<script src="js/reverselookup.js" type="module"></script>
<script src="js/start.js" type="module"></script>
<script src="js/commands.js" type="module"></script>
</body>
Expand Down
Loading

0 comments on commit 62b6826

Please sign in to comment.