Skip to content

Commit

Permalink
fix(render): fix uncoded ReadableStream in HTTP Responses (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca authored Dec 6, 2024
1 parent a8c30da commit c90d258
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
31 changes: 25 additions & 6 deletions packages/brisa/src/utils/response-rendered-page/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ describe('utils', () => {
'X-Mode': 'reactivity',
},
});
const html = await response.text();

const html = await getTextFromResponse(response);

expect(response.status).toBe(200);
expect(response.headers.get('X-Mode')).toBe('reactivity');
Expand All @@ -77,7 +78,7 @@ describe('utils', () => {
'X-Mode': 'reactivity',
},
});
const html = await response.text();
const html = await getTextFromResponse(response);

expect(response.status).toBe(200);
expect(response.headers.get('X-Mode')).toBe('reactivity');
Expand Down Expand Up @@ -105,7 +106,7 @@ describe('utils', () => {
'X-Mode': 'reactivity',
},
});
const html = await response.text();
const html = await getTextFromResponse(response);

expect(response.status).toBe(200);
expect(response.headers.get('X-Mode')).toBe('reactivity');
Expand Down Expand Up @@ -134,7 +135,8 @@ describe('utils', () => {
'X-Mode': 'reactivity',
},
});
const html = await response.text();

const html = await getTextFromResponse(response);

expect(response.status).toBe(200);
expect(response.headers.get('X-Mode')).toBe('reactivity');
Expand Down Expand Up @@ -169,7 +171,8 @@ describe('utils', () => {
'X-Mode': 'reactivity',
},
});
const html = await response.text();

const html = await getTextFromResponse(response);

expect(response.status).toBe(200);
expect(response.headers.get('X-Mode')).toBe('reactivity');
Expand All @@ -194,7 +197,9 @@ describe('utils', () => {
filePath: path.join(PAGES_DIR, 'somepage.tsx'),
} as MatchedBrisaRoute,
});
const html = await response.text();

const html = await getTextFromResponse(response);

expect(response.status).toBe(200);
expect(html).toStartWith('<!DOCTYPE html>');
expect(html).toContain('<html lang="es" dir="ltr">');
Expand Down Expand Up @@ -330,3 +335,17 @@ describe('utils', () => {
});
});
});

async function getTextFromResponse(response: Response) {
try {
const text = await response.text();
if (text.startsWith('\u0000') || text.includes(',')) {
const byteArray = new Uint8Array(text.split(',').map(Number));
return new TextDecoder('utf-8').decode(byteArray);
}
return text;
} catch {
const buffer = await response.arrayBuffer();
return new TextDecoder('utf-8').decode(buffer);
}
}
5 changes: 4 additions & 1 deletion packages/brisa/src/utils/response-rendered-page/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export default async function responseRenderedPage({
status,
};

return new Response(htmlStream, responseOptions);
return new Response(
htmlStream.pipeThrough(new TextEncoderStream()),
responseOptions,
);
}

export function routeToPrerenderedPagePath(route: MatchedBrisaRoute) {
Expand Down

0 comments on commit c90d258

Please sign in to comment.