# @gorhill/ubo-core 中文文档

规范地址：https://npmcn.com/package/%40gorhill/ubo-core/

版本：0\.1\.30
npm 元数据更新时间：2024-10-07T12:57:15.093Z
中文译文更新时间：2026-11-16T20:38:38.659Z
软件包许可：GPL-3\.0-or-later

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40gorhill/ubo-core
原文对照：https://npmcn.com/package/%40gorhill/ubo-core/?lang=original
项目主页：https://github.com/gorhill/uBlock#readme
源码仓库：https://github.com/gorhill/uBlock

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：1.5 MB，41 个文件
- 模块格式：ES Module
- TypeScript 类型：未声明
- Node.js 要求：\>=18\.0\.0
- 运行依赖：0 个
- 首次发布：2021-08-05T18:26:28.643Z
- 最新版本发布：2024-10-07T12:57:14.919Z
- 近 30 天下载量：10982（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：29（近 12 个月 0 个）

## 中文简介

创建 uBlock Origin 的静态网络过滤引擎的工作实例

## 安装

```sh
npm install @gorhill/ubo-core
```

也可以使用：`pnpm add @gorhill/ubo-core`、`yarn add @gorhill/ubo-core`、`bun add @gorhill/ubo-core`

## README

# uBlock 源代码核心

UBlock Origin("uBO")扩展中使用的核心过滤引擎,并且有 没有外部依赖关系.

## 安装

安装 : `npm install @gorhill/ubo-core`

这是一个非常早期的版本,API随时会被更改.

此软件包使用 [本地 JavaScript 模块](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)。 。 。 .

## 说明

包中包含uBO的静态网络过滤引擎("SNFE"),它 目的是分析并强制执行过滤列表。 匹配算法很高 高效率 _特别是_ 优化以匹配大组纯度 主机名称.

