Skip to content

Commit

Permalink
use platform agnostic feature test
Browse files Browse the repository at this point in the history
  • Loading branch information
satyarohith committed Mar 20, 2024
1 parent a6e6e22 commit 12a8006
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
6 changes: 2 additions & 4 deletions packages/astro/src/runtime/server/render/page.ts
Original file line number Diff line number Diff line change
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, isDeno } from './util.js';
import { supportsResponseBodyAsyncIterator } from './util.js';

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

let body: BodyInit | Response;
if (streaming) {
// 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) {
if (supportsResponseBodyAsyncIterator) {
const nodeBody = await renderToAsyncIterable(
result,
componentFactory,
Expand Down
15 changes: 11 additions & 4 deletions packages/astro/src/runtime/server/render/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,17 @@ 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';
export const supportsResponseBodyAsyncIterator = (() => {
let supported = false;
new Response({
// @ts-expect-error: Response types don't expect the property.
get [Symbol.asyncIterator]() {
supported = true;
return undefined;
},
});
return supported;
})();

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

0 comments on commit 12a8006

Please sign in to comment.