Skip to content

Commit

Permalink
fix(fslib): handle .zip in the middle of file name again
Browse files Browse the repository at this point in the history
  • Loading branch information
ratson committed Jun 24, 2021
1 parent 5ebd914 commit 485f09f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
22 changes: 11 additions & 11 deletions packages/yarnpkg-fslib/sources/ZipOpenFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ const DOT_ZIP = `.zip`;
* The indexOf-based implementation is ~3.7x faster than a RegExp-based implementation.
*/
export const getArchivePart = (path: string) => {
const idx = path.indexOf(DOT_ZIP);
let idx = path.indexOf(DOT_ZIP);
if (idx <= 0)
return null;

// Disallow files named ".zip"
if (path[idx - 1] === ppath.sep)
return null;

let nextCharIdx = idx + DOT_ZIP.length;
const idxBeforeSep = path.indexOf(ppath.sep, nextCharIdx);
if (idxBeforeSep >= 0)
nextCharIdx = idxBeforeSep;
else if (path.endsWith(DOT_ZIP))
nextCharIdx = path.length;
let nextCharIdx = idx;
while (idx >= 0) {
nextCharIdx = idx + DOT_ZIP.length;
if (path[nextCharIdx] === ppath.sep)
break;
// Disallow files named ".zip"
if (path[idx - 1] === ppath.sep)
return null;
idx = path.indexOf(DOT_ZIP, nextCharIdx);
}

// The path either has to end in ".zip" or contain an archive subpath (".zip/...")
if (path.length > nextCharIdx && path[nextCharIdx] !== ppath.sep)
Expand Down
1 change: 1 addition & 0 deletions packages/yarnpkg-fslib/tests/ZipOpenFS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe(`getArchivePart`, () => {
[`./a/b/c/foo.zip/bar/baz/qux.zip`, `./a/b/c/foo.zip`],
[`./a/b/c/foo.zip-bar.zip`, `./a/b/c/foo.zip-bar.zip`],
[`./a/b/c/foo.zip-bar.zip/bar/baz/qux.zip`, `./a/b/c/foo.zip-bar.zip`],
[`./a/b/c/foo.zip-bar/foo.zip-bar/foo.zip-bar.zip/d`, `./a/b/c/foo.zip-bar/foo.zip-bar/foo.zip-bar.zip`],
] as const;

for (const [path, result] of tests) {
Expand Down

0 comments on commit 485f09f

Please sign in to comment.