SNFE可以从各种来源,例如: [EasyList/简陋](https://easylist.to/), (中文(简体) ), [uBlock 过滤器](https://github.com/uBlockOrigin/uAssets/tree/master/filters), (中文(简体) ), 以及域名或主机文件格式列表(即来自 [区块列表项目](https://github.com/blocklistproject/Lists#the-block-list-project), (中文(简体) ), [史蒂文・布莱克的宿营](https://github.com/StevenBlack/hosts#readme)(见e/cn.4/sub.2/2002/l.48等).

## 使用量

见 `./demo.js` 用于快速启动指令.

目前只有一次静态网络过滤 引擎 ("SNFE"),其代理 API 必须导入如下 :

```js
import { StaticNetFilteringEngine } from '@gorhill/ubo-core';
```

如果您必须导入为节点JS模块 :

```js
const { StaticNetFilteringEngine } = await import('@gorhill/ubo-core');
```

创建 SNFE 实例 :

```js
const snfe = await StaticNetFilteringEngine.create();
```

用过滤列表输入 SNFE -- `useLists()` 接受一组 显示列表原始文本的对象(或承诺反对) 通过 `raw` 属性,通过 `name` 属性( 如何获取列表由您决定 ):

```js
await snfe.useLists([
    fetch('easylist').then(r => r.text()).then(raw => ({ name: 'easylist', raw })),
    fetch('easyprivacy').then(r => r.text()).then(raw => ({ name: 'easyprivacy', raw })),
]);
```

现在我们准备满足网络要求:

```js
// Not blocked
if ( snfe.matchRequest({
    originURL: 'https://www.bloomberg.com/',
    url: 'https://www.bloomberg.com/tophat/assets/v2.6.1/that.css',
    type: 'stylesheet'
}) !== 0 ) {
    console.log(snfe.toLogData());
}

// Blocked
if ( snfe.matchRequest({
    originURL: 'https://www.bloomberg.com/',
    url: 'https://securepubads.g.doubleclick.net/tag/js/gpt.js',
    type: 'script'
}) !== 0 ) {
    console.log(snfe.toLogData());
}

// Unblocked
if ( snfe.matchRequest({
    originURL: 'https://www.bloomberg.com/',
    url: 'https://sourcepointcmp.bloomberg.com/ccpa.js',
    type: 'script'
}) !== 0 ) {
    console.log(snfe.toLogData());
}
```

一旦所有过滤列表被装入静态网络过滤引擎, 您可以将引擎内容序列化为 JS 串:

```js
const serializedData = await snfe.serialize();
```

您可以保存并使用 JS 字符串快速装入内容 静态网络过滤引擎, 无需解析和编译列表 :

```js
const snfe = await StaticNetFilteringEngine.create();
await snfe.deserialize(serializedData);
```

* * *

## 额外

您可以直接使用此软件包所曝光的特定 API, 这里有一些 它们被UBO的SNFE内部使用.

### HNTrie 连接器

完美优化 [压缩三进制](https://en.wikipedia.org/wiki/Trie#Compressing_tries) 专门存储和查找主机名的容器.

匹配算法是为主机名设计,即主机名标签 组成主机名从右到左匹配, 如 `www.example.org` 与匹配,如果 `example.org` 保存在三进制中,而 `anotherexample.org` 不会是匹配.

`HNTrieContainer` 用于存储大量 CPU 和 内存效率作为一个主要关切,也是uBO的关键组成部分.

创建和使用独立的 `HNTrieContainer` 对象 :

```js
import HNTrieContainer from '@gorhill/ubo-core/js/hntrie.js';

const trieContainer = new HNTrieContainer();

const aTrie = trieContainer.createOne();
trieContainer.add(aTrie, 'example.org');
trieContainer.add(aTrie, 'example.com');

const anotherTrie = trieContainer.createOne();
trieContainer.add(anotherTrie, 'foo.invalid');
trieContainer.add(anotherTrie, 'bar.invalid');

// matches() return the position at which the match starts, or -1 when
// there is no match.

// Matches: return 4
console.log("trieContainer.matches(aTrie, 'www.example.org')", trieContainer.matches(aTrie, 'www.example.org'));

// Does not match: return -1
console.log("trieContainer.matches(aTrie, 'www.foo.invalid')", trieContainer.matches(aTrie, 'www.foo.invalid'));

// Does not match: return -1
console.log("trieContainer.matches(anotherTrie, 'www.example.org')", trieContainer.matches(anotherTrie, 'www.example.org'));

// Matches: return 0
console.log("trieContainer.matches(anotherTrie, 'foo.invalid')", trieContainer.matches(anotherTrie, 'foo.invalid'));
```

这个 `reset()` 必须使用方法去除三重容器中的所有尝试, 无法从容器中取出一条三角形.

```js
trieContainer.reset();
```

重设 三进制容器时, 无法使用前例的引用 3,即: 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 3, 3, 3, 5, 3, 3, 3, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 3, 3, 3, 3, 5, 3, 3, 4, 3, 3, 3, 3, 3, 3, 5, 3, 3, 3, 3, 3, 3, 3, . `aTrie` 财务报告和审定财务报表 `anotherTrie` 不再有效,不应该 用于重置后.

## 常见问题

### @gorhill/ubo-core 是什么？

创建 uBlock Origin 的静态网络过滤引擎的工作实例

### 如何安装 @gorhill/ubo-core？

在项目目录运行 npm install @gorhill/ubo-core，或使用 pnpm add / yarn add / bun add。

### @gorhill/ubo-core 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/gorhill\_\_ubo-core 是否存在。

### @gorhill/ubo-core 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @gorhill/ubo-core 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=18\.0\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @gorhill/ubo-core 可以商用吗？

@gorhill/ubo-core 使用 GPL-3\.0-or-later 许可。强 Copyleft：分发包含该代码的软件时，整个作品需以 GPL-3\.0 兼容许可开源，并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括，不构成法律意见，请以许可原文为准。

### @gorhill/ubo-core 的下载量有多少？

根据 npm 官方下载统计，@gorhill/ubo-core 在 2026/10/17 – 2026/11/15 这 30 天内被下载 10,982 次，日均约 366 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @gorhill/ubo-core 还在维护吗？

最新版本 0\.1\.30 发布于 2024/10/07（2 年前）；至今共发布 29 个版本，其中过去 12 个月发布了 0 个；首次发布于 2021/08/06。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

