Skip to content

Commit

Permalink
fix: use ReadableStream for response object if deno (#10495)
Browse files Browse the repository at this point in the history
  • Loading branch information
satyarohith committed Apr 1, 2024
1 parent b3028ca commit 046d69d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/cool-bulldogs-change.md
@@ -0,0 +1,7 @@
---
"astro": patch
---

This patch allows astro to run in node-compat mode in Deno. Deno doesn't support
construction of response from async iterables in node-compat mode so we need to
use ReadableStream.
6 changes: 4 additions & 2 deletions packages/astro/src/runtime/server/render/page.ts
Expand Up @@ -5,7 +5,7 @@ import type { AstroComponentFactory } from './index.js';
import { isAstroComponentFactory } from './astro/index.js';
import { renderToAsyncIterable, renderToReadableStream, renderToString } from './astro/render.js';
import { encoder } from './common.js';
import { isNode } from './util.js';
import { isNode, isDeno } from './util.js';

export async function renderPage(
result: SSRResult,
Expand Down Expand Up @@ -48,7 +48,9 @@ export async function renderPage(

let body: BodyInit | Response;
if (streaming) {
if (isNode) {
// isNode is true in Deno node-compat mode but response construction from
// async iterables is not supported, so we fallback to ReadableStream if isDeno is true.
if (isNode && !isDeno) {
const nodeBody = await renderToAsyncIterable(
result,
componentFactory,
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/runtime/server/render/util.ts
Expand Up @@ -207,6 +207,8 @@ export function renderToBufferDestination(bufferRenderFunction: RenderFunction):

export const isNode =
typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]';
// @ts-expect-error: Deno is not part of the types.
export const isDeno = typeof Deno !== 'undefined';

// We can get rid of this when Promise.withResolvers() is ready
export type PromiseWithResolvers<T> = {
Expand Down

0 comments on commit 046d69d

Please sign in to comment.