Skip to content

Commit 72846c0

Browse files
committed
deps: avoid using admZip in zipUtil
1 parent 45412b9 commit 72846c0

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

packages/core/src/codewhisperer/util/zipUtil.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5-
import admZip from 'adm-zip'
65
import * as vscode from 'vscode'
76
import path from 'path'
87
import { tempDirPath, testGenerationLogsDir } from '../../shared/filesystemUtilities'
@@ -25,6 +24,7 @@ import { ChildProcess, ChildProcessOptions } from '../../shared/utilities/proces
2524
import { ProjectZipError } from '../../amazonqTest/error'
2625
import { removeAnsi } from '../../shared/utilities/textUtilities'
2726
import { normalize } from '../../shared/utilities/pathUtils'
27+
import { ZipStream } from '../../shared/utilities/zipStream'
2828

2929
export interface ZipMetadata {
3030
rootDir: string
@@ -109,7 +109,7 @@ export class ZipUtil {
109109
if (!uri) {
110110
throw new NoActiveFileError()
111111
}
112-
const zip = new admZip()
112+
const zip = new ZipStream()
113113

114114
const content = await this.getTextContent(uri)
115115

@@ -121,14 +121,14 @@ export class ZipUtil {
121121
// Set includeWorkspaceFolder to false because we are already manually prepending the projectName
122122
const relativePath = vscode.workspace.asRelativePath(uri, false)
123123
const zipEntryPath = this.getZipEntryPath(projectName, relativePath)
124-
zip.addFile(zipEntryPath, Buffer.from(content, 'utf-8'))
124+
zip.writeString(content, zipEntryPath)
125125

126126
if (scope === CodeWhispererConstants.CodeAnalysisScope.FILE_ON_DEMAND) {
127127
const gitDiffContent = `+++ b/${normalize(zipEntryPath)}` // Sending file path in payload for LLM code review
128-
zip.addFile(ZipConstants.codeDiffFilePath, Buffer.from(gitDiffContent, 'utf-8'))
128+
zip.writeString(gitDiffContent, ZipConstants.codeDiffFilePath)
129129
}
130130
} else {
131-
zip.addFile(uri.fsPath, Buffer.from(content, 'utf-8'))
131+
zip.writeString(content, uri.fsPath)
132132
}
133133

134134
this._pickedSourceFiles.add(uri.fsPath)
@@ -139,7 +139,7 @@ export class ZipUtil {
139139
throw new FileSizeExceededError()
140140
}
141141
const zipFilePath = this.getZipDirPath(FeatureUseCase.CODE_SCAN) + CodeWhispererConstants.codeScanZipExt
142-
zip.writeZip(zipFilePath)
142+
await zip.finalizeToFile(zipFilePath)
143143
return zipFilePath
144144
}
145145

@@ -170,13 +170,13 @@ export class ZipUtil {
170170
* await processMetadataDir(zip, '/path/to/directory');
171171
* ```
172172
*/
173-
protected async processMetadataDir(zip: admZip, metadataDir: string) {
173+
protected async processMetadataDir(zip: ZipStream, metadataDir: string) {
174174
const metadataDirName = path.basename(metadataDir)
175175
// Helper function to add empty directory to zip
176176
const addEmptyDirectory = (dirPath: string) => {
177177
const relativePath = path.relative(metadataDir, dirPath)
178178
const pathWithMetadata = path.join(metadataDirName, relativePath, '/')
179-
zip.addFile(pathWithMetadata, Buffer.from(''))
179+
zip.writeString('', pathWithMetadata)
180180
}
181181

182182
// Recursive function to process directories
@@ -193,7 +193,7 @@ export class ZipUtil {
193193
const buffer = Buffer.from(fileContent)
194194
const relativePath = path.relative(metadataDir, filePath)
195195
const pathWithMetadata = path.join(metadataDirName, relativePath)
196-
zip.addFile(pathWithMetadata, buffer)
196+
zip.writeData(buffer, pathWithMetadata)
197197
} catch (error) {
198198
getLogger().error(`Failed to add file ${filePath} to zip: ${error}`)
199199
}
@@ -207,7 +207,7 @@ export class ZipUtil {
207207
}
208208

209209
protected async zipProject(useCase: FeatureUseCase, projectPath?: string, metadataDir?: string) {
210-
const zip = new admZip()
210+
const zip = new ZipStream()
211211
let projectPaths = []
212212
if (useCase === FeatureUseCase.TEST_GENERATION && projectPath) {
213213
projectPaths.push(projectPath)
@@ -232,12 +232,12 @@ export class ZipUtil {
232232
}
233233
this._language = [...languageCount.entries()].reduce((a, b) => (b[1] > a[1] ? b : a))[0]
234234
const zipFilePath = this.getZipDirPath(useCase) + CodeWhispererConstants.codeScanZipExt
235-
zip.writeZip(zipFilePath)
235+
await zip.finalizeToFile(zipFilePath)
236236
return zipFilePath
237237
}
238238

239239
protected async processCombinedGitDiff(
240-
zip: admZip,
240+
zip: ZipStream,
241241
projectPaths: string[],
242242
filePath?: string,
243243
scope?: CodeWhispererConstants.CodeAnalysisScope
@@ -254,7 +254,7 @@ export class ZipUtil {
254254
})
255255
}
256256
if (gitDiffContent) {
257-
zip.addFile(ZipConstants.codeDiffFilePath, Buffer.from(gitDiffContent, 'utf-8'))
257+
zip.writeString(gitDiffContent, ZipConstants.codeDiffFilePath)
258258
}
259259
}
260260

@@ -403,7 +403,7 @@ export class ZipUtil {
403403
}
404404

405405
protected async processSourceFiles(
406-
zip: admZip,
406+
zip: ZipStream,
407407
languageCount: Map<CodewhispererLanguage, number>,
408408
projectPaths: string[] | undefined,
409409
useCase: FeatureUseCase
@@ -444,7 +444,7 @@ export class ZipUtil {
444444
}
445445
}
446446

447-
protected processOtherFiles(zip: admZip, languageCount: Map<CodewhispererLanguage, number>) {
447+
protected processOtherFiles(zip: ZipStream, languageCount: Map<CodewhispererLanguage, number>) {
448448
for (const document of vscode.workspace.textDocuments
449449
.filter((document) => document.uri.scheme === 'file')
450450
.filter((document) => vscode.workspace.getWorkspaceFolder(document.uri) === undefined)) {
@@ -474,7 +474,7 @@ export class ZipUtil {
474474
}
475475

476476
protected processTextFile(
477-
zip: admZip,
477+
zip: ZipStream,
478478
uri: vscode.Uri,
479479
fileContent: string,
480480
languageCount: Map<CodewhispererLanguage, number>,
@@ -493,10 +493,10 @@ export class ZipUtil {
493493
this._totalLines += fileContent.split(ZipConstants.newlineRegex).length
494494

495495
this.incrementCountForLanguage(uri, languageCount)
496-
zip.addFile(zipEntryPath, Buffer.from(fileContent, 'utf-8'))
496+
zip.writeString(fileContent, zipEntryPath)
497497
}
498498

499-
protected async processBinaryFile(zip: admZip, uri: vscode.Uri, zipEntryPath: string) {
499+
protected async processBinaryFile(zip: ZipStream, uri: vscode.Uri, zipEntryPath: string) {
500500
const fileSize = (await fs.stat(uri.fsPath)).size
501501

502502
if (
@@ -508,7 +508,7 @@ export class ZipUtil {
508508
this._pickedSourceFiles.add(uri.fsPath)
509509
this._totalSize += fileSize
510510

511-
zip.addLocalFile(uri.fsPath, path.dirname(zipEntryPath))
511+
zip.writeFile(uri.fsPath, path.dirname(zipEntryPath))
512512
}
513513

514514
protected incrementCountForLanguage(uri: vscode.Uri, languageCount: Map<CodewhispererLanguage, number>) {

packages/core/src/shared/utilities/zipStream.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { readFileAsString } from '../filesystemUtilities'
88
// Use require instead of import since this package doesn't support commonjs
99
const { ZipWriter, TextReader, ZipReader, Uint8ArrayReader } = require('@zip.js/zip.js')
1010
import { getLogger } from '../logger/logger'
11+
import fs from '../fs/fs'
1112

1213
export interface ZipStreamResult {
1314
sizeInBytes: number
@@ -109,6 +110,16 @@ export class ZipStream {
109110
return this._zipWriter.add(path, new TextReader(data))
110111
}
111112

113+
// TODO: add tests for this.
114+
public writeData(data: Uint8Array, path: string) {
115+
return this._zipWriter.add(path, new Uint8ArrayReader(data))
116+
}
117+
118+
/**
119+
* Add the content for file to zip at path.
120+
* @param file file to read
121+
* @param path path to write data to in zip.
122+
*/
112123
public writeFile(file: string, path: string) {
113124
// We use _numberOfFilesToStream to make sure we don't finalize too soon
114125
// (before the progress event has been fired for the last file)
@@ -155,6 +166,14 @@ export class ZipStream {
155166
}
156167
}
157168

169+
// TODO: add tests for this.
170+
public async finalizeToFile(targetPath: string) {
171+
const result = await this.finalize()
172+
const contents = result.streamBuffer.getContents() || Buffer.from('')
173+
await fs.writeFile(targetPath, contents)
174+
return result
175+
}
176+
158177
public static async unzip(zipBuffer: Buffer): Promise<ZipReaderResult[]> {
159178
const reader = new ZipReader(new Uint8ArrayReader(new Uint8Array(zipBuffer)))
160179
try {

0 commit comments

Comments
 (0)