CodeDredd / h3-compression

Adds compression to h3 requests (brotli, gzip, deflate)
MIT License
57 stars 2 forks source link

Cannot read properties of undefined (reading 'startsWith') #2

Open imcm7 opened 9 months ago

imcm7 commented 9 months ago

Environment

Reproduction

import { useCompression } from 'h3-compression';

export default defineNitroPlugin((nitroApp) => {
    nitroApp.hooks.hook('render:response', async (response, { event }) => {
        if (!response.headers?.['content-type'].startsWith('text/html')) {  // undefined response.headers?.['content-type']
            return;
        }

        await useCompression(event, response);
    });
});

Describe the bug

I not see it any more, may be that case is registered when some errors is in nuxt config.

Cannot read properties of undefined (reading 'startsWith') at (./server/plugins/compression.ts:9:1)
at ./node_modules/hookable/dist/index.mjs:48:66
at ./node_modules/hookable/dist/index.mjs:48:56
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at (./node_modules/nitropack/dist/runtime/renderer.mjs:29:5)
at async Object.handler (./node_modules/h3/dist/index.mjs:1630:19)
at async Server.toNodeHandle (./node_modules/h3/dist/index.mjs:1840:7)

Additional context

No response

Logs

No response

imcm7 commented 9 months ago

May be if (!response.headers?.['content-type'] || !response.headers?.['content-type'].startsWith('text/html')) {}

AlbertBrand commented 4 months ago

You can add an extra nullish coalescing check after array lookups:

if (!response.headers?.['content-type']?.startsWith('text/html')) {