Skip to content

Commit cad1295

Browse files
Qa/1583 W-19096793 (#1591)
* perf: optimize zip tree container match * perf: optimize zip tree container match * perf: optimize zip tree container match * perf: optimize zip tree container match * perf: optimize zip tree container match * perf: optimize zip tree container match * chore: bump core --------- Co-authored-by: Jon Freed <[email protected]>
1 parent 2bc9435 commit cad1295

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/resolve/treeContainers.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
/* eslint-disable class-methods-use-this */
8-
import { join, dirname, basename, normalize, sep } from 'node:path';
8+
import { join, dirname, basename, sep, posix } from 'node:path';
99
import { Readable } from 'node:stream';
1010
import { statSync, existsSync, readdirSync, createReadStream, readFileSync } from 'graceful-fs';
1111
import JSZip from 'jszip';
@@ -126,10 +126,18 @@ export class NodeFSTreeContainer extends TreeContainer {
126126
*/
127127
export class ZipTreeContainer extends TreeContainer {
128128
private zip: JSZip;
129+
private zipKeyMap: Map<string, string> = new Map<string, string>();
129130

130131
private constructor(zip: JSZip) {
131132
super();
132133
this.zip = zip;
134+
for (const key of Object.keys(this.zip.files)) {
135+
if (key.endsWith('/')) {
136+
this.zipKeyMap.set(key.slice(0, -1), key);
137+
} else {
138+
this.zipKeyMap.set(key, key);
139+
}
140+
}
133141
}
134142

135143
public static async create(buffer: Buffer): Promise<ZipTreeContainer> {
@@ -191,23 +199,15 @@ export class ZipTreeContainer extends TreeContainer {
191199
throw new SfError(messages.getMessage('error_expected_file_path', [fsPath]), 'LibraryError');
192200
}
193201

194-
// Finds a matching entry in the zip by first comparing basenames, then dirnames.
195-
// Note that zip files always use forward slash separators, so paths within the
196-
// zip files are normalized for the OS file system before comparing.
202+
// Finds a matching entry in the map of zip keys (that have trailing /'s removed).
203+
// Note that zip files always use forward slash separators, so the provided path
204+
// is converted to use posix forward slash separators before comparing.
197205
private match(fsPath: string): string | undefined {
198206
// "dot" has a special meaning as a directory name and always matches. Just return it.
199207
if (fsPath === '.') {
200208
return fsPath;
201209
}
202-
203-
const fsPathBasename = basename(fsPath);
204-
const fsPathDirname = dirname(fsPath);
205-
return Object.keys(this.zip.files).find((filePath) => {
206-
const normFilePath = normalize(filePath);
207-
if (basename(normFilePath) === fsPathBasename) {
208-
return dirname(normFilePath) === fsPathDirname;
209-
}
210-
});
210+
return this.zipKeyMap.get(posix.normalize(fsPath.replaceAll('\\', '/')));
211211
}
212212

213213
private ensureDirectory(dirPath: string): boolean {

0 commit comments

Comments
 (0)