Skip to content

Commit

Permalink
feat: reduce number of regex operations for normalizing paths
Browse files Browse the repository at this point in the history
  • Loading branch information
loilo committed Oct 13, 2023
1 parent babcc16 commit fab1317
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions src/drivers/utils/opfs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,21 @@ function ignoreNotfoundError(error: any): null {
* Normalize a path, removing empty segments and leading/trailing slashes
*/
export function normalizePath(path: string): string {
const normalizedWrappedPath = `/${path}/`
// Replace colons with slashes
.replace(/:/g, "/")

// Remove . segments
.replace(/\/\.\//g, "/")

// Remove duplicate slashes
.replace(/\/{2,}/g, "/");
// Wrap path in slashes, remove . segments and collapse subsequent namespace separators
const normalizedWrappedPath = `/${path}/`.replace(
/[/:]+(\.[/:]+)*[/:]*/g,
"/"
);

// Disallow .. segments
// Prohibit .. segments
if (normalizedWrappedPath.includes("/../")) {
throw createError(
DRIVER_NAME,
`Invalid key: ${JSON.stringify(path)}. It must not contain .. segments`
);
}

return (
normalizedWrappedPath
// Remove leading slashes
.replace(/^\//g, "")

// Remove trailing slashes
.replace(/\/$/g, "")
);
return normalizedWrappedPath.slice(1, -1);
}

/**
Expand Down

0 comments on commit fab1317

Please sign in to comment.