mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
[ci] format
This commit is contained in:
parent
73f215df76
commit
778dce8c05
10 changed files with 40 additions and 33 deletions
|
@ -55,38 +55,41 @@ export function isHTMLBytes(value: any): value is HTMLBytes {
|
||||||
return Object.prototype.toString.call(value) === '[object HTMLBytes]';
|
return Object.prototype.toString.call(value) === '[object HTMLBytes]';
|
||||||
}
|
}
|
||||||
|
|
||||||
async function * unescapeChunksAsync(iterable: AsyncIterable<Uint8Array>): any {
|
async function* unescapeChunksAsync(iterable: AsyncIterable<Uint8Array>): any {
|
||||||
for await (const chunk of iterable) {
|
for await (const chunk of iterable) {
|
||||||
yield unescapeHTML(chunk as BlessedType);
|
yield unescapeHTML(chunk as BlessedType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function * unescapeChunks(iterable: Iterable<any>): any {
|
function* unescapeChunks(iterable: Iterable<any>): any {
|
||||||
for(const chunk of iterable) {
|
for (const chunk of iterable) {
|
||||||
yield unescapeHTML(chunk);
|
yield unescapeHTML(chunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function unescapeHTML(str: any): BlessedType | Promise<BlessedType | AsyncGenerator<BlessedType, void, unknown>> | AsyncGenerator<BlessedType, void, unknown> {
|
export function unescapeHTML(
|
||||||
|
str: any
|
||||||
|
):
|
||||||
|
| BlessedType
|
||||||
|
| Promise<BlessedType | AsyncGenerator<BlessedType, void, unknown>>
|
||||||
|
| AsyncGenerator<BlessedType, void, unknown> {
|
||||||
if (!!str && typeof str === 'object') {
|
if (!!str && typeof str === 'object') {
|
||||||
if(str instanceof Uint8Array) {
|
if (str instanceof Uint8Array) {
|
||||||
return markHTMLBytes(str);
|
return markHTMLBytes(str);
|
||||||
}
|
}
|
||||||
// If a response, stream out the chunks
|
// 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<Uint8Array>;
|
const body = str.body as unknown as AsyncIterable<Uint8Array>;
|
||||||
return unescapeChunksAsync(body);
|
return unescapeChunksAsync(body);
|
||||||
}
|
}
|
||||||
// If a promise, await the result and mark that.
|
// 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 Promise.resolve(str).then((value) => {
|
||||||
return unescapeHTML(value);
|
return unescapeHTML(value);
|
||||||
});
|
});
|
||||||
}
|
} else if (Symbol.iterator in str) {
|
||||||
else if(Symbol.iterator in str) {
|
|
||||||
return unescapeChunks(str);
|
return unescapeChunks(str);
|
||||||
}
|
} else if (Symbol.asyncIterator in str) {
|
||||||
else if(Symbol.asyncIterator in str) {
|
|
||||||
return unescapeChunksAsync(str);
|
return unescapeChunksAsync(str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
export { createAstro } from './astro-global.js';
|
export { createAstro } from './astro-global.js';
|
||||||
export { renderEndpoint } from './endpoint.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 type { Metadata } from './metadata';
|
||||||
export { createMetadata } from './metadata.js';
|
export { createMetadata } from './metadata.js';
|
||||||
export {
|
export {
|
||||||
|
|
|
@ -3,8 +3,8 @@ import { SSRResult } from '../../@types/astro.js';
|
||||||
import { AstroJSX, isVNode } from '../../jsx-runtime/index.js';
|
import { AstroJSX, isVNode } from '../../jsx-runtime/index.js';
|
||||||
import {
|
import {
|
||||||
escapeHTML,
|
escapeHTML,
|
||||||
HTMLString,
|
|
||||||
HTMLBytes,
|
HTMLBytes,
|
||||||
|
HTMLString,
|
||||||
markHTMLString,
|
markHTMLString,
|
||||||
renderComponent,
|
renderComponent,
|
||||||
RenderInstruction,
|
RenderInstruction,
|
||||||
|
|
|
@ -27,9 +27,12 @@ export async function* renderChild(child: any): AsyncIterable<any> {
|
||||||
Object.prototype.toString.call(child) === '[object AstroComponent]'
|
Object.prototype.toString.call(child) === '[object AstroComponent]'
|
||||||
) {
|
) {
|
||||||
yield* renderAstroComponent(child);
|
yield* renderAstroComponent(child);
|
||||||
} else if(ArrayBuffer.isView(child)) {
|
} else if (ArrayBuffer.isView(child)) {
|
||||||
yield 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;
|
yield* child;
|
||||||
} else {
|
} else {
|
||||||
yield child;
|
yield child;
|
||||||
|
|
|
@ -2,7 +2,7 @@ import type { SSRResult } from '../../../@types/astro';
|
||||||
import type { AstroComponentFactory } from './index';
|
import type { AstroComponentFactory } from './index';
|
||||||
import type { RenderInstruction } from './types';
|
import type { RenderInstruction } from './types';
|
||||||
|
|
||||||
import { markHTMLString, HTMLBytes } from '../escape.js';
|
import { HTMLBytes, markHTMLString } from '../escape.js';
|
||||||
import { HydrationDirectiveProps } from '../hydration.js';
|
import { HydrationDirectiveProps } from '../hydration.js';
|
||||||
import { renderChild } from './any.js';
|
import { renderChild } from './any.js';
|
||||||
import { HTMLParts } from './common.js';
|
import { HTMLParts } from './common.js';
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import type { SSRResult } from '../../../@types/astro';
|
import type { SSRResult } from '../../../@types/astro';
|
||||||
import type { RenderInstruction } from './types.js';
|
import type { RenderInstruction } from './types.js';
|
||||||
|
|
||||||
import { markHTMLString, HTMLBytes, isHTMLString } from '../escape.js';
|
import { HTMLBytes, markHTMLString } from '../escape.js';
|
||||||
import {
|
import {
|
||||||
determineIfNeedsHydrationScript,
|
determineIfNeedsHydrationScript,
|
||||||
determinesIfNeedsDirectiveScript,
|
determinesIfNeedsDirectiveScript,
|
||||||
|
@ -50,7 +50,7 @@ export class HTMLParts {
|
||||||
this.parts = [];
|
this.parts = [];
|
||||||
}
|
}
|
||||||
append(part: string | HTMLBytes | RenderInstruction, result: SSRResult) {
|
append(part: string | HTMLBytes | RenderInstruction, result: SSRResult) {
|
||||||
if(ArrayBuffer.isView(part)) {
|
if (ArrayBuffer.isView(part)) {
|
||||||
this.parts.push(part);
|
this.parts.push(part);
|
||||||
} else {
|
} else {
|
||||||
this.parts.push(stringifyChunk(result, part));
|
this.parts.push(stringifyChunk(result, part));
|
||||||
|
@ -58,8 +58,8 @@ export class HTMLParts {
|
||||||
}
|
}
|
||||||
toString() {
|
toString() {
|
||||||
let html = '';
|
let html = '';
|
||||||
for(const part of this.parts) {
|
for (const part of this.parts) {
|
||||||
if(ArrayBuffer.isView(part)) {
|
if (ArrayBuffer.isView(part)) {
|
||||||
html += decoder.decode(part);
|
html += decoder.decode(part);
|
||||||
} else {
|
} else {
|
||||||
html += part;
|
html += part;
|
||||||
|
@ -69,7 +69,7 @@ export class HTMLParts {
|
||||||
}
|
}
|
||||||
toArrayBuffer() {
|
toArrayBuffer() {
|
||||||
this.parts.forEach((part, i) => {
|
this.parts.forEach((part, i) => {
|
||||||
if(typeof part === 'string') {
|
if (typeof part === 'string') {
|
||||||
this.parts[i] = encoder.encode(String(part));
|
this.parts[i] = encoder.encode(String(part));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -77,8 +77,11 @@ export class HTMLParts {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function chunkToByteArray(result: SSRResult, chunk: string | HTMLBytes | RenderInstruction): Uint8Array {
|
export function chunkToByteArray(
|
||||||
if(chunk instanceof Uint8Array) {
|
result: SSRResult,
|
||||||
|
chunk: string | HTMLBytes | RenderInstruction
|
||||||
|
): Uint8Array {
|
||||||
|
if (chunk instanceof Uint8Array) {
|
||||||
return chunk as Uint8Array;
|
return chunk as Uint8Array;
|
||||||
}
|
}
|
||||||
return encoder.encode(stringifyChunk(result, chunk));
|
return encoder.encode(stringifyChunk(result, chunk));
|
||||||
|
@ -86,10 +89,10 @@ export function chunkToByteArray(result: SSRResult, chunk: string | HTMLBytes |
|
||||||
|
|
||||||
export function concatUint8Arrays(arrays: Array<Uint8Array>) {
|
export function concatUint8Arrays(arrays: Array<Uint8Array>) {
|
||||||
let len = 0;
|
let len = 0;
|
||||||
arrays.forEach(arr => len += arr.length);
|
arrays.forEach((arr) => (len += arr.length));
|
||||||
let merged = new Uint8Array(len);
|
let merged = new Uint8Array(len);
|
||||||
let offset = 0;
|
let offset = 0;
|
||||||
arrays.forEach(arr => {
|
arrays.forEach((arr) => {
|
||||||
merged.set(arr, offset);
|
merged.set(arr, offset);
|
||||||
offset += arr.length;
|
offset += arr.length;
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import type { AstroComponentMetadata, SSRLoadedRenderer, SSRResult } from '../../../@types/astro';
|
import type { AstroComponentMetadata, SSRLoadedRenderer, SSRResult } from '../../../@types/astro';
|
||||||
import type { RenderInstruction } from './types.js';
|
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 { extractDirectives, generateHydrateScript } from '../hydration.js';
|
||||||
import { serializeProps } from '../serialize.js';
|
import { serializeProps } from '../serialize.js';
|
||||||
import { shorthash } from '../shorthash.js';
|
import { shorthash } from '../shorthash.js';
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import type { SSRResult } from '../../../@types/astro';
|
import type { SSRResult } from '../../../@types/astro';
|
||||||
import type { AstroComponentFactory } from './index';
|
import type { AstroComponentFactory } from './index';
|
||||||
|
|
||||||
|
import { isHTMLString } from '../escape.js';
|
||||||
import { createResponse } from '../response.js';
|
import { createResponse } from '../response.js';
|
||||||
import { isAstroComponent, isAstroComponentFactory, renderAstroComponent } from './astro.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 { renderComponent } from './component.js';
|
||||||
import { isHTMLString } from '../escape.js';
|
|
||||||
import { maybeRenderHead } from './head.js';
|
import { maybeRenderHead } from './head.js';
|
||||||
|
|
||||||
const needsHeadRenderingSymbol = Symbol.for('astro.needsHeadRendering');
|
const needsHeadRenderingSymbol = Symbol.for('astro.needsHeadRendering');
|
||||||
|
@ -72,7 +72,7 @@ export async function renderPage(
|
||||||
let i = 0;
|
let i = 0;
|
||||||
try {
|
try {
|
||||||
for await (const chunk of iterable) {
|
for await (const chunk of iterable) {
|
||||||
if(isHTMLString(chunk)) {
|
if (isHTMLString(chunk)) {
|
||||||
if (i === 0) {
|
if (i === 0) {
|
||||||
if (!/<!doctype html/i.test(String(chunk))) {
|
if (!/<!doctype html/i.test(String(chunk))) {
|
||||||
controller.enqueue(encoder.encode('<!DOCTYPE html>\n'));
|
controller.enqueue(encoder.encode('<!DOCTYPE html>\n'));
|
||||||
|
@ -96,7 +96,7 @@ export async function renderPage(
|
||||||
let parts = new HTMLParts();
|
let parts = new HTMLParts();
|
||||||
let i = 0;
|
let i = 0;
|
||||||
for await (const chunk of iterable) {
|
for await (const chunk of iterable) {
|
||||||
if(isHTMLString(chunk)) {
|
if (isHTMLString(chunk)) {
|
||||||
if (i === 0) {
|
if (i === 0) {
|
||||||
if (!/<!doctype html/i.test(String(chunk))) {
|
if (!/<!doctype html/i.test(String(chunk))) {
|
||||||
parts.append('<!DOCTYPE html>\n', result);
|
parts.append('<!DOCTYPE html>\n', result);
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
export function serializeListValue(value: any) {
|
export function serializeListValue(value: any) {
|
||||||
const hash: Record<string, any> = {};
|
const hash: Record<string, any> = {};
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ import { expect } from 'chai';
|
||||||
import * as cheerio from 'cheerio';
|
import * as cheerio from 'cheerio';
|
||||||
import { loadFixture } from './test-utils.js';
|
import { loadFixture } from './test-utils.js';
|
||||||
|
|
||||||
|
|
||||||
describe('set:html', () => {
|
describe('set:html', () => {
|
||||||
/** @type {import('./test-utils').Fixture} */
|
/** @type {import('./test-utils').Fixture} */
|
||||||
let fixture;
|
let fixture;
|
||||||
|
|
Loading…
Reference in a new issue