Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 104 additions & 52 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ exclude = ["artifacts.json", "index.node"]
crate-type = ["cdylib"]

[dependencies]
napi = { version = "3", features = ["serde-json"] }
napi-derive = "3"
serde = { workspace = true }
serde_json = { workspace = true }
adblock = { path = "../", features = ["css-validation", "content-blocking", "resource-assembler"] }
neon = { version = "^0.10.1", default-features = false, features = ["napi-1"] }

[features]
default-panic-hook = []
[build-dependencies]
napi-build = "2"
39 changes: 39 additions & 0 deletions js/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node
'use strict';

// Builds the native addon by invoking cargo and copying the cdylib to js/index.node.
// Replaces @napi-rs/cli to avoid pulling in unnecessary npm dependencies.

const { execSync } = require('child_process');
const { copyFileSync } = require('fs');
const { join } = require('path');

const release = process.argv.includes('--release');
const cargoArgs = ['build', '--manifest-path', 'js/Cargo.toml', '--message-format=json'];
if (release) cargoArgs.push('--release');

const output = execSync(`cargo ${cargoArgs.join(' ')}`, {
stdio: ['inherit', 'pipe', 'inherit'],
encoding: 'utf8',
});

// Find the cdylib artifact path from cargo's JSON output
let cdylibPath;
for (const line of output.split('\n')) {
if (!line) continue;
try {
const msg = JSON.parse(line);
if (msg.reason === 'compiler-artifact' && msg.target?.crate_types?.includes('cdylib')) {
cdylibPath = msg.filenames[0];
}
} catch {}
}

if (!cdylibPath) {
console.error('Error: could not find cdylib artifact in cargo output');
process.exit(1);
}

const dest = join(__dirname, 'index.node');
copyFileSync(cdylibPath, dest);
console.log(`Copied ${cdylibPath} -> ${dest}`);
5 changes: 5 additions & 0 deletions js/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate napi_build;

fn main() {
napi_build::setup();
}
36 changes: 7 additions & 29 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
const blocker = require('./index.node');
const native = require('./index.node');

// Exposes the specified native method names on `className`
function forwardClassMethods(className, methods) {
for (const method of methods) {
className.prototype[method] = function(...args) {
const blocker_method = blocker[className.name + '_' + method];
return blocker_method(this.boxed, ...args);
}
}
}
exports.FilterSet = native.FilterSet;
exports.Engine = native.Engine;
exports.validateRequest = native.validateRequest;
exports.uBlockResources = native.uBlockResources;

class FilterSet {
constructor(...args) {
this.boxed = blocker.FilterSet_constructor(...args);
}
}
forwardClassMethods(FilterSet, ['addFilters', 'addFilter', 'intoContentBlocking']);

class Engine {
constructor(filter_set, ...args) {
this.boxed = blocker.Engine_constructor(filter_set.boxed, ...args);
}
}
forwardClassMethods(Engine, ['check', 'urlCosmeticResources', 'hiddenClassIdSelectors', 'serialize', 'deserialize', 'enableTag', 'useResources', 'tagExists', 'clearTags']);

exports.FilterFormat = blocker.FilterFormat;
exports.FilterSet = FilterSet;
exports.RuleTypes = blocker.RuleTypes;
exports.Engine = Engine;
exports.uBlockResources = blocker.uBlockResources;
exports.FilterFormat = Object.freeze(native.FilterFormat());
exports.RuleTypes = Object.freeze(native.RuleTypes());
Loading