Skip to content

Commit

Permalink
src: rework mapBucketPathToUrlPath to work better with the redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 committed Oct 16, 2023
1 parent 3fcba3e commit b53c11d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
1 change: 0 additions & 1 deletion scripts/origin/cache-purge.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ rl.on('line', line => {
!['', '.', './'].includes(directoryName) &&
!pathsToPurge.includes(directoryName)
) {
pathsToPurge.push(directoryName);
pathsToPurge.push(directoryName + '/');
}
});
Expand Down
81 changes: 71 additions & 10 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ export function mapUrlPathToBucketPath(
return bucketPath !== undefined ? decodeURIComponent(bucketPath) : undefined;
}

/**
* Get all of the directories beginning with 'latest' in a
* directory
* @param prefix Directory to look through
*/
function getAllLatestDirectories(prefix: string): Set<string> {
const paths = new Set<string>();
for (const [k] of REDIRECT_MAP) {
if (k.startsWith(`${prefix}/latest`)) {
paths.add(k.substring(prefix.length) + '/');
}
}
return paths;
}

/**
* Maps a path in the R2 bucket to the urls used to access it
* @param bucketPath Path to map
Expand All @@ -91,19 +106,65 @@ export function mapBucketPathToUrlPath(
env: Pick<Env, 'DIRECTORY_LISTING'>
): string[] | undefined {
// @TODO: Use a switch statement or a Design Pattern here
// I strongly dislike the workers cache api
if (bucketPath.startsWith(DIST_PATH_PREFIX)) {
const path = bucketPath.substring(15);
return [`/dist${path}`, `/download/releases${path}`];
} else if (
bucketPath.startsWith(API_PATH_PREFIX) ||
bucketPath.startsWith('nodejs/docs/latest/api')
) {
const path = bucketPath.substring(22);
return [`/api${path}`, `/docs/latest/api${path}`];
const path = bucketPath.substring(DIST_PATH_PREFIX.length);

const possibleUrlPaths = new Set<string>();

// Purge directory listing of /dist/ and /download/release/
possibleUrlPaths.add('/dist/');
possibleUrlPaths.add('/download/release/');

// Purge whatever the paths we're updating
possibleUrlPaths.add(`/dist${path}`);
possibleUrlPaths.add(`/download/release${path}`);

// Purge all of the directory listings of folders starting with 'latest'
// (e.g. `/dist/latest-hydrogen`)
// Bit of a hack, but I think this is the best we can do. The redirects
// we have in `src/constants/redirectLinks.json` will be out of date since
// a new version was uploaded and thus the latest has changed. We can't
// really determine the new latest here unless we run something
// similar to the `scripts/update-redirect-links.js` script here.
const latestDirectories = getAllLatestDirectories('nodejs/release');

for (const directory of latestDirectories) {
possibleUrlPaths.add(`/dist${directory}`);
possibleUrlPaths.add(`/download/release${directory}`);
}

return [...possibleUrlPaths];
} else if (bucketPath.startsWith(DOCS_PATH_PREFIX)) {
return [`/docs${bucketPath.substring(11)}`];
const path = bucketPath.substring(DOCS_PATH_PREFIX.length);

const possibleUrlPaths = new Set<string>();

// Purge directory listings for /docs/ and /download/docs/
possibleUrlPaths.add('/docs/');
possibleUrlPaths.add('/download/docs/');

possibleUrlPaths.add(`/docs${path}`);
possibleUrlPaths.add(`/download/docs${path}`);

if (bucketPath.includes('/api/')) {
// Html file, purge it
possibleUrlPaths.add(`/api${path.substring('api'.length)}`);
}

// Purge all of the directory listings of folders starting with 'latest'
// (e.g. `/docs/latest`)
// Refer to previous call for explanation
const latestDirectories = getAllLatestDirectories('nodejs/docs');

for (const directory of latestDirectories) {
possibleUrlPaths.add(`/docs${directory}`);
possibleUrlPaths.add(`/download/docs${directory}`);
}

return [...possibleUrlPaths];
} else if (bucketPath.startsWith(DOWNLOAD_PATH_PREFIX)) {
return [`/download${bucketPath.substring(6)}`];
return [`/download${bucketPath.substring(DOWNLOAD_PATH_PREFIX.length)}`];
} else if (bucketPath.startsWith('metrics')) {
return ['/' + bucketPath];
}
Expand Down

0 comments on commit b53c11d

Please sign in to comment.