Chore: embed content hash

This commit is contained in:
SukkaW
2026-08-01 01:30:47 +08:00
parent 32a0c1a49b
commit b3bcec6de6
6 changed files with 131 additions and 41 deletions

View File

@@ -13,7 +13,7 @@ export class AdGuardHome extends BaseWriteStrategy {
protected result: string[] = [];
// eslint-disable-next-line @typescript-eslint/class-methods-use-this -- abstract method
withPadding(title: string, description: string[] | readonly string[], date: Date, content: string[]): string[] {
withPadding(title: string, description: string[] | readonly string[], date: Date, content: string[], contentHash: string | null): string[] {
return [
`! Title: ${title}`,
'! Last modified: ' + date.toUTCString(),
@@ -21,6 +21,7 @@ export class AdGuardHome extends BaseWriteStrategy {
'! License: https://github.com/SukkaW/Surge/blob/master/LICENSE',
'! Homepage: https://github.com/SukkaW/Surge',
`! Description: ${description.join(' ')}`,
...(contentHash ? [`! ${contentHash}`] : []),
'!',
...content,
'! EOF'

View File

@@ -1,4 +1,5 @@
import type { Span } from '../../trace';
import { calculateContentHash } from '../content-hash';
import { compareAndWriteFile } from '../create-file';
/**
@@ -44,7 +45,7 @@ export abstract class BaseWriteStrategy {
abstract writeProtocols(protocol: Set<string>): void;
abstract writeOtherRules(rule: string[]): void;
protected abstract withPadding(title: string, description: string[] | readonly string[], date: Date, content: string[]): string[];
protected abstract withPadding(title: string, description: string[] | readonly string[], date: Date, content: string[], contentHash: string | null): string[];
public output(
span: Span,
@@ -53,19 +54,28 @@ export abstract class BaseWriteStrategy {
date: Date,
filePath: string
): void | Promise<void> {
if (!this.result) {
const result = this.result;
if (!result) {
return;
}
// The hash covers the real content (title, description and rules) but not the
// volatile date, so compareAndWriteFile can bail out by only reading the head
// of the previous output. Strategies whose withPadding doesn't embed the marker
// (e.g. JSON output) simply fall back to the full comparison.
const contentHash = calculateContentHash(title, description, result);
return compareAndWriteFile(
span,
this.withPadding(
title,
description,
date,
this.result
result,
contentHash
),
filePath
filePath,
contentHash
);
};