|
1 | 1 | import type { BufReader, BufWriter } from 'https://deno.land/[email protected]/io/bufio.ts'
|
2 | 2 | import type { MultipartFormData } from 'https://deno.land/[email protected]/mime/multipart.ts'
|
3 | 3 | import { MultipartReader } from 'https://deno.land/[email protected]/mime/multipart.ts'
|
4 |
| -import { compress as brotli } from 'https://deno.land/x/[email protected]/mod.ts' |
5 |
| -import { gzipEncode as gzip } from 'https://deno.land/x/[email protected]/mod.ts' |
6 | 4 | import log from '../shared/log.ts'
|
7 | 5 | import type { APIRequest, ServerRequest, ServerResponse } from '../types.ts'
|
8 | 6 |
|
| 7 | +let brotli: ((data: Uint8Array) => Uint8Array) | null = null |
| 8 | +let gzip: ((data: Uint8Array) => Uint8Array) | null = null |
| 9 | + |
9 | 10 | export class Request implements APIRequest {
|
10 | 11 | #req: ServerRequest
|
11 | 12 | #params: URLSearchParams
|
@@ -150,14 +151,23 @@ export class Request implements APIRequest {
|
150 | 151 | shouldCompress = true
|
151 | 152 | }
|
152 | 153 | }
|
153 |
| - if (shouldCompress && body.length > 1024) { |
154 |
| - if (this.headers.get('accept-encoding')?.includes('br')) { |
| 154 | + if (shouldCompress && Deno.env.get('ALEPH_BUILD_MODE') !== 'development' && body.length > 1024) { |
| 155 | + const ae = this.headers.get('accept-encoding') || '' |
| 156 | + if (ae.includes('br')) { |
155 | 157 | this.#resp.headers.set('Vary', 'Origin')
|
156 | 158 | this.#resp.headers.set('Content-Encoding', 'br')
|
| 159 | + if (brotli === null) { |
| 160 | + const { compress } = await import('https://deno.land/x/[email protected]/mod.ts') |
| 161 | + brotli = compress |
| 162 | + } |
157 | 163 | body = brotli(body)
|
158 |
| - } else if (this.headers.get('accept-encoding')?.includes('gzip')) { |
| 164 | + } else if (ae.includes('gzip')) { |
159 | 165 | this.#resp.headers.set('Vary', 'Origin')
|
160 | 166 | this.#resp.headers.set('Content-Encoding', 'gzip')
|
| 167 | + if (gzip === null) { |
| 168 | + const { gzipEncode } = await import('https://deno.land/x/[email protected]/mod.ts') |
| 169 | + gzip = gzipEncode |
| 170 | + } |
161 | 171 | body = gzip(body)
|
162 | 172 | }
|
163 | 173 | }
|
|
0 commit comments