Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 8a1744a

Browse files
committed
Improve compress
1 parent 663664a commit 8a1744a

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

server/compress.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ export const deno_x_flate = 'https://deno.land/x/[email protected]/mod.ts'
44
class Compression {
55
#brotli: ((data: Uint8Array) => Uint8Array) | null = null
66
#gzip: ((data: Uint8Array) => Uint8Array) | null = null
7-
#enable: boolean = false
87

9-
async enable() {
10-
this.#enable = true
11-
}
8+
enable: boolean = false
129

1310
accept(acceptEncoding: string, contentType: string, contentLength: number): 'br' | 'gzip' | null {
14-
const shouldCompress = this.#enable && (
11+
const shouldCompress = this.enable && (
1512
contentType.startsWith('text/') ||
1613
/^application\/(javascript|json|xml|wasm)/i.test(contentType) ||
1714
/^image\/svg\+xml/i.test(contentType)

server/server.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ export class Server {
8686

8787
const resp = new APIResponse()
8888
const end = async (status?: number): Promise<void> => {
89-
let { body, headers, } = resp
89+
const acceptEncoding = req.headers.get('accept-encoding')
90+
let { body, headers } = resp
91+
9092
let contentType: string | null = null
9193
if (headers.has('Content-Type')) {
9294
contentType = headers.get('Content-Type')!
@@ -95,8 +97,7 @@ export class Server {
9597
headers.set('Content-Type', contentType)
9698
}
9799

98-
const acceptEncoding = req.headers.get('accept-encoding')
99-
if (acceptEncoding && body && contentType) {
100+
if (compress.enable && acceptEncoding && body && contentType) {
100101
let data = new Uint8Array()
101102
if (typeof body === 'string') {
102103
data = new TextEncoder().encode(body)
@@ -360,7 +361,7 @@ export async function serve({ aleph, port, hostname, certFile, keyFile, signal }
360361
listener.close()
361362
})
362363
if (!aleph.isDev && aleph.config.server.compress) {
363-
compress.enable()
364+
compress.enable = true
364365
}
365366
log.info(`Server ready on http://${hostname || 'localhost'}:${port}${aleph.config.basePath}`)
366367

0 commit comments

Comments
 (0)