mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-04 10:03:29 +01:00
Merge pull request #915 from tdurieux/feat/shared-blob-cache
perf: deduplicate cached files across projects and track cache reuse
This commit is contained in:
31 files changed
+2107
-42
No files matched your search
@@ -1,3 +1,5 @@
|
||||
import { observeCache } from "./cache-observability";
|
||||
import { performance } from "perf_hooks";
|
||||
import { createHash } from "crypto";
|
||||
import { transformedFile } from "./transformed-cache";
|
||||
import { promises as fs, createReadStream, ReadStream } from "fs";
|
||||
@@ -105,7 +107,7 @@ function isTextualMime(mime: string): boolean {
|
||||
// Name-only classification: returns true (known text), false (known binary),
|
||||
// or null when the name alone is inconclusive. The streaming transformer
|
||||
// resolves null by sniffing the first chunk with isbinaryfile.
|
||||
function classifyByName(filePath: string): boolean | null {
|
||||
export function classifyByName(filePath: string): boolean | null {
|
||||
const name = basename(filePath);
|
||||
const extension = name.split(".").reverse()[0].toLowerCase();
|
||||
if (config.additionalExtensions.includes(extension)) return true;
|
||||
@@ -143,6 +145,7 @@ export class AnonymizeTransformer extends Transform {
|
||||
anonimizer: ContentAnonimizer;
|
||||
private wholeChunks: Buffer[] = [];
|
||||
private wholeBytes = 0;
|
||||
private passthroughBytes = 0;
|
||||
private digest = createHash("sha256");
|
||||
private spool?: string;
|
||||
private reservedSpool = 0;
|
||||
@@ -157,6 +160,7 @@ export class AnonymizeTransformer extends Transform {
|
||||
filePath: string;
|
||||
cacheGeneration?: string;
|
||||
cacheRevision?: string;
|
||||
decisionManaged?: boolean;
|
||||
} & ConstructorParameters<typeof ContentAnonimizer>[0]
|
||||
) {
|
||||
super();
|
||||
@@ -200,6 +204,7 @@ export class AnonymizeTransformer extends Transform {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
this.passthroughBytes += chunk.length;
|
||||
this.emit("transform", { isText: this.isText, wasAnonimized: false, chunk });
|
||||
this.push(chunk);
|
||||
}
|
||||
@@ -237,13 +242,17 @@ export class AnonymizeTransformer extends Transform {
|
||||
}
|
||||
|
||||
_flush(callback: (error?: Error) => void) {
|
||||
if (this.passthroughBytes) observeCache(this.isText ? "inspected_passthrough" : "inspected_binary", this.passthroughBytes);
|
||||
if (this.spool) {
|
||||
const output = join(this.spool, "output");
|
||||
void transformedFile(this.digest.digest("hex"), this.opt, output, () => anonymizeOnWorker({ input: join(this.spool!, "input"), output,
|
||||
options: this.opt, maxOutput: config.MAX_FILE_SIZE * 4, context: { mask: config.ANONYMIZATION_MASK, hostname: config.APP_HOSTNAME } }, this.wholeBytes, this.cancelled.signal))
|
||||
const started = performance.now();
|
||||
const produce = () => anonymizeOnWorker({ input: join(this.spool!, "input"), output,
|
||||
options: this.opt, maxOutput: config.MAX_FILE_SIZE * 4, context: { mask: config.ANONYMIZATION_MASK, hostname: config.APP_HOSTNAME } }, this.wholeBytes, this.cancelled.signal);
|
||||
void (this.opt.decisionManaged ? produce() : transformedFile(this.digest.digest("hex"), this.opt, output, produce))
|
||||
.then(changed => {
|
||||
if (this.destroyed) return;
|
||||
this.anonimizer.wasAnonymized = changed;
|
||||
observeCache(changed ? "inspected_changed" : "inspected_unchanged", this.wholeBytes, performance.now() - started);
|
||||
this.output = createReadStream(output);
|
||||
this.output.on("data", chunk => {
|
||||
this.emit("transform", { isText: true, wasAnonimized: changed, chunk });
|
||||
@@ -260,7 +269,9 @@ export class AnonymizeTransformer extends Transform {
|
||||
const original = Buffer.concat(this.wholeChunks, this.wholeBytes);
|
||||
this.wholeChunks = [];
|
||||
const text = original.toString("utf8");
|
||||
const started = performance.now();
|
||||
const out = this.anonimizer.anonymize(text);
|
||||
observeCache(out === text ? "inspected_unchanged" : "inspected_changed", original.length, performance.now() - started);
|
||||
// Preserve the original encoding when no replacement was needed.
|
||||
const chunk = out === text ? original : Buffer.from(out, "utf8");
|
||||
this.emit("transform", { isText: true, wasAnonimized: this.wasAnonimized, chunk });
|
||||
|
||||
Reference in new issue
Block a user