Skip to content

Commit e2542d0

Browse files
committed
feat: support search in dockerhub
1 parent 591a8f4 commit e2542d0

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/index.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import DOCS from './help.html'
1+
import DOCS from "./help.html";
22

33
addEventListener("fetch", (event) => {
44
event.passThroughOnException();
@@ -45,18 +45,41 @@ async function handleRequest(request) {
4545
status: 404,
4646
}
4747
);
48-
}
48+
}
4949
// return docs
5050
if (url.pathname === "/") {
5151
return new Response(DOCS, {
5252
status: 200,
5353
headers: {
54-
"content-type": "text/html"
55-
}
54+
"content-type": "text/html",
55+
},
5656
});
5757
}
5858
const isDockerHub = upstream == registry_dockerHub;
5959
const authorization = request.headers.get("Authorization");
60+
61+
if (url.pathname == "/v1/search" && isDockerHub) {
62+
const newUrl = new URL(index_dockerHub + "/v1/search");
63+
newUrl.search = url.search;
64+
65+
const headers = new Headers();
66+
if (authorization) {
67+
headers.set("Authorization", authorization);
68+
}
69+
70+
// check if need to authenticate
71+
const resp = await fetch(newUrl.toString(), {
72+
method: "GET",
73+
headers: headers,
74+
redirect: "follow",
75+
});
76+
77+
if (resp.status === 401) {
78+
return responseUnauthorized(url);
79+
}
80+
return resp;
81+
}
82+
6083
if (url.pathname == "/v2/") {
6184
const newUrl = new URL(upstream + "/v2/");
6285
const headers = new Headers();
@@ -166,7 +189,7 @@ async function fetchToken(wwwAuthenticate, scope, authorization) {
166189
}
167190

168191
function responseUnauthorized(url) {
169-
const headers = new(Headers);
192+
const headers = new Headers();
170193
if (MODE == "debug") {
171194
headers.set(
172195
"Www-Authenticate",

0 commit comments

Comments
 (0)