Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: rework mapBucketPathToUrlPath to work better with the redirects #49

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)}`];
ovflowd marked this conversation as resolved.
Show resolved Hide resolved
} else if (bucketPath.startsWith('metrics')) {
return ['/' + bucketPath];
}
Expand Down
Loading