npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@adguard/diff-builder

v1.0.18

Published

A tool for generating differential updates for filter lists.

Downloads

357

Readme

AdGuard Diff Builder

A tool for generating differential updates for filter lists.

How to install

yarn add @adguard/diff-builder

How to Use

CLI

diff-builder build [-c] [-d <seconds>] [-r <resolution>] [-v] -n <name> -t <expirationPeriod> <old_filter> <new_filter> <path_to_patches>

Where:

  • <old_filter> — the relative path to the old filter.
  • <new_filter> — the relative path to the new filter.
  • <path_to_patches> — the relative path to the directory with patches.
  • -n <name> or --name=<name> — name of the patch file, an arbitrary string to identify the patch. Must be a string of length 1-64 with no spaces or other special characters.
  • -r <timestampResolution> or --resolution=<timestampResolution> — is an optional flag, that specifies the resolution for both expirationPeriod and epochTimestamp (timestamp when the patch was generated). Possible values:
    • h — hours (used if resolution is not specified)
    • m — minutes
    • s — seconds
  • -t <expirationPeriod> or --time=<expirationPeriod> — expiration time for the diff update (the unit depends on resolution parameter).
  • -d <seconds> or --delete-older-than-sec=<seconds> — an optional parameter, this time in seconds will be used when scanning the <path_to_patches> folder to remove patches, which not empty and whose created epoch timestamp is older than the specified time. By default, it will be 604800 (7 days).
  • -v or --verbose — verbose mode.
  • -c or --checksum — an optional flag, indicating whether it should calculate the SHA sum for the filter and add it to the diff directive with the filter name and the number of changed lines, following this format: diff name:[name] checksum:[checksum] lines:[lines]:
    • name — the name of the corresponding filter list. This key-value pair is optional — it will be included only if there is a Diff-Name tag in the <old_filter>.
    • checksum — the expected SHA1 checksum of the file after the patch is applied. This is used to validate the patch.
    • lines — the number of lines that follow, making up the RCS diff block. Note that lines are counted using the same algorithm as used by wc -l, essentially counting \n.

Algorithm Overview

1. Setup

  • Resolve absolute paths for the old and new filters and the patches directory.

2. Prepare Patch Directory

  • Ensure the patches directory exists, creating it if necessary.

3. Clean Up Old Patches

  • Delete any outdated patches from the patches directory except empty patches.

4. Read Filters and Detect Changes

  • 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.

5. Handle No Changes

  • If no significant changes are found, revert any changes in the new filter and exit.

6. Process Changes

  • 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.

7. Finalize

  • 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.

API

CJS

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,
});

ESM

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,
});