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

Commit f7aa714

Browse files
committed
Add compress option for app Config
1 parent ab169e0 commit f7aa714

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

server/api.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,40 @@ import type { APIRequest, ServerRequest, ServerResponse } from '../types.ts'
88
let brotli: ((data: Uint8Array) => Uint8Array) | null = null
99
let gzip: ((data: Uint8Array) => Uint8Array) | null = null
1010

11+
type Response = {
12+
status: number
13+
headers: Headers
14+
compress: boolean
15+
done: boolean
16+
}
17+
1118
export class Request implements APIRequest {
1219
#req: ServerRequest
1320
#params: Record<string, string>
1421
#query: URLSearchParams
1522
#cookies: ReadonlyMap<string, string>
16-
#resp = {
17-
status: 200,
18-
headers: new Headers({
19-
Server: 'Aleph.js',
20-
}),
21-
done: false
22-
}
23+
#resp: Response
2324

24-
constructor(req: ServerRequest, params: Record<string, string>, query: URLSearchParams) {
25+
constructor(req: ServerRequest, params: Record<string, string>, query: URLSearchParams, compress: boolean = true) {
2526
this.#req = req
2627
this.#params = params
2728
this.#query = query
2829
const cookies = new Map()
2930
this.headers.get('cookie')?.split(';').forEach(cookie => {
30-
const p = cookie.trim().split('=')
31+
const p = cookie.split('=')
3132
if (p.length >= 2) {
32-
cookies.set(p.shift()!.trim(), decodeURI(p.join('=')))
33+
cookies.set(p.shift()!, decodeURI(p.join('=')))
3334
}
3435
})
3536
this.#cookies = cookies
37+
this.#resp = {
38+
status: 200,
39+
headers: new Headers({
40+
Server: 'Aleph.js',
41+
}),
42+
compress,
43+
done: false
44+
}
3645
}
3746

3847
get url(): string {
@@ -159,12 +168,12 @@ export class Request implements APIRequest {
159168
contentType = 'text/plain; charset=utf-8'
160169
this.#resp.headers.set('Content-Type', contentType)
161170
}
162-
if (Deno.env.get('ALEPH_BUILD_MODE') !== 'development') {
171+
if (this.#resp.compress) {
163172
let shouldCompress = false
164173
if (contentType) {
165174
if (contentType.startsWith('text/')) {
166175
shouldCompress = true
167-
} else if (/^application\/(javascript|typecript|wasm|json|xml)/i.test(contentType)) {
176+
} else if (/^application\/(javascript|json|xml|wasm)/i.test(contentType)) {
168177
shouldCompress = true
169178
} else if (/^image\/svg\+xml/i.test(contentType)) {
170179
shouldCompress = true

server/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const defaultConfig: Readonly<RequiredConfig> = {
3030
postcss: { plugins: ['autoprefixer'] },
3131
},
3232
headers: {},
33+
compress: true,
3334
env: {},
3435
react: {
3536
version: defaultReactVersion,
@@ -76,6 +77,7 @@ export async function loadConfig(workingDir: string): Promise<Config> {
7677
plugins,
7778
css,
7879
headers,
80+
compress,
7981
env,
8082
} = data
8183
if (isFramework(framework)) {
@@ -118,6 +120,9 @@ export async function loadConfig(workingDir: string): Promise<Config> {
118120
if (util.isPlainObject(headers)) {
119121
config.headers = toPlainStringRecord(headers)
120122
}
123+
if (typeof compress === 'boolean') {
124+
config.compress = compress
125+
}
121126
if (util.isPlainObject(env)) {
122127
config.env = toPlainStringRecord(env)
123128
Object.entries(env).forEach(([key, value]) => Deno.env.set(key, value))

server/server.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ export class Server {
2828
}
2929

3030
const app = this.#app
31-
const { baseUrl, rewrites } = app.config
31+
const { baseUrl, compress, headers, rewrites } = app.config
3232
const url = rewriteURL(r.url, baseUrl, rewrites)
3333
const pathname = decodeURI(url.pathname)
34-
const req = new Request(r, {}, url.searchParams)
34+
const req = new Request(r, {}, url.searchParams, !app.isDev && compress)
3535

3636
// set custom headers
37-
for (const key in app.config.headers) {
38-
req.setHeader(key, app.config.headers[key])
37+
for (const key in headers) {
38+
req.setHeader(key, headers[key])
3939
}
4040
if (app.isDev) {
4141
req.setHeader('Cache-Control', 'max-age=0')
@@ -158,7 +158,7 @@ export class Server {
158158
const [{ params, query }, { jsFile, hash }] = route
159159
const { default: handle } = await import(`file://${jsFile}#${hash.slice(0, 6)}`)
160160
if (util.isFunction(handle)) {
161-
await handle(new Request(req, params, query))
161+
await handle(new Request(req, params, query, !app.isDev && compress))
162162
} else {
163163
req.status(500).json({ status: 500, message: 'bad api handler' })
164164
}

types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export type Config = {
7979
headers?: Record<string, string>
8080
/** `rewrites` specifies the server rewrite map. */
8181
rewrites?: Record<string, string>
82+
/** `compress` enbles gzip/brotli compression for static files and SSR content. */
83+
compress?: boolean
8284
/** `env` appends system env variables. */
8385
env?: Record<string, string>
8486
}

0 commit comments

Comments
 (0)