-
-
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.
- Loading branch information
v1rtl
committed
Feb 22, 2021
1 parent
5e52689
commit 7bfa794
Showing
8 changed files
with
116 additions
and
28 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
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,32 @@ | ||
import { NextFunction } from 'https://esm.sh/@tinyhttp/router' | ||
import { App } from './app.ts' | ||
import { Request } from './request.ts' | ||
import { getRequestHeader, getFreshOrStale } from './extensions/req/headers.ts' | ||
import { send } from './extensions/res/send.ts' | ||
import { Response } from './response.ts' | ||
|
||
export const extendMiddleware = <RenderOptions = unknown>(app: App) => (req: Request, next: NextFunction) => { | ||
export const extendMiddleware = < | ||
RenderOptions = unknown, | ||
Req extends Request = Request, | ||
Res extends Response = Response | ||
>( | ||
app: App | ||
) => (req: Req, res: Res, next?: NextFunction) => { | ||
const { settings } = app | ||
|
||
// Request extensions | ||
if (settings?.bindAppToReqRes) { | ||
req.app = app | ||
} | ||
next() | ||
|
||
req.get = getRequestHeader(req) | ||
|
||
if (settings?.freshnessTesting) { | ||
req.fresh = getFreshOrStale(req, res) | ||
req.stale = !req.fresh | ||
} | ||
|
||
res.send = send(req, res) | ||
|
||
next?.() | ||
} |
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,43 @@ | ||
import { ServerRequest, Response } from 'https://deno.land/std@0.87.0/http/server.ts' | ||
import parseRange, { Options } from 'https://esm.sh/range-parser' | ||
import fresh from 'https://deno.land/x/fresh/mod.ts' | ||
|
||
export const getRequestHeader = (req: ServerRequest) => (header: string): string | string[] | null => { | ||
const lc = header.toLowerCase() | ||
|
||
switch (lc) { | ||
case 'referer': | ||
case 'referrer': | ||
return req.headers.get('referrer') || req.headers.get('referer') | ||
default: | ||
return req.headers.get(lc) | ||
} | ||
} | ||
export const getRangeFromHeader = (req: ServerRequest) => (size: number, options?: Options) => { | ||
const range = getRequestHeader(req)('Range') as string | ||
|
||
if (!range) return | ||
|
||
return parseRange(size, range, options) | ||
} | ||
|
||
export const getFreshOrStale = (req: ServerRequest, res: Response) => { | ||
const method = req.method | ||
const status = res.status || 200 | ||
|
||
// GET or HEAD for weak freshness validation only | ||
if (method !== 'GET' && method !== 'HEAD') return false | ||
|
||
// 2xx or 304 as per rfc2616 14.26 | ||
if ((status >= 200 && status < 300) || status === 304) { | ||
return fresh( | ||
req.headers, | ||
new Headers({ | ||
etag: getRequestHeader(req)('ETag') as string, | ||
'last-modified': res.headers?.get('Last-Modified') as string | ||
}) | ||
) | ||
} | ||
|
||
return false | ||
} |
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,7 @@ | ||
import { Request } from '../../request.ts' | ||
import { Response } from '../../response.ts' | ||
|
||
export const send = (req: Request, res: Response) => (body: string) => { | ||
req.respond({ ...res, body }) | ||
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,6 @@ | ||
import { Response as ServerResponse } from 'https://deno.land/std@0.87.0/http/server.ts' | ||
|
||
export interface Response extends ServerResponse { | ||
headers: Headers | ||
send(body: string): Response | ||
} |