Skip to content

Commit 80f98b9

Browse files
committed
Redirect hashed url
1 parent b505830 commit 80f98b9

File tree

8 files changed

+63
-10
lines changed

8 files changed

+63
-10
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: CI
2+
on: push
3+
jobs:
4+
test:
5+
name: CI
6+
runs-on: ubuntu-latest
7+
8+
steps:
9+
- uses: actions/checkout@v3
10+
- uses: denoland/setup-deno@v1
11+
with:
12+
deno-version: v1.x
13+
- name: Run tests
14+
run: deno test

deno.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"imports": {
3+
"preact": "https://esm.sh/preact@10.15.1",
4+
"preact/": "https://esm.sh/preact@10.15.1/",
5+
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0"
6+
}
7+
}

deno.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dprint.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"**/*-lock.json"
88
],
99
"plugins": [
10-
"https://plugins.dprint.dev/typescript-0.68.0.wasm",
11-
"https://plugins.dprint.dev/json-0.15.1.wasm",
12-
"https://plugins.dprint.dev/markdown-0.13.1.wasm",
13-
"https://plugins.dprint.dev/prettier-0.7.0.json@4e846f43b32981258cef5095b3d732522947592e090ef52333801f9d6e8adb33"
10+
"https://plugins.dprint.dev/typescript-0.85.0.wasm",
11+
"https://plugins.dprint.dev/json-0.17.4.wasm",
12+
"https://plugins.dprint.dev/markdown-0.15.3.wasm",
13+
"https://plugins.dprint.dev/prettier-0.26.1.json@fdbe31f6aecd24f9d6b924214710a6766050d03146163b4e241e6056b2462f2e"
1414
]
1515
}

handleRequest.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,15 @@ Deno.test("tryResolvePluginUrl", async () => {
320320
);
321321
});
322322

323+
Deno.test("should redirect when the url has an @ sign in it", async () => {
324+
assertEquals(
325+
await getRedirectUrl(
326+
"https://plugins.dprint.dev/prettier-0.24.0.json@9a57d0d8e440ad90d07a503166af47e7a6a886abd46767933f9c279f72468596",
327+
),
328+
"https://plugins.dprint.dev/prettier-0.24.0.json",
329+
);
330+
});
331+
323332
// todo: mock github api for these tests
324333

325334
Deno.test("tryResolveSchemaUrl", async () => {

handleRequest.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const contentTypes = {
1414

1515
export async function handleRequest(request: Request) {
1616
const url = new URL(request.url);
17+
const atSignIndex = url.pathname.lastIndexOf("@");
18+
if (atSignIndex >= 0 && url.pathname.length - atSignIndex === 64) {
19+
return redirectWithoutHash(url, atSignIndex);
20+
}
1721
const newUrl = await resolvePluginOrSchemaUrl(url);
1822
if (newUrl != null) {
1923
const contentType = newUrl.endsWith(".json")
@@ -161,3 +165,7 @@ function create404Response() {
161165
statusText: "Not Found",
162166
});
163167
}
168+
169+
function redirectWithoutHash(url: URL, atSignIndex: number) {
170+
return Response.redirect(url.pathname.slice(0, atSignIndex) + url.search + url.hash, 302);
171+
}

home.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @jsx h */
2-
import { h } from "https://x.lcas.dev/preact@10.5.12/mod.js";
3-
import { renderToString } from "https://x.lcas.dev/preact@10.5.12/ssr.js";
2+
import { h } from "preact";
3+
import { renderToString } from "preact-render-to-string";
44
import { PluginData, PluginsData, readInfoFile } from "./readInfoFile.ts";
55

66
export async function renderHome() {

plugins.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export async function tryResolveLatestJson(url: URL) {
4141
if (!result) {
4242
return undefined;
4343
}
44-
const username = result.pathname.groups[0];
45-
const shortRepoName = result.pathname.groups[1];
44+
const username = result.pathname.groups[0]!;
45+
const shortRepoName = result.pathname.groups[1]!;
4646
const latestInfo = await getLatestInfo(username, shortRepoName);
4747
if (latestInfo == null) {
4848
return 404;
@@ -104,8 +104,8 @@ async function userRepoTagPatternMapper(
104104
) {
105105
const result = pattern.exec(url);
106106
if (result) {
107-
const username = result.pathname.groups[0];
108-
const repo = await getFullRepoName(username, result.pathname.groups[1]);
107+
const username = result.pathname.groups[0]!;
108+
const repo = await getFullRepoName(username, result.pathname.groups[1]!);
109109
const tag = result.pathname.groups[2];
110110
if (tag === "latest") {
111111
return `https://github.com/${username}/${repo}/releases/latest/download/${fileName}`;

0 commit comments

Comments
 (0)