From d1a1b7dc947063aef5f8375a6a1e03246b272c84 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 18 Aug 2021 17:29:55 +0200 Subject: Implemented caching for certain API endpoints, Added documentation & made backend-fixes --- packages/server/src/git/patch.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'packages/server/src/git/patch.ts') diff --git a/packages/server/src/git/patch.ts b/packages/server/src/git/patch.ts index 4239ce4..4527d03 100644 --- a/packages/server/src/git/patch.ts +++ b/packages/server/src/git/patch.ts @@ -1,5 +1,6 @@ import { Diff } from "./diff"; import { ConvenientPatch as NodeGitPatch } from "nodegit"; +import { createError, ErrorWhere, PatchTooLargeError } from "./error"; type Hunk = { new_start: number, @@ -87,10 +88,9 @@ export class Patch { * * These bounds are in the context of it's whole diff * - * @returns A patch bounds instance which contains a start & an end property + * @returns The patch's bounds */ - private async _bounds(): Promise { - const raw_patches = (await this._diff.rawPatches()).split("\n"); + private async _bounds(raw_patches: string[]): Promise { const patch_header_data = await this._diff.patchHeaderData(); return { @@ -104,7 +104,7 @@ export class Patch { */ private async _content(): Promise { const raw_patches = (await this._diff.rawPatches()).split("\n"); - const bounds = await this._bounds(); + const bounds = await this._bounds(raw_patches); return raw_patches.slice(bounds.start, bounds.end).join("\n"); } @@ -115,10 +115,14 @@ export class Patch { * @returns Whether or not the patch is too large */ public async isTooLarge(): Promise { + if(this.additions > 2000 || this.deletions > 2000) { + return true; + } + const content = (await this._content()).split("\n"); const line_lengths = content.map(line => line.length).reduce((result, length) => result + length); - if(content.length > 5000 || line_lengths > 5000) { + if(content.length > 10000 || line_lengths > 10000) { return true; } @@ -131,6 +135,10 @@ export class Patch { * @returns An array of hunk instances */ public async getHunks(): Promise { + if(await this.isTooLarge()) { + throw(createError(ErrorWhere.Patch, PatchTooLargeError)); + } + const content = (await this._content()).split("\n"); const hunks = await this._ng_patch.hunks(); -- cgit v1.2.3-18-g5258