-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: flakey5 <[email protected]>
- Loading branch information
Showing
7 changed files
with
145 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,12 +1,27 @@ | ||
import { Context } from 'toucan-js/dist/types'; | ||
import { Context } from '../context'; | ||
import responses from '../responses'; | ||
import { parseUrl } from '../utils/request'; | ||
import { Middleware } from './middleware'; | ||
|
||
export class R2Middleware implements Middleware { | ||
handle( | ||
request: Request, | ||
ctx: Context, | ||
next?: (request: Request) => Promise<Response> | ||
): Promise<Response> { | ||
throw new Error(); | ||
export class OriginMiddleware implements Middleware { | ||
handle(request: Request, ctx: Context): Promise<Response> { | ||
const url = parseUrl(request); | ||
if (url === undefined) { | ||
return Promise.resolve(responses.badRequest()); | ||
} | ||
|
||
const res = fetch(ctx.env.ORIGIN_HOST + url.pathname, { | ||
method: request.method, | ||
headers: { | ||
'user-agent': 'release-cloudflare-worker', | ||
'if-match': request.headers.get('if-match') ?? '', | ||
'if-none-match': request.headers.get('if-none-match') ?? '', | ||
'if-modified-since': request.headers.get('if-modified-since') ?? '', | ||
'if-unmodified-since': request.headers.get('if-unmodified-since') ?? '', | ||
range: request.headers.get('if-match') ?? '', | ||
}, | ||
}); | ||
|
||
return res; | ||
} | ||
} |
This file contains 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 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,27 @@ | ||
import { Middleware } from '../middleware/middleware'; | ||
import { OriginMiddleware } from '../middleware/originMiddleware'; | ||
import { R2Middleware } from '../middleware/r2Middleware'; | ||
import { Router } from './router'; | ||
|
||
export function registerRoutes(router: Router) { | ||
router.get('/robots.txt', []); | ||
|
||
const tmp: Middleware = { | ||
handle: async request => { | ||
return Response.json(request.params); | ||
}, | ||
}; | ||
const r2Middleware = new R2Middleware(); | ||
const originMiddleware = new OriginMiddleware(); | ||
|
||
router.head('/metrics*', [r2Middleware, originMiddleware]); | ||
router.get('/metrics*', [/*r2Middleware, */ originMiddleware]); | ||
|
||
router.head('/dist:path+', [r2Middleware, originMiddleware]); | ||
router.get('/dist:path+', [r2Middleware, originMiddleware]); | ||
|
||
router.head('/download:path+', [r2Middleware, originMiddleware]); | ||
router.get('/download:path+', [r2Middleware, originMiddleware]); | ||
} | ||
|
||
export * from './router'; |
This file contains 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 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 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