mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: heuristics
This commit is contained in:
1 parent
2cfc65a990
commit
c4be6b78b6
1 file changed
+56
-18
@@ -7,6 +7,8 @@ export interface FileChange {
|
|||||||
content: Buffer;
|
content: Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const heuristicFormats = ['glb'];
|
||||||
|
|
||||||
export async function commitMultipleFiles(
|
export async function commitMultipleFiles(
|
||||||
repoRaw: string,
|
repoRaw: string,
|
||||||
branch: string,
|
branch: string,
|
||||||
@@ -21,31 +23,67 @@ export async function commitMultipleFiles(
|
|||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`https://raw.githubusercontent.com/${blobPath}`,
|
`https://raw.githubusercontent.com/${blobPath}`,
|
||||||
);
|
);
|
||||||
|
let isNew: boolean;
|
||||||
|
let isDifferent: boolean;
|
||||||
|
let diff: number;
|
||||||
|
|
||||||
if (response.status === 404) {
|
if (response.status === 404) {
|
||||||
|
isNew = true;
|
||||||
|
diff = change.content.length;
|
||||||
|
isDifferent = true;
|
||||||
|
} else if (
|
||||||
|
response.headers.has('Content-Length') &&
|
||||||
|
parseInt(response.headers.get('Content-Length')!) !==
|
||||||
|
change.content.length
|
||||||
|
) {
|
||||||
|
isNew = false;
|
||||||
|
diff =
|
||||||
|
change.content.length -
|
||||||
|
parseInt(response.headers.get('Content-Length')!);
|
||||||
|
isDifferent = true;
|
||||||
|
} else if (response.status === 200) {
|
||||||
|
isNew = false;
|
||||||
|
const buffer = Buffer.from(await response.arrayBuffer());
|
||||||
|
|
||||||
|
if (buffer.equals(change.content)) {
|
||||||
|
diff = 0;
|
||||||
|
isDifferent = false;
|
||||||
|
} else {
|
||||||
|
diff = change.content.length - buffer.length;
|
||||||
|
isDifferent = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error(
|
||||||
|
`Unexpected status code ${response.status} for ${blobPath}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Heuristic: if it's a large blob like a .glb, a diff = 0 might end up
|
||||||
|
* being a false positive with isDifferent = true. So, it's okay to
|
||||||
|
* assume it's unchanged.
|
||||||
|
*/
|
||||||
|
if (
|
||||||
|
diff === 0 &&
|
||||||
|
heuristicFormats.some((format) => change.path.endsWith(`.${format}`))
|
||||||
|
) {
|
||||||
|
isDifferent = false;
|
||||||
|
isNew = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isNew) {
|
||||||
console.log(
|
console.log(
|
||||||
`🟢 (+${change.content.length.toLocaleString()}B) ${blobPath}`,
|
`🟢 (+${change.content.length.toLocaleString()}B) ${blobPath}`,
|
||||||
);
|
);
|
||||||
changes.push(change);
|
changes.push(change);
|
||||||
} else if (response.status === 200) {
|
} else if (isDifferent) {
|
||||||
const buffer = Buffer.from(await response.arrayBuffer());
|
console.log(
|
||||||
|
`🟡 (${diff > 0 ? '+' : ''}${diff.toLocaleString()}B) ${blobPath}`,
|
||||||
if (!buffer.equals(change.content)) {
|
);
|
||||||
const diff = change.content.length - buffer.length;
|
changes.push(change);
|
||||||
|
|
||||||
console.log(
|
|
||||||
`🟡 (${diff > 0 ? '+' : ''}${diff.toLocaleString()}B) ${blobPath}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
changes.push(change);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`🔵 (${buffer.length.toLocaleString()}B) ${blobPath}`);
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
console.log(
|
||||||
`Unexpected status code ${response.status} for ${blobPath}`,
|
`🔵 (${change.content.length.toLocaleString()}B) ${blobPath}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in new issue
Block a user