Skip to content

Commit 7a1f157

Browse files
committed
Extract common functionality between unzip implementations
1 parent 7c00768 commit 7a1f157

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed
Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
11
import { availableParallelism } from "os";
2-
import { openZip, readZipEntries, unzipFile } from "./unzip";
2+
import { unzipToDirectory } from "./unzip";
33
import PQueue from "p-queue";
44

55
export async function unzipToDirectoryConcurrently(
66
archivePath: string,
77
destinationPath: string,
88
): Promise<void> {
9-
const zipFile = await openZip(archivePath, {
10-
autoClose: false,
11-
strictFileNames: true,
12-
lazyEntries: true,
9+
const queue = new PQueue({
10+
concurrency: availableParallelism(),
1311
});
1412

15-
try {
16-
const entries = await readZipEntries(zipFile);
17-
18-
const queue = new PQueue({
19-
concurrency: availableParallelism(),
20-
});
21-
22-
await queue.addAll(
23-
entries.map((entry) => async () => {
24-
await unzipFile(zipFile, entry, destinationPath);
25-
}),
26-
);
27-
} finally {
28-
zipFile.close();
29-
}
13+
return unzipToDirectory(archivePath, destinationPath, async (tasks) => {
14+
await queue.addAll(tasks);
15+
});
3016
}

extensions/ql-vscode/src/common/unzip.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ async function copyStream(
109109
* @param entry
110110
* @param rootDestinationPath
111111
*/
112-
export async function unzipFile(
112+
async function unzipFile(
113113
zipFile: ZipFile,
114114
entry: ZipEntry,
115115
rootDestinationPath: string,
@@ -141,16 +141,18 @@ export async function unzipFile(
141141
}
142142

143143
/**
144-
* Sequentially unzips all files from a zip archive. Please use
145-
* `unzipToDirectoryConcurrently` if you can. This function is only
146-
* provided because Jest cannot import `p-queue`.
144+
* Unzips all files from a zip archive. Please use
145+
* `unzipToDirectoryConcurrently` or `unzipToDirectorySequentially` instead
146+
* of this function.
147147
*
148148
* @param archivePath
149149
* @param destinationPath
150+
* @param taskRunner A function that runs the tasks (either sequentially or concurrently).
150151
*/
151-
export async function unzipToDirectorySequentially(
152+
export async function unzipToDirectory(
152153
archivePath: string,
153154
destinationPath: string,
155+
taskRunner: (tasks: Array<() => Promise<void>>) => Promise<void>,
154156
): Promise<void> {
155157
const zipFile = await openZip(archivePath, {
156158
autoClose: false,
@@ -161,10 +163,29 @@ export async function unzipToDirectorySequentially(
161163
try {
162164
const entries = await readZipEntries(zipFile);
163165

164-
for (const entry of entries) {
165-
await unzipFile(zipFile, entry, destinationPath);
166-
}
166+
await taskRunner(
167+
entries.map((entry) => () => unzipFile(zipFile, entry, destinationPath)),
168+
);
167169
} finally {
168170
zipFile.close();
169171
}
170172
}
173+
174+
/**
175+
* Sequentially unzips all files from a zip archive. Please use
176+
* `unzipToDirectoryConcurrently` if you can. This function is only
177+
* provided because Jest cannot import `p-queue`.
178+
*
179+
* @param archivePath
180+
* @param destinationPath
181+
*/
182+
export async function unzipToDirectorySequentially(
183+
archivePath: string,
184+
destinationPath: string,
185+
): Promise<void> {
186+
return unzipToDirectory(archivePath, destinationPath, async (tasks) => {
187+
for (const task of tasks) {
188+
await task();
189+
}
190+
});
191+
}

0 commit comments

Comments
 (0)