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 all commits
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
89 changes: 79 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 @@ -92,19 +107,73 @@ export function mapBucketPathToUrlPath(
): string[] | undefined {
// @TODO: Use a switch statement or a Design Pattern here
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}`];
// Main release folder, accessible at `/dist/` or `/download/release/`
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)}`];
// Docs for main releases, accessible at `/docs/` or `/api/` for latest docs
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

// /latest/api/assert.html
let apiPath = path.substring(1); // latest/api/assert.html
apiPath = apiPath.substring(apiPath.indexOf('/')); // /api/assert.html

possibleUrlPaths.add(apiPath);
}

// 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)}`];
// Rest of the `/download/...` paths (e.g. `/download/nightly/`)
return [`/download${bucketPath.substring(DOWNLOAD_PATH_PREFIX.length)}`];
ovflowd marked this conversation as resolved.
Show resolved Hide resolved
} else if (bucketPath.startsWith('metrics')) {
// Metrics doesn't need any redirects
return ['/' + bucketPath];
}

Expand Down
43 changes: 18 additions & 25 deletions tests/unit/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';
import { before, describe, it, skip } from 'node:test';
import { readFile } from 'node:fs/promises';
import {
isDirectoryPath,
mapBucketPathToUrlPath,
Expand Down Expand Up @@ -67,14 +68,14 @@ describe('mapUrlPathToBucketPath', () => {
assert.strictEqual(result, 'nodejs');
});

it('converts `/download/releases` to `nodejs/releases`', () => {
it('converts `/download/release` to `nodejs/release`', () => {
const result = mapUrlPathToBucketPath(
new URL('http://localhost/download/releases'),
new URL('http://localhost/download/release'),
{
DIRECTORY_LISTING: 'restricted',
}
);
assert.strictEqual(result, 'nodejs/releases');
assert.strictEqual(result, 'nodejs/release');
});

it('converts `/docs/latest` to `nodejs/release/v.X.X.X/docs/`', () => {
Expand Down Expand Up @@ -118,30 +119,27 @@ describe('mapBucketPathToUrlPath', () => {
});
assert.notStrictEqual(result, undefined);

assert.strictEqual(result!.length, 1);
assert(result!.includes('/unknown-base-path'));
});

it('converts `nodejs/releases` to `/dist` and `/download/releases`', () => {
const result = mapBucketPathToUrlPath('nodejs/releases', {
it('converts `nodejs/release` to `/dist` and `/download/release`', () => {
const result = mapBucketPathToUrlPath('nodejs/release', {
DIRECTORY_LISTING: 'restricted',
});
assert.notStrictEqual(result, undefined);

assert.strictEqual(result!.length, 2);
assert(result!.includes('/dist'));
assert(result!.includes('/download/releases'));
assert(result!.includes('/dist/'));
assert(result!.includes('/download/release/'));
});

it('converts `nodejs/releases/latest` to `/dist/latest` and `/download/releases/latest`', () => {
const result = mapBucketPathToUrlPath('nodejs/releases/latest', {
it('converts `nodejs/release/latest` to `/dist/latest` and `/download/release/latest`', () => {
const result = mapBucketPathToUrlPath('nodejs/release/latest', {
DIRECTORY_LISTING: 'restricted',
});
assert.notStrictEqual(result, undefined);

assert.strictEqual(result!.length, 2);
assert(result!.includes('/dist/latest'));
assert(result!.includes('/download/releases/latest'));
assert(result!.includes('/dist/latest/'));
assert(result!.includes('/download/release/latest/'));
});

it('converts `nodejs` to `/download`', () => {
Expand All @@ -150,7 +148,6 @@ describe('mapBucketPathToUrlPath', () => {
});
assert.notStrictEqual(result, undefined);

assert.strictEqual(result!.length, 1);
assert(result!.includes('/download'));
});

Expand All @@ -160,8 +157,7 @@ describe('mapBucketPathToUrlPath', () => {
});
assert.notStrictEqual(result, undefined);

assert.strictEqual(result!.length, 1);
assert(result!.includes('/docs'));
assert(result!.includes('/docs/'));
});

it('converts `nodejs/docs/latest` to `/docs/latest`', () => {
Expand All @@ -170,19 +166,17 @@ describe('mapBucketPathToUrlPath', () => {
});
assert.notStrictEqual(result, undefined);

assert.strictEqual(result!.length, 1);
assert(result!.includes('/docs/latest'));
assert(result!.includes('/docs/latest/'));
});

it('converts `nodejs/docs/latest/api` to `/api` and `/docs/latest/api`', () => {
const result = mapBucketPathToUrlPath('nodejs/docs/latest/api', {
const result = mapBucketPathToUrlPath('nodejs/docs/latest/api/', {
DIRECTORY_LISTING: 'restricted',
});
assert.notStrictEqual(result, undefined);

assert.strictEqual(result!.length, 2);
assert(result!.includes('/api'));
assert(result!.includes('/docs/latest/api'));
assert(result!.includes('/api/'));
assert(result!.includes('/docs/latest/api/'));
});

it('converts `nodejs/docs/latest/api/assert.html` to `/api/assert.html` and `/docs/latest/api/assert.html`', () => {
Expand All @@ -192,7 +186,6 @@ describe('mapBucketPathToUrlPath', () => {
);
assert.notStrictEqual(result, undefined);

assert.strictEqual(result!.length, 2);
assert(result!.includes('/api/assert.html'));
assert(result!.includes('/docs/latest/api/assert.html'));
});
Expand Down
Loading