1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-13 22:23:54 +01:00
uBlock/platform/nodejs/index.js

283 lines
8.4 KiB
JavaScript
Raw Normal View History

/*******************************************************************************
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
*/
/* globals WebAssembly */
'use strict';
/******************************************************************************/
import { createRequire } from 'module';
import { readFileSync } from 'fs';
import { dirname, resolve } from 'path';
2021-09-04 14:44:26 +02:00
import { domainToASCII, fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
import publicSuffixList from './lib/publicsuffixlist/publicsuffixlist.js';
import snfe from './js/static-net-filtering.js';
import { FilteringContext } from './js/filtering-context.js';
import { LineIterator } from './js/text-utils.js';
import { StaticFilteringParser } from './js/static-filtering-parser.js';
import {
CompiledListReader,
CompiledListWriter,
} from './js/static-filtering-io.js';
/******************************************************************************/
function loadJSON(path) {
return JSON.parse(readFileSync(resolve(__dirname, path), 'utf8'));
}
2021-08-08 15:17:14 +02:00
/******************************************************************************/
async function enableWASM() {
2021-07-29 23:04:32 +02:00
const wasmModuleFetcher = function(path) {
const require = createRequire(import.meta.url); // jshint ignore:line
const wasm = new Uint8Array(require(`${path}.wasm.json`));
return WebAssembly.compile(wasm);
};
try {
const results = await Promise.all([
publicSuffixList.enableWASM(wasmModuleFetcher, './lib/publicsuffixlist/wasm/'),
snfe.enableWASM(wasmModuleFetcher, './js/wasm/'),
]);
return results.every(a => a === true);
} catch(reason) {
console.log(reason);
}
return false;
}
2021-08-08 15:17:14 +02:00
/******************************************************************************/
2021-08-08 15:17:14 +02:00
function pslInit(raw) {
if ( typeof raw === 'string' && raw.trim() !== '' ) {
2021-09-04 14:44:26 +02:00
publicSuffixList.parse(raw, domainToASCII);
return publicSuffixList;
2021-08-08 15:17:14 +02:00
}
2021-08-08 15:17:14 +02:00
// Use serialized version if available
let serialized = null;
try {
// Use loadJSON() because require() would keep the string in memory.
serialized = loadJSON('build/publicsuffixlist.json');
} catch (error) {
if ( process.env.npm_lifecycle_event !== 'build' ) {
// This should never happen except during package building.
2021-08-08 15:17:14 +02:00
console.error(error);
}
2021-08-08 15:17:14 +02:00
}
if ( serialized !== null ) {
publicSuffixList.fromSelfie(serialized);
return publicSuffixList;
2021-08-08 15:17:14 +02:00
}
raw = readFileSync(
resolve(__dirname, './assets/thirdparties/publicsuffix.org/list/effective_tld_names.dat'),
'utf8'
);
2021-08-08 15:17:14 +02:00
if ( typeof raw !== 'string' || raw.trim() === '' ) {
console.error('Unable to populate public suffix list');
return;
}
2021-09-04 14:44:26 +02:00
publicSuffixList.parse(raw, domainToASCII);
return publicSuffixList;
}
2021-08-08 15:17:14 +02:00
/******************************************************************************/
2021-08-08 15:17:14 +02:00
function compileList({ name, raw }, compiler, writer, options = {}) {
const lineIter = new LineIterator(raw);
const events = Array.isArray(options.events) ? options.events : undefined;
2021-08-08 15:17:14 +02:00
if ( name ) {
writer.properties.set('name', name);
}
2021-08-08 15:17:14 +02:00
const { parser } = compiler;
2021-08-08 15:17:14 +02:00
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 ( compiler.compile(writer) ) { continue; }
if ( compiler.error !== undefined && events !== undefined ) {
options.events.push({
type: 'error',
text: compiler.error
});
}
}
2021-08-08 15:17:14 +02:00
return writer.toString();
}
2021-08-08 15:17:14 +02:00
/******************************************************************************/
async function useLists(lists, options = {}) {
if ( useLists.promise !== null ) {
throw new Error('Pending useLists() operation');
}
// Remove all filters
2021-08-08 15:17:14 +02:00
snfe.reset();
if ( Array.isArray(lists) === false || lists.length === 0 ) {
return;
}
2021-08-08 15:17:14 +02:00
let compiler = null;
const consumeList = list => {
2021-08-08 15:17:14 +02:00
let { compiled } = list;
if ( typeof compiled !== 'string' || compiled === '' ) {
const writer = new CompiledListWriter();
if ( compiler === null ) {
compiler = snfe.createCompiler(new StaticFilteringParser());
}
compiled = compileList(list, compiler, writer, options);
}
snfe.fromCompiled(new CompiledListReader(compiled));
};
2021-08-08 15:17:14 +02:00
// Populate filtering engine with resolved filter lists
const promises = [];
for ( const list of lists ) {
const promise = list instanceof Promise ? list : Promise.resolve(list);
promises.push(promise.then(list => consumeList(list)));
}
useLists.promise = Promise.all(promises);
await useLists.promise;
useLists.promise = null; // eslint-disable-line require-atomic-updates
// Commit changes
snfe.freeze();
snfe.optimize();
}
useLists.promise = null;
/******************************************************************************/
2021-08-08 15:17:14 +02:00
const fctx = new FilteringContext();
let snfeProxyInstance = null;
class StaticNetFilteringEngine {
constructor() {
if ( snfeProxyInstance !== null ) {
throw new Error('Only a single instance is supported.');
}
snfeProxyInstance = this;
}
useLists(lists) {
return useLists(lists);
}
2021-08-08 15:17:14 +02:00
matchRequest(details) {
return snfe.matchRequest(fctx.fromDetails(details));
}
matchAndFetchModifiers(details, modifier) {
return snfe.matchAndFetchModifiers(fctx.fromDetails(details), modifier);
}
hasQuery(details) {
return snfe.hasQuery(details);
}
filterQuery(details) {
const directives = snfe.filterQuery(fctx.fromDetails(details));
if ( directives === undefined ) { return; }
return { redirectURL: fctx.redirectURL, directives };
}
isBlockImportant() {
return snfe.isBlockImportant();
}
toLogData() {
return snfe.toLogData();
}
2021-08-08 15:17:14 +02:00
createCompiler(parser) {
return snfe.createCompiler(parser);
}
compileList(...args) {
return compileList(...args);
}
serialize() {
return snfe.serialize();
}
deserialize(serialized) {
return snfe.unserialize(serialized);
}
static async create({ noPSL = false } = {}) {
2021-08-08 15:17:14 +02:00
const instance = new StaticNetFilteringEngine();
if ( noPSL !== true && !pslInit() ) {
throw new Error('Failed to initialize public suffix list.');
}
2021-08-08 15:17:14 +02:00
return instance;
}
static async release() {
if ( snfeProxyInstance === null ) { return; }
snfeProxyInstance = null;
await useLists([]);
}
2021-08-08 15:17:14 +02:00
}
/******************************************************************************/
// rollup.js needs module.exports to be set back to the local exports object.
// This is because some of the code (e.g. publicsuffixlist.js) sets
// module.exports. Once all included files are written like ES modules, using
// export statements, this should no longer be necessary.
2021-08-03 18:03:11 +02:00
if ( typeof module !== 'undefined' && typeof exports !== 'undefined' ) {
module.exports = exports;
}
export {
enableWASM,
pslInit,
2021-08-08 15:17:14 +02:00
StaticNetFilteringEngine,
};