Fix benchmark script and Obsidian mock

This commit is contained in:
Silvano Cerza 2025-04-17 10:02:23 +02:00
parent 457a0b6d24
commit 96da957f18
2 changed files with 29 additions and 21 deletions

View file

@ -143,14 +143,22 @@ const generateRandomFiles = (
fs.writeFileSync(metadataFilePath, JSON.stringify(metadata), { flag: "w" }); fs.writeFileSync(metadataFilePath, JSON.stringify(metadata), { flag: "w" });
}; };
const cleanupRemote = async () => { const cleanupRemote = () => {
const url = `git@github.com:${process.env.REPO_OWNER}/${process.env.REPO_NAME}.git`; const url = `git@github.com:${process.env.REPO_OWNER}/${process.env.REPO_NAME}.git`;
const clonedDir = path.join(os.tmpdir(), "temp-clone"); const clonedDir = path.join(os.tmpdir(), "temp-clone");
// Remove the folder in case it already exists
fs.rmSync(clonedDir, { recursive: true, force: true });
try { try {
// Clone the repository // Clone the repository
execSync(`git clone ${url} ${clonedDir}`, { stdio: "ignore" }); execSync(`git clone ${url} ${clonedDir}`, { stdio: "ignore" });
const repoExists = fs.existsSync(clonedDir);
if (!repoExists) {
throw Error("Failed to clone repo");
}
// Remove all files except .git // Remove all files except .git
execSync('find . -type f -not -path "./.git*" -delete', { execSync('find . -type f -not -path "./.git*" -delete', {
stdio: "ignore", stdio: "ignore",
@ -166,16 +174,12 @@ const cleanupRemote = async () => {
// Push changes // Push changes
execSync("git push", { stdio: "ignore", cwd: clonedDir }); execSync("git push", { stdio: "ignore", cwd: clonedDir });
// Remove the folder
fs.rm(clonedDir, { recursive: true, force: true }, (err) => {
if (err) {
throw err;
}
});
} catch (error) { } catch (error) {
console.error(`Error: ${error.message}`); console.error(`Error: ${error.message}`);
} }
// Remove the folder when everything is done
fs.rmSync(clonedDir, { recursive: true, force: true });
}; };
const BENCHMARK_DATA = [ const BENCHMARK_DATA = [
@ -248,7 +252,7 @@ const BENCHMARK_DATA = [
const downloadTime = await runBenchmark(vaultRootDir); const downloadTime = await runBenchmark(vaultRootDir);
// Cleanup the remote repo so it's ready for another benchmark // Cleanup the remote repo so it's ready for another benchmark
await cleanupRemote(); cleanupRemote();
results.push({ results.push({
data, data,
@ -265,9 +269,5 @@ const BENCHMARK_DATA = [
} catch (error) { } catch (error) {
console.error("Benchmark failed:", error); console.error("Benchmark failed:", error);
} }
fs.rm(benchmarkRootDir, { recursive: true, force: true }, (err) => { fs.rmSync(benchmarkRootDir, { recursive: true, force: true });
if (err) {
throw err;
}
});
})(); })();

View file

@ -128,11 +128,22 @@ export async function requestUrl(options: RequestUrlParam) {
body: options.body, body: options.body,
}); });
const isJsonResponse = response.headers
.get("content-type")
?.includes("application/json");
// Convert to expected Obsidian response format // Convert to expected Obsidian response format
return { if (isJsonResponse) {
status: response.status, return {
json: await response.json(), status: response.status,
}; json: await response.json(),
};
} else {
return {
status: response.status,
arrayBuffer: await response.arrayBuffer(),
};
}
} }
// Mock utility functions // Mock utility functions
@ -150,6 +161,3 @@ export function base64ToArrayBuffer(base64: string): ArrayBuffer {
// Mock Event reference // Mock Event reference
export type EventRef = string; export type EventRef = string;
// Re-export the fileTypeFromBuffer function
export { fileTypeFromBuffer } from "file-type";