CompressionMiddleware
Overview
Usage
- Node
- Deno
- Browser/ESM
import { CompressionMiddleware } from 'keywork/middleware/classes'
import { CompressionMiddleware } from 'https://deno.land/x/keywork/middleware/classes'
let { CompressionMiddleware } = await import('https://esm.sh/keywork/middleware/classes')
Hierarchy
↳
CompressionMiddleware
Constructors
constructor
new CompressionMiddleware
(encodings?, db?, routerOptions?)
Parameters
Name | Type | Description |
---|---|---|
encodings | CompressionEncoding | CompressionEncoding [] |
db? | MimeDatabase | |
routerOptions? | RequestRouterOptions | - |
Overrides
RequestRouter.constructor
Defined in
middleware/classes/CompressionMiddleware.ts:67
Debug Properties
displayName
displayName: `string`
A display name used for debugging and log messages.
Inherited from
RequestRouter.displayName
Defined in
router/classes/RequestRouter.ts:391
Other Properties
$routesEndpoint
protected $routesEndpoint: [`RouteRequestHandler<{}, {}, `Record<string, unknown>`, [`ResponseLike`](../../http/types/ResponseLike.mdx)>`](../../router/interfaces/RouteRequestHandler.mdx)
Inherited from
RequestRouter.$routesEndpoint
Defined in
router/classes/RequestRouter.ts:452
applyCompression
protected applyCompression: [`RouteRequestHandler<{}, {}, `Record<string, unknown>`, [`ResponseLike`](../../http/types/ResponseLike.mdx)>`](../../router/interfaces/RouteRequestHandler.mdx)
Defined in
middleware/classes/CompressionMiddleware.ts:90
fetch
fetch: [`MiddlewareFetch<{}, `Promise<Response>`>`](../../router/interfaces/MiddlewareFetch.mdx)
The Worker's primary incoming fetch handler.
This delegates to a method-specific handler you define, such as RequestRouter#get
.
Generally, RequestRouter#fetch
should not be used within your app.
This is instead automatically called by the Worker runtime when an incoming request is received.
Inherited from
RequestRouter.fetch
Defined in
router/classes/RequestRouter.ts:501
logger
readonly logger: [`Logger`](../../logger/classes/Logger.mdx)
A server-side logger.
Inherited from
RequestRouter.logger
Defined in
router/classes/RequestRouter.ts:396
reactOptions
readonly reactOptions: [`ReactRendererOptions`](../../react/interfaces/ReactRendererOptions.mdx)
Inherited from
RequestRouter.reactOptions
Defined in
router/classes/RequestRouter.ts:465
routesByVerb
protected readonly routesByVerb: `Map<[`RouterMethod`](../../http/types/RouterMethod.mdx), [`ParsedRoute<{}>`](../../router/types/ParsedRoute.mdx)[]>`
This router's known routes, categorized by their normalized HTTP method verbs into arrays of route handlers.
e.g.
GET: [{'/', routeHandler1}, {'/foo/' routeHandler2}, {'/bar/', routeHandler3}...]
POST: [{'/', routeHandler1}, {'/foo/' routeHandler2}, {'/bar/', routeHandler3}...]
...etc
Route handlers are prioritized in order of insertion,
however, a handler can act as middleware by continuing the chain by returning next()
Inherited from
RequestRouter.routesByVerb
Defined in
router/classes/RequestRouter.ts:97
supportedServerEncodings
protected supportedServerEncodings: `CompressionEncoding`[]
Defined in
middleware/classes/CompressionMiddleware.ts:64
Debug Methods
$getRoutesByHTTPMethod
$getRoutesByHTTPMethod
(): [`RouteDebugEntrypoint`](../../router/interfaces/RouteDebugEntrypoint.mdx)[]
Collates the known routes by HTTP method verb.
See
Returns
Inherited from
RequestRouter.$getRoutesByHTTPMethod
Defined in
router/classes/RequestRouter.ts:405
$prettyPrintRoutes
$prettyPrintRoutes
(routesByHttpMethod?): `void`
Outputs the known routes to the console.
Parameters
Name | Type |
---|---|
routesByHttpMethod | RouteDebugEntrypoint [] |
Returns
void
Inherited from
RequestRouter.$prettyPrintRoutes
Defined in
router/classes/RequestRouter.ts:438
HTTP Method Handler Methods
all
all
all<ExpectedParams, Data>(...args): `void`
Defines a handler for incoming all HTTP requests.
This will always be a higher priority than an explicitly defined method handler.
If you're creating a router as middleware, RequestRouter#all
can be especially useful for intercepting incoming requests.
Type parameters
Name | Type |
---|---|
ExpectedParams | {} |
Data | {} |
Parameters
Name | Type |
---|---|
...args | [urlPattern: URLPatternLike, ...handlers: RouteRequestHandler<Object, ExpectedParams, Data, ResponseLike>[]] |
Returns
void
Inherited from
RequestRouter.all
Defined in
router/classes/RequestRouter.ts:294
delete
delete
delete<ExpectedParams, Data>(...args): `void`
Defines a handler for incoming DELETE
requests.
The HTTP DELETE request method deletes the specified resource.
See
Type parameters
Name | Type |
---|---|
ExpectedParams | {} |
Data | {} |
Parameters
Name | Type |
---|---|
...args | [urlPattern: URLPatternLike, ...handlers: RouteRequestHandler<Object, ExpectedParams, Data, ResponseLike>[]] |
Returns
void
Inherited from
RequestRouter.delete
Defined in
router/classes/RequestRouter.ts:242
get
get
get<ExpectedParams, Data>(...args): `void`
Defines a handler for incoming GET
requests.
The HTTP GET method requests a representation of the specified resource.
Requests using GET should only be used to request data.
The params
object in the IsomorphicFetchEvent
contains matched URL patterns
which can be used to pass routing data from a client.
See
Type parameters
Name | Type |
---|---|
ExpectedParams | {} |
Data | {} |
Parameters
Name | Type |
---|---|
...args | [urlPattern: URLPatternLike, ...handlers: RouteRequestHandler<Object, ExpectedParams, Data, ResponseLike>[]] |
Returns
void
Inherited from
RequestRouter.get
Defined in
router/classes/RequestRouter.ts:177
head
head
head<ExpectedParams, Data>(...args): `void`
Defines a handler for incoming HEAD
requests.
The HTTP HEAD method requests the headers that would be returned if the HEAD request's URL was instead requested with the HTTP GET method. For example, if a URL might produce a large download, a HEAD request could read its Content-Length header to check the filesize without actually downloading the file.
See
Type parameters
Name | Type |
---|---|
ExpectedParams | {} |
Data | {} |
Parameters
Name | Type |
---|---|
...args | [urlPattern: URLPatternLike, ...handlers: RouteRequestHandler<Object, ExpectedParams, Data, ResponseLike>[]] |
Returns
void
Inherited from
RequestRouter.head
Defined in
router/classes/RequestRouter.ts:261
options
options
options<ExpectedParams, Data>(...args): `void`
Defines a handler for incoming OPTIONS
requests.
The HTTP OPTIONS method requests permitted communication options for a given URL or server. A client can specify a URL with this method, or an asterisk (*) to refer to the entire server.
See
Type parameters
Name | Type |
---|---|
ExpectedParams | {} |
Data | {} |
Parameters
Name | Type |
---|---|
...args | [urlPattern: URLPatternLike, ...handlers: RouteRequestHandler<Object, ExpectedParams, Data, ResponseLike>[]] |
Returns
void
Inherited from
RequestRouter.options
Defined in
router/classes/RequestRouter.ts:279
patch
patch
patch<ExpectedParams, Data>(...args): `void`
Defines a handler for incoming PATCH
requests.
See
Type parameters
Name | Type |
---|---|
ExpectedParams | {} |
Data | {} |
Parameters
Name | Type |
---|---|
...args | [urlPattern: URLPatternLike, ...handlers: RouteRequestHandler<Object, ExpectedParams, Data, ResponseLike>[]] |
Returns
void
Inherited from
RequestRouter.patch
Defined in
router/classes/RequestRouter.ts:226
post
post
post<ExpectedParams, Data>(...args): `void`
Defines a handler for incoming POST
requests.
The HTTP POST method sends data to the server.
The type of the body of the request is indicated by the Content-Type
header.
See
Type parameters
Name | Type |
---|---|
ExpectedParams | {} |
Data | {} |
Parameters
Name | Type |
---|---|
...args | [urlPattern: URLPatternLike, ...handlers: RouteRequestHandler<Object, ExpectedParams, Data, ResponseLike>[]] |
Returns
void
Inherited from
RequestRouter.post
Defined in
router/classes/RequestRouter.ts:195
put
put
put<ExpectedParams, Data>(...args): `void`
Defines a handler for incoming PUT
requests.
The HTTP PUT request method creates a new resource or replaces a representation of the target resource with the request payload.
See
Type parameters
Name | Type |
---|---|
ExpectedParams | {} |
Data | {} |
Parameters
Name | Type |
---|---|
...args | [urlPattern: URLPatternLike, ...handlers: RouteRequestHandler<Object, ExpectedParams, Data, ResponseLike>[]] |
Returns
void
Inherited from
RequestRouter.put
Defined in
router/classes/RequestRouter.ts:212
Other Methods
appendMethodRoutes
appendMethodRoutes
(normalizedVerb, urlPatternLike, ...fetchersLike): `void`
Given a normalized HTTP method verb, create a method handler. This is mostly for internal use.
Parameters
Name | Type |
---|---|
normalizedVerb | RouterMethod |
urlPatternLike | URLPatternLike |
...fetchersLike | (Fetcher<{}> |
Returns
void
Inherited from
RequestRouter.appendMethodRoutes
Defined in
router/classes/RequestRouter.ts:107
dispose
dispose
(reason?): `void`
Parameters
Name | Type | Default value |
---|---|---|
reason | string | 'default' |
Returns
void
Inherited from
RequestRouter.dispose
Defined in
router/classes/RequestRouter.ts:665
match
match
match<BoundAliases>(parsedRoutes, matchingAgainst): `RouteMatch<`BoundAliases`>`[]
Finds the matching routes for a given pathname.
Type parameters
Name | Type |
---|---|
BoundAliases | {} |
Parameters
Name | Type |
---|---|
parsedRoutes | ParsedRoute< BoundAliases> [] |
matchingAgainst | URLPatternInput |
Returns
RouteMatch<
BoundAliases>
[]
Inherited from
RequestRouter.match
Defined in
router/classes/RequestRouter.ts:475
terminateMiddleware
Protected
terminateMiddleware
(): `Response`
Returns
Response
Inherited from
RequestRouter.terminateMiddleware
Defined in
router/classes/RequestRouter.ts:604
use
use
(fetcher): `void`
Combines additional routers and their respective route handlers to this router.
Route handlers are matched in the order of their declaration:
const app = new RequestRouter()
app.get('/foo', ({request}) => {
return new Response('This handler got here first!')
})
app.get('/foo', ({request}) => {
return new Response('This handler won't be called!')
})
However, if you want another router to act as middleware,
Call use
before defining your route handlers:
const authenticationRouter = new RequestRouter()
authenticationRouter.all('*', ({request, next}) => {
if (!hasAuthCookie(request)) {
return new KeyworkResourceError(401, "You need to be signed in to do that!")
}
// Pass the request along to the next matching route handler.
return next()
})
const app = new RequestRouter()
app.use('/', authenticationRouter)
app.get('/user/profile', ({request}) => {
return new Response("Some user only content.")
})
Parameters
Name | Type | Description |
---|---|---|
fetcher | FetcherLike<{}> |
Returns
void
Inherited from
RequestRouter.use
Defined in
router/classes/RequestRouter.ts:347
use
(mountURLPattern, fetcher): `void`
Parameters
Name | Type | Description |
---|---|---|
mountURLPattern | URLPatternLike | |
fetcher | FetcherLike<{}> |
Returns
void
Inherited from
RequestRouter.use
Defined in
router/classes/RequestRouter.ts:355
assertIsInstanceOf
Static
assertIsInstanceOf
assertIsInstanceOf<BoundAliases>(routerLike): routerLike is RequestRouter<BoundAliases>
Type parameters
Name | Type |
---|---|
BoundAliases | {} |
Parameters
Name | Type |
---|---|
routerLike | Fetcher< BoundAliases> |
Returns
routerLike is RequestRouter<BoundAliases>
Inherited from
RequestRouter.assertIsInstanceOf
Defined in
router/classes/RequestRouter.ts:617
Middleware that given a
Response
with abody
, compresses the contents according to a client'sAccept-Encoding
request header.This middleware is not necessary on Cloudflare as it is automatically applied by the Workers runtime.
Deno Deploy automatically applies compression to responses when possible.
Read more about from Deno Deploy's compression docs.