diff --git a/packages/astro/src/runtime/server/escape.ts b/packages/astro/src/runtime/server/escape.ts index 0118c17f3..ebba8e132 100644 --- a/packages/astro/src/runtime/server/escape.ts +++ b/packages/astro/src/runtime/server/escape.ts @@ -55,38 +55,41 @@ export function isHTMLBytes(value: any): value is HTMLBytes { return Object.prototype.toString.call(value) === '[object HTMLBytes]'; } -async function * unescapeChunksAsync(iterable: AsyncIterable): any { +async function* unescapeChunksAsync(iterable: AsyncIterable): any { for await (const chunk of iterable) { yield unescapeHTML(chunk as BlessedType); } } -function * unescapeChunks(iterable: Iterable): any { - for(const chunk of iterable) { +function* unescapeChunks(iterable: Iterable): any { + for (const chunk of iterable) { yield unescapeHTML(chunk); } } -export function unescapeHTML(str: any): BlessedType | Promise> | AsyncGenerator { +export function unescapeHTML( + str: any +): + | BlessedType + | Promise> + | AsyncGenerator { if (!!str && typeof str === 'object') { - if(str instanceof Uint8Array) { + if (str instanceof Uint8Array) { return markHTMLBytes(str); } // If a response, stream out the chunks - else if(str instanceof Response && str.body) { + else if (str instanceof Response && str.body) { const body = str.body as unknown as AsyncIterable; return unescapeChunksAsync(body); } // If a promise, await the result and mark that. - else if(typeof str.then === 'function') { + else if (typeof str.then === 'function') { return Promise.resolve(str).then((value) => { return unescapeHTML(value); }); - } - else if(Symbol.iterator in str) { + } else if (Symbol.iterator in str) { return unescapeChunks(str); - } - else if(Symbol.asyncIterator in str) { + } else if (Symbol.asyncIterator in str) { return unescapeChunksAsync(str); } } diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts index 8ffb64611..28ec19a92 100644 --- a/packages/astro/src/runtime/server/index.ts +++ b/packages/astro/src/runtime/server/index.ts @@ -1,6 +1,6 @@ export { createAstro } from './astro-global.js'; export { renderEndpoint } from './endpoint.js'; -export { escapeHTML, HTMLString, HTMLBytes, markHTMLString, unescapeHTML } from './escape.js'; +export { escapeHTML, HTMLBytes, HTMLString, markHTMLString, unescapeHTML } from './escape.js'; export type { Metadata } from './metadata'; export { createMetadata } from './metadata.js'; export { diff --git a/packages/astro/src/runtime/server/jsx.ts b/packages/astro/src/runtime/server/jsx.ts index 005949733..3e5816f28 100644 --- a/packages/astro/src/runtime/server/jsx.ts +++ b/packages/astro/src/runtime/server/jsx.ts @@ -3,8 +3,8 @@ import { SSRResult } from '../../@types/astro.js'; import { AstroJSX, isVNode } from '../../jsx-runtime/index.js'; import { escapeHTML, - HTMLString, HTMLBytes, + HTMLString, markHTMLString, renderComponent, RenderInstruction, diff --git a/packages/astro/src/runtime/server/render/any.ts b/packages/astro/src/runtime/server/render/any.ts index 4da9fe7c4..454ed700d 100644 --- a/packages/astro/src/runtime/server/render/any.ts +++ b/packages/astro/src/runtime/server/render/any.ts @@ -27,9 +27,12 @@ export async function* renderChild(child: any): AsyncIterable { Object.prototype.toString.call(child) === '[object AstroComponent]' ) { yield* renderAstroComponent(child); - } else if(ArrayBuffer.isView(child)) { + } else if (ArrayBuffer.isView(child)) { yield child; - } else if (typeof child === 'object' && (Symbol.asyncIterator in child || Symbol.iterator in child)) { + } else if ( + typeof child === 'object' && + (Symbol.asyncIterator in child || Symbol.iterator in child) + ) { yield* child; } else { yield child; diff --git a/packages/astro/src/runtime/server/render/astro.ts b/packages/astro/src/runtime/server/render/astro.ts index 3b8f5af4e..cd1c04885 100644 --- a/packages/astro/src/runtime/server/render/astro.ts +++ b/packages/astro/src/runtime/server/render/astro.ts @@ -2,7 +2,7 @@ import type { SSRResult } from '../../../@types/astro'; import type { AstroComponentFactory } from './index'; import type { RenderInstruction } from './types'; -import { markHTMLString, HTMLBytes } from '../escape.js'; +import { HTMLBytes, markHTMLString } from '../escape.js'; import { HydrationDirectiveProps } from '../hydration.js'; import { renderChild } from './any.js'; import { HTMLParts } from './common.js'; diff --git a/packages/astro/src/runtime/server/render/common.ts b/packages/astro/src/runtime/server/render/common.ts index 9be47d230..b8a998a4a 100644 --- a/packages/astro/src/runtime/server/render/common.ts +++ b/packages/astro/src/runtime/server/render/common.ts @@ -1,7 +1,7 @@ import type { SSRResult } from '../../../@types/astro'; import type { RenderInstruction } from './types.js'; -import { markHTMLString, HTMLBytes, isHTMLString } from '../escape.js'; +import { HTMLBytes, markHTMLString } from '../escape.js'; import { determineIfNeedsHydrationScript, determinesIfNeedsDirectiveScript, @@ -50,7 +50,7 @@ export class HTMLParts { this.parts = []; } append(part: string | HTMLBytes | RenderInstruction, result: SSRResult) { - if(ArrayBuffer.isView(part)) { + if (ArrayBuffer.isView(part)) { this.parts.push(part); } else { this.parts.push(stringifyChunk(result, part)); @@ -58,8 +58,8 @@ export class HTMLParts { } toString() { let html = ''; - for(const part of this.parts) { - if(ArrayBuffer.isView(part)) { + for (const part of this.parts) { + if (ArrayBuffer.isView(part)) { html += decoder.decode(part); } else { html += part; @@ -69,7 +69,7 @@ export class HTMLParts { } toArrayBuffer() { this.parts.forEach((part, i) => { - if(typeof part === 'string') { + if (typeof part === 'string') { this.parts[i] = encoder.encode(String(part)); } }); @@ -77,8 +77,11 @@ export class HTMLParts { } } -export function chunkToByteArray(result: SSRResult, chunk: string | HTMLBytes | RenderInstruction): Uint8Array { - if(chunk instanceof Uint8Array) { +export function chunkToByteArray( + result: SSRResult, + chunk: string | HTMLBytes | RenderInstruction +): Uint8Array { + if (chunk instanceof Uint8Array) { return chunk as Uint8Array; } return encoder.encode(stringifyChunk(result, chunk)); @@ -86,10 +89,10 @@ export function chunkToByteArray(result: SSRResult, chunk: string | HTMLBytes | export function concatUint8Arrays(arrays: Array) { let len = 0; - arrays.forEach(arr => len += arr.length); + arrays.forEach((arr) => (len += arr.length)); let merged = new Uint8Array(len); let offset = 0; - arrays.forEach(arr => { + arrays.forEach((arr) => { merged.set(arr, offset); offset += arr.length; }); diff --git a/packages/astro/src/runtime/server/render/component.ts b/packages/astro/src/runtime/server/render/component.ts index 3d10be33f..ff5ba2345 100644 --- a/packages/astro/src/runtime/server/render/component.ts +++ b/packages/astro/src/runtime/server/render/component.ts @@ -1,7 +1,7 @@ import type { AstroComponentMetadata, SSRLoadedRenderer, SSRResult } from '../../../@types/astro'; import type { RenderInstruction } from './types.js'; -import { markHTMLString, HTMLBytes } from '../escape.js'; +import { HTMLBytes, markHTMLString } from '../escape.js'; import { extractDirectives, generateHydrateScript } from '../hydration.js'; import { serializeProps } from '../serialize.js'; import { shorthash } from '../shorthash.js'; diff --git a/packages/astro/src/runtime/server/render/page.ts b/packages/astro/src/runtime/server/render/page.ts index 131b07fb1..9648a94bd 100644 --- a/packages/astro/src/runtime/server/render/page.ts +++ b/packages/astro/src/runtime/server/render/page.ts @@ -1,11 +1,11 @@ import type { SSRResult } from '../../../@types/astro'; import type { AstroComponentFactory } from './index'; +import { isHTMLString } from '../escape.js'; import { createResponse } from '../response.js'; import { isAstroComponent, isAstroComponentFactory, renderAstroComponent } from './astro.js'; -import { encoder, chunkToByteArray, HTMLParts } from './common.js'; +import { chunkToByteArray, encoder, HTMLParts } from './common.js'; import { renderComponent } from './component.js'; -import { isHTMLString } from '../escape.js'; import { maybeRenderHead } from './head.js'; const needsHeadRenderingSymbol = Symbol.for('astro.needsHeadRendering'); @@ -72,14 +72,14 @@ export async function renderPage( let i = 0; try { for await (const chunk of iterable) { - if(isHTMLString(chunk)) { + if (isHTMLString(chunk)) { if (i === 0) { if (!/\n')); } } } - + let bytes = chunkToByteArray(result, chunk); controller.enqueue(bytes); i++; @@ -96,7 +96,7 @@ export async function renderPage( let parts = new HTMLParts(); let i = 0; for await (const chunk of iterable) { - if(isHTMLString(chunk)) { + if (isHTMLString(chunk)) { if (i === 0) { if (!/\n', result); diff --git a/packages/astro/src/runtime/server/util.ts b/packages/astro/src/runtime/server/util.ts index 65cbfa0f5..9f0fdbec2 100644 --- a/packages/astro/src/runtime/server/util.ts +++ b/packages/astro/src/runtime/server/util.ts @@ -1,4 +1,3 @@ - export function serializeListValue(value: any) { const hash: Record = {}; diff --git a/packages/astro/test/set-html.test.js b/packages/astro/test/set-html.test.js index ed626c761..e43984513 100644 --- a/packages/astro/test/set-html.test.js +++ b/packages/astro/test/set-html.test.js @@ -2,7 +2,6 @@ import { expect } from 'chai'; import * as cheerio from 'cheerio'; import { loadFixture } from './test-utils.js'; - describe('set:html', () => { /** @type {import('./test-utils').Fixture} */ let fixture;