[ci] format

This commit is contained in:
matthewp 2022-06-24 19:37:09 +00:00 committed by github-actions[bot]
parent 3daaf510ea
commit 69c955b2bf
5 changed files with 70 additions and 70 deletions

View file

@ -83,9 +83,7 @@ export interface RenderOptions {
request: Request; request: Request;
} }
export async function render( export async function render(opts: RenderOptions): Promise<Response> {
opts: RenderOptions
): Promise<Response> {
const { const {
links, links,
styles, styles,

View file

@ -41,19 +41,19 @@ const svgEnumAttributes = /^(autoReverse|externalResourcesRequired|focusable|pre
// INVESTIGATE: Can we have more specific types both for the argument and output? // INVESTIGATE: Can we have more specific types both for the argument and output?
// If these are intentional, add comments that these are intention and why. // If these are intentional, add comments that these are intention and why.
// Or maybe type UserValue = any; ? // Or maybe type UserValue = any; ?
async function * _render(child: any): AsyncIterable<any> { async function* _render(child: any): AsyncIterable<any> {
child = await child; child = await child;
if (child instanceof HTMLString) { if (child instanceof HTMLString) {
yield child; yield child;
} else if (Array.isArray(child)) { } else if (Array.isArray(child)) {
for(const value of child) { for (const value of child) {
yield markHTMLString(await _render(value)); yield markHTMLString(await _render(value));
} }
} else if (typeof child === 'function') { } else if (typeof child === 'function') {
// Special: If a child is a function, call it automatically. // Special: If a child is a function, call it automatically.
// This lets you do {() => ...} without the extra boilerplate // This lets you do {() => ...} without the extra boilerplate
// of wrapping it in a function and calling it. // of wrapping it in a function and calling it.
yield * _render(child()); yield* _render(child());
} else if (typeof child === 'string') { } else if (typeof child === 'string') {
yield markHTMLString(escapeHTML(child)); yield markHTMLString(escapeHTML(child));
} else if (!child && child !== 0) { } else if (!child && child !== 0) {
@ -65,9 +65,9 @@ async function * _render(child: any): AsyncIterable<any> {
child instanceof AstroComponent || child instanceof AstroComponent ||
Object.prototype.toString.call(child) === '[object AstroComponent]' Object.prototype.toString.call(child) === '[object AstroComponent]'
) { ) {
yield * renderAstroComponent(child); yield* renderAstroComponent(child);
} else if(typeof child === 'object' && Symbol.asyncIterator in child) { } else if (typeof child === 'object' && Symbol.asyncIterator in child) {
yield * child; yield* child;
} else { } else {
yield child; yield child;
} }
@ -96,7 +96,7 @@ export class AstroComponent {
const expression = expressions[i]; const expression = expressions[i];
yield markHTMLString(html); yield markHTMLString(html);
yield * _render(expression); yield* _render(expression);
} }
} }
} }
@ -129,7 +129,7 @@ export async function renderSlot(_result: any, slotted: string, fallback?: any):
if (slotted) { if (slotted) {
let iterator = _render(slotted); let iterator = _render(slotted);
let content = ''; let content = '';
for await(const chunk of iterator) { for await (const chunk of iterator) {
content += chunk; content += chunk;
} }
return markHTMLString(content); return markHTMLString(content);
@ -334,7 +334,7 @@ If you're still stuck, please open an issue on GitHub or join us at https://astr
)}` )}`
); );
html = ''; html = '';
for await(const chunk of iterable) { for await (const chunk of iterable) {
html += chunk; html += chunk;
} }
} }
@ -617,7 +617,7 @@ export async function renderToString(
} }
let html = ''; let html = '';
for await(const chunk of renderAstroComponent(Component)) { for await (const chunk of renderAstroComponent(Component)) {
html += chunk; html += chunk;
} }
return html; return html;
@ -632,7 +632,9 @@ export async function renderToIterable(
const Component = await componentFactory(result, props, children); const Component = await componentFactory(result, props, children);
if (!isAstroComponent(Component)) { if (!isAstroComponent(Component)) {
console.warn(`Returning a Response is only supported inside of page components. Consider refactoring this logic into something like a function that can be used in the page.`); console.warn(
`Returning a Response is only supported inside of page components. Consider refactoring this logic into something like a function that can be used in the page.`
);
const response: Response = Component; const response: Response = Component;
throw response; throw response;
} }
@ -656,9 +658,9 @@ export async function renderPage(
start(controller) { start(controller) {
async function read() { async function read() {
let i = 0; let i = 0;
for await(const chunk of iterable) { for await (const chunk of iterable) {
let html = chunk.toString(); let html = chunk.toString();
if(i === 0) { if (i === 0) {
if (!/<!doctype html/i.test(html)) { if (!/<!doctype html/i.test(html)) {
controller.enqueue(encoder.encode('<!DOCTYPE html>\n')); controller.enqueue(encoder.encode('<!DOCTYPE html>\n'));
} }
@ -669,7 +671,7 @@ export async function renderPage(
controller.close(); controller.close();
} }
read(); read();
} },
}); });
let init = result.response; let init = result.response;
let response = createResponse(stream, init); let response = createResponse(stream, init);
@ -716,10 +718,12 @@ export function maybeRenderHead(result: SSRResult): string | Promise<string> {
return renderHead(result); return renderHead(result);
} }
export async function * renderAstroComponent(component: InstanceType<typeof AstroComponent>): AsyncIterable<string> { export async function* renderAstroComponent(
component: InstanceType<typeof AstroComponent>
): AsyncIterable<string> {
for await (const value of component) { for await (const value of component) {
if (value || value === 0) { if (value || value === 0) {
for await(const chunk of _render(value)) { for await (const chunk of _render(value)) {
yield markHTMLString(chunk); yield markHTMLString(chunk);
} }
} }

View file

@ -1,5 +1,5 @@
const isNodeJS =
const isNodeJS = typeof process === 'object' && Object.prototype.toString.call(process) === '[object process]'; typeof process === 'object' && Object.prototype.toString.call(process) === '[object process]';
let StreamingCompatibleResponse: typeof Response | undefined; let StreamingCompatibleResponse: typeof Response | undefined;
@ -19,17 +19,17 @@ function createResponseClass() {
} }
async text(): Promise<string> { async text(): Promise<string> {
if(this.#isStream && isNodeJS) { if (this.#isStream && isNodeJS) {
let decoder = new TextDecoder(); let decoder = new TextDecoder();
let body = this.#body as ReadableStream<Uint8Array>; let body = this.#body as ReadableStream<Uint8Array>;
let reader = body.getReader(); let reader = body.getReader();
let buffer: number[] = []; let buffer: number[] = [];
while(true) { while (true) {
let r = await reader.read(); let r = await reader.read();
if(r.value) { if (r.value) {
buffer.push(...r.value); buffer.push(...r.value);
} }
if(r.done) { if (r.done) {
break; break;
} }
} }
@ -39,16 +39,16 @@ function createResponseClass() {
} }
async arrayBuffer(): Promise<ArrayBuffer> { async arrayBuffer(): Promise<ArrayBuffer> {
if(this.#isStream && isNodeJS) { if (this.#isStream && isNodeJS) {
let body = this.#body as ReadableStream<Uint8Array>; let body = this.#body as ReadableStream<Uint8Array>;
let reader = body.getReader(); let reader = body.getReader();
let chunks: number[] = []; let chunks: number[] = [];
while(true) { while (true) {
let r = await reader.read(); let r = await reader.read();
if(r.value) { if (r.value) {
chunks.push(...r.value); chunks.push(...r.value);
} }
if(r.done) { if (r.done) {
break; break;
} }
} }
@ -56,16 +56,18 @@ function createResponseClass() {
} }
return super.arrayBuffer(); return super.arrayBuffer();
} }
} };
return StreamingCompatibleResponse; return StreamingCompatibleResponse;
} }
type CreateResponseFn = (body?: BodyInit | null, init?: ResponseInit) => Response; type CreateResponseFn = (body?: BodyInit | null, init?: ResponseInit) => Response;
export const createResponse: CreateResponseFn = isNodeJS ? (body, init) => { export const createResponse: CreateResponseFn = isNodeJS
if(typeof StreamingCompatibleResponse === 'undefined') { ? (body, init) => {
if (typeof StreamingCompatibleResponse === 'undefined') {
return new (createResponseClass())(body, init); return new (createResponseClass())(body, init);
} }
return new StreamingCompatibleResponse(body, init); return new StreamingCompatibleResponse(body, init);
} : (body, init) => new Response(body, init); }
: (body, init) => new Response(body, init);

View file

@ -76,9 +76,9 @@ async function writeWebResponse(res: http.ServerResponse, webResponse: Response)
res.writeHead(status, _headers); res.writeHead(status, _headers);
if (body) { if (body) {
if(Symbol.for('astro.responseBody') in webResponse) { if (Symbol.for('astro.responseBody') in webResponse) {
let stream = (webResponse as any)[Symbol.for('astro.responseBody')]; let stream = (webResponse as any)[Symbol.for('astro.responseBody')];
for await(const chunk of stream) { for await (const chunk of stream) {
res.write(chunk.toString()); res.write(chunk.toString());
} }
} else if (body instanceof Readable) { } else if (body instanceof Readable) {
@ -98,10 +98,7 @@ async function writeWebResponse(res: http.ServerResponse, webResponse: Response)
res.end(); res.end();
} }
async function writeSSRResult( async function writeSSRResult(webResponse: Response, res: http.ServerResponse) {
webResponse: Response,
res: http.ServerResponse
) {
return writeWebResponse(res, webResponse); return writeWebResponse(res, webResponse);
} }

View file

@ -3,7 +3,6 @@ import { expect } from 'chai';
import testAdapter from './test-adapter.js'; import testAdapter from './test-adapter.js';
import * as cheerio from 'cheerio'; import * as cheerio from 'cheerio';
describe('Streaming', () => { describe('Streaming', () => {
if (isWindows) return; if (isWindows) return;
@ -35,7 +34,7 @@ describe('Streaming', () => {
it('Body is chunked', async () => { it('Body is chunked', async () => {
let res = await fixture.fetch('/'); let res = await fixture.fetch('/');
let chunks = []; let chunks = [];
for await(const bytes of res.body) { for await (const bytes of res.body) {
let chunk = bytes.toString('utf-8'); let chunk = bytes.toString('utf-8');
chunks.push(chunk); chunks.push(chunk);
} }
@ -64,7 +63,7 @@ describe('Streaming', () => {
const response = await app.render(request); const response = await app.render(request);
let chunks = []; let chunks = [];
let decoder = new TextDecoder(); let decoder = new TextDecoder();
for await(const bytes of response.body) { for await (const bytes of response.body) {
let chunk = decoder.decode(bytes); let chunk = decoder.decode(bytes);
chunks.push(chunk); chunks.push(chunk);
} }