Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: changed to mapResponse #32

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 16 additions & 36 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
*/
app.onAfterHandle({ as: lifeCycleType }, async (ctx) => {
app.mapResponse({ as: lifeCycleType }, async (ctx) => {
// Disable compression when `x-no-compression` header is set
if (options?.disableByHeader && ctx.headers['x-no-compression']) {
return
Expand All @@ -83,7 +83,7 @@

const encoding = encodings[0] as CompressionEncoding
let compressed: Buffer | ReadableStream<Uint8Array>
let headers: Record<string, any> = {}
let contentType = set.headers['Content-Type'] ?? ''

Check failure on line 86 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L86

Unsafe assignment of an `any` value.

Check failure on line 86 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L86

Unsafe member access .headers on an `any` value.

/**
* Compress ReadableStream Object if stream exists (SSE)
Expand All @@ -93,20 +93,13 @@
if (response?.stream && response.stream instanceof ReadableStream) {
const stream = response.stream as ReadableStream
compressed = stream.pipeThrough(CompressionStream(encoding, options))
headers = {
...set.headers,
...{
'Content-Encoding': encoding,
},
} as any
} else {
const res = mapResponse(response, {
headers: set.headers,
headers: {},
})
const resContentType = res.headers.get('Content-Type')

Check failure on line 100 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L100

Unsafe assignment of an `any` value.

Check failure on line 100 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L100

Unsafe call of an `any` typed value.

Check failure on line 100 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L100

Unsafe member access .headers on an `any` value.

if (!res.headers.get('Content-Type')) {
res.headers.set('Content-Type', 'text/plain')
}
contentType = resContentType ? resContentType : 'text/plain'

Check failure on line 102 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L102

Unsafe assignment of an `any` value.

const buffer = await res.arrayBuffer()
// Disable compression when buffer size is less than threshold
Expand All @@ -115,9 +108,7 @@
}

// Disable compression when Content-Type is not compressible
const isCompressible = defaultCompressibleTypes.test(
res.headers.get('Content-Type') ?? '',
)
const isCompressible = defaultCompressibleTypes.test(contentType)

Check failure on line 111 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L111

Unsafe argument of type `any` assigned to a parameter of type `string`.
if (!isCompressible) {
return
}
Expand All @@ -131,11 +122,6 @@
} else {
return
}
res.headers.set('Content-Encoding', encoding)

res.headers.forEach((val, key) => {
headers[key] = val
})
}

/**
Expand All @@ -147,10 +133,11 @@
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Vary
*/
if (headers.Vary) {
const rawHeaderValue = headers.Vary?.split(',').map((v: any) =>
if (set.headers.Vary) {
const rawHeaderValue = set.headers.Vary?.split(',').map((v: any) =>

Check warning on line 137 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L137

Unexpected any. Specify a different type.

Check failure on line 137 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L137

Unsafe call of an `any` typed value.
v.trim().toLowerCase(),
)

const headerValueArray = Array.isArray(rawHeaderValue)
? rawHeaderValue
: [rawHeaderValue]
Expand All @@ -161,25 +148,18 @@
!headerValueArray.some((h) => h.includes('accept-encoding')) &&
!headerValueArray.includes('*')
) {
headers.Vary = headerValueArray.concat('accept-encoding').join(', ')
set.headers.Vary = headerValueArray.concat('accept-encoding').join(', ')

Check failure on line 151 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L151

Unsafe member access .headers on an `any` value.
}
} else {
headers.Vary = 'accept-encoding'
set.headers.Vary = 'accept-encoding'

Check failure on line 154 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L154

Unsafe member access .headers on an `any` value.
}
set.headers['Content-Encoding'] = encoding

Check failure on line 156 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L156

Unsafe member access .headers on an `any` value.

// Build response from compressed body
const compressedResponse = mapResponse(compressed, {
headers,
status: set.status,
cookie: set.cookie,
redirect: set.redirect,
return new Response(compressed, {
headers: {
'Content-Type': contentType,

Check failure on line 160 in src/main.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main.ts#L160

Unsafe assignment of an `any` value.
},
})

set.status = undefined
set.cookie = undefined
set.redirect = undefined
set.headers = {}
return compressedResponse
})
return app
}