|
1 | | -import DOCS from './help.html' |
| 1 | +import DOCS from "./help.html"; |
2 | 2 |
|
3 | 3 | addEventListener("fetch", (event) => { |
4 | 4 | event.passThroughOnException(); |
@@ -45,18 +45,41 @@ async function handleRequest(request) { |
45 | 45 | status: 404, |
46 | 46 | } |
47 | 47 | ); |
48 | | - } |
| 48 | + } |
49 | 49 | // return docs |
50 | 50 | if (url.pathname === "/") { |
51 | 51 | return new Response(DOCS, { |
52 | 52 | status: 200, |
53 | 53 | headers: { |
54 | | - "content-type": "text/html" |
55 | | - } |
| 54 | + "content-type": "text/html", |
| 55 | + }, |
56 | 56 | }); |
57 | 57 | } |
58 | 58 | const isDockerHub = upstream == registry_dockerHub; |
59 | 59 | 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 | + |
60 | 83 | if (url.pathname == "/v2/") { |
61 | 84 | const newUrl = new URL(upstream + "/v2/"); |
62 | 85 | const headers = new Headers(); |
@@ -166,7 +189,7 @@ async function fetchToken(wwwAuthenticate, scope, authorization) { |
166 | 189 | } |
167 | 190 |
|
168 | 191 | function responseUnauthorized(url) { |
169 | | - const headers = new(Headers); |
| 192 | + const headers = new Headers(); |
170 | 193 | if (MODE == "debug") { |
171 | 194 | headers.set( |
172 | 195 | "Www-Authenticate", |
|
0 commit comments