A tool for generating differential updates for filter lists.
npm install @adguard/diff-builderA tool for generating differential updates for filter lists.
- Prerequisites
- How to Install
- How to Use
- CLI
- API
- Algorithm Overview
- [Node.js] v18.13.0 or higher.
- diff utility — this tool relies on the standard Unix diff utility to generate patches efficiently.
Make sure it's installed on your system:
- On macOS: Available by default or through XCode CLI tools.
- On Linux: Available by default or install via your package manager (e.g., apt-get install diffutils).
- On Windows: Available via WSL or Git Bash.
[Node.js]: https://nodejs.org/en/download
``bash`
pnpm add @adguard/diff-builder
`bash`
diff-builder build [-c] [-d
Where:
- — the relative path to the old filter.
- — the relative path to the new filter.
- — the relative path to the directory with patches.-n
- or --name= — name of the patch file, an arbitrary string to identify the patch.-r
Must be a string of length 1-64 with no spaces or other special characters.
- or --resolution= — is an optional flag,expirationPeriod
that specifies the resolution for both and epochTimestamp (timestamp when the patch was generated).h
Possible values:
- — hours (used if resolution is not specified)m
- — minutess
- — seconds-t
- or --time= — expiration time for the diff updateresolution
(the unit depends on parameter).-d
- or --delete-older-than-sec= — an optional parameter,
this time in seconds will be used when scanning the folder to remove patches,604800
which not empty and whose created epoch timestamp is older than the specified time.
By default, it will be (7 days).-v
- or --verbose — verbose mode.-c
- or --checksum — an optional flag, indicating whether it should calculate the SHA sum for the filterdiff
and add it to the directive with the filter name and the number of changed lines,diff name:[name] checksum:[checksum] lines:[lines]
following this format: :name
- — the name of the corresponding filter list.Diff-Name
This key-value pair is optional — it will be included only if there is a tag in the .checksum
- — the expected SHA1 checksum of the file after the patch is applied.lines
This is used to validate the patch.
- — the number of lines that follow, making up the RCS diff block.lines
Note that are counted using the same algorithm as used by wc -l, essentially counting \n.
- Resolve absolute paths for the old and new filters and the patches directory.
- Ensure the patches directory exists, creating it if necessary.
- Delete any outdated patches from the patches directory except empty patches.
- Read and split the old and new filter files into lines.
- Check if there are significant changes between the two sets of lines, excluding 'Diff-Path' and 'Checksum' tags.
- If no significant changes are found, revert any changes in the new filter and exit.
- Generate a new patch name and validate its uniqueness.
- Update the 'Diff-Path' tag in the new filter.
- Create a diff patch between the old and new filters.
- Optionally, add a checksum to the patch.
- Write the updated new filter back to its file.
- Create an empty patch file for future use if necessary.
- Save the diff patch to the appropriate file.
`javascript
const { DiffBuilder } = require('@adguard/diff-builder');
const { DiffUpdater } = require('@adguard/diff-builder/diff-updater');
await DiffBuilder.buildDiff({
oldFilterPath,
newFilterPath,
patchesPath,
name,
time,
resolution,
verbose: true,
});
const updatedFilter = await DiffUpdater.applyPatch({
filterUrl,
filterContent,
verbose: true,
});
`
`javascript
import { DiffBuilder } from '@adguard/diff-builder/es';
import { DiffUpdater } from '@adguard/diff-builder/diff-updater/es';
await DiffBuilder.buildDiff({
oldFilterPath,
newFilterPath,
patchesPath,
name,
time,
resolution,
verbose: true,
});
const updatedFilter = await DiffUpdater.applyPatch({
filterUrl,
filterContent,
verbose: true,
});
``