mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-10 09:07:54 +01:00
22022f636f
Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/1664 The changes are enough to fulfill the related issue. A new platform has been added in order to allow for building a NodeJS package. From the root of the project: ./tools/make-nodejs This will create new uBlock0.nodejs directory in the ./dist/build directory, which is a valid NodeJS package. From the root of the package, you can try: node test This will instantiate a static network filtering engine, populated by easylist and easyprivacy, which can be used to match network requests by filling the appropriate filtering context object. The test.js file contains code which is typical example of usage of the package. Limitations: the NodeJS package can't execute the WASM versions of the code since the WASM module requires the use of fetch(), which is not available in NodeJS. This is a first pass at modularizing the codebase, and while at it a number of opportunistic small rewrites have also been made. This commit requires the minimum supported version for Chromium and Firefox be raised to 61 and 60 respectively.
125 lines
3.9 KiB
JavaScript
125 lines
3.9 KiB
JavaScript
/*******************************************************************************
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
Copyright (C) 2014-present Raymond Hill
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
/******************************************************************************/
|
|
|
|
import './lib/publicsuffixlist/publicsuffixlist.js';
|
|
import './lib/punycode.js';
|
|
|
|
import globals from './js/globals.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,
|
|
CompiledListWriter
|
|
} from './js/static-filtering-io.js';
|
|
|
|
/******************************************************************************/
|
|
|
|
function compileList(rawText, writer) {
|
|
const lineIter = new LineIterator(rawText);
|
|
const parser = new StaticFilteringParser(true);
|
|
|
|
parser.setMaxTokenLength(staticNetFilteringEngine.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',
|
|
type: 'error',
|
|
text: staticNetFilteringEngine.error
|
|
}));
|
|
}
|
|
}
|
|
|
|
return writer.toString();
|
|
}
|
|
|
|
function applyList(name, raw) {
|
|
const writer = new CompiledListWriter();
|
|
writer.properties.set('name', name);
|
|
const compiled = compileList(raw, writer);
|
|
const reader = new CompiledListReader(compiled);
|
|
staticNetFilteringEngine.fromCompiled(reader);
|
|
}
|
|
|
|
function enableWASM(path) {
|
|
return Promise.all([
|
|
globals.publicSuffixList.enableWASM(`${path}/lib/publicsuffixlist`),
|
|
staticNetFilteringEngine.enableWASM(`${path}/js`),
|
|
]);
|
|
}
|
|
|
|
function pslInit(raw) {
|
|
if ( typeof raw !== 'string' || raw.trim() === '' ) {
|
|
console.info('Unable to populate public suffix list');
|
|
return;
|
|
}
|
|
globals.publicSuffixList.parse(raw, globals.punycode.toASCII);
|
|
console.info('Public suffix list populated');
|
|
}
|
|
|
|
function restart(lists) {
|
|
// 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);
|
|
}
|
|
// Commit changes
|
|
staticNetFilteringEngine.freeze();
|
|
staticNetFilteringEngine.optimize();
|
|
}
|
|
|
|
return staticNetFilteringEngine;
|
|
}
|
|
|
|
function reset() {
|
|
staticNetFilteringEngine.reset();
|
|
}
|
|
|
|
export {
|
|
FilteringContext,
|
|
enableWASM,
|
|
pslInit,
|
|
restart,
|
|
};
|