-
-
Notifications
You must be signed in to change notification settings - Fork 526
fix: redirect /org/<name> to /~<name> for user accounts #3225
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
Open
coder-tejas
wants to merge
2
commits into
npmx-dev:main
Choose a base branch
from
coder-tejas:fix/org-user-redirect-3213
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| { "qwerzl": "owner" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {} |
107 changes: 107 additions & 0 deletions
107
test/unit/server/middleware/canonical-redirects.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import type { H3Event } from 'h3' | ||
|
|
||
| const fetchMock = vi.fn() | ||
| const setHeaderMock = vi.fn() | ||
| const sendRedirectMock = vi.fn() | ||
|
|
||
| vi.stubGlobal('$fetch', fetchMock) | ||
| vi.stubGlobal('setHeader', setHeaderMock) | ||
| vi.stubGlobal('sendRedirect', sendRedirectMock) | ||
| vi.stubGlobal('defineEventHandler', (fn: Function) => fn) | ||
| vi.stubGlobal('getRouteRules', () => ({})) | ||
|
|
||
| const handler = (await import('#server/middleware/canonical-redirects.global')).default | ||
|
|
||
| function makeEvent(path: string): H3Event { | ||
| return { path } as H3Event | ||
| } | ||
|
|
||
| afterAll(() => { | ||
| vi.unstubAllGlobals() | ||
| }) | ||
|
|
||
| describe('canonical-redirects middleware (/org/<name> β /~<name>)', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('does not redirect for a real org (/-/org/<name>/user returns {})', async () => { | ||
| fetchMock.mockResolvedValue({}) | ||
|
|
||
| await handler(makeEvent('/org/nuxt')) | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledWith('https://registry.npmjs.org/-/org/nuxt/user', { | ||
| timeout: 5000, | ||
| retry: 0, | ||
| }) | ||
| expect(sendRedirectMock).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('redirects with 301 to /~<name> for a user account (/-/org/<name>/user returns non-empty)', async () => { | ||
| fetchMock.mockResolvedValue({ qwerzl: 'owner' }) | ||
|
|
||
| await handler(makeEvent('/org/qwerzl')) | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledWith('https://registry.npmjs.org/-/org/qwerzl/user', { | ||
| timeout: 5000, | ||
| retry: 0, | ||
| }) | ||
| expect(setHeaderMock).toHaveBeenCalledWith( | ||
| expect.anything(), | ||
| 'cache-control', | ||
| expect.any(String), | ||
| ) | ||
| expect(sendRedirectMock).toHaveBeenCalledWith(expect.anything(), '/~qwerzl', 301) | ||
| }) | ||
|
|
||
| it('preserves the query string when redirecting a user account', async () => { | ||
| fetchMock.mockResolvedValue({ QWERZL: 'owner' }) | ||
|
|
||
| await handler(makeEvent('/org/QWERZL?tab=members')) | ||
|
|
||
| // name is lowercased, matching npmjs.com behavior | ||
| expect(sendRedirectMock).toHaveBeenCalledWith(expect.anything(), '/~qwerzl?tab=members', 301) | ||
| }) | ||
|
|
||
| it('does not redirect for a nonexistent name (/-/org/<name>/user 404s), letting the org page 404', async () => { | ||
| fetchMock.mockRejectedValue({ statusCode: 404, message: 'Not found' }) | ||
|
|
||
| await expect(handler(makeEvent('/org/nonexistent-org-12345'))).resolves.toBeUndefined() | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledWith( | ||
| 'https://registry.npmjs.org/-/org/nonexistent-org-12345/user', | ||
| { timeout: 5000, retry: 0 }, | ||
| ) | ||
| expect(sendRedirectMock).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('fails open when the user check itself errors (network error, 500, ...)', async () => { | ||
| fetchMock.mockRejectedValue(new Error('network failure')) | ||
|
|
||
| await expect(handler(makeEvent('/org/nuxt'))).resolves.toBeUndefined() | ||
|
|
||
| expect(sendRedirectMock).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('fails open on registry timeout (bounded lookup, no retries)', async () => { | ||
| const timeoutError = new Error('Request aborted due to timeout') | ||
| timeoutError.name = 'TimeoutError' | ||
| fetchMock.mockRejectedValue(timeoutError) | ||
|
|
||
| await expect(handler(makeEvent('/org/nuxt'))).resolves.toBeUndefined() | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledWith( | ||
| 'https://registry.npmjs.org/-/org/nuxt/user', | ||
| expect.objectContaining({ timeout: 5000, retry: 0 }), | ||
| ) | ||
| expect(sendRedirectMock).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not run the user check for non-/org/ paths', async () => { | ||
| await handler(makeEvent('/package/vue')) | ||
|
|
||
| expect(fetchMock).not.toHaveBeenCalled() | ||
| expect(sendRedirectMock).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.