11import { compress as brotli } from 'https://deno.land/x/[email protected] /mod.ts' 22import { gzip , deflate } from 'https://deno.land/x/[email protected] /mod.ts' 3- import { ServerRequest } from 'https://deno.land/[email protected] /http/server.ts' 43import { Accepts } from 'https://deno.land/x/[email protected] /mod.ts' 5- import { readAll } from 'https://deno.land/std@0.106 .0/io/util.ts'
4+ import { readAll } from 'https://deno.land/std@0.107 .0/io/util.ts'
65
76const funcs = {
87 br : brotli ,
@@ -32,7 +31,7 @@ export type CompressionOptions = {
3231 *
3332 * @example
3433 * ```ts
35- *import { serve } from 'https://deno.land/std@0.106 .0/http/server.ts'
34+ *import { serve } from 'https://deno.land/std@0.107 .0/http/server.ts'
3635 *import { compression } from 'https://deno.land/x/compression/brotli.ts'
3736 *
3837 *const s = serve({ port: 3000 })
@@ -45,7 +44,7 @@ export type CompressionOptions = {
4544 *}
4645 * ```
4746 */
48- export const compression = ( opts : CompressionOptions ) => async ( req : ServerRequest ) => {
47+ export const compression = ( opts : CompressionOptions ) => async ( req : Request ) => {
4948 const acceptHeader = req . headers . get ( 'Accept-Encoding' )
5049
5150 const accepts = new Accepts ( req . headers )
@@ -55,8 +54,7 @@ export const compression = (opts: CompressionOptions) => async (req: ServerReque
5554 const buf = await readAll ( await Deno . open ( opts . path ) )
5655
5756 if ( ! acceptHeader || acceptHeader === 'identity' || ( Array . isArray ( encodings ) && encodings [ 0 ] === 'identity' ) ) {
58- return await req . respond ( {
59- body : buf ,
57+ return new Response ( buf , {
6058 status : 200 ,
6159 headers : new Headers ( {
6260 'Content-Encoding' : 'identity'
@@ -67,8 +65,7 @@ export const compression = (opts: CompressionOptions) => async (req: ServerReque
6765
6866 const compressed = funcs [ preferredAlgo ] ( buf )
6967
70- return await req . respond ( {
71- body : compressed ,
68+ return new Response ( compressed , {
7269 headers : new Headers ( {
7370 'Content-Encoding' : preferredAlgo
7471 } ) ,
@@ -88,26 +85,21 @@ export const compression = (opts: CompressionOptions) => async (req: ServerReque
8885 }
8986 }
9087
91- return await req . respond ( {
92- body : compressed ,
88+ return new Response ( compressed , {
9389 headers : new Headers ( {
9490 'Content-Encoding' : encs . join ( ', ' )
9591 } )
9692 } )
9793 } else {
98- return await req . respond (
99- Object . keys ( funcs ) . includes ( encodings as string )
100- ? {
101- body : funcs [ encodings as Compression ] ( buf ) ,
102- headers : new Headers ( {
103- 'Content-Encoding' : encodings as string
104- } )
105- }
106- : {
107- status : 406 ,
108- body : 'Not Acceptable'
109- }
110- )
94+ return Object . keys ( funcs ) . includes ( encodings as string )
95+ ? new Response ( funcs [ encodings as Compression ] ( buf ) , {
96+ headers : new Headers ( {
97+ 'Content-Encoding' : encodings as string
98+ } )
99+ } )
100+ : new Response ( 'Not Acceptable' , {
101+ status : 406
102+ } )
111103 }
112104 }
113105}
0 commit comments