Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gitrest: Compress latest summary with Pako deflate #20728

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions server/gitrest/packages/gitrest-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"memfs": "^3.4.12",
"nconf": "^0.11.4",
"object-sizeof": "^1.6.3",
"pako": "~2.1.0",
"prettier": "~3.0.3",
"split": "^1.0.0",
"uuid": "^3.3.2",
Expand All @@ -92,6 +93,7 @@
"@types/mocha": "^10.0.0",
"@types/nconf": "^0.10.0",
"@types/node": "^18.17.1",
"@types/pako": "~2.0.3",
"@types/rimraf": "^3.0.2",
"@types/sinon": "^17.0.2",
"@types/supertest": "^2.0.7",
Expand Down
37 changes: 33 additions & 4 deletions server/gitrest/packages/gitrest-base/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { PathLike, Stats, type BigIntStats } from "fs";
import * as path from "path";
import { inflate, deflate } from "pako";
import { Request } from "express";
import {
IGetRefParamsExternal,
Expand Down Expand Up @@ -111,6 +112,28 @@ export async function exists(
const latestFullSummaryFilename = "latestFullSummary";
const getLatestFullSummaryFilePath = (dir: string) => `${dir}/${latestFullSummaryFilename}`;

function compressData(data: string): Uint8Array {
return deflate(data);
}
function decompressData(data: string | Buffer): Uint8Array {
const bufferData = typeof data === "string" ? Buffer.from(data) : data;
try {
// Pako has its own compression type check that happens before the actual decompression.
// We can rely on that to determine if the data is compressed or not.
return inflate(bufferData);
} catch (error: unknown) {
if (
typeof error === "string" &&
(error === "incorrect header check" || error === "unknown compression method")
) {
// If the data is not compressed, return the original data.
return bufferData;
}
// Don't log any error information because this specifically deals with customer data.
throw new NetworkError(500, "Failed to decompress data");
}
}

export async function persistLatestFullSummaryInStorage(
fileSystemManager: IFileSystemManager,
storageDirectoryPath: string,
Expand All @@ -134,7 +157,7 @@ export async function persistLatestFullSummaryInStorage(
}
await fileSystemManager.promises.writeFile(
getLatestFullSummaryFilePath(storageDirectoryPath),
JSON.stringify(latestFullSummary),
compressData(JSON.stringify(latestFullSummary)),
);
persistLatestFullSummaryInStorageMetric.success(
"Successfully persisted latest full summary in storage",
Expand Down Expand Up @@ -162,7 +185,7 @@ export async function retrieveLatestFullSummaryFromStorage(
getLatestFullSummaryFilePath(storageDirectoryPath),
);
// TODO: This will be converted back to a JSON string for the HTTP response
const summary: IWholeFlatSummary = JSON.parse(summaryFile.toString());
const summary: IWholeFlatSummary = JSON.parse(decompressData(summaryFile).toString());
retrieveLatestFullSummaryMetric.setProperty(
BaseGitRestTelemetryProperties.emptyFullSummary,
false,
Expand Down Expand Up @@ -211,11 +234,17 @@ export function getRepoPath(tenantId: string, documentId?: string, owner?: strin
return [owner, tenantId, documentId].filter((x) => x !== undefined).join("/");
}

export function getGitDirectory(repoPath: string, baseDir?: string, suffixPath?: string): string {
export function getGitDirectory(
repoPath: string,
baseDir?: string,
suffixPath?: string,
): string {
return [baseDir, repoPath, suffixPath].filter((x) => x !== undefined).join("/");
}

export function parseStorageRoutingId(storageRoutingId?: string): IStorageRoutingId | undefined {
export function parseStorageRoutingId(
storageRoutingId?: string,
): IStorageRoutingId | undefined {
if (!storageRoutingId) {
return undefined;
}
Expand Down
11 changes: 10 additions & 1 deletion server/gitrest/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading