Add transformPath utility to handle platform prefixes

Introduces the transformPath function to remove platform-specific prefixes from paths. This utility helps standardize path handling by stripping prefixes like /gh/ or /cr/docker/ based on the platform key.
This commit is contained in:
xixu-me committed 2025-12-10 14:36:29 +08:00
1 parent a6a0e97ada
commit 89c8da936d
1 file changed
+19
+19
View File
@@ -594,6 +594,24 @@ function parseAuthenticate(authenticateStr) {
};
}
/**
* Transforms the effective path by removing the platform prefix.
*
* @param {string} path - The original path (e.g. /gh/user/repo, /cr/docker/image)
* @param {string} platform - The platform key (e.g. gh, cr-docker)
* @returns {string} The transformed path (e.g. /user/repo, /image)
*/
function transformPath(path, platform) {
// Convert platform key to path prefix format (e.g. cr-docker -> /cr/docker/)
const platformPrefix = `/${platform.replace(/-/g, '/')}/`;
if (path.startsWith(platformPrefix)) {
return path.substring(platformPrefix.length - 1); // Keep the leading slash
}
return path;
}
/**
* Fetches authentication token from container registry token service.
*
@@ -1223,6 +1241,7 @@ async function handleRequest(request, env, ctx) {
// Try to get a token for public access (without authorization)
const tokenResponse = await fetchToken(wwwAuthenticate, scope || '', '');
if (tokenResponse.ok) {
const tokenData = await tokenResponse.json();
if (tokenData.token) {