@@ -23,6 +23,7 @@ import { getTextContent } from '../../shared/utilities/textDocumentUtilities'
2323import { ChildProcess , ChildProcessOptions } from '../../shared/utilities/processUtils'
2424import { removeAnsi } from '../../shared/utilities/textUtilities'
2525import { isFileOpenAndDirty } from '../../shared/utilities/vsCodeUtils'
26+ import { ToolkitError } from '../../shared/errors'
2627
2728export interface ZipMetadata {
2829 rootDir : string
@@ -51,6 +52,7 @@ export const ZipConstants = {
5152}
5253
5354type ZipType = 'file' | 'project'
55+ type PayloadLimits = Record < ZipType , number >
5456
5557export class ZipUtil {
5658 protected _pickedSourceFiles : Set < string > = new Set < string > ( )
@@ -63,18 +65,23 @@ export class ZipUtil {
6365 protected _fetchedDirs : Set < string > = new Set < string > ( )
6466 protected _language : CodewhispererLanguage | undefined
6567 protected _timestamp : string = Date . now ( ) . toString ( )
66- protected static _payloadByteLimits = {
67- file : CodeWhispererConstants . fileScanPayloadSizeLimitBytes ,
68- project : CodeWhispererConstants . projectScanPayloadSizeLimitBytes ,
68+ protected _payloadByteLimits : PayloadLimits
69+ constructor (
70+ protected _zipDirPrefix : string ,
71+ payloadLimits ?: PayloadLimits
72+ ) {
73+ this . _payloadByteLimits = payloadLimits ?? {
74+ file : CodeWhispererConstants . fileScanPayloadSizeLimitBytes ,
75+ project : CodeWhispererConstants . projectScanPayloadSizeLimitBytes ,
76+ }
6977 }
70- constructor ( protected _zipDirPrefix : string ) { }
7178
72- public static aboveByteLimit ( size : number , limitType : ZipType ) : boolean {
79+ public aboveByteLimit ( size : number , limitType : ZipType ) : boolean {
7380 return size > this . _payloadByteLimits [ limitType ]
7481 }
7582
76- public static willReachProjectByteLimit ( current : number , adding : number ) : boolean {
77- return ZipUtil . aboveByteLimit ( current + adding , 'project' )
83+ public willReachProjectByteLimit ( current : number , adding : number ) : boolean {
84+ return this . aboveByteLimit ( current + adding , 'project' )
7885 }
7986
8087 public async zipFile ( uri : vscode . Uri | undefined , includeGitDiffHeader ?: boolean ) {
@@ -107,7 +114,7 @@ export class ZipUtil {
107114 this . _totalSize += ( await fs . stat ( uri . fsPath ) ) . size
108115 this . _totalLines += content . split ( ZipConstants . newlineRegex ) . length
109116
110- if ( ZipUtil . aboveByteLimit ( this . _totalSize , 'file' ) ) {
117+ if ( this . aboveByteLimit ( this . _totalSize , 'file' ) ) {
111118 throw new FileSizeExceededError ( )
112119 }
113120 const zipDirPath = this . getZipDirPath ( )
@@ -226,10 +233,15 @@ export class ZipUtil {
226233 if ( ! projectPaths || projectPaths . length === 0 ) {
227234 return
228235 }
229-
230236 const sourceFiles = await collectFiles ( projectPaths , workspaceFolders , {
231- maxTotalSizeBytes : ZipUtil . _payloadByteLimits [ 'project' ] ,
237+ maxTotalSizeBytes : this . _payloadByteLimits [ 'project' ] ,
232238 excludePatterns,
239+ } ) . catch ( ( e ) => {
240+ // Check if project is too large to collect.
241+ if ( e instanceof ToolkitError && e . code === 'ContentLengthError' ) {
242+ throw new ProjectSizeExceededError ( )
243+ }
244+ throw e
233245 } )
234246 for ( const file of sourceFiles ) {
235247 const projectName = path . basename ( file . workspaceFolder . uri . fsPath )
@@ -263,8 +275,8 @@ export class ZipUtil {
263275 const fileSize = Buffer . from ( fileContent ) . length
264276
265277 if (
266- ZipUtil . aboveByteLimit ( this . _totalSize , 'project' ) ||
267- ZipUtil . willReachProjectByteLimit ( this . _totalSize , fileSize )
278+ this . aboveByteLimit ( this . _totalSize , 'project' ) ||
279+ this . willReachProjectByteLimit ( this . _totalSize , fileSize )
268280 ) {
269281 throw new ProjectSizeExceededError ( )
270282 }
@@ -280,8 +292,8 @@ export class ZipUtil {
280292 const fileSize = ( await fs . stat ( uri . fsPath ) ) . size
281293
282294 if (
283- ZipUtil . aboveByteLimit ( this . _totalSize , 'project' ) ||
284- ZipUtil . willReachProjectByteLimit ( this . _totalSize , fileSize )
295+ this . aboveByteLimit ( this . _totalSize , 'project' ) ||
296+ this . willReachProjectByteLimit ( this . _totalSize , fileSize )
285297 ) {
286298 throw new ProjectSizeExceededError ( )
287299 }
0 commit comments